/*
 * $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.Complex;
import edu.jas.poly.ComplexRing;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingFactory;


/**
 * Factorization algorithms factory. Select appropriate factorization engine
 * based on the coefficient types.
 * @author Heinz Kredel
 * @usage To create objects that implement the Factorization
 *        interface use the FactorFactory. 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 FactorAbstract class which implements
 *        the Factorization interface.
 * 
 *        
 * Factorization<CT> engine;
 * engine = FactorFactory.<CT> getImplementation(cofac);
 * c = engine.factors(a);
 * 
* * For example, if the coefficient type is BigInteger, the usage looks * like * *
 * BigInteger cofac = new BigInteger();
 * Factorization<BigInteger> engine;
 * engine = FactorFactory.getImplementation(cofac);
 * Sm = engine.factors(poly);
 * 
* * @see edu.jas.ufd.Factorization#factors(edu.jas.poly.GenPolynomial P) */ public class FactorFactory { private static final Logger logger = Logger.getLogger(FactorFactory.class); /** * Protected factory constructor. */ protected FactorFactory() { } /** * Determine suitable implementation of factorization algorithm, case * ModInteger. * @param fac ModIntegerRing. * @return factorization algorithm implementation. */ public static FactorAbstract getImplementation(ModIntegerRing fac) { return new FactorModular(fac); } /** * Determine suitable implementation of factorization algorithm, case * ModInteger. * @param fac ModIntegerRing. * @return factorization algorithm implementation. */ public static FactorAbstract getImplementation(ModLongRing fac) { return new FactorModular(fac); } /** * Determine suitable implementation of factorization algorithm, case * BigInteger. * @param fac BigInteger. * @return factorization algorithm implementation. */ public static FactorAbstract getImplementation(BigInteger fac) { return new FactorInteger(); } /** * Determine suitable implementation of factorization algorithms, case * BigRational. * @param fac BigRational. * @return factorization algorithm implementation. */ public static FactorAbstract getImplementation(BigRational fac) { return new FactorRational(); } /** * Determine suitable implementation of factorization algorithms, case * AlgebraicNumber<C>. * @param fac AlgebraicNumberRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return factorization algorithm implementation. */ public static > FactorAbstract> getImplementation( AlgebraicNumberRing fac) { return new FactorAlgebraic(fac); } /** * Determine suitable implementation of factorization algorithms, case * Complex<C>. * @param fac ComplexRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return factorization algorithm implementation. */ public static > FactorAbstract> getImplementation(ComplexRing fac) { return new FactorComplex(fac); } /** * Determine suitable implementation of factorization algorithms, case * Quotient<C>. * @param fac QuotientRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return factorization algorithm implementation. */ public static > FactorAbstract> getImplementation(QuotientRing fac) { return new FactorQuotient(fac); } /** * Determine suitable implementation of factorization algorithms, case * recursive GenPolynomial<C>. Use recursiveFactors(). * @param fac GenPolynomialRing<C>. * @param coefficient type, e.g. BigRational, ModInteger. * @return factorization algorithm implementation. */ public static > FactorAbstract getImplementation(GenPolynomialRing fac) { return getImplementation(fac.coFac); } /** * Determine suitable implementation of factorization algorithms, other * cases. * @param coefficient type * @param fac RingFactory<C>. * @return factorization algorithm implementation. */ @SuppressWarnings("unchecked") public static > FactorAbstract getImplementation(RingFactory fac) { logger.info("factor factory = " + fac.getClass().getName()); //System.out.println("fac_o_ufd = " + fac.getClass().getName()); FactorAbstract/*raw type*/ufd = null; AlgebraicNumberRing afac = null; ComplexRing cfac = null; QuotientRing qfac = null; GenPolynomialRing pfac = null; Object ofac = fac; if (ofac instanceof BigInteger) { ufd = new FactorInteger(); } else if (ofac instanceof BigRational) { ufd = new FactorRational(); } else if (ofac instanceof ModIntegerRing) { ufd = new FactorModular(fac); } else if (ofac instanceof ModLongRing) { ufd = new FactorModular(fac); } else if (ofac instanceof ComplexRing) { cfac = (ComplexRing) ofac; ufd = new FactorComplex(cfac); } else if (ofac instanceof AlgebraicNumberRing) { //System.out.println("afac_o = " + ofac); afac = (AlgebraicNumberRing) ofac; //ofac = afac.ring.coFac; ufd = new FactorAlgebraic/*raw */(afac); } else if (ofac instanceof QuotientRing) { //System.out.println("qfac_o = " + ofac); qfac = (QuotientRing) ofac; ufd = new FactorQuotient/*raw */(qfac); } else if (ofac instanceof GenPolynomialRing) { //System.out.println("qfac_o = " + ofac); pfac = (GenPolynomialRing) ofac; ufd = getImplementation(pfac.coFac); } else { throw new IllegalArgumentException("no factorization implementation for " + fac.getClass().getName()); } //logger.info("implementation = " + ufd); return (FactorAbstract) ufd; } }

Ads help maintain this website.