001/*
002 * $Id: GroebnerBaseParSyzPairTest.java 4015 2012-07-22 12:05:38Z kredel $
003 */
004
005package edu.jas.gb;
006
007//import edu.jas.poly.GroebnerBase;
008
009import java.util.List;
010import java.util.ArrayList;
011import java.io.IOException;
012import java.io.Reader;
013import java.io.StringReader;
014
015import junit.framework.Test;
016import junit.framework.TestCase;
017import junit.framework.TestSuite;
018
019import org.apache.log4j.BasicConfigurator;
020//import org.apache.log4j.Logger;
021
022import edu.jas.arith.BigRational;
023import edu.jas.gb.GroebnerBase;
024import edu.jas.poly.GenPolynomial;
025import edu.jas.poly.GenPolynomialRing;
026import edu.jas.poly.GenPolynomialTokenizer;
027import edu.jas.poly.PolynomialList;
028
029
030/**
031 * Groebner base parallel, syzygy pair list, tests with JUnit.
032 * @author Heinz Kredel.
033 */
034
035public class GroebnerBaseParSyzPairTest extends TestCase {
036
037    //private static final Logger logger = Logger.getLogger(GroebnerBaseParSyzPairTest.class);
038
039    /**
040     * main
041     */
042    public static void main (String[] args) {
043        BasicConfigurator.configure();
044        junit.textui.TestRunner.run( suite() );
045    }
046
047    /**
048     * Constructs a <CODE>GroebnerBaseParSyzPairTest</CODE> object.
049     * @param name String.
050     */
051    public GroebnerBaseParSyzPairTest(String name) {
052        super(name);
053    }
054
055    /**
056     * suite.
057     */ 
058    public static Test suite() {
059        TestSuite suite= new TestSuite(GroebnerBaseParSyzPairTest.class);
060        return suite;
061    }
062
063    GenPolynomialRing<BigRational> fac;
064
065    List<GenPolynomial<BigRational>> L;
066    PolynomialList<BigRational> F;
067    List<GenPolynomial<BigRational>> G;
068
069    GroebnerBaseAbstract<BigRational> bbseq;
070    GroebnerBaseAbstract<BigRational> bbpar;
071    GroebnerBaseAbstract<BigRational> bbspar;
072
073    GenPolynomial<BigRational> a;
074    GenPolynomial<BigRational> b;
075    GenPolynomial<BigRational> c;
076    GenPolynomial<BigRational> d;
077    GenPolynomial<BigRational> e;
078
079    int rl = 3; //4; //3; 
080    int kl = 10;
081    int ll = 7;
082    int el = 3;
083    float q = 0.2f; //0.4f
084
085    int threads = 2;
086
087    protected void setUp() {
088        BigRational coeff = new BigRational(9);
089        fac = new GenPolynomialRing<BigRational>(coeff,rl);
090        a = b = c = d = e = null;
091        bbseq = new GroebnerBaseSeq<BigRational>();
092        bbpar = new GroebnerBaseParallel<BigRational>(threads);
093        bbspar = new GroebnerBaseParallel<BigRational>(threads, new ReductionPar<BigRational>(),
094                                                       new OrderedSyzPairlist<BigRational>());
095    }
096
097    protected void tearDown() {
098        a = b = c = d = e = null;
099        fac = null;
100        bbseq = null;
101        bbpar.terminate(); 
102        bbpar = null;
103        bbspar.terminate(); 
104        bbspar = null;
105    }
106
107
108    /**
109     * Test syzygy pair parallel GBase.
110     * 
111     */
112    public void testSyzPairParallelGBase() {
113
114        L = new ArrayList<GenPolynomial<BigRational>>();
115
116        a = fac.random(kl, ll, el, q );
117        b = fac.random(kl, ll, el, q );
118        c = fac.random(kl, ll, el, q );
119        d = fac.random(kl, ll, el, q );
120        e = d; //fac.random(kl, ll, el, q );
121
122        if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
123            return;
124        }
125
126        assertTrue("not isZERO( a )", !a.isZERO() );
127        L.add(a);
128
129        L = bbspar.GB( L );
130        assertTrue("isGB( { a } )", bbspar.isGB(L) );
131
132        assertTrue("not isZERO( b )", !b.isZERO() );
133        L.add(b);
134        //System.out.println("L = " + L.size() );
135
136        L = bbspar.GB( L );
137        assertTrue("isGB( { a, b } )", bbspar.isGB(L) );
138
139        assertTrue("not isZERO( c )", !c.isZERO() );
140        L.add(c);
141
142        L = bbspar.GB( L );
143        assertTrue("isGB( { a, b, c } )", bbspar.isGB(L) );
144
145        assertTrue("not isZERO( d )", !d.isZERO() );
146        L.add(d);
147
148        L = bbspar.GB( L );
149        assertTrue("isGB( { a, b, c, d } )", bbspar.isGB(L) );
150
151        assertTrue("not isZERO( e )", !e.isZERO() );
152        L.add(e);
153
154        L = bbspar.GB( L );
155        assertTrue("isGB( { a, b, c, d, e } )", bbspar.isGB(L) );
156    }
157
158
159    /**
160     * Test compare sequential with syzygy pair parallel GBase.
161     * 
162     */
163    public void testSequentialSyzPairParallelGBase() {
164
165        List<GenPolynomial<BigRational>> Gs, Gp;
166
167        L = new ArrayList<GenPolynomial<BigRational>>();
168
169        a = fac.random(kl, ll, el, q );
170        b = fac.random(kl, ll, el, q );
171        c = fac.random(kl, ll, el, q );
172        d = fac.random(kl, ll, el, q );
173        e = d; //fac.random(kl, ll, el, q );
174
175        if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
176            return;
177        }
178
179        L.add(a);
180        Gs = bbseq.GB( L );
181        Gp = bbspar.GB( L );
182
183        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
184        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
185
186        L = Gs;
187        L.add(b);
188        Gs = bbseq.GB( L );
189        Gp = bbspar.GB( L );
190
191        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
192        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
193
194        L = Gs;
195        L.add(c);
196        Gs = bbseq.GB( L );
197        Gp = bbspar.GB( L );
198
199        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
200        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
201
202        L = Gs;
203        L.add(d);
204        Gs = bbseq.GB( L );
205        Gp = bbspar.GB( L );
206
207        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
208        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
209
210        L = Gs;
211        L.add(e);
212        Gs = bbseq.GB( L );
213        Gp = bbspar.GB( L );
214
215        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
216        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
217    }
218
219
220    /**
221     * Test compare parallel with syzygy pair parallel GBase.
222     * 
223     */
224    public void testParallelSyzPairParallelGBase() {
225
226        List<GenPolynomial<BigRational>> Gs, Gp;
227
228        L = new ArrayList<GenPolynomial<BigRational>>();
229
230        a = fac.random(kl, ll, el, q );
231        b = fac.random(kl, ll, el, q );
232        c = fac.random(kl, ll, el, q );
233        d = fac.random(kl, ll, el, q );
234        e = d; //fac.random(kl, ll, el, q );
235
236        if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
237            return;
238        }
239
240        L.add(a);
241        Gs = bbpar.GB( L );
242        Gp = bbspar.GB( L );
243
244        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
245        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
246
247        L = Gs;
248        L.add(b);
249        Gs = bbpar.GB( L );
250        Gp = bbspar.GB( L );
251
252        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
253        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
254
255        L = Gs;
256        L.add(c);
257        Gs = bbpar.GB( L );
258        Gp = bbspar.GB( L );
259
260        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
261        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
262
263        L = Gs;
264        L.add(d);
265        Gs = bbpar.GB( L );
266        Gp = bbspar.GB( L );
267
268        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
269        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
270
271        L = Gs;
272        L.add(e);
273        Gs = bbpar.GB( L );
274        Gp = bbspar.GB( L );
275
276        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
277        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
278    }
279
280
281    /**
282     * Test Trinks7 GBase.
283     * 
284     */
285    public void testTrinks7GBase() {
286        String exam = "(B,S,T,Z,P,W) L "
287            + "( "  
288            + "( 45 P + 35 S - 165 B - 36 ), " 
289            + "( 35 P + 40 Z + 25 T - 27 S ), "
290            + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
291            + "( - 9 W + 15 T P + 20 S Z ), "
292            + "( P W + 2 T Z - 11 B**3 ), "
293            + "( 99 W - 11 B S + 3 B**2 ) "
294            + ", ( B**2 + 33/50 B + 2673/10000 ) "
295            + ") ";
296        Reader source = new StringReader( exam );
297        GenPolynomialTokenizer parser
298            = new GenPolynomialTokenizer( source );
299        try {
300            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
301        } catch(ClassCastException e) {
302            fail(""+e);
303        } catch(IOException e) {
304            fail(""+e);
305        }
306        //System.out.println("F = " + F);
307
308        long t;
309        /*     
310               t = System.currentTimeMillis();
311               G = bbseq.GB( F.list );
312               t = System.currentTimeMillis() - t;
313               System.out.println("bbseq ms = " + t);     
314               t = System.currentTimeMillis();
315               G = bbpar.GB( F.list );
316               t = System.currentTimeMillis() - t;
317               System.out.println("bbpar ms = " + t);     
318        */
319        t = System.currentTimeMillis();
320        G = bbspar.GB( F.list );
321        t = System.currentTimeMillis() - t;
322        //System.out.println("bbspar ms = " + t);     
323        assertTrue("nonsense ", t >= 0L);
324
325        assertTrue("isGB( GB(Trinks7) )", bbspar.isGB(G) );
326        assertEquals("#GB(Trinks7) == 6", 6, G.size() );
327        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
328        //System.out.println("G = " + trinks);
329    }
330
331}