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