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