001/*
002 * $Id$
003 */
004
005package edu.jas.gb;
006
007
008// import edu.jas.poly.GroebnerBase;
009
010import java.io.IOException;
011import java.io.Reader;
012import java.io.StringReader;
013import java.util.ArrayList;
014import java.util.List;
015
016import junit.framework.Test;
017import junit.framework.TestCase;
018import junit.framework.TestSuite;
019
020
021import edu.jas.arith.BigRational;
022import edu.jas.kern.ComputerThreads;
023import edu.jas.poly.GenPolynomial;
024import edu.jas.poly.GenPolynomialRing;
025import edu.jas.poly.GenPolynomialTokenizer;
026import edu.jas.poly.PolynomialList;
027import edu.jas.util.ExecutableServer;
028
029
030/**
031 * Distributed GroebnerBase tests with JUnit.
032 * @author Heinz Kredel
033 */
034
035public class GroebnerBaseDistECTest extends TestCase {
036
037
038
039    /**
040     * main
041     */
042    public static void main(String[] args) {
043        junit.textui.TestRunner.run(suite());
044        //ComputerThreads.terminate();
045    }
046
047
048    /**
049     * Constructs a <CODE>GroebnerBaseDistECTest</CODE> object.
050     * @param name String.
051     */
052    public GroebnerBaseDistECTest(String name) {
053        super(name);
054    }
055
056
057    /**
058     * suite.
059     */
060    public static Test suite() {
061        TestSuite suite = new TestSuite(GroebnerBaseDistECTest.class);
062        return suite;
063    }
064
065
066    int port = 55711;
067
068
069    String host = "localhost";
070
071
072    String mfile = "examples/machines.localhost"; // contains localhost
073
074
075    GenPolynomialRing<BigRational> fac;
076
077
078    List<GenPolynomial<BigRational>> L;
079
080
081    PolynomialList<BigRational> F;
082
083
084    List<GenPolynomial<BigRational>> G;
085
086
087    GroebnerBase<BigRational> bbseq;
088
089
090    GroebnerBaseAbstract<BigRational> bbdist;
091
092
093    GroebnerBaseAbstract<BigRational> bbdists;
094
095
096    GenPolynomial<BigRational> a;
097
098
099    GenPolynomial<BigRational> b;
100
101
102    GenPolynomial<BigRational> c;
103
104
105    GenPolynomial<BigRational> d;
106
107
108    GenPolynomial<BigRational> e;
109
110
111    int rl = 3; //4; //3; 
112
113
114    int kl = 4;
115
116
117    int ll = 7;
118
119
120    int el = 3;
121
122
123    float q = 0.2f; //0.4f
124
125
126    int threads = 2;
127
128
129    ExecutableServer es1;
130
131
132    ExecutableServer es2;
133
134
135    @Override
136    protected void setUp() {
137        es1 = new ExecutableServer(4712); // == machines.localhost:4712
138        es1.init();
139        es2 = new ExecutableServer(4711); // == machines.localhost:4711
140        es2.init();
141        //System.out.println("es1 = " + es1);
142        //System.out.println("es2 = " + es2);
143        BigRational coeff = new BigRational(9);
144        fac = new GenPolynomialRing<BigRational>(coeff, rl);
145        a = b = c = d = e = null;
146        bbseq = new GroebnerBaseSeq<BigRational>();
147        bbdist = new GroebnerBaseDistributedEC<BigRational>(mfile, threads, port);
148        //bbdists = new GroebnerBaseDistributedEC<BigRational>(mfile, threads, new OrderedSyzPairlist<BigRational>(), port);
149    }
150
151
152    @Override
153    protected void tearDown() {
154        a = b = c = d = e = null;
155        fac = null;
156        bbseq = null;
157        bbdist.terminate();
158        bbdist = null;
159        //bbdists.terminate();
160        //bbdists = null;
161        es1.terminate();
162        es2.terminate();
163        es1 = null;
164        es2 = null;
165        ComputerThreads.terminate();
166    }
167
168
169    /**
170     * Test distributed GBase corner cases.
171     */
172    public void testDistributedGBaseCorner() {
173        L = new ArrayList<GenPolynomial<BigRational>>();
174
175        a = fac.getZERO();
176
177        L.add(a);
178        L = bbdist.GB(L);
179        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
180        assertTrue("L == {}: " + L, L.isEmpty());
181
182        b = fac.getONE();
183
184        L.add(b);
185        L = bbdist.GB(L);
186        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
187        assertTrue("L == {1}: " + L, L.size() == 1);
188    }
189
190
191    /**
192     * Test distributed GBase.
193     */
194    public void testDistributedGBase() {
195        L = new ArrayList<GenPolynomial<BigRational>>();
196
197        a = fac.random(kl, ll, el, q);
198        b = fac.random(kl, ll, el, q);
199        c = fac.random(kl, ll, el, q);
200        d = fac.random(kl, ll, el, q);
201        e = d; //fac.random(kl, ll, el, q );
202
203        L.add(a);
204        L = bbdist.GB(L);
205        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
206
207        L.add(b);
208        //System.out.println("L = " + L.size() );
209        L = bbdist.GB(L);
210        assertTrue("isGB( { a, b } ): " + L, bbseq.isGB(L));
211
212        L.add(c);
213        L = bbdist.GB(L);
214        assertTrue("isGB( { a, b, c } ): " + L, bbseq.isGB(L));
215
216        L.add(d);
217        L = bbdist.GB(L);
218        assertTrue("isGB( { a, b, c, d } ): " + L, bbseq.isGB(L));
219
220        L.add(e);
221        L = bbdist.GB(L);
222        assertTrue("isGB( { a, b, c, d, e } ): " + L, bbseq.isGB(L));
223    }
224
225
226    /**
227     * Test compare sequential with distributed GBase.
228     */
229    public void testSequentialDistributedGBase() {
230        List<GenPolynomial<BigRational>> Gs, Gp = null;
231        L = new ArrayList<GenPolynomial<BigRational>>();
232
233        a = fac.random(kl, ll, el, q);
234        b = fac.random(kl, ll, el, q);
235        c = fac.random(kl, ll, el, q);
236        d = fac.random(kl, ll, el, q);
237        e = d; //fac.random(kl, ll, el, q );
238
239        L.add(a);
240        Gs = bbseq.GB(L);
241        Gp = bbdist.GB(L);
242        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
243        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
244
245        L = Gs;
246        L.add(b);
247        Gs = bbseq.GB(L);
248        Gp = bbdist.GB(L);
249        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
250        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
251
252        L = Gs;
253        L.add(c);
254        Gs = bbseq.GB(L);
255        Gp = bbdist.GB(L);
256        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
257        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
258
259        L = Gs;
260        L.add(d);
261        Gs = bbseq.GB(L);
262        Gp = bbdist.GB(L);
263        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
264        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
265
266        L = Gs;
267        L.add(e);
268        Gs = bbseq.GB(L);
269        Gp = bbdist.GB(L);
270        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
271        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
272    }
273
274
275    /**
276     * Test Trinks7 GBase.
277     */
278    @SuppressWarnings("unchecked")
279    public void testTrinks7GBase() {
280        List<GenPolynomial<BigRational>> Gs, Gp = null;
281        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
282                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
283                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
284                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
285        //exam = "(x3,x4,x5) L " + 
286        //       "( (x3^2 - 13974703710478159/3775194259200) , (x4 - 34297/840), (x5^2 - 6389/480), (-4/3 x5^2 + x3^2 + x3 - 833/180) ) ";
287        //exam = "(x3,x4,x5) G " +
288        //       "( x4^2 + 1809/30976 x3^2 + 190760/17787 x4 + 1755/10648 x3 + 296895202578451/10529038840320 , x3 * x4 - 15/64 x3^2 + 1223/294 x3 - 68247/58240 , x5 - 11/6 x4 - 3/16 x3 - 44162/4851 , x3^3 + 441280/68651 x3^2 + 29361376/2839655 x4 + 7055752657/687196510 x3 + 28334577136/417429285 , -8/9 x3^2 - 2 x5 + x3 - 159/44 )";
289
290        Reader source = new StringReader(exam);
291        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
292        try {
293            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
294        } catch (IOException e) {
295            fail("" + e);
296        }
297        //System.out.println("F = " + F);
298
299        Gs = bbseq.GB(F.list);
300        //System.out.println("Gs = " + Gs);
301        Gp = bbdist.GB(F.list);
302        //System.out.println("Gp = " + Gp);
303
304        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(Gp));
305        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(Gs));
306        //assertEquals("#GB(Trinks7) == 6", 6, G.size());
307        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + F, Gs.containsAll(Gp));
308        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + F, Gp.containsAll(Gs));
309        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, Gp);
310        //System.out.println("G = " + trinks);
311    }
312
313}