001/*
002 * $Id$
003 */
004
005package edu.jas.application;
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.Logger;
018import org.apache.logging.log4j.LogManager; 
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 * LocalSolvablePolynomialRing 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.
046 * @param <C> coefficient type.
047 * @author Heinz Kredel
048 * will be deprecated use QLRSolvablePolynomialRing
049 */
050
051public class LocalSolvablePolynomialRing<C extends GcdRingElem<C>> extends
052                GenSolvablePolynomialRing<SolvableLocal<C>> {
053
054
055    /**
056     * Recursive solvable polynomial ring with polynomial coefficients.
057     */
058    public final RecSolvablePolynomialRing<C> polCoeff;
059
060
061    /**
062     * The constant polynomial 0 for this ring. Hides super ZERO.
063     */
064    public final LocalSolvablePolynomial<C> ZERO;
065
066
067    /**
068     * The constant polynomial 1 for this ring. Hides super ONE.
069     */
070    public final LocalSolvablePolynomial<C> ONE;
071
072
073    private static final Logger logger = LogManager.getLogger(LocalSolvablePolynomialRing.class);
074
075
076    //private static final boolean debug = logger.isDebugEnabled();
077
078
079    /**
080     * The constructor creates a solvable polynomial factory object with the
081     * default term order and commutative relations.
082     * @param cf factory for coefficients of type C.
083     * @param n number of variables.
084     */
085    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, int n) {
086        this(cf, n, new TermOrder(), null, null);
087    }
088
089
090    /**
091     * The constructor creates a solvable polynomial factory object with the
092     * default term order.
093     * @param cf factory for coefficients of type C.
094     * @param n number of variables.
095     * @param rt solvable multiplication relations.
096     */
097    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, int n,
098                    RelationTable<SolvableLocal<C>> rt) {
099        this(cf, n, new TermOrder(), null, rt);
100    }
101
102
103    /**
104     * The constructor creates a solvable polynomial factory object with the
105     * given term order and commutative relations.
106     * @param cf factory for coefficients of type C.
107     * @param n number of variables.
108     * @param t a term order.
109     */
110    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, int n, TermOrder t) {
111        this(cf, n, t, null, null);
112    }
113
114
115    /**
116     * The constructor creates a solvable polynomial factory object with the
117     * given term order.
118     * @param cf factory for coefficients of type C.
119     * @param n number of variables.
120     * @param t a term order.
121     * @param rt solvable multiplication relations.
122     */
123    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, int n, TermOrder t,
124                    RelationTable<SolvableLocal<C>> rt) {
125        this(cf, n, t, null, rt);
126    }
127
128
129    /**
130     * The constructor creates a solvable polynomial factory object with the
131     * given term order and commutative relations.
132     * @param cf factory for coefficients of type C.
133     * @param n number of variables.
134     * @param t a term order.
135     * @param v names for the variables.
136     */
137    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, int n, TermOrder t, String[] v) {
138        this(cf, n, t, v, null);
139    }
140
141
142    /**
143     * The constructor creates a solvable polynomial factory object with the
144     * given term order and commutative relations.
145     * @param cf factory for coefficients of type C.
146     * @param t a term order.
147     * @param v names for the variables.
148     */
149    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, TermOrder t, String[] v) {
150        this(cf, v.length, t, v, null);
151    }
152
153
154    /**
155     * The constructor creates a solvable polynomial factory object with the
156     * default term order.
157     * @param cf factory for coefficients of type C.
158     * @param v names for the variables.
159     */
160    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, String[] v) {
161        this(cf, v.length, new TermOrder(), v, null);
162    }
163
164
165    /**
166     * The constructor creates a solvable polynomial factory object with the
167     * given term order.
168     * @param cf factory for coefficients of type C.
169     * @param n number of variables.
170     * @param t a term order.
171     * @param v names for the variables.
172     * @param rt solvable multiplication relations.
173     */
174    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, int n, TermOrder t, String[] v,
175                    RelationTable<SolvableLocal<C>> rt) {
176        super(cf, n, t, v, rt);
177        //if (rt == null) { // handled in super }
178        SolvableLocalRing<C> cfring = (SolvableLocalRing<C>) cf; // == coFac
179        polCoeff = new RecSolvablePolynomialRing<C>(cfring.ring, n, t, v);
180        if (table.size() > 0) {
181            List<GenSolvablePolynomial<GenPolynomial<C>>> nt
182                = new ArrayList<GenSolvablePolynomial<GenPolynomial<C>>>(); 
183            for (GenSolvablePolynomial<SolvableLocal<C>> q : table.relationList()) {
184                nt.add( this.toPolyCoefficients(q) ); // only with den == 1
185            }
186            polCoeff.table.addSolvRelations(nt);
187        }
188        ZERO = new LocalSolvablePolynomial<C>(this);
189        SolvableLocal<C> coeff = coFac.getONE();
190        //evzero = ExpVector.create(nvar); // from super
191        ONE = new LocalSolvablePolynomial<C>(this, coeff, evzero);
192    }
193
194
195    /**
196     * The constructor creates a solvable polynomial factory object with the the
197     * same term order, number of variables and variable names as the given
198     * polynomial factory, only the coefficient factories differ and the
199     * solvable multiplication relations are <b>empty</b>.
200     * @param cf factory for coefficients of type C.
201     * @param o other solvable polynomial ring.
202     */
203    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, GenSolvablePolynomialRing o) {
204        this(cf, o.nvar, o.tord, o.getVars(), null);
205    }
206
207
208    /**
209     * The constructor creates a solvable polynomial factory object with the the
210     * same term order, number of variables and variable names as the given
211     * polynomial factory, only the coefficient factories differ and the
212     * solvable multiplication relations are <b>empty</b>.
213     * @param cf factory for coefficients of type C.
214     * @param o other solvable polynomial ring.
215     */
216    public LocalSolvablePolynomialRing(RingFactory<SolvableLocal<C>> cf, LocalSolvablePolynomialRing o) {
217        this(cf, (GenSolvablePolynomialRing) o);
218    }
219
220
221    /**
222     * Get the String representation.
223     * @see java.lang.Object#toString()
224     */
225    @Override
226    public String toString() {
227        String res = super.toString();
228        if (PrettyPrint.isTrue()) {
229            //res += "\n" + table.toString(vars);
230            res += "\n" + polCoeff.coeffTable.toString(vars);
231        } else {
232            res += ", #rel = " + table.size() + " + " + polCoeff.coeffTable.size();
233        }
234        return res;
235    }
236
237
238    /**
239     * Get a scripting compatible string representation.
240     * @return script compatible representation for this Element.
241     * @see edu.jas.structure.Element#toScript()
242     */
243    @Override
244    public String toScript() {
245        StringBuffer s = new StringBuffer();
246        switch (Scripting.getLang()) {
247        case Ruby:
248            s.append("SolvPolyRing.new(");
249            break;
250        case Python:
251        default:
252            s.append("SolvPolyRing(");
253        }
254        if (coFac instanceof RingElem) {
255            s.append(((RingElem<SolvableLocal<C>>) coFac).toScriptFactory());
256        } else {
257            s.append(coFac.toScript().trim());
258        }
259        s.append(",\"" + varsToString() + "\",");
260        String to = tord.toScript();
261        s.append(to);
262        if (table.size() > 0) {
263            String rel = table.toScript();
264            s.append(",rel=");
265            s.append(rel);
266        }
267        // if (polCoeff.coeffTable.size() > 0) {
268        //     String rel = polCoeff.coeffTable.toScript();
269        //     s.append(",coeffrel=");
270        //     s.append(rel);
271        // }
272        s.append(")");
273        String cpol = polCoeff.toScript();
274        s.append("\n  # ");
275        s.append(cpol);
276        return s.toString();
277    }
278
279
280    /**
281     * Comparison with any other object.
282     * @see java.lang.Object#equals(java.lang.Object)
283     */
284    @Override
285    @SuppressWarnings("unchecked")
286    public boolean equals(Object other) {
287        if (!(other instanceof LocalSolvablePolynomialRing)) {
288            return false;
289        }
290        LocalSolvablePolynomialRing<C> oring = null;
291        try {
292            oring = (LocalSolvablePolynomialRing<C>) other;
293        } catch (ClassCastException ignored) {
294        }
295        if (oring == null) {
296            return false;
297        }
298        // do a super.equals( )
299        if (!super.equals(other)) {
300            return false;
301        }
302        // check same base relations
303        //if ( ! table.equals(oring.table) ) { // done in super
304        //    return false;
305        //}
306        if (!polCoeff.coeffTable.equals(oring.polCoeff.coeffTable)) {
307            return false;
308        }
309        return true;
310    }
311
312
313    /**
314     * Hash code for this polynomial ring.
315     * @see java.lang.Object#hashCode()
316     */
317    @Override
318    public int hashCode() {
319        int h;
320        h = super.hashCode();
321        h = 37 * h + table.hashCode(); // may be different after some computations
322        h = 37 * h + polCoeff.coeffTable.hashCode(); // may be different
323        return h;
324    }
325
326
327    /**
328     * Get the zero element.
329     * @return 0 as LocalSolvablePolynomial<C>.
330     */
331    @Override
332    public LocalSolvablePolynomial<C> getZERO() {
333        return ZERO;
334    }
335
336
337    /**
338     * Get the one element.
339     * @return 1 as LocalSolvablePolynomial<C>.
340     */
341    @Override
342    public LocalSolvablePolynomial<C> getONE() {
343        return ONE;
344    }
345
346
347    /**
348     * Query if this ring is commutative.
349     * @return true if this ring is commutative, else false.
350     */
351    @Override
352    public boolean isCommutative() {
353        if (polCoeff.coeffTable.size() == 0) {
354            return super.isCommutative();
355        }
356        // check structure of relations?
357        return false;
358    }
359
360
361    /**
362     * Query if this ring is associative. Test if the relations between the mian
363     * variables and the coefficient generators define an associative solvable
364     * ring.
365     * @return true, if this ring is associative, else false.
366     */
367    @Override
368    public boolean isAssociative() {
369        if (!coFac.isAssociative()) {
370            return false;
371        }
372        LocalSolvablePolynomial<C> Xi, Xj, Xk, p, q;
373        List<GenPolynomial<SolvableLocal<C>>> gens = generators();
374        int ngen = gens.size();
375        for (int i = 0; i < ngen; i++) {
376            Xi = (LocalSolvablePolynomial<C>) gens.get(i);
377            for (int j = i + 1; j < ngen; j++) {
378                Xj = (LocalSolvablePolynomial<C>) gens.get(j);
379                for (int k = j + 1; k < ngen; k++) {
380                    Xk = (LocalSolvablePolynomial<C>) gens.get(k);
381                    try {
382                        p = Xk.multiply(Xj).multiply(Xi);
383                        q = Xk.multiply(Xj.multiply(Xi));
384                    } catch (IllegalArgumentException e) {
385                        //e.printStackTrace();
386                        continue;
387                    }
388                    if (p.compareTo(q) != 0) {
389                        logger.info("Xk = {}, Xj = {}, Xi = {}", Xk, Xj, Xi);
390                        logger.info("p = ( Xk * Xj ) * Xi = {}", p);
391                        logger.info("q = Xk * ( Xj * Xi ) = {}", q);
392                        logger.info("q-p = {}", p.subtract(q));
393                        return false;
394                    }
395                }
396            }
397        }
398        return true; //coFac.isAssociative();
399    }
400
401
402    /**
403     * Get a (constant) LocalSolvablePolynomial&lt;C&gt; element from a long
404     * value.
405     * @param a long.
406     * @return a LocalSolvablePolynomial&lt;C&gt;.
407     */
408    @Override
409    public LocalSolvablePolynomial<C> fromInteger(long a) {
410        return new LocalSolvablePolynomial<C>(this, coFac.fromInteger(a), evzero);
411    }
412
413
414    /**
415     * Get a (constant) LocalSolvablePolynomial&lt;C&gt; element from a
416     * BigInteger value.
417     * @param a BigInteger.
418     * @return a LocalSolvablePolynomial&lt;C&gt;.
419     */
420    @Override
421    public LocalSolvablePolynomial<C> fromInteger(BigInteger a) {
422        return new LocalSolvablePolynomial<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 LocalSolvablePolynomial<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 LocalSolvablePolynomial<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 LocalSolvablePolynomial<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 LocalSolvablePolynomial<C> random(int k, int l, int d, float q, Random rnd) {
479        LocalSolvablePolynomial<C> r = getZERO(); // copy( ZERO ); 
480        ExpVector e;
481        SolvableLocal<C> a;
482        // add random coeffs and exponents
483        for (int i = 0; i < l; i++) {
484            e = ExpVector.random(nvar, d, q, rnd);
485            a = coFac.random(k, rnd);
486            r = (LocalSolvablePolynomial<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 LocalSolvablePolynomial<C> copy(LocalSolvablePolynomial<C> c) {
499        return new LocalSolvablePolynomial<C>(this, c.getMap());
500    }
501
502
503    /**
504     * Parse a solvable polynomial with the use of GenPolynomialTokenizer
505     * @param s String.
506     * @return LocalSolvablePolynomial from s.
507     */
508    @Override
509    public LocalSolvablePolynomial<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 LocalSolvablePolynomial from r.
518     */
519    @Override
520    @SuppressWarnings("unchecked")
521    public LocalSolvablePolynomial<C> parse(Reader r) {
522        GenPolynomialTokenizer pt = new GenPolynomialTokenizer(this, r);
523        LocalSolvablePolynomial<C> p = null;
524        try {
525            GenSolvablePolynomial<SolvableLocal<C>> s = pt.nextSolvablePolynomial();
526            p = new LocalSolvablePolynomial<C>(this, s);
527        } catch (IOException e) {
528            logger.error("{} parse {}", e, 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 LocalSolvablePolynomial<C> univariate(int i) {
542        return (LocalSolvablePolynomial<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 LocalSolvablePolynomial<C> univariate(int i, long e) {
555        return (LocalSolvablePolynomial<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 LocalSolvablePolynomial<C> univariate(int modv, int i, long e) {
569        return (LocalSolvablePolynomial<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    @Override
578    public List<LocalSolvablePolynomial<C>> univariateList() {
579        return univariateList(0, 1L);
580    }
581
582
583    /**
584     * Generate list of univariate polynomials in all variables.
585     * @param modv number of module variables.
586     * @return List(X_1,...,X_n) a list of univariate polynomials.
587     */
588    @Override
589    public List<LocalSolvablePolynomial<C>> univariateList(int modv) {
590        return univariateList(modv, 1L);
591    }
592
593
594    /**
595     * Generate list of univariate polynomials in all variables with given
596     * exponent.
597     * @param modv number of module variables.
598     * @param e the exponent of the variables.
599     * @return List(X_1^e,...,X_n^e) a list of univariate polynomials.
600     */
601    @Override
602    public List<LocalSolvablePolynomial<C>> univariateList(int modv, long e) {
603        List<LocalSolvablePolynomial<C>> pols = new ArrayList<LocalSolvablePolynomial<C>>(nvar);
604        int nm = nvar - modv;
605        for (int i = 0; i < nm; i++) {
606            LocalSolvablePolynomial<C> p = univariate(modv, nm - 1 - i, e);
607            pols.add(p);
608        }
609        return pols;
610    }
611
612
613    /**
614     * Extend variables. Used e.g. in module embedding. Extend number of
615     * variables by i.
616     * @param i number of variables to extend.
617     * @return extended solvable polynomial ring factory.
618     */
619    @Override
620    public LocalSolvablePolynomialRing<C> extend(int i) {
621        return extend(i,false);
622    }
623
624    
625    /**
626     * Extend variables. Used e.g. in module embedding. Extend number of
627     * variables by i.
628     * @param i number of variables to extend.
629     * @param top true for TOP term order, false for POT term order.
630     * @return extended solvable polynomial ring factory.
631     */
632    @Override
633    public LocalSolvablePolynomialRing<C> extend(int i, boolean top) {
634        GenPolynomialRing<SolvableLocal<C>> pfac = super.extend(i, top);
635        LocalSolvablePolynomialRing<C> spfac = new LocalSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
636                        pfac.tord, pfac.getVars());
637        spfac.table.extend(this.table);
638        spfac.polCoeff.coeffTable.extend(this.polCoeff.coeffTable);
639        return spfac;
640    }
641
642
643    /**
644     * Extend variables. Used e.g. in module embedding. Extend number of
645     * variables by length(vn). New variables commute with the exiting
646     * variables.
647     * @param vn names for extended variables.
648     * @return extended polynomial ring factory.
649     */
650    @Override
651    public LocalSolvablePolynomialRing<C> extend(String[] vn) {
652        return extend(vn, false);
653    }
654
655    
656    /**
657     * Extend variables. Used e.g. in module embedding. Extend number of
658     * variables by length(vn). New variables commute with the exiting
659     * variables.
660     * @param vn names for extended variables.
661     * @param top true for TOP term order, false for POT term order.
662     * @return extended polynomial ring factory.
663     */
664    @Override
665    public LocalSolvablePolynomialRing<C> extend(String[] vn, boolean top) {
666        GenPolynomialRing<SolvableLocal<C>> pfac = super.extend(vn, top);
667        LocalSolvablePolynomialRing<C> spfac = new LocalSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
668                        pfac.tord, pfac.getVars());
669        spfac.table.extend(this.table);
670        spfac.polCoeff.coeffTable.extend(this.polCoeff.coeffTable);
671        return spfac;
672    }
673
674 
675    /**
676     * Contract variables. Used e.g. in module embedding. Contract number of
677     * variables by i.
678     * @param i number of variables to remove.
679     * @return contracted solvable polynomial ring factory.
680     */
681    @Override
682    public LocalSolvablePolynomialRing<C> contract(int i) {
683        GenPolynomialRing<SolvableLocal<C>> pfac = super.contract(i);
684        LocalSolvablePolynomialRing<C> spfac = new LocalSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
685                        pfac.tord, pfac.getVars());
686        spfac.table.contract(this.table);
687        spfac.polCoeff.coeffTable.contract(this.polCoeff.coeffTable);
688        return spfac;
689    }
690
691
692    /**
693     * Reverse variables. Used e.g. in opposite rings.
694     * @return solvable polynomial ring factory with reversed variables.
695     */
696    @Override
697    public LocalSolvablePolynomialRing<C> reverse() {
698        return reverse(false);
699    }
700
701
702    /**
703     * Reverse variables. Used e.g. in opposite rings.
704     * @param partial true for partialy reversed term orders.
705     * @return solvable polynomial ring factory with reversed variables.
706     */
707    @Override
708    public LocalSolvablePolynomialRing<C> reverse(boolean partial) {
709        GenPolynomialRing<SolvableLocal<C>> pfac = super.reverse(partial);
710        LocalSolvablePolynomialRing<C> spfac = new LocalSolvablePolynomialRing<C>(pfac.coFac, pfac.nvar,
711                        pfac.tord, pfac.getVars());
712        spfac.partial = partial;
713        spfac.table.reverse(this.table);
714        spfac.polCoeff.coeffTable.reverse(this.polCoeff.coeffTable);
715        return spfac;
716    }
717
718
719    /**
720     * Rational function from integral polynomial coefficients. Represent as
721     * polynomial with type SolvableLocal<C> coefficients.
722     * @param A polynomial with integral polynomial coefficients to be
723     *            converted.
724     * @return polynomial with type SolvableLocal<C> coefficients.
725     */
726    public LocalSolvablePolynomial<C> fromPolyCoefficients(GenSolvablePolynomial<GenPolynomial<C>> A) {
727        LocalSolvablePolynomial<C> B = getZERO().copy();
728        if (A == null || A.isZERO()) {
729            return B;
730        }
731        RingFactory<SolvableLocal<C>> cfac = coFac;
732        SolvableLocalRing<C> qfac = (SolvableLocalRing<C>) cfac;
733        for (Map.Entry<ExpVector, GenPolynomial<C>> y : A.getMap().entrySet()) {
734            ExpVector e = y.getKey();
735            GenSolvablePolynomial<C> a = (GenSolvablePolynomial<C>) y.getValue();
736            SolvableLocal<C> p = new SolvableLocal<C>(qfac, a); // can not be zero
737            if (!p.isZERO()) {
738                //B = B.sum( p, e ); // inefficient
739                B.doPutToMap(e, p);
740            }
741        }
742        return B;
743    }
744
745
746    /**
747     * Integral function from rational polynomial coefficients. Represent as
748     * polynomial with type GenSolvablePolynomial<C> coefficients.
749     * @param A polynomial with rational polynomial coefficients to be
750     *            converted.
751     * @return polynomial with type GenSolvablePolynomial<C> coefficients.
752     */
753    public RecSolvablePolynomial<C> toPolyCoefficients(LocalSolvablePolynomial<C> A) {
754        RecSolvablePolynomial<C> B = polCoeff.getZERO().copy();
755        if (A == null || A.isZERO()) {
756            return B;
757        }
758        for (Map.Entry<ExpVector, SolvableLocal<C>> y : A.getMap().entrySet()) {
759            ExpVector e = y.getKey();
760            SolvableLocal<C> a = y.getValue();
761            if (!a.den.isONE()) {
762                throw new IllegalArgumentException("den != 1 not supported: " + a);
763            }
764            GenPolynomial<C> p = a.num; // can not be zero
765            if (!p.isZERO()) {
766                //B = B.sum( p, e ); // inefficient
767                B.doPutToMap(e, p);
768            }
769        }
770        return B;
771    }
772
773
774    /**
775     * Integral function from rational polynomial coefficients. Represent as
776     * polynomial with type GenSolvablePolynomial<C> coefficients.
777     * @param A polynomial with rational polynomial coefficients to be
778     *            converted.
779     * @return polynomial with type GenSolvablePolynomial<C> coefficients.
780     */
781    public RecSolvablePolynomial<C> toPolyCoefficients(GenPolynomial<SolvableLocal<C>> A) {
782        RecSolvablePolynomial<C> B = polCoeff.getZERO().copy();
783        if (A == null || A.isZERO()) {
784            return B;
785        }
786        for (Map.Entry<ExpVector, SolvableLocal<C>> y : A.getMap().entrySet()) {
787            ExpVector e = y.getKey();
788            SolvableLocal<C> a = y.getValue();
789            if (!a.den.isONE()) {
790                throw new IllegalArgumentException("den != 1 not supported: " + a);
791            }
792            GenPolynomial<C> p = a.num; // can not be zero
793            if (!p.isZERO()) {
794                //B = B.sum( p, e ); // inefficient
795                B.doPutToMap(e, p);
796            }
797        }
798        return B;
799    }
800
801}