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