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