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