001/*
002 * $Id: DGroebnerBaseSeqTest.java 5863 2018-07-20 11:13:34Z kredel $
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 junit.framework.Test;
015import junit.framework.TestCase;
016import junit.framework.TestSuite;
017
018
019import edu.jas.arith.BigInteger;
020import edu.jas.arith.BigRational;
021import edu.jas.kern.ComputerThreads;
022import edu.jas.poly.GenPolynomial;
023import edu.jas.poly.GenPolynomialRing;
024import edu.jas.poly.GenPolynomialTokenizer;
025import edu.jas.poly.PolyUtil;
026import edu.jas.poly.PolynomialList;
027
028
029/**
030 * DGroebner base sequential tests with JUnit.
031 * @author Heinz Kredel
032 */
033
034public class DGroebnerBaseSeqTest extends TestCase {
035
036
037    /**
038     * main
039     */
040    public static void main(String[] args) {
041        junit.textui.TestRunner.run(suite());
042    }
043
044
045    /**
046     * Constructs a <CODE>DGroebnerBaseSeqTest</CODE> object.
047     * @param name String.
048     */
049    public DGroebnerBaseSeqTest(String name) {
050        super(name);
051    }
052
053
054    /**
055     * suite.
056     */
057    public static Test suite() {
058        TestSuite suite = new TestSuite(DGroebnerBaseSeqTest.class);
059        return suite;
060    }
061
062
063    GenPolynomialRing<BigInteger> fac;
064
065
066    List<GenPolynomial<BigInteger>> L;
067
068
069    PolynomialList<BigInteger> F;
070
071
072    List<GenPolynomial<BigInteger>> G;
073
074
075    GroebnerBase<BigInteger> bb;
076
077
078    GenPolynomial<BigInteger> a;
079
080
081    GenPolynomial<BigInteger> b;
082
083
084    GenPolynomial<BigInteger> c;
085
086
087    GenPolynomial<BigInteger> d;
088
089
090    GenPolynomial<BigInteger> e;
091
092
093    int rl = 3; //4; //3; 
094
095
096    int kl = 4; //4; 10
097
098
099    int ll = 4;
100
101
102    int el = 3;
103
104
105    float q = 0.2f; //0.4f
106
107
108    @Override
109    protected void setUp() {
110        BigInteger coeff = new BigInteger(9);
111        fac = new GenPolynomialRing<BigInteger>(coeff, rl);
112        a = b = c = d = e = null;
113        bb = new DGroebnerBaseSeq<BigInteger>();
114    }
115
116
117    @Override
118    protected void tearDown() {
119        a = b = c = d = e = null;
120        fac = null;
121        bb = null;
122    }
123
124
125    /**
126     * Test sequential GBase.
127     * 
128     */
129    public void testSequentialGBase() {
130
131        L = new ArrayList<GenPolynomial<BigInteger>>();
132
133        a = fac.random(kl, ll, el, q); //.abs();
134        b = fac.random(kl, ll, el, q); //.abs();
135        c = fac.random(kl, ll / 2, el, q); //.abs();
136        d = fac.random(kl, ll / 2, el, q); //.abs();
137        e = d; //fac.random(kl, ll, el, q );
138
139        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
140            return;
141        }
142
143        L.add(a);
144        //System.out.println("    L  = " + L );
145        L = bb.GB(L);
146        //System.out.println("dGB(L) = " + L );
147        assertTrue("isGB( { a } )", bb.isGB(L));
148
149        L.add(b);
150        //System.out.println("    L  = " + L );
151        L = bb.GB(L);
152        //System.out.println("dGB(L) = " + L );
153        assertTrue("isGB( { a, b } )", bb.isGB(L));
154
155        L.add(c);
156        //System.out.println("    L  = " + L );
157        L = bb.GB(L);
158        //System.out.println("dGB(L) = " + L );
159        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
160
161        L.add(d);
162        //System.out.println("    L  = " + L );
163        L = bb.GB(L);
164        //System.out.println("dGB(L) = " + L );
165        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
166
167        L.add(e);
168        //System.out.println("    L  = " + L );
169        L = bb.GB(L);
170        //System.out.println("dGB(L) = " + L );
171        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
172    }
173
174
175    /**
176     * Test Trinks7 GBase over Z.
177     * 
178     */
179    @SuppressWarnings("unchecked")
180    public void xtestTrinks7GBaseZ() {
181        String exam = "Z(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
182                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
183                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
184                        + "( 99 W - 11 B S + 3 B**2 ), " + "( 10000 B**2 + 6600 B + 2673 ) " + ") ";
185        Reader source = new StringReader(exam);
186        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
187        try {
188            F = (PolynomialList<BigInteger>) parser.nextPolynomialSet();
189        } catch (ClassCastException e) {
190            fail("" + e);
191        } catch (IOException e) {
192            fail("" + e);
193        }
194        System.out.println("F = " + F);
195
196        G = bb.GB(F.list);
197        PolynomialList<BigInteger> trinks = new PolynomialList<BigInteger>(F.ring, G);
198        System.out.println("G = " + trinks);
199        System.out.println("G.size() = " + G.size());
200        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
201        //assertEquals("#GB(Trinks7) == 6", 6, G.size() );
202    }
203
204
205    /**
206     * Test Trinks7 GBase over Z(B).
207     * 
208     */
209    @SuppressWarnings("unchecked")
210    public void xtestTrinks7GBaseZ_B() {
211        String exam = "IntFunc{ B } (S,T,Z,P,W) G " + "( " + "( { 45 } P + { 35 } S - { 165 B } - { 36 } ), "
212                        + "( { 35 } P + { 40 } Z + { 25 } T - { 27 } S ), "
213                        + "( { 15 } W + { 25 } S P + { 30 } Z - { 18 } T - { 165 B**2 } ), "
214                        + "( { - 9 } W + { 15 } T P + { 20 } S Z ), " + "( P W + { 2 } T Z - { 11 B**3 } ), "
215                        + "( { 99 } W - { 11 B } S + { 3 B**2 } ), " + "( { 10000 B**2 + 6600 B + 2673 } ) "
216                        + ") ";
217        Reader source = new StringReader(exam);
218        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
219        DGroebnerBaseSeq<GenPolynomial<BigRational>> bb = new DGroebnerBaseSeq<GenPolynomial<BigRational>>();
220        PolynomialList<GenPolynomial<BigRational>> F = null;
221        List<GenPolynomial<GenPolynomial<BigRational>>> G = null;
222        try {
223            F = (PolynomialList<GenPolynomial<BigRational>>) parser.nextPolynomialSet();
224        } catch (ClassCastException e) {
225            fail("" + e);
226        } catch (IOException e) {
227            fail("" + e);
228        }
229        System.out.println("F = " + F);
230
231        List<GenPolynomial<GenPolynomial<BigRational>>> Fp = new ArrayList<GenPolynomial<GenPolynomial<BigRational>>>(
232                        F.list.size());
233        for (GenPolynomial<GenPolynomial<BigRational>> p : F.list) {
234            p = PolyUtil.<BigRational> monic(p);
235            Fp.add(p);
236        }
237        //System.out.println("Fp = " + Fp);
238        G = bb.GB(Fp);
239        //System.out.println("G = " + G);
240
241        List<GenPolynomial<GenPolynomial<BigRational>>> Gp = new ArrayList<GenPolynomial<GenPolynomial<BigRational>>>(
242                        F.list.size());
243        for (GenPolynomial<GenPolynomial<BigRational>> p : G) {
244            p = PolyUtil.<BigRational> monic(p);
245            Gp.add(p);
246        }
247        PolynomialList<GenPolynomial<BigRational>> trinks = new PolynomialList<GenPolynomial<BigRational>>(
248                        F.ring, Gp);
249        System.out.println("G = " + trinks);
250        System.out.println("G.size() = " + Gp.size());
251        ComputerThreads.terminate();
252        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
253        //assertEquals("#GB(Trinks7) == 1", 1, G.size() );
254    }
255
256}