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