001/*
002 * $Id$
003 */
004
005package edu.jas.gbufd;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.ArrayList;
012import java.util.Arrays;
013import java.util.List;
014
015import junit.framework.Test;
016import junit.framework.TestCase;
017import junit.framework.TestSuite;
018
019
020import edu.jas.arith.BigRational;
021import edu.jas.gb.GroebnerBase;
022import edu.jas.gb.GroebnerBaseSeq;
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.poly.TermOrderOptimization;
029import edu.jas.util.KsubSet;
030
031
032/**
033 * Partial Groebner base sequential tests with JUnit.
034 * @author Heinz Kredel
035 */
036
037public class GroebnerBasePartTest extends TestCase {
038
039
040
041    /**
042     * main
043     */
044    public static void main(String[] args) {
045        junit.textui.TestRunner.run(suite());
046    }
047
048
049    /**
050     * Constructs a <CODE>GroebnerBasePartTest</CODE> object.
051     * @param name String.
052     */
053    public GroebnerBasePartTest(String name) {
054        super(name);
055    }
056
057
058    /**
059     * suite.
060     */
061    public static Test suite() {
062        TestSuite suite = new TestSuite(GroebnerBasePartTest.class);
063        return suite;
064    }
065
066
067    GenPolynomialRing<BigRational> fac;
068
069
070    List<GenPolynomial<BigRational>> L;
071
072
073    PolynomialList<BigRational> F;
074
075
076    List<GenPolynomial<BigRational>> G;
077
078
079    GroebnerBase<BigRational> bb;
080
081
082    GroebnerBasePartial<BigRational> bbp;
083
084
085    GenPolynomial<BigRational> a;
086
087
088    GenPolynomial<BigRational> b;
089
090
091    GenPolynomial<BigRational> c;
092
093
094    GenPolynomial<BigRational> d;
095
096
097    GenPolynomial<BigRational> e;
098
099
100    int rl = 3; //4; //3; 
101
102
103    int kl = 10;
104
105
106    int ll = 7;
107
108
109    int el = 3;
110
111
112    float q = 0.2f; //0.4f
113
114
115    @Override
116    protected void setUp() {
117        BigRational coeff = new BigRational(9);
118        fac = new GenPolynomialRing<BigRational>(coeff, rl);
119        a = b = c = d = e = null;
120        bb = new GroebnerBaseSeq<BigRational>();
121        bbp = new GroebnerBasePartial<BigRational>();
122    }
123
124
125    @Override
126    protected void tearDown() {
127        a = b = c = d = e = null;
128        fac = null;
129        bb = null;
130        ComputerThreads.terminate();
131    }
132
133
134    /**
135     * Test partial recursive Trinks7 GBase.
136     * 
137     */
138    @SuppressWarnings("unchecked")
139    public void testTrinks7GBasePartRec() {
140        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
141                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
142                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
143                        + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
144
145        Reader source = new StringReader(exam);
146        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
147        try {
148            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
149        } catch (ClassCastException e) {
150            fail("" + e);
151        } catch (IOException e) {
152            fail("" + e);
153        }
154        //System.out.println("F = " + F);
155
156        //PolynomialList<BigRational> Fo = TermOrderOptimization.optimizeTermOrder(F);
157        //System.out.println("\nFo = " + Fo);
158
159        PolynomialList<GenPolynomial<BigRational>> rtrinks = bbp.partialGBrec(F.list, new String[] { "P",
160                "Z", "T", "W" });
161        assertTrue("isGB( GB(Trinks7) )", bbp.isGBrec(rtrinks.list));
162        //System.out.println("\nTrinksR = " + rtrinks);
163
164        // not meaning-full
165        PolynomialList<BigRational> trinks = bbp.partialGB(F.list, new String[] { "P", "Z", "T", "W" });
166        //System.out.println("\ntrinks = " + trinks);
167        assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list));
168    }
169
170
171    /**
172     * Test partial Trinks7 GBase.
173     * 
174     */
175    @SuppressWarnings("unchecked")
176    public void testTrinks7GBasePart() {
177        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
178                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
179                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
180                        + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
181
182        Reader source = new StringReader(exam);
183        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
184        try {
185            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
186        } catch (ClassCastException e) {
187            fail("" + e);
188        } catch (IOException e) {
189            fail("" + e);
190        }
191        //System.out.println("F = " + F);
192
193        //PolynomialList<BigRational> Fo = TermOrderOptimization.optimizeTermOrder(F);
194        //System.out.println("\nFo = " + Fo);
195
196        PolynomialList<BigRational> trinks = bbp.partialGB(F.list, new String[] { "B", "S", "P", "Z", "T",
197                "W" });
198        //PolynomialList<BigRational> trinks = bbp.partialGB(F.list, new String[] { "T", "Z", "P", "W", "B", "S" });
199        assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list));
200        //System.out.println("\nG = " + trinks);
201
202        try {
203            @SuppressWarnings("unused")
204            PolynomialList<GenPolynomial<BigRational>> tr = bbp.partialGBrec(F.list, new String[] { "B", "S",
205                    "P", "Z", "T", "W" });
206            fail("must throw exception");
207        } catch (IllegalArgumentException e) {
208            //pass
209        }
210    }
211
212
213    /**
214     * Test partial permutation.
215     * 
216     */
217    public void testPartialPermutation() {
218        String[] vars = new String[] { "B", "S", "T", "Z", "P", "W" };
219        String[] pvars = new String[] { "P", "Z", "T", "W" };
220        String[] rvars = new String[] { "S", "B" };
221        List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, pvars);
222        //System.out.println("perm1 = " + perm1);
223
224        List<Integer> perm2 = GroebnerBasePartial.partialPermutation(vars, pvars, null);
225        //System.out.println("perm2 = " + perm2);
226
227        assertEquals("perm1 == perm2 ", perm1, perm2);
228
229        List<Integer> perm3 = GroebnerBasePartial.partialPermutation(vars, pvars, rvars);
230        //System.out.println("perm3 = " + perm3);
231        assertFalse("perm1 != perm3 ", perm1.equals(perm3));
232    }
233
234
235    /**
236     * Test elimination partial permutation.
237     * 
238     */
239    public void xtestElimPartialPermutation() {
240        String[] vars = new String[] { "B", "S", "T", "Z", "P", "W" };
241        String[] evars = new String[] { "P", "Z" };
242        String[] pvars = new String[] { "T", "W" };
243        String[] rvars = new String[] { "B", "S" };
244        List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, rvars);
245        System.out.println("perm1 = " + perm1);
246
247        List<Integer> perm2 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, null);
248        System.out.println("perm2 = " + perm2);
249
250        assertEquals("perm1 == perm2 ", perm1, perm2);
251
252        rvars = new String[] { "S", "B" };
253
254        List<Integer> perm3 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, rvars);
255        System.out.println("perm3 = " + perm3);
256        assertFalse("perm1 != perm3 ", perm1.equals(perm3));
257    }
258
259
260    /**
261     * Test elim partial Trinks7 GBase.
262     * 
263     */
264    @SuppressWarnings("unchecked")
265    public void testTrinks7GBaseElimPart() {
266        String exam = "(B,S,T,Z,P,W) G " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
267                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
268                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
269                        + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
270
271        Reader source = new StringReader(exam);
272        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
273        try {
274            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
275        } catch (ClassCastException e) {
276            fail("" + e);
277        } catch (IOException e) {
278            fail("" + e);
279        }
280        //System.out.println("F = " + F);
281
282        String[] evars = new String[] { "P", "Z" };
283        String[] pvars = new String[] { "B", "S", "T", "W" };
284        //System.out.println("evars = " + Arrays.toString(evars));
285        //System.out.println("pvars = " + Arrays.toString(pvars));
286
287        PolynomialList<BigRational> trinks = bbp.elimPartialGB(F.list, evars, pvars);
288        assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list));
289        //System.out.println("\nG = " + trinks);
290    }
291
292
293    /**
294     * Test partial GBase.
295     * 
296     */
297    @SuppressWarnings("unchecked")
298    public void testGBasePart() {
299        String exam = "(a,b,c,d,e,f) G " + "( " + "( a ), " + "( b^2 ), " + "( c^3 ), " + "( d^4 ), "
300                        + "( e^5 ), " + "( f^6 ) " + ") ";
301
302        Reader source = new StringReader(exam);
303        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
304        try {
305            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
306        } catch (ClassCastException e) {
307            fail("" + e);
308        } catch (IOException e) {
309            fail("" + e);
310        }
311        //System.out.println("F = " + F);
312
313        // String[] evars = new String[] { "c", "d", "e", "f", "a", "b"};
314        String[] evars = new String[] { "a", "b" };
315        //System.out.println("evars = " + Arrays.toString(evars));
316
317        PolynomialList<BigRational> G = bbp.partialGB(F.list, evars);
318        assertTrue("isGB( GB(G) )", bbp.isGB(G.list));
319        //System.out.println("evars = " + Arrays.toString(evars));
320        //System.out.println("G = " + G);
321    }
322
323
324    /**
325     * Test permutation generation.
326     * 
327     */
328    @SuppressWarnings("unchecked")
329    public void testPermGen() {
330        String[] vars = new String[] { "a", "b", "c", "d", "e", "f" };
331        //System.out.println("vars  = " + Arrays.toString(vars));
332
333        List<String> sv = new ArrayList<String>(vars.length);
334        for (int i = 0; i < vars.length; i++) {
335            sv.add(vars[i]);
336        }
337        //System.out.println("sv    = " + sv);
338
339        String exam = "(a,b,c,d,e,f) G " + "( " + "( a ), " + "( b^2 ), " + "( c^3 ), " + "( d^4 ), "
340                        + "( e^5 ), " + "( f^6 ) " + ") ";
341
342        Reader source = new StringReader(exam);
343        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
344        PolynomialList<BigRational> F = null;
345        try {
346            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
347        } catch (ClassCastException e) {
348            fail("" + e);
349        } catch (IOException e) {
350            fail("" + e);
351        }
352        //System.out.println("F = " + F);
353
354        for (int i = 0; i <= vars.length; i++) {
355            KsubSet<String> ps = new KsubSet<String>(sv, i);
356            //System.out.println("========================== ps : " + i);
357            for (List<String> ev : ps) {
358                //System.out.println("ev = " + ev);
359
360                String[] evars = new String[ev.size()];
361                for (int j = 0; j < ev.size(); j++) {
362                    evars[j] = ev.get(j);
363                }
364                //System.out.println("evars = " + Arrays.toString(evars));
365                String[] rvars = GroebnerBasePartial.remainingVars(vars, evars);
366                //System.out.println("rvars = " + Arrays.toString(rvars));
367
368                List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, evars);
369                //System.out.println("perm1 = " + perm1);
370                List<Integer> perm2 = GroebnerBasePartial.getPermutation(vars, rvars);
371                //System.out.println("perm2 = " + perm2); 
372                assertEquals("perm1 == perm2 " + Arrays.toString(evars), perm1, perm2);
373
374                GenPolynomialRing<BigRational> r = new GenPolynomialRing<BigRational>(fac.coFac, vars);
375                GenPolynomialRing<BigRational> pr1 = TermOrderOptimization
376                                .<BigRational> permutation(perm1, r);
377                //System.out.println("pr1 = " + pr1);
378                GenPolynomialRing<BigRational> pr2 = TermOrderOptimization
379                                .<BigRational> permutation(perm2, r);
380                //System.out.println("pr2 = " + pr2);
381                assertEquals("pr1 == pr2 ", pr1, pr2);
382
383                List<GenPolynomial<BigRational>> pF1 = TermOrderOptimization.<BigRational> permutation(perm1,
384                                pr1, F.list);
385                //System.out.println("pF1 = " + pF1);
386                List<GenPolynomial<BigRational>> pF2 = TermOrderOptimization.<BigRational> permutation(perm2,
387                                pr2, F.list);
388                //System.out.println("pF2 = " + pF2);
389                assertEquals("pF1 == pF2 ", pF1, pF2);
390            }
391        }
392    }
393
394}