001    /*
002     * $Id: FactorFactory.java 3676 2011-07-02 10:51:16Z kredel $
003     */
004    
005    package edu.jas.application;
006    
007    
008    import org.apache.log4j.Logger;
009    
010    import edu.jas.arith.Rational;
011    import edu.jas.structure.GcdRingElem;
012    import edu.jas.structure.RingFactory;
013    import edu.jas.ufd.FactorAbstract;
014    
015    
016    /**
017     * Factorization algorithms factory. Select appropriate factorization engine
018     * based on the coefficient types.
019     * @author Heinz Kredel
020     * @usage To create objects that implement the <code>Factorization</code>
021     *        interface use the <code>FactorFactory</code>. It will select an
022     *        appropriate implementation based on the types of polynomial
023     *        coefficients C. To obtain an implementation use
024     *        <code>getImplementation()</code>, it returns an object of a class
025     *        which extends the <code>FactorAbstract</code> class which implements
026     *        the <code>Factorization</code> interface.
027     * 
028     *        <pre>
029     * Factorization&lt;CT&gt; engine;
030     * engine = FactorFactory.&lt;CT&gt; getImplementation(cofac);
031     * c = engine.factors(a);
032     * </pre>
033     * 
034     *        For example, if the coefficient type is BigInteger, the usage looks
035     *        like
036     * 
037     *        <pre>
038     * BigInteger cofac = new BigInteger();
039     * Factorization&lt;BigInteger&gt; engine;
040     * engine = FactorFactory.getImplementation(cofac);
041     * Sm = engine.factors(poly);
042     * </pre>
043     * 
044     * @see edu.jas.ufd.Factorization#factors(edu.jas.poly.GenPolynomial P)
045     * @see edu.jas.ufd.FactorFactory#getImplementation(edu.jas.structure.RingFactory
046     *      P)
047     */
048    
049    public class FactorFactory extends edu.jas.ufd.FactorFactory {
050    
051    
052        private static final Logger logger = Logger.getLogger(FactorFactory.class);
053    
054    
055        /**
056         * Protected factory constructor.
057         */
058        protected FactorFactory() {
059        }
060    
061    
062        /**
063         * Determine suitable implementation of factorization algorithms, case
064         * RealAlgebraicNumber&lt;C&gt;.
065         * @param fac RealAlgebraicRing&lt;C&gt;.
066         * @param <C> coefficient type, e.g. BigRational.
067         * @return factorization algorithm implementation.
068         */
069        public static <C extends GcdRingElem<C> & Rational> FactorAbstract<edu.jas.application.RealAlgebraicNumber<C>> getImplementation(
070                        edu.jas.application.RealAlgebraicRing<C> fac) {
071            return new FactorRealReal<C>(fac);
072        }
073    
074    
075        /**
076         * Determine suitable implementation of factorization algorithms, other
077         * cases.
078         * @param <C> coefficient type
079         * @param fac RingFactory&lt;C&gt;.
080         * @return factorization algorithm implementation.
081         */
082        @SuppressWarnings("unchecked")
083        public static <C extends GcdRingElem<C>> FactorAbstract<C> getImplementation(RingFactory<C> fac) {
084            //logger.info("fac = " + fac.getClass().getName());
085            //System.out.println("fac_o = " + fac.getClass().getName());
086            FactorAbstract/*raw type<C>*/ufd = null;
087            edu.jas.application.RealAlgebraicRing rrfac = null;
088            Object ofac = fac;
089            if (ofac instanceof edu.jas.application.RealAlgebraicRing) {
090                //System.out.println("rrfac_o = " + ofac);
091                rrfac = (edu.jas.application.RealAlgebraicRing) ofac;
092                ofac = rrfac.realRing;
093                ufd = new FactorRealReal/*raw <C>*/(rrfac);
094            } else {
095                ufd = edu.jas.ufd.FactorFactory.getImplementation(fac);
096                return (FactorAbstract<C>) ufd;
097            }
098            logger.info("ufd = " + ufd);
099            return (FactorAbstract<C>) ufd;
100        }
101    
102    }