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