001/*
002 * $Id$
003 */
004
005package edu.jas.application;
006
007
008import java.util.ArrayList;
009import java.util.Collections;
010import java.util.List;
011
012import org.apache.logging.log4j.Logger;
013import org.apache.logging.log4j.LogManager; 
014
015import edu.jas.gb.GroebnerBase;
016import edu.jas.gb.SolvableGroebnerBase;
017import edu.jas.gbufd.GBFactory;
018import edu.jas.gbufd.SGBFactory;
019import edu.jas.poly.ExpVector;
020import edu.jas.poly.GenPolynomial;
021import edu.jas.poly.GenPolynomialRing;
022import edu.jas.poly.GenSolvablePolynomial;
023import edu.jas.poly.GenSolvablePolynomialRing;
024import edu.jas.poly.PolynomialList;
025import edu.jas.structure.GcdRingElem;
026import edu.jas.structure.RingFactory;
027import edu.jas.ufd.SquarefreeAbstract;
028import edu.jas.ufd.SquarefreeFactory;
029
030
031/**
032 * Comprehensive Groebner Base sequential algorithm. Implements faithful
033 * comprehensive Groebner bases via Groebner systems and CGB test.
034 * @param <C> coefficient type
035 * @author Heinz Kredel
036 */
037
038public class ComprehensiveGroebnerBaseSeq<C extends GcdRingElem<C>>
039/* extends GroebnerBaseAbstract<GenPolynomial<C>> */{
040
041
042    private static final Logger logger = LogManager.getLogger(ComprehensiveGroebnerBaseSeq.class);
043
044
045    private static final boolean debug = logger.isDebugEnabled();
046
047
048    /**
049     * Squarefree for coefficient content and primitive parts.
050     */
051    protected final SquarefreeAbstract<C> engine;
052
053
054    /*
055     * Flag if gcd engine should be used.
056     */
057    //private final boolean notFaithfull = false;
058
059
060    /**
061     * Comprehensive reduction engine.
062     */
063    protected final CReductionSeq<C> cred;
064
065
066    /**
067     * Polynomial coefficient ring factory.
068     */
069    protected final RingFactory<C> cofac;
070
071
072    /**
073     * Constructor.
074     * @param rf base coefficient ring factory.
075     */
076    public ComprehensiveGroebnerBaseSeq(RingFactory<C> rf) {
077        this(new CReductionSeq<C>(rf), rf);
078    }
079
080
081    /**
082     * Constructor.
083     * @param red C-pseudo-Reduction engine
084     * @param rf base coefficient ring factory.
085     */
086    @SuppressWarnings("unchecked")
087    public ComprehensiveGroebnerBaseSeq(CReductionSeq<C> red, RingFactory<C> rf) {
088        // super(null); // red not possible since type of type
089        cred = red;
090        cofac = rf;
091        // selection for C but used for R:
092        //engine e = GCDFactory.<C> getImplementation(cofac);
093        engine = SquarefreeFactory.<C> getImplementation(rf);
094    }
095
096
097    /**
098     * Comprehensive-Groebner base test.
099     * @param F polynomial list.
100     * @return true, if F is a Comprehensive-Groebner base, else false.
101     */
102    // @Override
103    public boolean isGB(List<GenPolynomial<GenPolynomial<C>>> F) {
104        return isGB(0, F);
105    }
106
107
108    /**
109     * Comprehensive-Groebner base test.
110     * @param modv module variable number.
111     * @param F polynomial list.
112     * @return true, if F is a Comprehensive-Groebner base, else false.
113     */
114    // @Override
115    public boolean isGB(int modv, List<GenPolynomial<GenPolynomial<C>>> F) {
116        // return isGBcol( modv, F );
117        return isGBsubst(modv, F);
118    }
119
120
121    /**
122     * Comprehensive-Groebner base test using colored systems.
123     * @param F polynomial list.
124     * @return true, if F is a Comprehensive-Groebner base, else false.
125     */
126    // @Override
127    public boolean isGBcol(List<GenPolynomial<GenPolynomial<C>>> F) {
128        return isGBcol(0, F);
129    }
130
131
132    /**
133     * Comprehensive-Groebner base test using colored systems.
134     * @param modv module variable number.
135     * @param F polynomial list.
136     * @return true, if F is a Comprehensive-Groebner base, else false.
137     */
138    // @Override
139    public boolean isGBcol(int modv, List<GenPolynomial<GenPolynomial<C>>> F) {
140        if (F == null || F.size() == 0) {
141            return true;
142        }
143        List<ColoredSystem<C>> CS = cred.determine(F);
144        return isGBsys(modv, CS);
145    }
146
147
148    /**
149     * Comprehensive-Groebner system test.
150     * @param CS list of colored systems.
151     * @return true, if CS is a Comprehensive-Groebner system, else false.
152     */
153    // @Override
154    public boolean isGBsys(List<ColoredSystem<C>> CS) {
155        return isGBsys(0, CS);
156    }
157
158
159    /**
160     * Comprehensive-Groebner system test.
161     * @param modv module variable number, unused.
162     * @param CS list of colored systems.
163     * @return true, if CS is a Comprehensive-Groebner system, else false.
164     */
165    // @Override
166    public boolean isGBsys(int modv, List<ColoredSystem<C>> CS) {
167        if (CS == null || CS.size() == 0) {
168            return true;
169        }
170        if (modv != 0) {
171            throw new IllegalArgumentException("modv !0 not supported.");
172        }
173        ColorPolynomial<C> p, q, h, hp;
174        for (ColoredSystem<C> cs : CS) {
175            if (debug) {
176                if (!cs.isDetermined()) {
177                    System.out.println("not determined, cs = " + cs);
178                    return false;
179                }
180                if (!cs.checkInvariant()) {
181                    System.out.println("not invariant, cs = " + cs);
182                    return false;
183                }
184            }
185            Condition<C> cond = cs.condition;
186            List<ColorPolynomial<C>> S = cs.list;
187            int k = S.size();
188            for (int j = 0; j < k; j++) {
189                p = S.get(j);
190                for (int l = j + 1; l < k; l++) {
191                    q = S.get(l);
192                    h = cred.SPolynomial(p, q);
193                    // System.out.println("spol(a,b) = " + h);
194                    h = cred.normalform(cond, S, h);
195                    // System.out.println("NF(spol(a,b)) = " + h);
196                    if (debug) {
197                        if (!cred.isNormalform(S, h)) {
198                            System.out.println("not normalform, h = " + h);
199                            System.out.println("cs = " + cs);
200                            return false;
201                        }
202                    }
203                    if (!h.isZERO()) {
204                        hp = cond.reDetermine(h);
205                        if (!hp.isZERO()) {
206                            System.out.println("p = " + p);
207                            System.out.println("q = " + q);
208                            System.out.println("not zero:   NF(spol(p,q))  = " + h);
209                            System.out.println("redetermine(NF(spol(p,q))) = " + hp);
210                            System.out.println("cs = " + cs);
211                            return false;
212                        }
213                    }
214                }
215            }
216        }
217        return true;
218    }
219
220
221    /**
222     * Comprehensive-Groebner base test using substitution.
223     * @param F polynomial list.
224     * @return true, if F is a Comprehensive-Groebner base, else false.
225     */
226    // @Override
227    public boolean isGBsubst(List<GenPolynomial<GenPolynomial<C>>> F) {
228        return isGBsubst(0, F);
229    }
230
231
232    /**
233     * Comprehensive-Groebner base test using substitution.
234     * @param modv module variable number, unused.
235     * @param F polynomial list.
236     * @return true, if F is a Comprehensive-Groebner base, else false.
237     */
238    // @Override
239    public boolean isGBsubst(int modv, List<GenPolynomial<GenPolynomial<C>>> F) {
240        if (F == null || F.isEmpty()) {
241            return true;
242        }
243        if (modv != 0) {
244            throw new IllegalArgumentException("modv !0 not supported.");
245        }
246        GenPolynomial<GenPolynomial<C>> f = F.get(0); // assert non Zero
247        GenPolynomialRing<GenPolynomial<C>> cf = f.ring;
248
249        List<ColoredSystem<C>> CS = cred.determine(F);
250        if (logger.isDebugEnabled()) {
251            logger.info("determined polynomials =\n{}", CS);
252        }
253        // substitute zero conditions into parameter coefficients and test
254        for (ColoredSystem<C> cs : CS) {
255            Ideal<C> id = cs.condition.zero;
256            ResidueRing<C> r = new ResidueRing<C>(id);
257            //GroebnerBase<Residue<C>> bb = new GroebnerBasePseudoSeq<Residue<C>>(r);
258            List<GenPolynomial<Residue<C>>> list;
259            boolean t;
260            if (cf instanceof GenSolvablePolynomialRing) {
261                GenSolvablePolynomialRing<Residue<C>> rf = new GenSolvablePolynomialRing<Residue<C>>(r, cf);
262                List<GenSolvablePolynomial<GenPolynomial<C>>> rel = ((GenSolvablePolynomialRing<GenPolynomial<C>>) cf).table
263                                .relationList();
264                List<GenPolynomial<Residue<C>>> relres = PolyUtilApp.<C> toResidue(rf,
265                                PolynomialList.<GenPolynomial<C>> castToList(rel));
266                rf.addRelations(relres);
267                //System.out.println("rf = " + rf.toScript());
268                list = PolyUtilApp.<C> toResidue(rf, F);
269                SolvableGroebnerBase<Residue<C>> bb = SGBFactory.getImplementation(r);
270                t = bb.isLeftGB(PolynomialList.<Residue<C>> castToSolvableList(list));
271            } else {
272                GenPolynomialRing<Residue<C>> rf = new GenPolynomialRing<Residue<C>>(r, cf);
273                //System.out.println("rf = " + rf.toScript());
274                list = PolyUtilApp.<C> toResidue(rf, F);
275                GroebnerBase<Residue<C>> bb = GBFactory.getImplementation(r);
276                t = bb.isGB(list);
277            }
278            if (!t) {
279                System.out.println("test condition = " + cs.condition);
280                System.out.println("test ideal     = " + id.toScript());
281                System.out.println("test F         = " + F);
282                System.out.println("no GB for residue coefficients = " + list);
283                return false;
284            }
285        }
286
287        // substitute random ideal into parameter coefficients and test
288        GenPolynomialRing<C> ccf = (GenPolynomialRing<C>) cf.coFac;
289        int nv = ccf.nvar - 2;
290        if (nv < 1) {
291            nv = 1;
292        }
293        List<GenPolynomial<C>> il = new ArrayList<GenPolynomial<C>>();
294        int i = 0;
295        //int j = 1;
296        while (i < nv) {
297            //j++;
298            GenPolynomial<C> p = ccf.random(3, 3, 3, 0.3f); //j + 1);
299            // System.out.println("p = " + p);
300            if (p.isConstant()) {
301                continue;
302            }
303            if (p.isZERO()) {
304                continue;
305            }
306            p = engine.squarefreePart(p);
307            il.add(p);
308            i++;
309        }
310        logger.info("random ideal = {}", il);
311        Ideal<C> id = new Ideal<C>(ccf, il);
312        ResidueRing<C> r = new ResidueRing<C>(id);
313        //GroebnerBase<Residue<C>> bb = new GroebnerBasePseudoSeq<Residue<C>>(r);
314        List<GenPolynomial<Residue<C>>> list;
315        boolean t;
316        if (cf instanceof GenSolvablePolynomialRing) {
317            GenSolvablePolynomialRing<Residue<C>> rf = new GenSolvablePolynomialRing<Residue<C>>(r, cf);
318            List<GenSolvablePolynomial<GenPolynomial<C>>> rel = ((GenSolvablePolynomialRing<GenPolynomial<C>>) cf).table
319                            .relationList();
320            List<GenPolynomial<Residue<C>>> relres = PolyUtilApp.<C> toResidue(rf,
321                            PolynomialList.<GenPolynomial<C>> castToList(rel));
322            rf.addRelations(relres);
323            //System.out.println("rf = " + rf.toScript());
324            list = PolyUtilApp.<C> toResidue(rf, F);
325            SolvableGroebnerBase<Residue<C>> bb = SGBFactory.getImplementation(r);
326            t = bb.isLeftGB(PolynomialList.<Residue<C>> castToSolvableList(list));
327        } else {
328            GenPolynomialRing<Residue<C>> rf = new GenPolynomialRing<Residue<C>>(r, cf);
329            //System.out.println("rf = " + rf.toScript());
330            list = PolyUtilApp.<C> toResidue(rf, F);
331            GroebnerBase<Residue<C>> bb = GBFactory.getImplementation(r);
332            t = bb.isGB(list);
333        }
334        if (!t) {
335            System.out.println("test random ideal = " + id.toScript());
336            System.out.println("no GB for residue coefficients = " + list);
337            return false;
338        }
339        return true;
340    }
341
342
343    /**
344     * Comprehensive-Groebner system test.
345     * @param F Groebner system.
346     * @return true, if F is a Comprehensive-Groebner system, else false.
347     */
348    // @Override
349    public boolean isGBsys(GroebnerSystem<C> F) {
350        return isGBsys(0, F.list);
351    }
352
353
354    /**
355     * Comprehensive-Groebner base test.
356     * @param F Groebner system.
357     * @return true, if F is a Comprehensive-Groebner base, else false.
358     */
359    // @Override
360    public boolean isCGB(GroebnerSystem<C> F) {
361        return isGB(F.getCGB());
362    }
363
364
365    /**
366     * Comprehensive-Groebner system and base test.
367     * @param F Groebner system.
368     * @return true, if F is a Comprehensive-Groebner system and base, else
369     *         false.
370     */
371    // @Override
372    public boolean isGB(GroebnerSystem<C> F) {
373        return isGBsys(0, F.list) && isGB(F.getCGB());
374    }
375
376
377    /**
378     * Comprehensive Groebner base system using pairlist class.
379     * @param F polynomial list.
380     * @return GBsys(F) a Comprehensive Groebner system of F.
381     */
382    // @Override
383    // @SuppressWarnings("unchecked")
384    public GroebnerSystem<C> GBsys(List<GenPolynomial<GenPolynomial<C>>> F) {
385        if (F == null) {
386            return null;
387        }
388        List<ColoredSystem<C>> CSp = new ArrayList<ColoredSystem<C>>();
389        if (F.size() == 0) {
390            return new GroebnerSystem<C>(CSp);
391        }
392        // extract coefficient factory
393        GenPolynomial<GenPolynomial<C>> f = F.get(0);
394        GenPolynomialRing<GenPolynomial<C>> fac = f.ring;
395        // determine polynomials
396        List<ColoredSystem<C>> CS = cred.determine(F);
397        // System.out.println("CS = " + CS);
398        // CS.remove(0); // empty colored system
399        logger.info("determined polynomials =\n{}", CS);
400
401        // setup pair lists
402        List<ColoredSystem<C>> CSs = new ArrayList<ColoredSystem<C>>();
403        ColoredSystem<C> css;
404        for (ColoredSystem<C> cs : CS) {
405            OrderedCPairlist<C> pairlist = new OrderedCPairlist<C>(fac);
406            for (ColorPolynomial<C> p : cs.list) {
407                // System.out.println("p = " + p);
408                pairlist.put(p);
409            }
410            css = new ColoredSystem<C>(cs.condition, cs.list, pairlist);
411            CSs.add(css);
412        }
413
414        // main loop
415        List<ColoredSystem<C>> CSb = new ArrayList<ColoredSystem<C>>();
416        List<ColoredSystem<C>> ncs;
417        List<ColoredSystem<C>> CSh; //, CSbh;
418        ColoredSystem<C> cs;
419        List<ColorPolynomial<C>> G;
420        OrderedCPairlist<C> pairlist;
421        Condition<C> cond;
422        int si = 0;
423        while (CSs.size() > 0) {
424            cs = CSs.get(0); // remove(0);
425            si++;
426            logger.info("poped GBsys number    {} with condition = {}", si, cs.condition);
427            logger.info("poped GBsys (remaining {}) with pairlist  = {}", (CSs.size() - 1), cs.pairlist);
428            if (!cs.isDetermined()) {
429                cs = cs.reDetermine();
430            }
431            pairlist = cs.pairlist;
432            G = cs.list;
433            cond = cs.condition;
434            // logger.info("{}", pairlist);
435
436            CPair<C> pair;
437            ColorPolynomial<C> pi;
438            ColorPolynomial<C> pj;
439            ColorPolynomial<C> S;
440            // GenPolynomial<GenPolynomial<C>> H;
441            ColorPolynomial<C> H;
442            while (pairlist.hasNext()) {
443                pair = pairlist.removeNext();
444                if (pair == null)
445                    continue;
446
447                pi = pair.pi;
448                pj = pair.pj;
449                if (debug) {
450                    logger.info("pi = {}, pj = {}", pi, pj);
451                }
452
453                S = cred.SPolynomial(pi, pj);
454                if (S.isZERO()) {
455                    pair.setZero();
456                    continue;
457                }
458                if (debug) {
459                    // logger.info("ht(S) = {}", S.leadingExpVector() );
460                    logger.info("S = {}", S);
461                }
462
463                H = cred.normalform(cond, G, S);
464                if (H.isZERO()) {
465                    pair.setZero();
466                    continue;
467                }
468                if (debug) {
469                    logger.info("ht(H) = {}", H.leadingExpVector());
470                }
471
472                H = H.abs();
473                if (debug) {
474                    logger.debug("H = {}", H);
475                }
476                logger.info("H = {}", H);
477                if (!H.isZERO()) {
478                    //CSh = new ArrayList<ColoredSystem<C>>();
479                    ncs = determineAddPairs(cs, H);
480                    if (ncs.size() == 0) {
481                        continue;
482                    }
483                    cs = ncs.remove(0); // remove other?
484                    pairlist = cs.pairlist;
485                    G = cs.list;
486                    cond = cs.condition;
487                    logger.info("replaced main branch = {}", cond);
488                    logger.info("#new systems       = {}", ncs.size());
489                    int yi = CSs.size();
490                    for (ColoredSystem<C> x : ncs) {
491                        if (!x.isDetermined()) {
492                            x = x.reDetermine();
493                        }
494                        CSs = x.addToList(CSs);
495                    }
496                    logger.info("#new systems added = {}", (CSs.size() - yi));
497                }
498            }
499            // all s-pols reduce to zero in this branch
500            if (!cs.isDetermined()) {
501                cs = cs.reDetermine();
502            }
503            CSb.add(cs);
504            CSs.remove(0);
505            logger.info("done with = {}", cs.condition);
506        }
507        // all branches done
508        CSh = new ArrayList<ColoredSystem<C>>();
509        for (ColoredSystem<C> x : CSb) {
510            // System.out.println("G = " + x.list );
511            if (!x.isDetermined()) {
512                x = x.reDetermine();
513            }
514            cs = minimalGB(x);
515            // System.out.println("min(G) = " + cs.list );
516            if (!cs.isDetermined()) {
517                cs = cs.reDetermine();
518            }
519            // cs = new ColoredSystem<C>( x.condition, G, x.pairlist );
520            CSh.add(cs);
521            logger.info("#sequential done = {}", x.condition);
522            logger.info("{}", x.pairlist);
523        }
524        CSb = new ArrayList<ColoredSystem<C>>(CSh);
525        return new GroebnerSystem<C>(CSb);
526    }
527
528
529    /**
530     * Determine polynomial relative to a condition of a colored system and add
531     * pairs.
532     * @param cs a colored system.
533     * @param A color polynomial.
534     * @return list of colored systems, the conditions extending the condition
535     *         of cs.
536     */
537    public List<ColoredSystem<C>> determineAddPairs(ColoredSystem<C> cs, ColorPolynomial<C> A) {
538        List<ColoredSystem<C>> NCS = new ArrayList<ColoredSystem<C>>();
539        if (A == null || A.isZERO()) {
540            // NCS.add( cs );
541            return NCS;
542        }
543        List<ColorPolynomial<C>> S = cs.list;
544        Condition<C> cond = cs.condition; // .clone(); done in Condition
545        // itself
546        OrderedCPairlist<C> pl = cs.pairlist;
547
548        List<ColorPolynomial<C>> Sp;
549        ColorPolynomial<C> nz;
550        ColoredSystem<C> NS;
551        // if ( A.isDetermined() ) { ... } // dont use this
552        // System.out.println("to determine = " + A);
553        GenPolynomial<GenPolynomial<C>> Ap = A.getPolynomial();
554        List<Condition<C>> cd = cred.caseDistinction(cond, Ap);
555        logger.info("# cases = {}", cd.size());
556        for (Condition<C> cnd : cd) {
557            //nz = cnd.determine(Ap);
558            nz = cnd.reDetermine(A);
559            if (nz.isZERO()) {
560                logger.info("zero determined nz = {}", nz);
561                Sp = new ArrayList<ColorPolynomial<C>>(S);
562                OrderedCPairlist<C> PL = pl.copy();
563                NS = new ColoredSystem<C>(cnd, Sp, PL);
564                try {
565                    if (!NS.isDetermined()) {
566                        NS = NS.reDetermine();
567                    }
568                } catch (RuntimeException e) {
569                    System.out.println("Contradiction in NS_0 = " + NS);
570                    //e.printStackTrace();
571                    continue;
572                }
573                NCS = NS.addToList(NCS);
574                continue;
575            }
576            if (S.contains(nz)) {
577                System.out.println("*** S.contains(nz) ***");
578                continue;
579            }
580            logger.info("new determined nz = {}", nz);
581            Sp = new ArrayList<ColorPolynomial<C>>(S);
582            Sp.add(nz);
583            OrderedCPairlist<C> PL = pl.copy();
584            PL.put(nz);
585            NS = new ColoredSystem<C>(cnd, Sp, PL);
586            try {
587                if (!NS.isDetermined()) {
588                    NS = NS.reDetermine();
589                }
590            } catch (RuntimeException e) {
591                System.out.println("Contradiction in NS = " + NS);
592                //e.printStackTrace();
593                continue;
594            }
595            NCS = NS.addToList(NCS);
596        }
597        // System.out.println("new determination = " + NCS);
598        return NCS;
599    }
600
601
602    /**
603     * Comprehensive Groebner base via Groebner system.
604     * @param F polynomial list.
605     * @return GB(F) a Comprehensive Groebner base of F.
606     */
607    // @Override
608    // @SuppressWarnings("unchecked")
609    public List<GenPolynomial<GenPolynomial<C>>> GB(List<GenPolynomial<GenPolynomial<C>>> F) {
610        if (F == null) {
611            return F;
612        }
613        // compute Groebner system
614        GroebnerSystem<C> gs = GBsys(F);
615        // System.out.println("\n\nGBsys = " + gs);
616        return gs.getCGB();
617    }
618
619
620    /**
621     * Minimal ordered Groebner basis.
622     * @param cs colored system.
623     * @return a reduced Groebner base of Gp.
624     */
625    // @Override
626    public ColoredSystem<C> minimalGB(ColoredSystem<C> cs) {
627        // List<ColorPolynomial<C>> Gp ) {
628        if (cs == null || cs.list == null || cs.list.size() <= 1) {
629            return cs;
630        }
631        // remove zero polynomials
632        List<ColorPolynomial<C>> G = new ArrayList<ColorPolynomial<C>>(cs.list.size());
633        for (ColorPolynomial<C> a : cs.list) {
634            if (a != null && !a.isZERO()) { // always true in GB()
635                // already positive a = a.abs();
636                G.add(a);
637            }
638        }
639        if (G.size() <= 1) {
640            return new ColoredSystem<C>(cs.condition, G, cs.pairlist);
641        }
642        // System.out.println("G check " + G);
643        // remove top reducible polynomials
644        Condition<C> cond = cs.condition;
645        ColorPolynomial<C> a, b;
646        List<ColorPolynomial<C>> F;
647        F = new ArrayList<ColorPolynomial<C>>(G.size());
648        while (G.size() > 0) {
649            a = G.remove(0);
650            b = a;
651            // System.out.println("check " + b);
652            //if (false) {
653            //    if (a.red.leadingBaseCoefficient().isConstant()) { // dont drop
654            //        // these
655            //        F.add(a);
656            //        continue;
657            //    }
658            //}
659            if (cred.isTopReducible(G, a) || cred.isTopReducible(F, a)) {
660                // drop polynomial
661                if (debug) {
662                    // System.out.println("trying to drop " + a);
663                    List<ColorPolynomial<C>> ff;
664                    ff = new ArrayList<ColorPolynomial<C>>(G);
665                    ff.addAll(F);
666                    a = cred.normalform(cond, ff, a);
667                    try {
668                        a = cond.reDetermine(a);
669                    } catch (RuntimeException ignored) {
670                    }
671                    if (!a.isZERO()) {
672                        logger.error("nf(a) != 0 {}, {}", b, a);
673                        F.add(b);
674                    }
675                }
676            } else {
677                F.add(a);
678            }
679        }
680        G = F;
681        if (G.size() <= 1) {
682            return new ColoredSystem<C>(cs.condition, G, cs.pairlist);
683        }
684        Collections.reverse(G); // important for lex GB
685        // reduce remaining polynomials
686        int len = G.size();
687        int i = 0;
688        while (i < len) {
689            a = G.remove(0);
690            b = a;
691            ExpVector e = a.red.leadingExpVector();
692            // System.out.println("reducing " + a);
693            a = cred.normalform(cond, G, a); // unchanged by top reduction
694            // System.out.println("reduced " + a);
695            try {
696                a = cond.reDetermine(a);
697            } catch (RuntimeException ignored) {
698            }
699            ExpVector f = a.red.leadingExpVector();
700            // a = ##engine.basePrimitivePart(a); //a.monic(); was not required
701            // a = a.abs();
702            // a = red.normalform( F, a );
703            if (e.equals(f)) {
704                G.add(a); // adds as last
705            } else { // should not happen
706                if (debug) {
707                    logger.error("nf(a) not determined {}, {}", b, a);
708                }
709                G.add(b); // adds as last
710            }
711            i++;
712        }
713        return new ColoredSystem<C>(cs.condition, G, cs.pairlist);
714    }
715
716}