001/*
002 * $Id$
003 */
004
005package edu.jas.gb;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.ArrayList;
012import java.util.List;
013
014import edu.jas.arith.BigInteger;
015import edu.jas.arith.BigRational;
016import edu.jas.kern.ComputerThreads;
017import edu.jas.poly.GenPolynomial;
018import edu.jas.poly.GenPolynomialRing;
019import edu.jas.poly.GenPolynomialTokenizer;
020import edu.jas.poly.PolyUtil;
021import edu.jas.poly.PolynomialList;
022
023import junit.framework.Test;
024import junit.framework.TestCase;
025import junit.framework.TestSuite;
026
027
028/**
029 * EGroebner base sequential tests with JUnit.
030 * @author Heinz Kredel
031 */
032
033public class EGroebnerBaseSeqTest extends TestCase {
034
035
036    /**
037     * main
038     */
039    public static void main(String[] args) {
040        junit.textui.TestRunner.run(suite());
041    }
042
043
044    /**
045     * Constructs a <CODE>EGroebnerBaseSeqTest</CODE> object.
046     * @param name String.
047     */
048    public EGroebnerBaseSeqTest(String name) {
049        super(name);
050    }
051
052
053    /**
054     * suite.
055     */
056    public static Test suite() {
057        TestSuite suite = new TestSuite(EGroebnerBaseSeqTest.class);
058        return suite;
059    }
060
061
062    GenPolynomialRing<BigInteger> fac;
063
064
065    List<GenPolynomial<BigInteger>> L, G;
066
067
068    PolynomialList<BigInteger> F;
069
070
071    GroebnerBaseAbstract<BigInteger> bb;
072
073
074    GenPolynomial<BigInteger> a, b, c, d, e;
075
076
077    int rl = 3; //4; //3; 
078
079
080    int kl = 4; //4; 10
081
082
083    int ll = 4;
084
085
086    int el = 3;
087
088
089    float q = 0.2f; //0.4f
090
091
092    @Override
093    protected void setUp() {
094        BigInteger coeff = new BigInteger(9);
095        fac = new GenPolynomialRing<BigInteger>(coeff, rl);
096        a = b = c = d = e = null;
097        bb = new EGroebnerBaseSeq<BigInteger>();
098    }
099
100
101    @Override
102    protected void tearDown() {
103        a = b = c = d = e = null;
104        fac = null;
105        bb = null;
106        ComputerThreads.terminate();
107    }
108
109
110    /**
111     * Test sequential GBase.
112     */
113    public void testSequentialGBase() {
114        L = new ArrayList<GenPolynomial<BigInteger>>();
115
116        a = fac.random(kl, ll, el, q).abs();
117        b = fac.random(kl, ll, el, q).abs();
118        c = fac.random(kl, ll / 2, el, q).abs();
119        d = fac.random(kl, ll / 2, el, q).abs();
120        e = d; //fac.random(kl, ll, el, q );
121
122        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
123            return;
124        }
125
126        L.add(a);
127        //System.out.println("    L  = " + L );
128        L = bb.GB(L);
129        //System.out.println("eGB(L) = " + L );
130        assertTrue("isGB( { a } )", bb.isGB(L));
131
132        L.add(b);
133        //System.out.println("    L  = " + L );
134        L = bb.GB(L);
135        //System.out.println("eGB(L) = " + L );
136        assertTrue("isGB( { a, b } )", bb.isGB(L));
137
138        L.add(c);
139        //System.out.println("    L  = " + L );
140        L = bb.GB(L);
141        //System.out.println("eGB(L) = " + L );
142        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
143
144        L.add(d);
145        //System.out.println("    L  = " + L );
146        L = bb.GB(L);
147        //System.out.println("eGB(L) = " + L );
148        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
149
150        L.add(e);
151        //System.out.println("    L  = " + L );
152        L = bb.GB(L);
153        //System.out.println("eGB(L) = " + L );
154        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
155    }
156
157
158    /**
159     * Test Trinks7 GBase over Z.
160     */
161    @SuppressWarnings("unchecked")
162    public void testTrinks7GBaseZ() { // needs 20 sec
163        String exam = "Z(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
164                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
165                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
166                        + "( 99 W - 11 B S + 3 B**2 ), " + "( 10000 B**2 + 6600 B + 2673 ) " + ") ";
167        Reader source = new StringReader(exam);
168        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
169        try {
170            F = (PolynomialList<BigInteger>) parser.nextPolynomialSet();
171        } catch (ClassCastException e) {
172            fail("" + e);
173        } catch (IOException e) {
174            fail("" + e);
175        }
176        //System.out.println("F = " + F);
177
178        G = bb.GB(F.list);
179        PolynomialList<BigInteger> trinks = new PolynomialList<BigInteger>(F.ring, G);
180        //System.out.println("G = " + trinks);
181        //System.out.println("G.size() = " + G.size());
182        assertTrue("isGB( eGB(Trinks7) )", bb.isGB(G));
183        //assertEquals("#GB(Trinks7) == 44", 44, G.size() );
184    }
185
186
187    /**
188     * Test Trinks7 GBase over Z(B).
189     */
190    @SuppressWarnings("unchecked")
191    public void testTrinks7GBaseZ_B() {
192        String exam = "IntFunc{ B } (S,T,Z,P,W) G " + "( " + "( { 45 } P + { 35 } S - { 165 B } - { 36 } ), "
193                        + "( { 35 } P + { 40 } Z + { 25 } T - { 27 } S ), "
194                        + "( { 15 } W + { 25 } S P + { 30 } Z - { 18 } T - { 165 B**2 } ), "
195                        + "( { - 9 } W + { 15 } T P + { 20 } S Z ), " + "( P W + { 2 } T Z - { 11 B**3 } ), "
196                        + "( { 99 } W - { 11 B } S + { 3 B**2 } ), " + "( { 10000 B**2 + 6600 B + 2673 } ) "
197                        + ") ";
198        Reader source = new StringReader(exam);
199        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
200        EGroebnerBaseSeq<GenPolynomial<BigRational>> bb = new EGroebnerBaseSeq<GenPolynomial<BigRational>>();
201        PolynomialList<GenPolynomial<BigRational>> F = null;
202        List<GenPolynomial<GenPolynomial<BigRational>>> G = null;
203        try {
204            F = (PolynomialList<GenPolynomial<BigRational>>) parser.nextPolynomialSet();
205        } catch (ClassCastException e) {
206            fail("" + e);
207        } catch (IOException e) {
208            fail("" + e);
209        }
210        //System.out.println("F = " + F);
211
212        List<GenPolynomial<GenPolynomial<BigRational>>> Fp = new ArrayList<GenPolynomial<GenPolynomial<BigRational>>>(
213                        F.list.size());
214        for (GenPolynomial<GenPolynomial<BigRational>> p : F.list) {
215            p = PolyUtil.<BigRational> monic(p);
216            Fp.add(p);
217        }
218        //System.out.println("Fp = " + Fp);
219        G = bb.GB(Fp);
220        //System.out.println("G = " + G);
221
222        List<GenPolynomial<GenPolynomial<BigRational>>> Gp = new ArrayList<GenPolynomial<GenPolynomial<BigRational>>>(
223                        F.list.size());
224        for (GenPolynomial<GenPolynomial<BigRational>> p : G) {
225            p = PolyUtil.<BigRational> monic(p);
226            Gp.add(p);
227        }
228        PolynomialList<GenPolynomial<BigRational>> trinks = new PolynomialList<GenPolynomial<BigRational>>(
229                        F.ring, Gp);
230        //System.out.println("G = " + trinks);
231        //System.out.println("G.size() = " + G.size());
232
233        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
234        //assertEquals("#GB(Trinks7) == 1", 1, G.size() );
235    }
236
237
238    /**
239     * Test sequential extended GBase.
240     */
241    public void testSequentialExtendedGBase() {
242        L = new ArrayList<GenPolynomial<BigInteger>>();
243        ExtendedGB<BigInteger> exgb;
244
245        a = fac.random(kl, ll, el, q);
246        b = fac.random(kl, ll, el, q);
247        c = fac.random(kl, ll, el, q);
248        d = fac.random(kl, ll, el, q);
249        e = d; //fac.random(kl, ll, el, q );
250
251        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
252            return;
253        }
254
255        assertTrue("not isZERO( a )", !a.isZERO());
256        L.add(a);
257        //System.out.println("L1 = " + L);
258
259        exgb = bb.extGB(L);
260        //System.out.println("exgb 1 = " + exgb);
261        assertTrue("isGB( { a } )", bb.isGB(exgb.G));
262        assertTrue("isRmat( { a } )", bb.isMinReductionMatrix(exgb));
263
264        assertTrue("not isZERO( b )", !b.isZERO());
265        L.add(b);
266        //System.out.println("L2 = " + L);
267
268        exgb = bb.extGB(L);
269        //System.out.println("exgb 2 = " + exgb);
270        assertTrue("isGB( { a, b } )", bb.isGB(exgb.G));
271        assertTrue("isRmat( { a, b } )", bb.isMinReductionMatrix(exgb));
272
273        assertTrue("not isZERO( c )", !c.isZERO());
274        L.add(c);
275        //System.out.println("L3 = " + L);
276
277        exgb = bb.extGB(L);
278        //System.out.println("exgb 3 = " + exgb );
279        assertTrue("isGB( { a, b, c } )", bb.isGB(exgb.G));
280        assertTrue("isRmat( { a, b, c } )", bb.isMinReductionMatrix(exgb));
281        //if (true) { return; }
282
283        assertTrue("not isZERO( d )", !d.isZERO());
284        L.add(d);
285        //System.out.println("L4 = " + L);
286
287        exgb = bb.extGB(L);
288        //System.out.println("exgb 4 = " + exgb );
289        assertTrue("isGB( { a, b, c, d } )", bb.isGB(exgb.G));
290        assertTrue("isRmat( { a, b, c, d } )", bb.isMinReductionMatrix(exgb));
291
292
293        assertTrue("not isZERO( e )", !e.isZERO());
294        L.add(e);
295        //System.out.println("L5 = " + L);
296
297        exgb = bb.extGB(L);
298        //System.out.println("exgb 5 = " + exgb );
299        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(exgb.G));
300        assertTrue("isRmat( { a, b, c, d, e } )", bb.isMinReductionMatrix(exgb));
301    }
302
303}