001/*
002 * $Id: GroebnerBaseParTest.java 4010 2012-07-21 20:39:56Z kredel $
003 */
004
005package edu.jas.gb;
006
007//import edu.jas.poly.GroebnerBase;
008
009import java.io.IOException;
010import java.io.Reader;
011import java.io.StringReader;
012import java.util.ArrayList;
013import java.util.List;
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;
023
024import edu.jas.poly.GenPolynomial;
025import edu.jas.poly.GenPolynomialRing;
026import edu.jas.poly.GenPolynomialTokenizer;
027import edu.jas.poly.PolynomialList;
028
029/**
030 * GroebnerBase parallel tests with JUnit.
031 * @author Heinz Kredel.
032 */
033
034public class GroebnerBaseParTest extends TestCase {
035
036    //private static final Logger logger = Logger.getLogger(GroebnerBaseParTest.class);
037
038/**
039 * main
040 */
041   public static void main (String[] args) {
042          BasicConfigurator.configure();
043          junit.textui.TestRunner.run( suite() );
044   }
045
046/**
047 * Constructs a <CODE>GroebnerBaseParTest</CODE> object.
048 * @param name String.
049 */
050   public GroebnerBaseParTest(String name) {
051          super(name);
052   }
053
054/**
055 * suite.
056 */ 
057 public static Test suite() {
058     TestSuite suite= new TestSuite(GroebnerBaseParTest.class);
059     return suite;
060   }
061
062   GenPolynomialRing<BigRational> fac;
063
064   List<GenPolynomial<BigRational>> L;
065   PolynomialList<BigRational> F;
066   List<GenPolynomial<BigRational>> G;
067
068   GroebnerBase<BigRational> bbseq;
069   GroebnerBase<BigRational> bbpar;
070
071   GenPolynomial<BigRational> a;
072   GenPolynomial<BigRational> b;
073   GenPolynomial<BigRational> c;
074   GenPolynomial<BigRational> d;
075   GenPolynomial<BigRational> e;
076
077   int rl = 3; //4; //3; 
078   int kl = 10;
079   int ll = 7;
080   int el = 3;
081   float q = 0.2f; //0.4f
082
083   int threads = 2;
084
085   protected void setUp() {
086       BigRational coeff = new BigRational(9);
087       fac = new GenPolynomialRing<BigRational>(coeff,rl);
088       a = b = c = d = e = null;
089       bbseq = new GroebnerBaseSeq<BigRational>();
090       bbpar = new GroebnerBaseParallel<BigRational>(threads);
091   }
092
093   protected void tearDown() {
094       a = b = c = d = e = null;
095       fac = null;
096       bbseq = null;
097       ((GroebnerBaseParallel<BigRational>)bbpar).terminate(); 
098       bbpar = null;
099   }
100
101
102/**
103 * Test parallel GBase.
104 * 
105 */
106 public void testParallelGBase() {
107
108     L = new ArrayList<GenPolynomial<BigRational>>();
109
110     a = fac.random(kl, ll, el, q );
111     b = fac.random(kl, ll, el, q );
112     c = fac.random(kl, ll, el, q );
113     d = fac.random(kl, ll, el, q );
114     e = d; //fac.random(kl, ll, el, q );
115
116     if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
117        return;
118     }
119
120     assertTrue("not isZERO( a )", !a.isZERO() );
121     L.add(a);
122
123     L = bbpar.GB( L );
124     assertTrue("isGB( { a } )", bbpar.isGB(L) );
125
126     assertTrue("not isZERO( b )", !b.isZERO() );
127     L.add(b);
128     //System.out.println("L = " + L.size() );
129
130     L = bbpar.GB( L );
131     assertTrue("isGB( { a, b } )", bbpar.isGB(L) );
132
133     assertTrue("not isZERO( c )", !c.isZERO() );
134     L.add(c);
135
136     L = bbpar.GB( L );
137     assertTrue("isGB( { a, b, c } )", bbpar.isGB(L) );
138
139     assertTrue("not isZERO( d )", !d.isZERO() );
140     L.add(d);
141
142     L = bbpar.GB( L );
143     assertTrue("isGB( { a, b, c, d } )", bbpar.isGB(L) );
144
145     assertTrue("not isZERO( e )", !e.isZERO() );
146     L.add(e);
147
148     L = bbpar.GB( L );
149     assertTrue("isGB( { a, b, c, d, e } )", bbpar.isGB(L) );
150 }
151
152
153/**
154 * Test compare sequential with parallel GBase.
155 * 
156 */
157 public void testSequentialParallelGBase() {
158
159     List<GenPolynomial<BigRational>> Gs, Gp;
160
161     L = new ArrayList<GenPolynomial<BigRational>>();
162
163     a = fac.random(kl, ll, el, q );
164     b = fac.random(kl, ll, el, q );
165     c = fac.random(kl, ll, el, q );
166     d = fac.random(kl, ll, el, q );
167     e = d; //fac.random(kl, ll, el, q );
168
169     if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
170        return;
171     }
172
173     L.add(a);
174     Gs = bbseq.GB( L );
175     Gp = bbpar.GB( L );
176
177     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
178     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
179
180     L = Gs;
181     L.add(b);
182     Gs = bbseq.GB( L );
183     Gp = bbpar.GB( L );
184
185     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
186     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
187
188     L = Gs;
189     L.add(c);
190     Gs = bbseq.GB( L );
191     Gp = bbpar.GB( L );
192
193     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
194     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
195
196     L = Gs;
197     L.add(d);
198     Gs = bbseq.GB( L );
199     Gp = bbpar.GB( L );
200
201     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
202     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
203
204     L = Gs;
205     L.add(e);
206     Gs = bbseq.GB( L );
207     Gp = bbpar.GB( L );
208
209     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
210     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
211 }
212
213
214/**
215 * Test Trinks7 GBase.
216 * 
217 */
218 public void testTrinks7GBase() {
219     String exam = "(B,S,T,Z,P,W) L "
220                 + "( "  
221                 + "( 45 P + 35 S - 165 B - 36 ), " 
222                 + "( 35 P + 40 Z + 25 T - 27 S ), "
223                 + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
224                 + "( - 9 W + 15 T P + 20 S Z ), "
225                 + "( P W + 2 T Z - 11 B**3 ), "
226                 + "( 99 W - 11 B S + 3 B**2 ), "
227                 + "( B**2 + 33/50 B + 2673/10000 ) "
228                 + ") ";
229     Reader source = new StringReader( exam );
230     GenPolynomialTokenizer parser
231                  = new GenPolynomialTokenizer( source );
232     try {
233         F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
234     } catch(ClassCastException e) {
235         fail(""+e);
236     } catch(IOException e) {
237         fail(""+e);
238     }
239     //System.out.println("F = " + F);
240
241     G = bbpar.GB( F.list );
242     assertTrue("isGB( GB(Trinks7) )", bbpar.isGB(G) );
243     assertEquals("#GB(Trinks7) == 6", 6, G.size() );
244     //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
245     //System.out.println("G = " + trinks);
246
247 }
248}