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