001/*
002 * $Id$
003 */
004
005package edu.jas.fd;
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.logging.log4j.LogManager;
018import org.apache.logging.log4j.Logger;
019
020import edu.jas.kern.PrettyPrint;
021import edu.jas.kern.Scripting;
022import edu.jas.poly.ExpVector;
023import edu.jas.poly.GenPolynomial;
024import edu.jas.poly.GenPolynomialRing;
025import edu.jas.poly.GenPolynomialTokenizer;
026import edu.jas.poly.GenSolvablePolynomial;
027import edu.jas.poly.GenSolvablePolynomialRing;
028import edu.jas.poly.RecSolvablePolynomial;
029import edu.jas.poly.RecSolvablePolynomialRing;
030import edu.jas.poly.RelationTable;
031import edu.jas.poly.TermOrder;
032import edu.jas.structure.GcdRingElem;
033import edu.jas.structure.RingElem;
034import edu.jas.structure.RingFactory;
035
036
037/**
038 * QuotSolvablePolynomialRing generic recursive solvable polynomial factory
039 * implementing RingFactory and extending GenSolvablePolynomialRing factory.
040 * Factory for n-variate ordered solvable polynomials over solvable polynomial
041 * coefficients. The non-commutative multiplication relations are maintained in
042 * a relation table and the non-commutative multiplication relations between the
043 * coefficients and the main variables are maintained in a coefficient relation
044 * table. Almost immutable object, except variable names and relation table
045 * contents. Will be deprecated use <code>QLRSolvablePolynomialRing</code>
046 * @param <C> coefficient type
047 * @author Heinz Kredel
048 */
049
050public class QuotSolvablePolynomialRing<C extends GcdRingElem<C>>
051                extends 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 = LogManager.getLogger(QuotSolvablePolynomialRing.class);
079
080
081    //private static 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.toScript();
265        s.append(to);
266        if (table.size() > 0) {
267            String rel = table.toScript();
268            s.append(",rel=");
269            s.append(rel);
270        }
271        if (polCoeff.coeffTable.size() > 0) {
272            String rel = polCoeff.coeffTable.toScript();
273            s.append(",coeffrel=");
274            s.append(rel);
275        }
276        s.append(")");
277        return s.toString();
278    }
279
280
281    /**
282     * Comparison with any other object.
283     * @see java.lang.Object#equals(java.lang.Object)
284     */
285    @Override
286    @SuppressWarnings("unchecked")
287    public boolean equals(Object other) {
288        if (other == null) {
289            return false;
290        }
291        if (!(other instanceof QuotSolvablePolynomialRing)) {
292            return false;
293        }
294        QuotSolvablePolynomialRing<C> oring = (QuotSolvablePolynomialRing<C>) other;
295        // do a super.equals( )
296        if (!super.equals(other)) {
297            return false;
298        }
299        // check same base relations
300        //if ( ! table.equals(oring.table) ) { // done in super
301        //    return false;
302        //}
303        if (!polCoeff.coeffTable.equals(oring.polCoeff.coeffTable)) {
304            return false;
305        }
306        return true;
307    }
308
309
310    /**
311     * Hash code for this polynomial ring.
312     * @see java.lang.Object#hashCode()
313     */
314    @Override
315    public int hashCode() {
316        int h;
317        h = super.hashCode();
318        h = 37 * h + table.hashCode(); // may be different after some computations
319        h = 37 * h + polCoeff.coeffTable.hashCode(); // may be different
320        return h;
321    }
322
323
324    /**
325     * Get the zero element.
326     * @return 0 as QuotSolvablePolynomial<C>.
327     */
328    @Override
329    public QuotSolvablePolynomial<C> getZERO() {
330        return ZERO;
331    }
332
333
334    /**
335     * Get the one element.
336     * @return 1 as QuotSolvablePolynomial<C>.
337     */
338    @Override
339    public QuotSolvablePolynomial<C> getONE() {
340        return ONE;
341    }
342
343
344    /**
345     * Query if this ring is commutative.
346     * @return true if this ring is commutative, else false.
347     */
348    @Override
349    public boolean isCommutative() {
350        if (polCoeff.coeffTable.size() == 0) {
351            return super.isCommutative();
352        }
353        // todo: check structure of relations
354        return false;
355    }
356
357
358    /**
359     * Query if this ring is associative. Test if the relations between the mian
360     * variables and the coefficient generators define an associative solvable
361     * ring.
362     * @return true, if this ring is associative, else false.
363     */
364    @Override
365    public boolean isAssociative() {
366        if (!coFac.isAssociative()) {
367            return false;
368        }
369        QuotSolvablePolynomial<C> Xi, Xj, Xk, p, q;
370        List<GenPolynomial<SolvableQuotient<C>>> gens = generators();
371        int ngen = gens.size();
372        for (int i = 0; i < ngen; i++) {
373            Xi = (QuotSolvablePolynomial<C>) gens.get(i);
374            for (int j = i + 1; j < ngen; j++) {
375                Xj = (QuotSolvablePolynomial<C>) gens.get(j);
376                for (int k = j + 1; k < ngen; k++) {
377                    Xk = (QuotSolvablePolynomial<C>) gens.get(k);
378                    p = Xk.multiply(Xj).multiply(Xi);
379                    q = Xk.multiply(Xj.multiply(Xi));
380                    if (!p.equals(q)) {
381                        logger.info("Xk = {}, Xj = {}, Xi = {}", Xk, Xj, Xi);
382                        logger.info("p = ( Xk * Xj ) * Xi = {}", p);
383                        logger.info("q = Xk * ( Xj * Xi ) = {}", q);
384                        logger.info("q-p = {}", p.subtract(q));
385                        return false;
386                    }
387                }
388            }
389        }
390        return true; //coFac.isAssociative();
391    }
392
393
394    /**
395     * Get a (constant) QuotSolvablePolynomial&lt;C&gt; element from a long
396     * value.
397     * @param a long.
398     * @return a QuotSolvablePolynomial&lt;C&gt;.
399     */
400    @Override
401    public QuotSolvablePolynomial<C> fromInteger(long a) {
402        return new QuotSolvablePolynomial<C>(this, coFac.fromInteger(a), evzero);
403    }
404
405
406    /**
407     * Get a (constant) QuotSolvablePolynomial&lt;C&gt; element from a
408     * BigInteger value.
409     * @param a BigInteger.
410     * @return a QuotSolvablePolynomial&lt;C&gt;.
411     */
412    @Override
413    public QuotSolvablePolynomial<C> fromInteger(BigInteger a) {
414        return new QuotSolvablePolynomial<C>(this, coFac.fromInteger(a), evzero);
415    }
416
417
418    /**
419     * Random solvable polynomial. Generates a random solvable polynomial with k
420     * = 5, l = n, d = (nvar == 1) ? n : 3, q = (nvar == 1) ? 0.7 : 0.3.
421     * @param n number of terms.
422     * @return a random solvable polynomial.
423     */
424    @Override
425    public QuotSolvablePolynomial<C> random(int n) {
426        return random(n, random);
427    }
428
429
430    /**
431     * Random solvable polynomial. Generates a random solvable polynomial with k
432     * = 5, l = n, d = (nvar == 1) ? n : 3, q = (nvar == 1) ? 0.7 : 0.3.
433     * @param n number of terms.
434     * @param rnd is a source for random bits.
435     * @return a random solvable polynomial.
436     */
437    @Override
438    public QuotSolvablePolynomial<C> random(int n, Random rnd) {
439        if (nvar == 1) {
440            return random(5, n, n, 0.7f, rnd);
441        }
442        return random(5, n, 3, 0.3f, rnd);
443    }
444
445
446    /**
447     * Generate a random solvable polynomial.
448     * @param k bitsize of random coefficients.
449     * @param l number of terms.
450     * @param d maximal degree in each variable.
451     * @param q density of nozero exponents.
452     * @return a random solvable polynomial.
453     */
454    @Override
455    public QuotSolvablePolynomial<C> random(int k, int l, int d, float q) {
456        return random(k, l, d, q, random);
457    }
458
459
460    /**
461     * Random solvable polynomial.
462     * @param k size of random coefficients.
463     * @param l number of terms.
464     * @param d maximal degree in each variable.
465     * @param q density of nozero exponents.
466     * @param rnd is a source for random bits.
467     * @return a random solvable polynomial.
468     */
469    @Override
470    public QuotSolvablePolynomial<C> random(int k, int l, int d, float q, Random rnd) {
471        QuotSolvablePolynomial<C> r = getZERO(); // copy( ZERO ); 
472        ExpVector e;
473        SolvableQuotient<C> a;
474        // add random coeffs and exponents
475        for (int i = 0; i < l; i++) {
476            e = ExpVector.random(nvar, d, q, rnd);
477            a = coFac.random(k, rnd);
478            r = (QuotSolvablePolynomial<C>) r.sum(a, e);
479            // somewhat inefficient but clean
480        }
481        return r;
482    }
483
484
485    /**
486     * Copy polynomial c.
487     * @param c
488     * @return a copy of c.
489     */
490    public QuotSolvablePolynomial<C> copy(QuotSolvablePolynomial<C> c) {
491        return new QuotSolvablePolynomial<C>(this, c.getMap());
492    }
493
494
495    /**
496     * Parse a solvable polynomial with the use of GenPolynomialTokenizer
497     * @param s String.
498     * @return QuotSolvablePolynomial from s.
499     */
500    @Override
501    public QuotSolvablePolynomial<C> parse(String s) {
502        return parse(new StringReader(s));
503    }
504
505
506    /**
507     * Parse a solvable polynomial with the use of GenPolynomialTokenizer
508     * @param r Reader.
509     * @return next QuotSolvablePolynomial from r.
510     */
511    @Override
512    @SuppressWarnings("unchecked")
513    public QuotSolvablePolynomial<C> parse(Reader r) {
514        GenPolynomialTokenizer pt = new GenPolynomialTokenizer(this, r);
515        QuotSolvablePolynomial<C> p = null;
516        try {
517            GenSolvablePolynomial<SolvableQuotient<C>> s = pt.nextSolvablePolynomial();
518            p = new QuotSolvablePolynomial<C>(this, s);
519        } catch (IOException e) {
520            logger.error("{} parse {}", e, this);
521            p = ZERO;
522        }
523        return p;
524    }
525
526
527    /**
528     * Generate univariate solvable polynomial in a given variable.
529     * @param i the index of the variable.
530     * @return X_i as solvable univariate polynomial.
531     */
532    @Override
533    public QuotSolvablePolynomial<C> univariate(int i) {
534        return (QuotSolvablePolynomial<C>) super.univariate(i);
535    }
536
537
538    /**
539     * Generate univariate solvable polynomial in a given variable with given
540     * exponent.
541     * @param i the index of the variable.
542     * @param e the exponent of the variable.
543     * @return X_i^e as solvable univariate polynomial.
544     */
545    @Override
546    public QuotSolvablePolynomial<C> univariate(int i, long e) {
547        return (QuotSolvablePolynomial<C>) super.univariate(i, e);
548    }
549
550
551    /**
552     * Generate univariate solvable polynomial in a given variable with given
553     * exponent.
554     * @param modv number of module variables.
555     * @param i the index of the variable.
556     * @param e the exponent of the variable.
557     * @return X_i^e as solvable univariate polynomial.
558     */
559    @Override
560    public QuotSolvablePolynomial<C> univariate(int modv, int i, long e) {
561        return (QuotSolvablePolynomial<C>) super.univariate(modv, i, e);
562    }
563
564
565    /**
566     * Generate list of univariate polynomials in all variables.
567     * @return List(X_1,...,X_n) a list of univariate polynomials.
568     */
569    @Override
570    public List<QuotSolvablePolynomial<C>> univariateList() {
571        return univariateList(0, 1L);
572    }
573
574
575    /**
576     * Generate list of univariate polynomials in all variables.
577     * @param modv number of module variables.
578     * @return List(X_1,...,X_n) a list of univariate polynomials.
579     */
580    @Override
581    public List<QuotSolvablePolynomial<C>> univariateList(int modv) {
582        return univariateList(modv, 1L);
583    }
584
585
586    /**
587     * Generate list of univariate polynomials in all variables with given
588     * exponent.
589     * @param modv number of module variables.
590     * @param e the exponent of the variables.
591     * @return List(X_1^e,...,X_n^e) a list of univariate polynomials.
592     */
593    @Override
594    public List<QuotSolvablePolynomial<C>> univariateList(int modv, long e) {
595        List<QuotSolvablePolynomial<C>> pols = new ArrayList<QuotSolvablePolynomial<C>>(nvar);
596        int nm = nvar - modv;
597        for (int i = 0; i < nm; i++) {
598            QuotSolvablePolynomial<C> p = univariate(modv, nm - 1 - i, e);
599            pols.add(p);
600        }
601        return pols;
602    }
603
604
605    /**
606     * Extend variables. Used e.g. in module embedding. Extend number of
607     * variables by i.
608     * @param i number of variables to extend.
609     * @return extended solvable polynomial ring factory.
610     */
611    @Override
612    public QuotSolvablePolynomialRing<C> extend(int i) {
613        return extend(i, false);
614    }
615
616
617    /**
618     * Extend variables. Used e.g. in module embedding. Extend number of
619     * variables by i.
620     * @param i number of variables to extend.
621     * @param top true for TOP term order, false for POT term order.
622     * @return extended solvable polynomial ring factory.
623     */
624    @Override
625    public QuotSolvablePolynomialRing<C> extend(int i, boolean top) {
626        GenPolynomialRing<SolvableQuotient<C>> pfac = super.extend(i, top);
627        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
628                        pfac.tord, pfac.getVars());
629        spfac.table.extend(this.table);
630        spfac.polCoeff.coeffTable.extend(this.polCoeff.coeffTable);
631        return spfac;
632    }
633
634
635    /**
636     * Extend variables. Used e.g. in module embedding. Extend number of
637     * variables by i.
638     * @param vn names for extended variables.
639     * @return extended solvable polynomial ring factory.
640     */
641    @Override
642    public QuotSolvablePolynomialRing<C> extend(String[] vn) {
643        return extend(vn, false);
644    }
645
646
647    /**
648     * Extend variables. Used e.g. in module embedding. Extend number of
649     * variables by i.
650     * @param vn names for extended variables.
651     * @param top true for TOP term order, false for POT term order.
652     * @return extended solvable polynomial ring factory.
653     */
654    @Override
655    public QuotSolvablePolynomialRing<C> extend(String[] vn, boolean top) {
656        GenPolynomialRing<SolvableQuotient<C>> pfac = super.extend(vn, top);
657        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
658                        pfac.tord, pfac.getVars());
659        spfac.table.extend(this.table);
660        spfac.polCoeff.coeffTable.extend(this.polCoeff.coeffTable);
661        return spfac;
662    }
663
664
665    /**
666     * Contract variables. Used e.g. in module embedding. Contract number of
667     * variables by i.
668     * @param i number of variables to remove.
669     * @return contracted solvable polynomial ring factory.
670     */
671    @Override
672    public QuotSolvablePolynomialRing<C> contract(int i) {
673        GenPolynomialRing<SolvableQuotient<C>> pfac = super.contract(i);
674        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
675                        pfac.tord, pfac.getVars());
676        spfac.table.contract(this.table);
677        spfac.polCoeff.coeffTable.contract(this.polCoeff.coeffTable);
678        return spfac;
679    }
680
681
682    /**
683     * Reverse variables. Used e.g. in opposite rings.
684     * @return solvable polynomial ring factory with reversed variables.
685     */
686    @Override
687    public QuotSolvablePolynomialRing<C> reverse() {
688        return reverse(false);
689    }
690
691
692    /**
693     * Reverse variables. Used e.g. in opposite rings.
694     * @param partial true for partialy reversed term orders.
695     * @return solvable polynomial ring factory with reversed variables.
696     */
697    @Override
698    public QuotSolvablePolynomialRing<C> reverse(boolean partial) {
699        GenPolynomialRing<SolvableQuotient<C>> pfac = super.reverse(partial);
700        QuotSolvablePolynomialRing<C> spfac = new QuotSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
701                        pfac.tord, pfac.getVars());
702        spfac.partial = partial;
703        spfac.table.reverse(this.table);
704        spfac.polCoeff.coeffTable.reverse(this.polCoeff.coeffTable);
705        return spfac;
706    }
707
708
709    /**
710     * Rational function from integral polynomial coefficients. Represent as
711     * polynomial with type SolvableQuotient<C> coefficients.
712     * @param A polynomial with integral polynomial coefficients to be
713     *            converted.
714     * @return polynomial with type SolvableQuotient<C> coefficients.
715     */
716    public QuotSolvablePolynomial<C> fromPolyCoefficients(GenSolvablePolynomial<GenPolynomial<C>> A) {
717        QuotSolvablePolynomial<C> B = getZERO().copy();
718        if (A == null || A.isZERO()) {
719            return B;
720        }
721        RingFactory<SolvableQuotient<C>> cfac = coFac;
722        SolvableQuotientRing<C> qfac = (SolvableQuotientRing<C>) cfac;
723        for (Map.Entry<ExpVector, GenPolynomial<C>> y : A.getMap().entrySet()) {
724            ExpVector e = y.getKey();
725            GenSolvablePolynomial<C> a = (GenSolvablePolynomial<C>) y.getValue();
726            SolvableQuotient<C> p = new SolvableQuotient<C>(qfac, a); // can not be zero
727            if (!p.isZERO()) {
728                //B = B.sum( p, e ); // inefficient
729                B.doPutToMap(e, p);
730            }
731        }
732        return B;
733    }
734
735
736    /**
737     * Integral function from rational polynomial coefficients. Represent as
738     * polynomial with type GenSolvablePolynomial<C> coefficients.
739     * @param A polynomial with rational polynomial coefficients to be
740     *            converted.
741     * @return polynomial with type GenSolvablePolynomial<C> coefficients.
742     */
743    public RecSolvablePolynomial<C> toPolyCoefficients(QuotSolvablePolynomial<C> A) {
744        RecSolvablePolynomial<C> B = polCoeff.getZERO().copy();
745        if (A == null || A.isZERO()) {
746            return B;
747        }
748        for (Map.Entry<ExpVector, SolvableQuotient<C>> y : A.getMap().entrySet()) {
749            ExpVector e = y.getKey();
750            SolvableQuotient<C> a = y.getValue();
751            if (!a.den.isONE()) {
752                throw new IllegalArgumentException("den != 1 not supported: " + a);
753            }
754            GenPolynomial<C> p = a.num; // can not be zero
755            if (!p.isZERO()) {
756                //B = B.sum( p, e ); // inefficient
757                B.doPutToMap(e, p);
758            }
759        }
760        return B;
761    }
762
763
764    /**
765     * Integral function from rational polynomial coefficients. Represent as
766     * polynomial with type GenSolvablePolynomial<C> coefficients.
767     * @param A polynomial with rational polynomial coefficients to be
768     *            converted.
769     * @return polynomial with type GenSolvablePolynomial<C> coefficients.
770     */
771    public RecSolvablePolynomial<C> toPolyCoefficients(GenPolynomial<SolvableQuotient<C>> A) {
772        RecSolvablePolynomial<C> B = polCoeff.getZERO().copy();
773        if (A == null || A.isZERO()) {
774            return B;
775        }
776        for (Map.Entry<ExpVector, SolvableQuotient<C>> y : A.getMap().entrySet()) {
777            ExpVector e = y.getKey();
778            SolvableQuotient<C> a = y.getValue();
779            if (!a.den.isONE()) {
780                throw new IllegalArgumentException("den != 1 not supported: " + a);
781            }
782            GenPolynomial<C> p = a.num; // can not be zero
783            if (!p.isZERO()) {
784                //B = B.sum( p, e ); // inefficient
785                B.doPutToMap(e, p);
786            }
787        }
788        return B;
789    }
790
791}