001/*
002 * $Id: ComprehensiveGroebnerBaseSeq.java 5868 2018-07-20 15:44:13Z kredel $
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        if (logger.isInfoEnabled()) {
400            logger.info("determined polynomials =\n" + CS);
401        }
402
403        // setup pair lists
404        List<ColoredSystem<C>> CSs = new ArrayList<ColoredSystem<C>>();
405        ColoredSystem<C> css;
406        for (ColoredSystem<C> cs : CS) {
407            OrderedCPairlist<C> pairlist = new OrderedCPairlist<C>(fac);
408            for (ColorPolynomial<C> p : cs.list) {
409                // System.out.println("p = " + p);
410                pairlist.put(p);
411            }
412            css = new ColoredSystem<C>(cs.condition, cs.list, pairlist);
413            CSs.add(css);
414        }
415
416        // main loop
417        List<ColoredSystem<C>> CSb = new ArrayList<ColoredSystem<C>>();
418        List<ColoredSystem<C>> ncs;
419        List<ColoredSystem<C>> CSh; //, CSbh;
420        ColoredSystem<C> cs;
421        List<ColorPolynomial<C>> G;
422        OrderedCPairlist<C> pairlist;
423        Condition<C> cond;
424        int si = 0;
425        while (CSs.size() > 0) {
426            cs = CSs.get(0); // remove(0);
427            si++;
428            logger.info("poped GBsys number    " + si + " with condition = " + cs.condition);
429            logger.info("poped GBsys (remaining " + (CSs.size() - 1) + ") with pairlist  = " + cs.pairlist);
430            if (!cs.isDetermined()) {
431                cs = cs.reDetermine();
432            }
433            pairlist = cs.pairlist;
434            G = cs.list;
435            cond = cs.condition;
436            // logger.info( pairlist.toString() );
437
438            CPair<C> pair;
439            ColorPolynomial<C> pi;
440            ColorPolynomial<C> pj;
441            ColorPolynomial<C> S;
442            // GenPolynomial<GenPolynomial<C>> H;
443            ColorPolynomial<C> H;
444            while (pairlist.hasNext()) {
445                pair = pairlist.removeNext();
446                if (pair == null)
447                    continue;
448
449                pi = pair.pi;
450                pj = pair.pj;
451                if (debug) {
452                    logger.info("pi    = " + pi);
453                    logger.info("pj    = " + pj);
454                }
455
456                S = cred.SPolynomial(pi, pj);
457                if (S.isZERO()) {
458                    pair.setZero();
459                    continue;
460                }
461                if (debug) {
462                    // logger.info("ht(S) = " + S.leadingExpVector() );
463                    logger.info("S = " + S);
464                }
465
466                H = cred.normalform(cond, G, S);
467                if (H.isZERO()) {
468                    pair.setZero();
469                    continue;
470                }
471                if (debug) {
472                    logger.info("ht(H) = " + H.leadingExpVector());
473                }
474
475                H = H.abs();
476                if (debug) {
477                    logger.debug("H = " + H);
478                }
479                logger.info("H = " + H);
480                if (!H.isZERO()) {
481                    //CSh = new ArrayList<ColoredSystem<C>>();
482                    ncs = determineAddPairs(cs, H);
483                    if (ncs.size() == 0) {
484                        continue;
485                    }
486                    cs = ncs.remove(0); // remove other?
487                    pairlist = cs.pairlist;
488                    G = cs.list;
489                    cond = cs.condition;
490                    logger.info("replaced main branch = " + cond);
491                    logger.info("#new systems       = " + ncs.size());
492                    int yi = CSs.size();
493                    for (ColoredSystem<C> x : ncs) {
494                        if (!x.isDetermined()) {
495                            x = x.reDetermine();
496                        }
497                        CSs = x.addToList(CSs);
498                    }
499                    logger.info("#new systems added = " + (CSs.size() - yi));
500                }
501            }
502            // all s-pols reduce to zero in this branch
503            if (!cs.isDetermined()) {
504                cs = cs.reDetermine();
505            }
506            CSb.add(cs);
507            CSs.remove(0);
508            logger.info("done with = " + cs.condition);
509        }
510        // all branches done
511        CSh = new ArrayList<ColoredSystem<C>>();
512        for (ColoredSystem<C> x : CSb) {
513            // System.out.println("G = " + x.list );
514            if (!x.isDetermined()) {
515                x = x.reDetermine();
516            }
517            cs = minimalGB(x);
518            // System.out.println("min(G) = " + cs.list );
519            if (!cs.isDetermined()) {
520                cs = cs.reDetermine();
521            }
522            // cs = new ColoredSystem<C>( x.condition, G, x.pairlist );
523            CSh.add(cs);
524            logger.info("#sequential done = " + x.condition);
525            logger.info(x.pairlist.toString());
526        }
527        CSb = new ArrayList<ColoredSystem<C>>(CSh);
528        return new GroebnerSystem<C>(CSb);
529    }
530
531
532    /**
533     * Determine polynomial relative to a condition of a colored system and add
534     * pairs.
535     * @param cs a colored system.
536     * @param A color polynomial.
537     * @return list of colored systems, the conditions extending the condition
538     *         of cs.
539     */
540    public List<ColoredSystem<C>> determineAddPairs(ColoredSystem<C> cs, ColorPolynomial<C> A) {
541        List<ColoredSystem<C>> NCS = new ArrayList<ColoredSystem<C>>();
542        if (A == null || A.isZERO()) {
543            // NCS.add( cs );
544            return NCS;
545        }
546        List<ColorPolynomial<C>> S = cs.list;
547        Condition<C> cond = cs.condition; // .clone(); done in Condition
548        // itself
549        OrderedCPairlist<C> pl = cs.pairlist;
550
551        List<ColorPolynomial<C>> Sp;
552        ColorPolynomial<C> nz;
553        ColoredSystem<C> NS;
554        // if ( A.isDetermined() ) { ... } // dont use this
555        // System.out.println("to determine = " + A);
556        GenPolynomial<GenPolynomial<C>> Ap = A.getPolynomial();
557        List<Condition<C>> cd = cred.caseDistinction(cond, Ap);
558        logger.info("# cases = " + cd.size());
559        for (Condition<C> cnd : cd) {
560            //nz = cnd.determine(Ap);
561            nz = cnd.reDetermine(A);
562            if (nz.isZERO()) {
563                logger.info("zero determined nz = " + nz);
564                Sp = new ArrayList<ColorPolynomial<C>>(S);
565                OrderedCPairlist<C> PL = pl.copy();
566                NS = new ColoredSystem<C>(cnd, Sp, PL);
567                try {
568                    if (!NS.isDetermined()) {
569                        NS = NS.reDetermine();
570                    }
571                } catch (RuntimeException e) {
572                    System.out.println("Contradiction in NS_0 = " + NS);
573                    //e.printStackTrace();
574                    continue;
575                }
576                NCS = NS.addToList(NCS);
577                continue;
578            }
579            if (S.contains(nz)) {
580                System.out.println("*** S.contains(nz) ***");
581                continue;
582            }
583            logger.info("new determined nz = " + nz);
584            Sp = new ArrayList<ColorPolynomial<C>>(S);
585            Sp.add(nz);
586            OrderedCPairlist<C> PL = pl.copy();
587            PL.put(nz);
588            NS = new ColoredSystem<C>(cnd, Sp, PL);
589            try {
590                if (!NS.isDetermined()) {
591                    NS = NS.reDetermine();
592                }
593            } catch (RuntimeException e) {
594                System.out.println("Contradiction in NS = " + NS);
595                //e.printStackTrace();
596                continue;
597            }
598            NCS = NS.addToList(NCS);
599        }
600        // System.out.println("new determination = " + NCS);
601        return NCS;
602    }
603
604
605    /**
606     * Comprehensive Groebner base via Groebner system.
607     * @param F polynomial list.
608     * @return GB(F) a Comprehensive Groebner base of F.
609     */
610    // @Override
611    // @SuppressWarnings("unchecked")
612    public List<GenPolynomial<GenPolynomial<C>>> GB(List<GenPolynomial<GenPolynomial<C>>> F) {
613        if (F == null) {
614            return F;
615        }
616        // compute Groebner system
617        GroebnerSystem<C> gs = GBsys(F);
618        // System.out.println("\n\nGBsys = " + gs);
619        return gs.getCGB();
620    }
621
622
623    /**
624     * Minimal ordered Groebner basis.
625     * @param cs colored system.
626     * @return a reduced Groebner base of Gp.
627     */
628    // @Override
629    public ColoredSystem<C> minimalGB(ColoredSystem<C> cs) {
630        // List<ColorPolynomial<C>> Gp ) {
631        if (cs == null || cs.list == null || cs.list.size() <= 1) {
632            return cs;
633        }
634        // remove zero polynomials
635        List<ColorPolynomial<C>> G = new ArrayList<ColorPolynomial<C>>(cs.list.size());
636        for (ColorPolynomial<C> a : cs.list) {
637            if (a != null && !a.isZERO()) { // always true in GB()
638                // already positive a = a.abs();
639                G.add(a);
640            }
641        }
642        if (G.size() <= 1) {
643            return new ColoredSystem<C>(cs.condition, G, cs.pairlist);
644        }
645        // System.out.println("G check " + G);
646        // remove top reducible polynomials
647        Condition<C> cond = cs.condition;
648        ColorPolynomial<C> a, b;
649        List<ColorPolynomial<C>> F;
650        F = new ArrayList<ColorPolynomial<C>>(G.size());
651        while (G.size() > 0) {
652            a = G.remove(0);
653            b = a;
654            // System.out.println("check " + b);
655            //if (false) {
656            //    if (a.red.leadingBaseCoefficient().isConstant()) { // dont drop
657            //        // these
658            //        F.add(a);
659            //        continue;
660            //    }
661            //}
662            if (cred.isTopReducible(G, a) || cred.isTopReducible(F, a)) {
663                // drop polynomial
664                if (debug) {
665                    // System.out.println("trying to drop " + a);
666                    List<ColorPolynomial<C>> ff;
667                    ff = new ArrayList<ColorPolynomial<C>>(G);
668                    ff.addAll(F);
669                    a = cred.normalform(cond, ff, a);
670                    try {
671                        a = cond.reDetermine(a);
672                    } catch (RuntimeException ignored) {
673                    }
674                    if (!a.isZERO()) {
675                        logger.error("nf(a) != 0 " + b + ", " + a);
676                        F.add(b);
677                    }
678                }
679            } else {
680                F.add(a);
681            }
682        }
683        G = F;
684        if (G.size() <= 1) {
685            return new ColoredSystem<C>(cs.condition, G, cs.pairlist);
686        }
687        Collections.reverse(G); // important for lex GB
688        // reduce remaining polynomials
689        int len = G.size();
690        int i = 0;
691        while (i < len) {
692            a = G.remove(0);
693            b = a;
694            ExpVector e = a.red.leadingExpVector();
695            // System.out.println("reducing " + a);
696            a = cred.normalform(cond, G, a); // unchanged by top reduction
697            // System.out.println("reduced " + a);
698            try {
699                a = cond.reDetermine(a);
700            } catch (RuntimeException ignored) {
701            }
702            ExpVector f = a.red.leadingExpVector();
703            // a = ##engine.basePrimitivePart(a); //a.monic(); was not required
704            // a = a.abs();
705            // a = red.normalform( F, a );
706            if (e.equals(f)) {
707                G.add(a); // adds as last
708            } else { // should not happen
709                if (debug) {
710                    logger.error("nf(a) not determined " + b + ", " + a);
711                }
712                G.add(b); // adds as last
713            }
714            i++;
715        }
716        return new ColoredSystem<C>(cs.condition, G, cs.pairlist);
717    }
718
719}