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