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