001/*
002 * $Id: GroebnerBaseSeqPairParTest.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, sequential pair list, tests with JUnit.
032 * @author Heinz Kredel.
033 */
034
035public class GroebnerBaseSeqPairParTest extends TestCase {
036
037    //private static final Logger logger = Logger.getLogger(GroebnerBaseSeqPairParTest.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>GroebnerBaseSeqPairParTest</CODE> object.
049     * @param name String.
050     */
051    public GroebnerBaseSeqPairParTest(String name) {
052        super(name);
053    }
054
055    /**
056     * suite.
057     */ 
058    public static Test suite() {
059        TestSuite suite= new TestSuite(GroebnerBaseSeqPairParTest.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 GroebnerBaseSeqPairParallel<BigRational>(threads);
094    }
095
096    protected void tearDown() {
097        a = b = c = d = e = null;
098        fac = null;
099        bbseq = null;
100        bbpar.terminate(); 
101        bbpar = null;
102        bbspar.terminate(); 
103        bbspar = null;
104    }
105
106
107    /**
108     * Test parallel GBase.
109     * 
110     */
111    public void testSeqPairParallelGBase() {
112
113        L = new ArrayList<GenPolynomial<BigRational>>();
114
115        a = fac.random(kl, ll, el, q );
116        b = fac.random(kl, ll, el, q );
117        c = fac.random(kl, ll, el, q );
118        d = fac.random(kl, ll, el, q );
119        e = d; //fac.random(kl, ll, el, q );
120
121        if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
122            return;
123        }
124
125        assertTrue("not isZERO( a )", !a.isZERO() );
126        L.add(a);
127
128        L = bbspar.GB( L );
129        assertTrue("isGB( { a } )", bbspar.isGB(L) );
130
131        assertTrue("not isZERO( b )", !b.isZERO() );
132        L.add(b);
133        //System.out.println("L = " + L.size() );
134
135        L = bbspar.GB( L );
136        assertTrue("isGB( { a, b } )", bbspar.isGB(L) );
137
138        assertTrue("not isZERO( c )", !c.isZERO() );
139        L.add(c);
140
141        L = bbspar.GB( L );
142        assertTrue("isGB( { a, b, c } )", bbspar.isGB(L) );
143
144        assertTrue("not isZERO( d )", !d.isZERO() );
145        L.add(d);
146
147        L = bbspar.GB( L );
148        assertTrue("isGB( { a, b, c, d } )", bbspar.isGB(L) );
149
150        assertTrue("not isZERO( e )", !e.isZERO() );
151        L.add(e);
152
153        L = bbspar.GB( L );
154        assertTrue("isGB( { a, b, c, d, e } )", bbspar.isGB(L) );
155    }
156
157
158    /**
159     * Test compare sequential with parallel GBase.
160     * 
161     */
162    public void testSequentialSeqPairParallelGBase() {
163
164        List<GenPolynomial<BigRational>> Gs, Gp;
165
166        L = new ArrayList<GenPolynomial<BigRational>>();
167
168        a = fac.random(kl, ll, el, q );
169        b = fac.random(kl, ll, el, q );
170        c = fac.random(kl, ll, el, q );
171        d = fac.random(kl, ll, el, q );
172        e = d; //fac.random(kl, ll, el, q );
173
174        if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
175            return;
176        }
177
178        L.add(a);
179        Gs = bbseq.GB( L );
180        Gp = bbspar.GB( L );
181
182        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
183        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
184
185        L = Gs;
186        L.add(b);
187        Gs = bbseq.GB( L );
188        Gp = bbspar.GB( L );
189
190        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
191        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
192
193        L = Gs;
194        L.add(c);
195        Gs = bbseq.GB( L );
196        Gp = bbspar.GB( L );
197
198        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
199        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
200
201        L = Gs;
202        L.add(d);
203        Gs = bbseq.GB( L );
204        Gp = bbspar.GB( L );
205
206        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
207        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
208
209        L = Gs;
210        L.add(e);
211        Gs = bbseq.GB( L );
212        Gp = bbspar.GB( L );
213
214        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
215        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
216    }
217
218
219    /**
220     * Test compare parallel with sequential pair parallel GBase.
221     * 
222     */
223    public void testParallelSeqPairParallelGBase() {
224
225        List<GenPolynomial<BigRational>> Gs, Gp;
226
227        L = new ArrayList<GenPolynomial<BigRational>>();
228
229        a = fac.random(kl, ll, el, q );
230        b = fac.random(kl, ll, el, q );
231        c = fac.random(kl, ll, el, q );
232        d = fac.random(kl, ll, el, q );
233        e = d; //fac.random(kl, ll, el, q );
234
235        if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
236            return;
237        }
238
239        L.add(a);
240        Gs = bbpar.GB( L );
241        Gp = bbspar.GB( L );
242
243        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
244        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
245
246        L = Gs;
247        L.add(b);
248        Gs = bbpar.GB( L );
249        Gp = bbspar.GB( L );
250
251        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
252        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
253
254        L = Gs;
255        L.add(c);
256        Gs = bbpar.GB( L );
257        Gp = bbspar.GB( L );
258
259        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
260        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
261
262        L = Gs;
263        L.add(d);
264        Gs = bbpar.GB( L );
265        Gp = bbspar.GB( L );
266
267        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
268        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
269
270        L = Gs;
271        L.add(e);
272        Gs = bbpar.GB( L );
273        Gp = bbspar.GB( L );
274
275        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp) );
276        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs) );
277    }
278
279
280    /**
281     * Test Trinks7 GBase.
282     * 
283     */
284    public void testTrinks7GBase() {
285        String exam = "(B,S,T,Z,P,W) L "
286            + "( "  
287            + "( 45 P + 35 S - 165 B - 36 ), " 
288            + "( 35 P + 40 Z + 25 T - 27 S ), "
289            + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
290            + "( - 9 W + 15 T P + 20 S Z ), "
291            + "( P W + 2 T Z - 11 B**3 ), "
292            + "( 99 W - 11 B S + 3 B**2 ) "
293            + ", ( B**2 + 33/50 B + 2673/10000 ) "
294            + ") ";
295        Reader source = new StringReader( exam );
296        GenPolynomialTokenizer parser
297            = new GenPolynomialTokenizer( source );
298        try {
299            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
300        } catch(ClassCastException e) {
301            fail(""+e);
302        } catch(IOException e) {
303            fail(""+e);
304        }
305        //System.out.println("F = " + F);
306
307        long t;
308        /*     
309               t = System.currentTimeMillis();
310               G = bbseq.GB( F.list );
311               t = System.currentTimeMillis() - t;
312               System.out.println("bbseq ms = " + t);     
313               t = System.currentTimeMillis();
314               G = bbpar.GB( F.list );
315               t = System.currentTimeMillis() - t;
316               System.out.println("bbpar ms = " + t);     
317        */
318        t = System.currentTimeMillis();
319        G = bbspar.GB( F.list );
320        t = System.currentTimeMillis() - t;
321        //System.out.println("bbspar ms = " + t);     
322        assertTrue("nonsense ", t >= 0L);
323
324        assertTrue("isGB( GB(Trinks7) )", bbspar.isGB(G) );
325        assertEquals("#GB(Trinks7) == 6", 6, G.size() );
326        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
327        //System.out.println("G = " + trinks);
328    }
329
330}