001/*
002 * $Id$
003 */
004
005package edu.jas.ufd;
006
007
008import junit.framework.Test;
009import junit.framework.TestCase;
010import junit.framework.TestSuite;
011
012
013import edu.jas.arith.BigComplex;
014import edu.jas.arith.BigInteger;
015import edu.jas.arith.BigRational;
016import edu.jas.arith.ModInteger;
017import edu.jas.arith.ModIntegerRing;
018import edu.jas.poly.AlgebraicNumber;
019import edu.jas.poly.AlgebraicNumberRing;
020import edu.jas.poly.GenPolynomial;
021import edu.jas.poly.GenPolynomialRing;
022import edu.jas.poly.TermOrder;
023
024
025/**
026 * GreatestCommonDivisor factory tests with JUnit.
027 * @author Heinz Kredel
028 */
029
030public class GCDFactoryTest extends TestCase {
031
032
033    /**
034     * main.
035     */
036    public static void main(String[] args) {
037        junit.textui.TestRunner.run(suite());
038    }
039
040
041    /**
042     * Constructs a <CODE>GCDFactoryTest</CODE> object.
043     * @param name String.
044     */
045    public GCDFactoryTest(String name) {
046        super(name);
047    }
048
049
050    /**
051     */
052    public static Test suite() {
053        TestSuite suite = new TestSuite(GCDFactoryTest.class);
054        return suite;
055    }
056
057
058    TermOrder to = new TermOrder(TermOrder.INVLEX);
059
060
061    GenPolynomialRing<BigInteger> dfac;
062
063
064    GenPolynomialRing<BigInteger> cfac;
065
066
067    GenPolynomialRing<GenPolynomial<BigInteger>> rfac;
068
069
070    //BigInteger ai, bi, ci, di, ei;
071
072
073    GenPolynomial<BigInteger> a, b, c, d, e;
074
075
076    //GenPolynomial<GenPolynomial<BigInteger>> ar, br, cr, dr, er;
077
078
079    int rl = 5;
080
081
082    int kl = 4;
083
084
085    int ll = 5;
086
087
088    int el = 3;
089
090
091    float q = 0.3f;
092
093
094    @Override
095    protected void setUp() {
096        a = b = c = d = e = null;
097        //ai = bi = ci = di = ei = null;
098        //ar = br = cr = dr = er = null;
099        dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to);
100        cfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl - 1, to);
101        rfac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to);
102    }
103
104
105    @Override
106    protected void tearDown() {
107        a = b = c = d = e = null;
108        //ai = bi = ci = di = ei = null;
109        //ar = br = cr = dr = er = null;
110        dfac = null;
111        cfac = null;
112        rfac = null;
113    }
114
115
116    /**
117     * Test get BigInteger implementation.
118     */
119    public void testBigInteger() {
120        BigInteger bi = new BigInteger();
121        GreatestCommonDivisor<BigInteger> ufd;
122
123        ufd = GCDFactory.getImplementation(bi);
124        //System.out.println("ufd = " + ufd);
125        assertTrue("ufd = Modular " + ufd, ufd instanceof GreatestCommonDivisorModular);
126    }
127
128
129    /**
130     * Test get ModInteger implementation.
131     */
132    public void testModInteger() {
133        ModIntegerRing mi = new ModIntegerRing(19, true);
134        GreatestCommonDivisor<ModInteger> ufd;
135
136        ufd = GCDFactory.getImplementation(mi);
137        //System.out.println("ufd = " + ufd);
138        assertTrue("ufd != ModEval " + ufd, ufd instanceof GreatestCommonDivisorModEval);
139
140        mi = new ModIntegerRing(30);
141        ufd = GCDFactory.getImplementation(mi);
142        //System.out.println("ufd = " + ufd);
143        assertTrue("ufd != Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres);
144    }
145
146
147    /**
148     * Test get BigRational implementation.
149     */
150    public void testBigRational() {
151        BigRational b = new BigRational();
152        GreatestCommonDivisor<BigRational> ufd;
153
154        ufd = GCDFactory.getImplementation(b);
155        //System.out.println("ufd = " + ufd);
156        assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive);
157    }
158
159
160    /**
161     * Test get BigComplex implementation.
162     */
163    public void testBigComplex() {
164        BigComplex b = new BigComplex();
165        GreatestCommonDivisor<BigComplex> ufd;
166
167        ufd = GCDFactory.<BigComplex> getImplementation(b);
168        //System.out.println("ufd = " + ufd);
169        assertTrue("ufd != Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple);
170    }
171
172
173    /**
174     * Test get AlgebraicNumber&lt;BigRational&gt; implementation.
175     */
176    public void testAlgebraicNumberBigRational() {
177        BigRational b = new BigRational();
178        GenPolynomialRing<BigRational> fac;
179        fac = new GenPolynomialRing<BigRational>(b, 1);
180        GenPolynomial<BigRational> mo = fac.random(kl, ll, el, q);
181        while (mo.isZERO() || mo.isONE() || mo.isConstant()) {
182            mo = fac.random(kl, ll, el, q);
183        }
184
185        AlgebraicNumberRing<BigRational> afac;
186        afac = new AlgebraicNumberRing<BigRational>(mo);
187
188        GreatestCommonDivisor<AlgebraicNumber<BigRational>> ufd;
189
190        ufd = GCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac);
191        //System.out.println("ufd1 = " + ufd);
192        assertTrue("ufd = Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres);
193
194
195        mo = fac.univariate(0).subtract(fac.getONE());
196        afac = new AlgebraicNumberRing<BigRational>(mo, true);
197
198        ufd = GCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac);
199        //System.out.println("ufd1 = " + ufd);
200        assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple);
201    }
202
203
204    /**
205     * Test get AlgebraicNumber&lt;ModInteger&gt; implementation.
206     */
207    public void testAlgebraicNumberModInteger() {
208        ModIntegerRing b = new ModIntegerRing(19, true);
209        GenPolynomialRing<ModInteger> fac;
210        fac = new GenPolynomialRing<ModInteger>(b, 1);
211        GenPolynomial<ModInteger> mo = fac.random(kl, ll, el, q);
212        while (mo.isZERO() || mo.isONE() || mo.isConstant()) {
213            mo = fac.random(kl, ll, el, q);
214        }
215
216        AlgebraicNumberRing<ModInteger> afac;
217        afac = new AlgebraicNumberRing<ModInteger>(mo);
218
219        AlgebraicNumber<ModInteger> a = afac.getONE();
220        assertTrue("a == 1 " + a, a.isONE());
221        GreatestCommonDivisor<AlgebraicNumber<ModInteger>> ufd;
222
223        ufd = GCDFactory.<AlgebraicNumber<ModInteger>> getImplementation(afac);
224        //System.out.println("ufd2 = " + ufd);
225        assertTrue("ufd = Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres);
226    }
227
228}