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