/*
 * $Id$
 */

package edu.jas.gbufd;


import org.apache.log4j.Logger;

import edu.jas.arith.BigInteger;
import edu.jas.arith.BigRational;
import edu.jas.arith.ModInteger;
import edu.jas.arith.ModIntegerRing;
import edu.jas.arith.ModLong;
import edu.jas.arith.ModLongRing;
import edu.jas.arith.Product;
import edu.jas.arith.ProductRing;
import edu.jas.gb.DGroebnerBaseSeq;
import edu.jas.gb.EGroebnerBaseSeq;
import edu.jas.gb.GBProxy;
import edu.jas.gb.GroebnerBaseAbstract;
import edu.jas.gb.GroebnerBaseParallel;
import edu.jas.gb.GroebnerBaseSeq;
import edu.jas.gb.OrderedPairlist;
import edu.jas.gb.PairList;
import edu.jas.gb.ReductionSeq;
import edu.jas.kern.ComputerThreads;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingElem;
import edu.jas.structure.RingFactory;
import edu.jas.ufd.Quotient;
import edu.jas.ufd.QuotientRing;


/**
 * Groebner bases algorithms factory. Select appropriate Groebner bases engine
 * based on the coefficient types.
 * @author Heinz Kredel
 * @usage To create objects that implement the GroebnerBase
 *        interface use the GBFactory. It will select an
 *        appropriate implementation based on the types of polynomial
 *        coefficients C. The method to obtain an implementation is
 *        getImplementation(). getImplementation()
 *        returns an object of a class which implements the
 *        GroebnerBase interface, more precisely an object of
 *        abstract class GroebnerBaseAbstract.
 * 
 *        
 * 
 * GroebnerBase<CT> engine;
 * engine = GBFactory.<CT> getImplementation(cofac);
 * c = engine.GB(A);
 * 
* * For example, if the coefficient type is BigInteger, the usage looks * like * *
 * 
 * BigInteger cofac = new BigInteger();
 * GroebnerBase<BigInteger> engine;
 * engine = GBFactory.getImplementation(cofac);
 * c = engine.GB(A);
 * 
* * @see edu.jas.gb.GroebnerBase * @see edu.jas.application.GBAlgorithmBuilder */ public class GBFactory { private static final Logger logger = Logger.getLogger(GBFactory.class); /** * Algorithm indicators: igb = integerGB, egb = e-GB, dgb = d-GB, qgb = * fraction coefficients GB, ffgb = fraction free GB. */ public static enum Algo { igb, egb, dgb, qgb, ffgb }; /** * Protected factory constructor. */ protected GBFactory() { } /** * Determine suitable implementation of GB algorithms, no factory case. * @return GB algorithm implementation for field coefficients. */ public static > GroebnerBaseAbstract getImplementation() { logger.warn("no coefficent factory given, assuming field coeffcients"); GroebnerBaseAbstract bba = new GroebnerBaseSeq(); return bba; } /** * Determine suitable implementation of GB algorithms, case ModLong. * @param fac ModLongRing. * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(ModLongRing fac) { return getImplementation(fac, new OrderedPairlist()); } /** * Determine suitable implementation of GB algorithms, case ModLong. * @param fac ModLongRing. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(ModLongRing fac, PairList pl) { GroebnerBaseAbstract bba; if (fac.isField()) { bba = new GroebnerBaseSeq(pl); } else { bba = new GroebnerBasePseudoSeq(fac, pl); } return bba; } /** * Determine suitable implementation of GB algorithms, case ModInteger. * @param fac ModIntegerRing. * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(ModIntegerRing fac) { return getImplementation(fac, new OrderedPairlist()); } /** * Determine suitable implementation of GB algorithms, case ModInteger. * @param fac ModIntegerRing. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(ModIntegerRing fac, PairList pl) { GroebnerBaseAbstract bba; if (fac.isField()) { bba = new GroebnerBaseSeq(pl); } else { bba = new GroebnerBasePseudoSeq(fac, pl); } return bba; } /** * Determine suitable implementation of GB algorithms, case BigInteger. * @param fac BigInteger. * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigInteger fac) { return getImplementation(fac, Algo.igb); } /** * Determine suitable implementation of GB algorithms, case BigInteger. * @param fac BigInteger. * @param a algorithm, a = igb, egb, dgb. * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigInteger fac, Algo a) { return getImplementation(fac, a, new OrderedPairlist()); } /** * Determine suitable implementation of GB algorithms, case BigInteger. * @param fac BigInteger. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigInteger fac, PairList pl) { return getImplementation(fac, Algo.igb, pl); } /** * Determine suitable implementation of GB algorithms, case BigInteger. * @param fac BigInteger. * @param a algorithm, a = igb, egb, dgb. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigInteger fac, Algo a, PairList pl) { GroebnerBaseAbstract bba; switch (a) { case igb: bba = new GroebnerBasePseudoSeq(fac, pl); break; case egb: bba = new EGroebnerBaseSeq(); // pl not suitable break; case dgb: bba = new DGroebnerBaseSeq(); // pl not suitable break; default: throw new IllegalArgumentException("algorithm not available for BigInteger " + a); } return bba; } /** * Determine suitable implementation of GB algorithms, case BigRational. * @param fac BigRational. * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigRational fac) { return getImplementation(fac, Algo.qgb); } /** * Determine suitable implementation of GB algorithms, case BigRational. * @param fac BigRational. * @param a algorithm, a = qgb, ffgb. * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigRational fac, Algo a) { return getImplementation(fac, a, new OrderedPairlist()); } /** * Determine suitable implementation of GB algorithms, case BigRational. * @param fac BigRational. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigRational fac, PairList pl) { return getImplementation(fac, Algo.qgb, pl); } /** * Determine suitable implementation of GB algorithms, case BigRational. * @param fac BigRational. * @param a algorithm, a = qgb, ffgb. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static GroebnerBaseAbstract getImplementation(BigRational fac, Algo a, PairList pl) { GroebnerBaseAbstract bba; switch (a) { case qgb: bba = new GroebnerBaseSeq(pl); break; case ffgb: bba = new GroebnerBaseRational(); // pl not possible break; default: throw new IllegalArgumentException("algorithm not available for BigRational " + a); } return bba; } /** * Determine suitable implementation of GB algorithms, case Quotient * coefficients. * @param fac QuotientRing. * @return GB algorithm implementation. */ public static > GroebnerBaseAbstract> getImplementation( QuotientRing fac) { return getImplementation(fac, Algo.qgb); } /** * Determine suitable implementation of GB algorithms, case Quotient * coefficients. * @param fac QuotientRing. * @param a algorithm, a = qgb, ffgb. * @return GB algorithm implementation. */ public static > GroebnerBaseAbstract> getImplementation( QuotientRing fac, Algo a) { return getImplementation(fac, a, new OrderedPairlist>()); } /** * Determine suitable implementation of GB algorithms, case Quotient * coefficients. * @param fac QuotientRing. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static > GroebnerBaseAbstract> getImplementation( QuotientRing fac, PairList> pl) { return getImplementation(fac, Algo.qgb, pl); } /** * Determine suitable implementation of GB algorithms, case Quotient * coefficients. * @param fac QuotientRing. * @param a algorithm, a = qgb, ffgb. * @param pl pair selection strategy * @return GB algorithm implementation. */ public static > GroebnerBaseAbstract> getImplementation( QuotientRing fac, Algo a, PairList> pl) { GroebnerBaseAbstract> bba; switch (a) { case qgb: bba = new GroebnerBaseSeq>(new ReductionSeq>(), pl); break; case ffgb: bba = new GroebnerBaseSeqQuotient(fac); // pl not possible break; default: throw new IllegalArgumentException("algorithm not available for Quotient " + a); } return bba; } /** * Determine suitable implementation of GB algorithms, case (recursive) * polynomial. * @param fac GenPolynomialRing<C>. * @return GB algorithm implementation. */ public static > GroebnerBaseAbstract> getImplementation( GenPolynomialRing fac) { GroebnerBaseAbstract> bba; bba = new GroebnerBasePseudoRecSeq(fac); return bba; } /** * Determine suitable implementation of GB algorithms, case regular rings. * @param fac RegularRing. * @return GB algorithm implementation. */ public static > GroebnerBaseAbstract> getImplementation( ProductRing fac) { GroebnerBaseAbstract> bba; if (fac.onlyFields()) { bba = new RGroebnerBaseSeq>(); } else { bba = new RGroebnerBasePseudoSeq>(fac); } return bba; } /** * Determine suitable implementation of GB algorithms, other cases. * @param fac RingFactory<C>. * @return GB algorithm implementation. */ public static > // interface RingElem not sufficient GroebnerBaseAbstract getImplementation(RingFactory fac) { return getImplementation(fac, new OrderedPairlist()); } /** * Determine suitable implementation of GB algorithms, other cases. * @param fac RingFactory<C>. * @param pl pair selection strategy * @return GB algorithm implementation. */ @SuppressWarnings("unchecked") public static > // interface RingElem not sufficient GroebnerBaseAbstract getImplementation(RingFactory fac, PairList pl) { logger.debug("fac = " + fac.getClass().getName()); if (fac.isField()) { return new GroebnerBaseSeq(pl); } GroebnerBaseAbstract bba = null; Object ofac = fac; if (ofac instanceof GenPolynomialRing) { GenPolynomialRing rofac = (GenPolynomialRing) ofac; GroebnerBaseAbstract> bbr = new GroebnerBasePseudoRecSeq(rofac); // no pl bba = (GroebnerBaseAbstract) bbr; } else if (ofac instanceof ProductRing) { ProductRing pfac = (ProductRing) ofac; if (pfac.onlyFields()) { bba = new RGroebnerBaseSeq>(); } else { bba = new RGroebnerBasePseudoSeq>(pfac); } } else { bba = new GroebnerBasePseudoSeq(fac, pl); } logger.debug("bba = " + bba.getClass().getName()); return bba; } /** * Determine suitable concurrent implementation of GB algorithms if * possible. * @param fac RingFactory<C>. * @return GB proxy algorithm implementation. */ public static > // interface RingElem not sufficient GroebnerBaseAbstract getProxy(RingFactory fac) { return getProxy(fac, new OrderedPairlist()); } /** * Determine suitable concurrent implementation of GB algorithms if * possible. * @param fac RingFactory<C>. * @param pl pair selection strategy * @return GB proxy algorithm implementation. */ public static > // interface RingElem not sufficient GroebnerBaseAbstract getProxy(RingFactory fac, PairList pl) { if (ComputerThreads.NO_THREADS) { return GBFactory. getImplementation(fac, pl); } logger.debug("fac = " + fac.getClass().getName()); int th = (ComputerThreads.N_CPUS > 2 ? ComputerThreads.N_CPUS - 1 : 2); if (fac.isField()) { GroebnerBaseAbstract e1 = new GroebnerBaseSeq(pl); GroebnerBaseAbstract e2 = new GroebnerBaseParallel(th, pl); return new GBProxy(e1, e2); } else if (fac.characteristic().signum() == 0) { GroebnerBaseAbstract e1 = new GroebnerBasePseudoSeq(fac, pl); GroebnerBaseAbstract e2 = new GroebnerBasePseudoParallel(th, fac, pl); return new GBProxy(e1, e2); } return getImplementation(fac); } }

Ads help maintain this website.