001/*
002 * $Id: QuotSolvablePolynomialRing.java 5267 2015-07-27 17:57:50Z kredel $
003 */
004
005package edu.jas.gbufd;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.math.BigInteger;
012import java.util.ArrayList;
013import java.util.List;
014import java.util.Map;
015import java.util.Random;
016
017import org.apache.log4j.Logger;
018
019import edu.jas.kern.PrettyPrint;
020import edu.jas.kern.Scripting;
021import edu.jas.poly.ExpVector;
022import edu.jas.poly.GenPolynomial;
023import edu.jas.poly.GenPolynomialRing;
024import edu.jas.poly.GenPolynomialTokenizer;
025import edu.jas.poly.GenSolvablePolynomial;
026import edu.jas.poly.GenSolvablePolynomialRing;
027import edu.jas.poly.RecSolvablePolynomial;
028import edu.jas.poly.RecSolvablePolynomialRing;
029import edu.jas.poly.RelationTable;
030import edu.jas.poly.TermOrder;
031import edu.jas.structure.GcdRingElem;
032import edu.jas.structure.RingElem;
033import edu.jas.structure.RingFactory;
034
035
036/**
037 * QuotSolvablePolynomialRing generic recursive solvable polynomial factory
038 * implementing RingFactory and extending GenSolvablePolynomialRing factory.
039 * Factory for n-variate ordered solvable polynomials over solvable polynomial
040 * coefficients. The non-commutative multiplication relations are maintained in
041 * a relation table and the non-commutative multiplication relations between the
042 * coefficients and the main variables are maintained in a coefficient relation
043 * table. Almost immutable object, except variable names and relation table
044 * contents.
045 * @param <C> coefficient type.
046 * @author Heinz Kredel
047 * will be deprecated use QLRSolvablePolynomialRing
048 */
049
050public class QuotSolvablePolynomialRing<C extends GcdRingElem<C>> extends
051                GenSolvablePolynomialRing<SolvableQuotient<C>> {
052
053
054    /*
055     * The solvable multiplication relations between variables and coefficients.
056     */
057    //public final RelationTable<GenPolynomial<C>> coeffTable;
058
059
060    /**
061     * Recursive solvable polynomial ring with polynomial coefficients.
062     */
063    public final RecSolvablePolynomialRing<C> polCoeff;
064
065
066    /**
067     * The constant polynomial 0 for this ring. Hides super ZERO.
068     */
069    public final QuotSolvablePolynomial<C> ZERO;
070
071
072    /**
073     * The constant polynomial 1 for this ring. Hides super ONE.
074     */
075    public final QuotSolvablePolynomial<C> ONE;
076
077
078    private static final Logger logger = Logger.getLogger(QuotSolvablePolynomialRing.class);
079
080
081    //private final boolean debug = logger.isDebugEnabled();
082
083
084    /**
085     * The constructor creates a solvable polynomial factory object with the
086     * default term order and commutative relations.
087     * @param cf factory for coefficients of type C.
088     * @param n number of variables.
089     */
090    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, int n) {
091        this(cf, n, new TermOrder(), null, null);
092    }
093
094
095    /**
096     * The constructor creates a solvable polynomial factory object with the
097     * default term order.
098     * @param cf factory for coefficients of type C.
099     * @param n number of variables.
100     * @param rt solvable multiplication relations.
101     */
102    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, int n,
103                    RelationTable<SolvableQuotient<C>> rt) {
104        this(cf, n, new TermOrder(), null, rt);
105    }
106
107
108    /**
109     * The constructor creates a solvable polynomial factory object with the
110     * given term order and commutative relations.
111     * @param cf factory for coefficients of type C.
112     * @param n number of variables.
113     * @param t a term order.
114     */
115    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, int n, TermOrder t) {
116        this(cf, n, t, null, null);
117    }
118
119
120    /**
121     * The constructor creates a solvable polynomial factory object with the
122     * given term order.
123     * @param cf factory for coefficients of type C.
124     * @param n number of variables.
125     * @param t a term order.
126     * @param rt solvable multiplication relations.
127     */
128    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, int n, TermOrder t,
129                    RelationTable<SolvableQuotient<C>> rt) {
130        this(cf, n, t, null, rt);
131    }
132
133
134    /**
135     * The constructor creates a solvable polynomial factory object with the
136     * given term order and commutative relations.
137     * @param cf factory for coefficients of type C.
138     * @param n number of variables.
139     * @param t a term order.
140     * @param v names for the variables.
141     */
142    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, int n, TermOrder t, String[] v) {
143        this(cf, n, t, v, null);
144    }
145
146
147    /**
148     * The constructor creates a solvable polynomial factory object with the
149     * given term order and commutative relations.
150     * @param cf factory for coefficients of type C.
151     * @param t a term order.
152     * @param v names for the variables.
153     */
154    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, TermOrder t, String[] v) {
155        this(cf, v.length, t, v, null);
156    }
157
158
159    /**
160     * The constructor creates a solvable polynomial factory object with the
161     * default term order.
162     * @param cf factory for coefficients of type C.
163     * @param v names for the variables.
164     */
165    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, String[] v) {
166        this(cf, v.length, new TermOrder(), v, null);
167    }
168
169
170    /**
171     * The constructor creates a solvable polynomial factory object with the
172     * given term order.
173     * @param cf factory for coefficients of type C.
174     * @param n number of variables.
175     * @param t a term order.
176     * @param v names for the variables.
177     * @param rt solvable multiplication relations.
178     */
179    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, int n, TermOrder t, String[] v,
180                    RelationTable<SolvableQuotient<C>> rt) {
181        super(cf, n, t, v, rt);
182        //if (rt == null) { // handled in super }
183        SolvableQuotientRing<C> cfring = (SolvableQuotientRing<C>) cf; // == coFac
184        polCoeff = new RecSolvablePolynomialRing<C>(cfring.ring, n, t, v);
185        if (table.size() > 0) { // TODO
186            ExpVector e = null;
187            ExpVector f = null;
188            GenSolvablePolynomial<GenPolynomial<C>> p = null;
189            polCoeff.table.update(e, f, p); // from rt
190        }
191        //coeffTable = polCoeff.coeffTable; //new RelationTable<GenPolynomial<C>>(polCoeff, true);
192        ZERO = new QuotSolvablePolynomial<C>(this);
193        SolvableQuotient<C> coeff = coFac.getONE();
194        //evzero = ExpVector.create(nvar); // from super
195        ONE = new QuotSolvablePolynomial<C>(this, coeff, evzero);
196    }
197
198
199    /**
200     * The constructor creates a solvable polynomial factory object with the the
201     * same term order, number of variables and variable names as the given
202     * polynomial factory, only the coefficient factories differ and the
203     * solvable multiplication relations are <b>empty</b>.
204     * @param cf factory for coefficients of type C.
205     * @param o other solvable polynomial ring.
206     */
207    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, GenSolvablePolynomialRing o) {
208        this(cf, o.nvar, o.tord, o.getVars(), null);
209    }
210
211
212    /**
213     * The constructor creates a solvable polynomial factory object with the the
214     * same term order, number of variables and variable names as the given
215     * polynomial factory, only the coefficient factories differ and the
216     * solvable multiplication relations are <b>empty</b>.
217     * @param cf factory for coefficients of type C.
218     * @param o other solvable polynomial ring.
219     */
220    public QuotSolvablePolynomialRing(RingFactory<SolvableQuotient<C>> cf, QuotSolvablePolynomialRing o) {
221        this(cf, (GenSolvablePolynomialRing) o);
222    }
223
224
225    /**
226     * Get the String representation.
227     * @see java.lang.Object#toString()
228     */
229    @Override
230    public String toString() {
231        String res = super.toString();
232        if (PrettyPrint.isTrue()) {
233            //res += "\n" + table.toString(vars);
234            res += "\n" + polCoeff.coeffTable.toString(vars);
235        } else {
236            res += ", #rel = " + table.size() + " + " + polCoeff.coeffTable.size();
237        }
238        return res;
239    }
240
241
242    /**
243     * Get a scripting compatible string representation.
244     * @return script compatible representation for this Element.
245     * @see edu.jas.structure.Element#toScript()
246     */
247    @Override
248    public String toScript() {
249        StringBuffer s = new StringBuffer();
250        switch (Scripting.getLang()) {
251        case Ruby:
252            s.append("SolvPolyRing.new(");
253            break;
254        case Python:
255        default:
256            s.append("SolvPolyRing(");
257        }
258        if (coFac instanceof RingElem) {
259            s.append(((RingElem<SolvableQuotient<C>>) coFac).toScriptFactory());
260        } else {
261            s.append(coFac.toScript().trim());
262        }
263        s.append(",\"" + varsToString() + "\",");
264        String to = tord.toString();
265        if (tord.getEvord() == TermOrder.INVLEX) {
266            to = "PolyRing.lex";
267        }
268        if (tord.getEvord() == TermOrder.IGRLEX) {
269            to = "PolyRing.grad";
270        }
271        s.append(to);
272        if (table.size() > 0) {
273            String rel = table.toScript();
274            s.append(",rel=");
275            s.append(rel);
276        }
277        if (polCoeff.coeffTable.size() > 0) {
278            String rel = polCoeff.coeffTable.toScript();
279            s.append(",coeffrel=");
280            s.append(rel);
281        }
282        s.append(")");
283        return s.toString();
284    }
285
286
287    /**
288     * Comparison with any other object.
289     * @see java.lang.Object#equals(java.lang.Object)
290     */
291    @Override
292    @SuppressWarnings("unchecked")
293    public boolean equals(Object other) {
294        if (other == null) {
295            return false;
296        }
297        if (!(other instanceof QuotSolvablePolynomialRing)) {
298            return false;
299        }
300        QuotSolvablePolynomialRing<C> oring = (QuotSolvablePolynomialRing<C>) other;
301        // do a super.equals( )
302        if (!super.equals(other)) {
303            return false;
304        }
305        // check same base relations
306        //if ( ! table.equals(oring.table) ) { // done in super
307        //    return false;
308        //}
309        if (!polCoeff.coeffTable.equals(oring.polCoeff.coeffTable)) {
310            return false;
311        }
312        return true;
313    }
314
315
316    /**
317     * Hash code for this polynomial ring.
318     * @see java.lang.Object#hashCode()
319     */
320    @Override
321    public int hashCode() {
322        int h;
323        h = super.hashCode();
324        h = 37 * h + table.hashCode(); // may be different after some computations
325        h = 37 * h + polCoeff.coeffTable.hashCode(); // may be different
326        return h;
327    }
328
329
330    /**
331     * Get the zero element.
332     * @return 0 as QuotSolvablePolynomial<C>.
333     */
334    @Override
335    public QuotSolvablePolynomial<C> getZERO() {
336        return ZERO;
337    }
338
339
340    /**
341     * Get the one element.
342     * @return 1 as QuotSolvablePolynomial<C>.
343     */
344    @Override
345    public QuotSolvablePolynomial<C> getONE() {
346        return ONE;
347    }
348
349
350    /**
351     * Query if this ring is commutative.
352     * @return true if this ring is commutative, else false.
353     */
354    @Override
355    public boolean isCommutative() {
356        if (polCoeff.coeffTable.size() == 0) {
357            return super.isCommutative();
358        }
359        // todo: check structure of relations
360        return false;
361    }
362
363
364    /**
365     * Query if this ring is associative. Test if the relations between the mian
366     * variables and the coefficient generators define an associative solvable
367     * ring.
368     * @return true, if this ring is associative, else false.
369     */
370    @Override
371    public boolean isAssociative() {
372        if (!coFac.isAssociative()) {
373            return false;
374        }
375        QuotSolvablePolynomial<C> Xi, Xj, Xk, p, q;
376        List<GenPolynomial<SolvableQuotient<C>>> gens = generators();
377        int ngen = gens.size();
378        for (int i = 0; i < ngen; i++) {
379            Xi = (QuotSolvablePolynomial<C>) gens.get(i);
380            for (int j = i + 1; j < ngen; j++) {
381                Xj = (QuotSolvablePolynomial<C>) gens.get(j);
382                for (int k = j + 1; k < ngen; k++) {
383                    Xk = (QuotSolvablePolynomial<C>) gens.get(k);
384                    p = Xk.multiply(Xj).multiply(Xi);
385                    q = Xk.multiply(Xj.multiply(Xi));
386                    if (!p.equals(q)) {
387                        if (logger.isInfoEnabled()) {
388                            logger.info("Xk = " + Xk + ", Xj = " + Xj + ", Xi = " + Xi);
389                            logger.info("p = ( Xk * Xj ) * Xi = " + p);
390                            logger.info("q = Xk * ( Xj * Xi ) = " + q);
391                            logger.info("q-p = " + p.subtract(q));
392                        }
393                        return false;
394                    }
395                }
396            }
397        }
398        return true; //coFac.isAssociative();
399    }
400
401
402    /**
403     * Get a (constant) QuotSolvablePolynomial&lt;C&gt; element from a long
404     * value.
405     * @param a long.
406     * @return a QuotSolvablePolynomial&lt;C&gt;.
407     */
408    @Override
409    public QuotSolvablePolynomial<C> fromInteger(long a) {
410        return new QuotSolvablePolynomial<C>(this, coFac.fromInteger(a), evzero);
411    }
412
413
414    /**
415     * Get a (constant) QuotSolvablePolynomial&lt;C&gt; element from a
416     * BigInteger value.
417     * @param a BigInteger.
418     * @return a QuotSolvablePolynomial&lt;C&gt;.
419     */
420    @Override
421    public QuotSolvablePolynomial<C> fromInteger(BigInteger a) {
422        return new QuotSolvablePolynomial<C>(this, coFac.fromInteger(a), evzero);
423    }
424
425
426    /**
427     * Random solvable polynomial. Generates a random solvable polynomial with k
428     * = 5, l = n, d = (nvar == 1) ? n : 3, q = (nvar == 1) ? 0.7 : 0.3.
429     * @param n number of terms.
430     * @return a random solvable polynomial.
431     */
432    @Override
433    public QuotSolvablePolynomial<C> random(int n) {
434        return random(n, random);
435    }
436
437
438    /**
439     * Random solvable polynomial. Generates a random solvable polynomial with k
440     * = 5, l = n, d = (nvar == 1) ? n : 3, q = (nvar == 1) ? 0.7 : 0.3.
441     * @param n number of terms.
442     * @param rnd is a source for random bits.
443     * @return a random solvable polynomial.
444     */
445    @Override
446    public QuotSolvablePolynomial<C> random(int n, Random rnd) {
447        if (nvar == 1) {
448            return random(5, n, n, 0.7f, rnd);
449        }
450        return random(5, n, 3, 0.3f, rnd);
451    }
452
453
454    /**
455     * Generate a random solvable polynomial.
456     * @param k bitsize of random coefficients.
457     * @param l number of terms.
458     * @param d maximal degree in each variable.
459     * @param q density of nozero exponents.
460     * @return a random solvable polynomial.
461     */
462    @Override
463    public QuotSolvablePolynomial<C> random(int k, int l, int d, float q) {
464        return random(k, l, d, q, random);
465    }
466
467
468    /**
469     * Random solvable polynomial.
470     * @param k size of random coefficients.
471     * @param l number of terms.
472     * @param d maximal degree in each variable.
473     * @param q density of nozero exponents.
474     * @param rnd is a source for random bits.
475     * @return a random solvable polynomial.
476     */
477    @Override
478    public QuotSolvablePolynomial<C> random(int k, int l, int d, float q, Random rnd) {
479        QuotSolvablePolynomial<C> r = getZERO(); // copy( ZERO ); 
480        ExpVector e;
481        SolvableQuotient<C> a;
482        // add random coeffs and exponents
483        for (int i = 0; i < l; i++) {
484            e = ExpVector.EVRAND(nvar, d, q, rnd);
485            a = coFac.random(k, rnd);
486            r = (QuotSolvablePolynomial<C>) r.sum(a, e);
487            // somewhat inefficient but clean
488        }
489        return r;
490    }
491
492
493    /**
494     * Copy polynomial c.
495     * @param c
496     * @return a copy of c.
497     */
498    public QuotSolvablePolynomial<C> copy(QuotSolvablePolynomial<C> c) {
499        return new QuotSolvablePolynomial<C>(this, c.getMap());
500    }
501
502
503    /**
504     * Parse a solvable polynomial with the use of GenPolynomialTokenizer
505     * @param s String.
506     * @return QuotSolvablePolynomial from s.
507     */
508    @Override
509    public QuotSolvablePolynomial<C> parse(String s) {
510        return parse(new StringReader(s));
511    }
512
513
514    /**
515     * Parse a solvable polynomial with the use of GenPolynomialTokenizer
516     * @param r Reader.
517     * @return next QuotSolvablePolynomial from r.
518     */
519    @Override
520    @SuppressWarnings("unchecked")
521    public QuotSolvablePolynomial<C> parse(Reader r) {
522        GenPolynomialTokenizer pt = new GenPolynomialTokenizer(this, r);
523        QuotSolvablePolynomial<C> p = null;
524        try {
525            GenSolvablePolynomial<SolvableQuotient<C>> s = pt.nextSolvablePolynomial();
526            p = new QuotSolvablePolynomial<C>(this, s);
527        } catch (IOException e) {
528            logger.error(e.toString() + " parse " + this);
529            p = ZERO;
530        }
531        return p;
532    }
533
534
535    /**
536     * Generate univariate solvable polynomial in a given variable.
537     * @param i the index of the variable.
538     * @return X_i as solvable univariate polynomial.
539     */
540    @Override
541    public QuotSolvablePolynomial<C> univariate(int i) {
542        return (QuotSolvablePolynomial<C>) super.univariate(i);
543    }
544
545
546    /**
547     * Generate univariate solvable polynomial in a given variable with given
548     * exponent.
549     * @param i the index of the variable.
550     * @param e the exponent of the variable.
551     * @return X_i^e as solvable univariate polynomial.
552     */
553    @Override
554    public QuotSolvablePolynomial<C> univariate(int i, long e) {
555        return (QuotSolvablePolynomial<C>) super.univariate(i, e);
556    }
557
558
559    /**
560     * Generate univariate solvable polynomial in a given variable with given
561     * exponent.
562     * @param modv number of module variables.
563     * @param i the index of the variable.
564     * @param e the exponent of the variable.
565     * @return X_i^e as solvable univariate polynomial.
566     */
567    @Override
568    public QuotSolvablePolynomial<C> univariate(int modv, int i, long e) {
569        return (QuotSolvablePolynomial<C>) super.univariate(modv, i, e);
570    }
571
572
573    /**
574     * Generate list of univariate polynomials in all variables.
575     * @return List(X_1,...,X_n) a list of univariate polynomials.
576     */
577    //todo Override
578    @SuppressWarnings("unchecked")
579    public List<QuotSolvablePolynomial<C>> recUnivariateList() {
580        //return castToSolvableList( super.univariateList() );
581        return (List<QuotSolvablePolynomial<C>>) (Object) univariateList(0, 1L);
582    }
583
584
585    /**
586     * Generate list of univariate polynomials in all variables.
587     * @param modv number of module variables.
588     * @return List(X_1,...,X_n) a list of univariate polynomials.
589     */
590    //todo Override
591    @SuppressWarnings("unchecked")
592    public List<QuotSolvablePolynomial<C>> recUnivariateList(int modv) {
593        return (List<QuotSolvablePolynomial<C>>) (Object) univariateList(modv, 1L);
594    }
595
596
597    /**
598     * Generate list of univariate polynomials in all variables with given
599     * exponent.
600     * @param modv number of module variables.
601     * @param e the exponent of the variables.
602     * @return List(X_1^e,...,X_n^e) a list of univariate polynomials.
603     */
604    //todo Override
605    public List<QuotSolvablePolynomial<C>> recUnivariateList(int modv, long e) {
606        List<QuotSolvablePolynomial<C>> pols = new ArrayList<QuotSolvablePolynomial<C>>(nvar);
607        int nm = nvar - modv;
608        for (int i = 0; i < nm; i++) {
609            QuotSolvablePolynomial<C> p = univariate(modv, nm - 1 - i, e);
610            pols.add(p);
611        }
612        return pols;
613    }
614
615
616    /*
617     * Generate list of univariate polynomials in all variables with given exponent.
618     * @param modv number of module variables.
619     * @param e the exponent of the variables.
620     * @return List(X_1^e,...,X_n^e) a list of univariate polynomials.
621     @Override
622     public List<QuotSolvablePolynomial<C>> univariateList(int modv, long e) {
623     List<GenPolynomial<C>> pol = super.univariateList(modv,e);
624     UnaryFunctor<GenPolynomial<C>,QuotSolvablePolynomial<C>> fc 
625     = new UnaryFunctor<GenPolynomial<C>,QuotSolvablePolynomial<C>>() {
626     public QuotSolvablePolynomial<C> eval(GenPolynomial<C> p) {
627     if ( ! (p instanceof QuotSolvablePolynomial) ) {
628     throw new RuntimeException("no solvable polynomial "+p);
629     }
630     return (QuotSolvablePolynomial<C>) p;
631     }
632     };
633     List<QuotSolvablePolynomial<C>> pols 
634     = ListUtil.<GenPolynomial<C>,QuotSolvablePolynomial<C>>map(this,pol,fc);
635     return pols;
636     }
637    */
638
639
640    /**
641     * Extend variables. Used e.g. in module embedding. Extend number of
642     * variables by i.
643     * @param i number of variables to extend.
644     * @return extended solvable polynomial ring factory.
645     */
646    @Override
647    public QuotSolvablePolynomialRing<C> extend(int i) {
648        GenPolynomialRing<SolvableQuotient<C>> pfac = super.extend(i);
649        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
650                        pfac.tord, pfac.getVars());
651        spfac.table.extend(this.table);
652        spfac.polCoeff.coeffTable.extend(this.polCoeff.coeffTable);
653        return spfac;
654    }
655
656
657    /**
658     * Contract variables. Used e.g. in module embedding. Contract number of
659     * variables by i.
660     * @param i number of variables to remove.
661     * @return contracted solvable polynomial ring factory.
662     */
663    @Override
664    public QuotSolvablePolynomialRing<C> contract(int i) {
665        GenPolynomialRing<SolvableQuotient<C>> pfac = super.contract(i);
666        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
667                        pfac.tord, pfac.getVars());
668        spfac.table.contract(this.table);
669        spfac.polCoeff.coeffTable.contract(this.polCoeff.coeffTable);
670        return spfac;
671    }
672
673
674    /**
675     * Reverse variables. Used e.g. in opposite rings.
676     * @return solvable polynomial ring factory with reversed variables.
677     */
678    @Override
679    public QuotSolvablePolynomialRing<C> reverse() {
680        return reverse(false);
681    }
682
683
684    /**
685     * Reverse variables. Used e.g. in opposite rings.
686     * @param partial true for partialy reversed term orders.
687     * @return solvable polynomial ring factory with reversed variables.
688     */
689    @Override
690    public QuotSolvablePolynomialRing<C> reverse(boolean partial) {
691        GenPolynomialRing<SolvableQuotient<C>> pfac = super.reverse(partial);
692        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
693                        pfac.tord, pfac.getVars());
694        spfac.partial = partial;
695        spfac.table.reverse(this.table);
696        spfac.polCoeff.coeffTable.reverse(this.polCoeff.coeffTable);
697        return spfac;
698    }
699
700
701    /**
702     * Rational function from integral polynomial coefficients. Represent as
703     * polynomial with type SolvableQuotient<C> coefficients.
704     * @param A polynomial with integral polynomial coefficients to be
705     *            converted.
706     * @return polynomial with type SolvableQuotient<C> coefficients.
707     */
708    public QuotSolvablePolynomial<C> fromPolyCoefficients(GenSolvablePolynomial<GenPolynomial<C>> A) {
709        QuotSolvablePolynomial<C> B = getZERO().copy();
710        if (A == null || A.isZERO()) {
711            return B;
712        }
713        RingFactory<SolvableQuotient<C>> cfac = coFac;
714        SolvableQuotientRing<C> qfac = (SolvableQuotientRing<C>) cfac;
715        for (Map.Entry<ExpVector, GenPolynomial<C>> y : A.getMap().entrySet()) {
716            ExpVector e = y.getKey();
717            GenSolvablePolynomial<C> a = (GenSolvablePolynomial<C>) y.getValue();
718            SolvableQuotient<C> p = new SolvableQuotient<C>(qfac, a); // can not be zero
719            if (!p.isZERO()) {
720                //B = B.sum( p, e ); // inefficient
721                B.doPutToMap(e, p);
722            }
723        }
724        return B;
725    }
726
727
728    /**
729     * Integral function from rational polynomial coefficients. Represent as
730     * polynomial with type GenSolvablePolynomial<C> coefficients.
731     * @param A polynomial with rational polynomial coefficients to be
732     *            converted.
733     * @return polynomial with type GenSolvablePolynomial<C> coefficients.
734     */
735    public RecSolvablePolynomial<C> toPolyCoefficients(QuotSolvablePolynomial<C> A) {
736        RecSolvablePolynomial<C> B = polCoeff.getZERO().copy();
737        if (A == null || A.isZERO()) {
738            return B;
739        }
740        for (Map.Entry<ExpVector, SolvableQuotient<C>> y : A.getMap().entrySet()) {
741            ExpVector e = y.getKey();
742            SolvableQuotient<C> a = y.getValue();
743            if (!a.den.isONE()) {
744                throw new IllegalArgumentException("den != 1 not supported: " + a);
745            }
746            GenPolynomial<C> p = a.num; // can not be zero
747            if (!p.isZERO()) {
748                //B = B.sum( p, e ); // inefficient
749                B.doPutToMap(e, p);
750            }
751        }
752        return B;
753    }
754
755
756    /**
757     * Integral function from rational polynomial coefficients. Represent as
758     * polynomial with type GenSolvablePolynomial<C> coefficients.
759     * @param A polynomial with rational polynomial coefficients to be
760     *            converted.
761     * @return polynomial with type GenSolvablePolynomial<C> coefficients.
762     */
763    public RecSolvablePolynomial<C> toPolyCoefficients(GenPolynomial<SolvableQuotient<C>> A) {
764        RecSolvablePolynomial<C> B = polCoeff.getZERO().copy();
765        if (A == null || A.isZERO()) {
766            return B;
767        }
768        for (Map.Entry<ExpVector, SolvableQuotient<C>> y : A.getMap().entrySet()) {
769            ExpVector e = y.getKey();
770            SolvableQuotient<C> a = y.getValue();
771            if (!a.den.isONE()) {
772                throw new IllegalArgumentException("den != 1 not supported: " + a);
773            }
774            GenPolynomial<C> p = a.num; // can not be zero
775            if (!p.isZERO()) {
776                //B = B.sum( p, e ); // inefficient
777                B.doPutToMap(e, p);
778            }
779        }
780        return B;
781    }
782
783}