001/*
002 * $Id: RGroebnerBaseSeqTest.java 4071 2012-07-27 19:59:38Z 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;
080
081
082    PolynomialList<Product<BigRational>> F;
083
084
085    List<GenPolynomial<Product<BigRational>>> G;
086
087
088    GroebnerBase<Product<BigRational>> bb;
089
090
091    GenPolynomial<Product<BigRational>> a;
092
093
094    GenPolynomial<Product<BigRational>> b;
095
096
097    GenPolynomial<Product<BigRational>> c;
098
099
100    GenPolynomial<Product<BigRational>> d;
101
102
103    GenPolynomial<Product<BigRational>> e;
104
105
106    int rl = 3; //4; //3; 
107
108
109    int kl = 10;
110
111
112    int ll = 7;
113
114
115    int el = 3;
116
117
118    float q = 0.2f; //0.4f
119
120
121    @Override
122    protected void setUp() {
123        BigRational coeff = new BigRational(9);
124        pfac = new ProductRing<BigRational>(coeff, 4);
125        fac = new GenPolynomialRing<Product<BigRational>>(pfac, rl);
126        a = b = c = d = e = null;
127        bb = new RGroebnerBaseSeq<Product<BigRational>>();
128    }
129
130
131    @Override
132    protected void tearDown() {
133        a = b = c = d = e = null;
134        fac = null;
135        bb = null;
136    }
137
138
139    /**
140     * Test sequential GBase.
141     * 
142     */
143    public void testSequentialGBase() {
144
145        L = new ArrayList<GenPolynomial<Product<BigRational>>>();
146
147        a = fac.random(kl, ll, el, q);
148        b = fac.random(kl, ll, el, q);
149        c = fac.random(kl, ll, el, q);
150        d = fac.random(kl, ll, el, q);
151        e = d; //fac.random(kl, ll, el, q );
152
153        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
154            return;
155        }
156
157        assertTrue("not isZERO( a )", !a.isZERO());
158        L.add(a);
159
160        L = bb.GB(L);
161        assertTrue("isGB( { a } )", bb.isGB(L));
162        //System.out.println("L = " + L );
163
164        assertTrue("not isZERO( b )", !b.isZERO());
165        L.add(b);
166        //System.out.println("L = " + L.size() );
167
168        L = bb.GB(L);
169        assertTrue("isGB( { a, b } )", bb.isGB(L));
170        //System.out.println("L = " + L );
171
172        assertTrue("not isZERO( c )", !c.isZERO());
173        L.add(c);
174
175        L = bb.GB(L);
176        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
177        //System.out.println("L = " + L );
178
179        assertTrue("not isZERO( d )", !d.isZERO());
180        L.add(d);
181
182        L = bb.GB(L);
183        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
184        //System.out.println("L = " + L );
185
186        assertTrue("not isZERO( e )", !e.isZERO());
187        L.add(e);
188
189        L = bb.GB(L);
190        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
191        //System.out.println("L = " + L );
192    }
193
194
195    /**
196     * Test Trinks7 GBase.
197     * 
198     */
199    @SuppressWarnings("unchecked")
200    public void testTrinks7() {
201        String exam = "Z(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
202                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
203                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
204                        + "( 99 W - 11 B S + 3 B**2 ), " + "( 10000 B**2 + 6600 B + 2673 ) " + ") ";
205        Reader source = new StringReader(exam);
206        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
207
208        PolynomialList<BigInteger> F = null;
209
210        try {
211            F = (PolynomialList<BigInteger>) parser.nextPolynomialSet();
212        } catch (ClassCastException e) {
213            fail("" + e);
214        } catch (IOException e) {
215            fail("" + e);
216        }
217        //System.out.println("F = " + F);
218        int rl = F.ring.nvar;
219        TermOrder to = F.ring.tord;
220        String[] vars = F.ring.getVars();
221        PolynomialList<Product<ModInteger>> trinks;
222
223        List<RingFactory<ModInteger>> colist;
224        colist = new ArrayList<RingFactory<ModInteger>>();
225
226        //colist.add( new ModIntegerRing(2) );
227        //colist.add( new ModIntegerRing(3) );
228        //colist.add( new ModIntegerRing(5) );
229        //colist.add( new ModIntegerRing(30) ); // now ok, was not possible
230        colist.add(new ModIntegerRing(19));
231        colist.add(new ModIntegerRing(23));
232        //colist.add( new ModIntegerRing((2<<30)-19) );
233        //System.out.println("colist = " + colist);
234
235        ProductRing<ModInteger> pfac;
236        pfac = new ProductRing<ModInteger>(colist);
237        //System.out.println("pfac   = " + pfac);
238
239        GenPolynomialRing<Product<ModInteger>> fac;
240        fac = new GenPolynomialRing<Product<ModInteger>>(pfac, rl, to, vars);
241        //System.out.println("fac    = " + fac);
242
243        List<GenPolynomial<Product<ModInteger>>> Fp;
244        Fp = PolyUtil.toProduct(fac, F.list);
245
246        List<GenPolynomial<Product<ModInteger>>> Fpp;
247        Fpp = new ArrayList<GenPolynomial<Product<ModInteger>>>();
248
249        for (GenPolynomial<Product<ModInteger>> a : Fp) {
250            Fpp.add(a.multiply(pfac.getAtomic(0)));
251            Fpp.add(a.multiply(pfac.getAtomic(1)));
252            //Fpp.add( a );
253        }
254        Fp = Fpp;
255
256        //trinks = new PolynomialList<Product<ModInteger>>(fac, Fp);
257        //System.out.println("Fp     = " + trinks);
258
259        GroebnerBase<Product<ModInteger>> bbr = new RGroebnerBaseSeq<Product<ModInteger>>();
260
261        List<GenPolynomial<Product<ModInteger>>> G;
262        G = bbr.GB(Fp);
263        //System.out.println("gb = " + G );
264
265        //assertEquals("#GB(Trinks7) == 6", 6, G.size() );
266        //System.out.println("Fp = " + trinks);
267        trinks = new PolynomialList<Product<ModInteger>>(fac, G);
268        assertFalse("trings != null", trinks == null);
269        //System.out.println("G  = " + trinks);
270
271        assertTrue("isGB( GB(Trinks7) )", bbr.isGB(G));
272    }
273
274
275}