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