/*
 * $Id$
 */

package edu.jas.ufd;


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.poly.AlgebraicNumber;
import edu.jas.poly.AlgebraicNumberRing;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingFactory;


/**
 * Squarefree factorization algorithms factory. Select appropriate squarefree
 * factorization engine based on the coefficient types.
 * @author Heinz Kredel
 * @usage To create objects that implement the Squarefree interface
 *        use the SquarefreeFactory. It will select an appropriate
 *        implementation based on the types of polynomial coefficients C. To
 *        obtain an implementation use getImplementation(), it
 *        returns an object of a class which extends the
 *        SquarefreeAbstract class which implements the
 *        Squarefree interface.
 * 
 *        
 * Squarefree<CT> engine;
 * engine = SquarefreeFactory.<CT> getImplementation(cofac);
 * c = engine.squarefreeFactors(a);
 * 
* * For example, if the coefficient type is BigInteger, the usage looks * like * *
 * BigInteger cofac = new BigInteger();
 * Squarefree<BigInteger> engine;
 * engine = SquarefreeFactory.getImplementation(cofac);
 * Sm = engine.sqaurefreeFactors(poly);
 * 
* * @see edu.jas.ufd.Squarefree#squarefreeFactors(edu.jas.poly.GenPolynomial P) */ public class SquarefreeFactory { private static final Logger logger = Logger.getLogger(SquarefreeFactory.class); /** * Protected factory constructor. */ protected SquarefreeFactory() { } /** * Determine suitable implementation of factorization algorithm, case * ModInteger. * @param fac ModIntegerRing. * @return squarefree factorization algorithm implementation. */ public static SquarefreeAbstract getImplementation(ModIntegerRing fac) { return new SquarefreeFiniteFieldCharP(fac); } /** * Determine suitable implementation of factorization algorithm, case * ModLong. * @param fac ModLongRing. * @return squarefree factorization algorithm implementation. */ public static SquarefreeAbstract getImplementation(ModLongRing fac) { return new SquarefreeFiniteFieldCharP(fac); } /** * Determine suitable implementation of squarefree factorization algorithm, * case BigInteger. * @param fac BigInteger. * @return squarefree factorization algorithm implementation. */ public static SquarefreeAbstract getImplementation(BigInteger fac) { return new SquarefreeRingChar0(fac); } /** * Determine suitable implementation of squarefree factorization algorithms, * case BigRational. * @param fac BigRational. * @return squarefree factorization algorithm implementation. */ public static SquarefreeAbstract getImplementation(BigRational fac) { return new SquarefreeFieldChar0(fac); } /** * Determine suitable implementation of squarefree factorization algorithms, * case AlgebraicNumber<C>. * @param fac AlgebraicNumberRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return squarefree factorization algorithm implementation. */ public static > SquarefreeAbstract> getImplementation( AlgebraicNumberRing fac) { PolyUfdUtil. ensureFieldProperty(fac); if (fac.isField()) { if (fac.characteristic().signum() == 0) { return new SquarefreeFieldChar0>(fac); } if (fac.isFinite()) { return new SquarefreeFiniteFieldCharP>(fac); } return new SquarefreeInfiniteAlgebraicFieldCharP(fac); } throw new ArithmeticException("eventually no integral domain " + fac.getClass().getName()); } /** * Determine suitable implementation of squarefree factorization algorithms, * case Quotient<C>. * @param fac QuotientRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return squarefree factorization algorithm implementation. */ public static > SquarefreeAbstract> getImplementation( QuotientRing fac) { if (fac.characteristic().signum() == 0) { return new SquarefreeFieldChar0>(fac); } return new SquarefreeInfiniteFieldCharP(fac); } /** * Determine suitable implementation of squarefree factorization algorithms, * case GenPolynomial<C>. * @param fac GenPolynomialRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return squarefree factorization algorithm implementation. */ public static > SquarefreeAbstract getImplementation(GenPolynomialRing fac) { return getImplementationPoly(fac); } /* * Determine suitable implementation of squarefree factorization algorithms, * case GenPolynomial<C>. * @param fac GenPolynomialRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return squarefree factorization algorithm implementation. */ @SuppressWarnings("unchecked") protected static > SquarefreeAbstract getImplementationPoly( GenPolynomialRing fac) { if (fac.characteristic().signum() == 0) { if (fac.coFac.isField()) { return new SquarefreeFieldChar0(fac.coFac); } return new SquarefreeRingChar0(fac.coFac); } if (fac.coFac.isFinite()) { return new SquarefreeFiniteFieldCharP(fac.coFac); } Object ocfac = fac.coFac; SquarefreeAbstract saq = null; if (ocfac instanceof QuotientRing) { QuotientRing qf = (QuotientRing) ocfac; saq = new SquarefreeInfiniteFieldCharP(qf); } else if (ocfac instanceof AlgebraicNumberRing) { AlgebraicNumberRing af = (AlgebraicNumberRing) ocfac; saq = new SquarefreeInfiniteAlgebraicFieldCharP(af); } if (saq == null) { throw new IllegalArgumentException("no squarefree factorization " + fac.coFac); } SquarefreeAbstract sa = (SquarefreeAbstract) saq; return sa; } /** * Determine suitable implementation of squarefree factorization algorithms, * other cases. * @param coefficient type * @param fac RingFactory<C>. * @return squarefree factorization algorithm implementation. */ @SuppressWarnings("unchecked") public static > SquarefreeAbstract getImplementation(RingFactory fac) { //logger.info("fac = " + fac.getClass().getName()); //System.out.println("fac_o = " + fac.getClass().getName()); SquarefreeAbstract/*raw type*/ufd = null; AlgebraicNumberRing afac = null; QuotientRing qfac = null; GenPolynomialRing pfac = null; Object ofac = fac; if (ofac instanceof BigInteger) { ufd = new SquarefreeRingChar0(fac); } else if (ofac instanceof BigRational) { ufd = new SquarefreeFieldChar0(fac); } else if (ofac instanceof ModIntegerRing) { ufd = new SquarefreeFiniteFieldCharP(fac); } else if (ofac instanceof ModLongRing) { ufd = new SquarefreeFiniteFieldCharP(fac); } else if (ofac instanceof AlgebraicNumberRing) { afac = (AlgebraicNumberRing) ofac; //ofac = afac.ring.coFac; //System.out.println("o_afac = " + ofac); ufd = getImplementation(afac); } else if (ofac instanceof QuotientRing) { qfac = (QuotientRing) ofac; ufd = getImplementation(qfac); } else if (ofac instanceof GenPolynomialRing) { pfac = (GenPolynomialRing) ofac; ufd = getImplementationPoly(pfac); } else if (fac.isField()) { //System.out.println("fac_field = " + fac); if (fac.characteristic().signum() == 0) { ufd = new SquarefreeFieldChar0(fac); } else { if (fac.isFinite()) { ufd = new SquarefreeFiniteFieldCharP(fac); } else { ufd = new SquarefreeInfiniteFieldCharP/*raw*/(fac); } } } else if (fac.characteristic().signum() == 0) { ufd = new SquarefreeRingChar0(fac); } else { throw new IllegalArgumentException("no squarefree factorization implementation for " + fac.getClass().getName()); } logger.debug("ufd = " + ufd); return (SquarefreeAbstract) ufd; } }

Ads help maintain this website.