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