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