001/*
002 * $Id$
003 */
004
005package edu.jas.application;
006
007
008import java.util.SortedMap;
009import java.util.List;
010
011
012
013import junit.framework.Test;
014import junit.framework.TestCase;
015import junit.framework.TestSuite;
016
017import edu.jas.arith.BigRational;
018import edu.jas.kern.ComputerThreads;
019import edu.jas.poly.AlgebraicNumber;
020import edu.jas.poly.AlgebraicNumberRing;
021import edu.jas.poly.GenPolynomial;
022import edu.jas.poly.GenPolynomialRing;
023import edu.jas.poly.TermOrder;
024import edu.jas.poly.Complex;
025import edu.jas.poly.ComplexRing;
026import edu.jas.root.Interval;
027import edu.jas.root.RootUtil;
028import edu.jas.ufd.FactorAbstract;
029
030
031/**
032 * Factor real algebraic tests with JUnit.
033 * @author Heinz Kredel
034 */
035
036public class FactorRealRealTest extends TestCase {
037
038
039    /**
040     * main.
041     */
042    public static void main(String[] args) {
043        
044        junit.textui.TestRunner.run(suite());
045    }
046
047
048    /**
049     * Constructs a <CODE>FactorRealRealTest</CODE> object.
050     * @param name String.
051     */
052    public FactorRealRealTest(String name) {
053        super(name);
054    }
055
056
057    /**
058     */
059    public static Test suite() {
060        TestSuite suite = new TestSuite(FactorRealRealTest.class);
061        return suite;
062    }
063
064
065    int rl = 1;
066
067
068    int kl = 5;
069
070
071    int ll = 5;
072
073
074    int el = 3;
075
076
077    float q = 0.3f;
078
079
080    @Override
081    protected void setUp() {
082    }
083
084
085    @Override
086    protected void tearDown() {
087        ComputerThreads.terminate();
088    }
089
090
091    /**
092     * Test dummy for Junit.
093     */
094    public void testDummy() {
095    }
096
097
098    /**
099     * Test real real algebraic factorization.
100     */
101    public void testRealRealAlgebraicFactorization() {
102
103        TermOrder to = new TermOrder(TermOrder.INVLEX);
104        BigRational bfac = new BigRational(1);
105
106        ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(bfac);
107        String[] vars = new String[] { "z" };
108        GenPolynomialRing<Complex<BigRational>> dfac = new GenPolynomialRing<Complex<BigRational>>(cfac, to, vars);
109        GenPolynomial<Complex<BigRational>> ap = dfac.parse("z^3 - i2");
110
111        List<Complex<RealAlgebraicNumber<BigRational>>> roots
112            = RootFactoryApp.<BigRational> complexAlgebraicNumbersComplex(ap); 
113        //System.out.println("ap = " + ap);
114        //System.out.println("roots = " + roots);
115        assertTrue("#roots == deg(ap) ", roots.size() == ap.degree(0));
116
117        for ( Complex<RealAlgebraicNumber<BigRational>> root : roots ) {
118            RealAlgebraicRing<BigRational> rfac = root.getRe().ring;
119            rfac.setField(true); // ?? to check
120            assertTrue("isField(rfac) ", rfac.isField());
121            FactorRealReal<BigRational> fac = new FactorRealReal<BigRational>(rfac);
122            //FactorAbstract<RealAlgebraicNumber<BigRational>> fac = FactorFactory.<RealAlgebraicNumber<BigRational>> getImplementation(rfac);
123            String[] var = new String[] { "t" };
124            GenPolynomialRing<RealAlgebraicNumber<BigRational>> rpfac 
125                = new GenPolynomialRing<RealAlgebraicNumber<BigRational>>(rfac, to, var); // univariate
126
127            GenPolynomial<RealAlgebraicNumber<BigRational>> a;
128            GenPolynomial<RealAlgebraicNumber<BigRational>> b = rpfac.random(2, ll, el, q);
129            GenPolynomial<RealAlgebraicNumber<BigRational>> c = rpfac.random(2, ll, el, q);
130            if (b.degree() == 0) {
131                b = b.multiply(rpfac.univariate(0));
132            }
133            //b = b.monic();
134            int facs = 0;
135            if (c.degree() > 0) {
136                facs++;
137            }
138            if (b.degree() > 0) {
139                facs++;
140            }
141            a = c.multiply(b);
142            //a = c;
143            //a = a.monic();
144            //System.out.println("\na = " + a);
145            //System.out.println("b = " + b);
146            //System.out.println("c = " + c);
147            //System.out.println("b = " + b.monic());
148            //System.out.println("c = " + c.monic());
149
150            SortedMap<GenPolynomial<RealAlgebraicNumber<BigRational>>, Long> sm = fac.baseFactors(a);
151            //System.out.println("\na   =  " + a);
152            //System.out.println("sm = " + sm);
153            if (sm.size() >= facs) {
154                assertTrue("#facs < " + facs, sm.size() >= facs);
155            } else {
156                System.out.println("sm.size() < facs = " + facs);
157            }
158            boolean t = fac.isFactorization(a, sm);
159            //System.out.println("t        = " + t);
160            assertTrue("prod(factor(a)) = a", t);
161            break;
162        }
163    }
164
165}