001/*
002 * $Id: GroebnerBaseWalkTest.java 5731 2017-02-11 11:38:15Z kredel $
003 */
004
005package edu.jas.gbufd;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.List;
012
013import org.apache.log4j.BasicConfigurator;
014
015import edu.jas.arith.BigRational;
016import edu.jas.gb.GroebnerBaseAbstract;
017import edu.jas.poly.GenPolynomial;
018import edu.jas.poly.GenPolynomialTokenizer;
019import edu.jas.poly.PolynomialList;
020import edu.jas.poly.TermOrderByName;
021
022import junit.framework.Test;
023import junit.framework.TestCase;
024import junit.framework.TestSuite;
025
026
027/**
028 * Groebner base Groebner Walk tests with JUnit.
029 * @author Heinz Kredel
030 */
031
032public class GroebnerBaseWalkTest extends TestCase {
033
034
035    //private static final Logger logger = Logger.getLogger(GroebnerBaseWalkTest.class);
036
037
038    /**
039     * main
040     */
041    public static void main(String[] args) {
042        BasicConfigurator.configure();
043        junit.textui.TestRunner.run(suite());
044    }
045
046
047    /**
048     * Constructs a <CODE>GroebnerBaseWalkTest</CODE> object.
049     * @param name String.
050     */
051    public GroebnerBaseWalkTest(String name) {
052        super(name);
053    }
054
055
056    /**
057     * suite.
058     */
059    public static Test suite() {
060        TestSuite suite = new TestSuite(GroebnerBaseWalkTest.class);
061        return suite;
062    }
063
064
065    List<GenPolynomial<BigRational>> L, Lp;
066
067
068    PolynomialList<BigRational> F;
069
070
071    List<GenPolynomial<BigRational>> G, Gp;
072
073
074    GroebnerBaseAbstract<BigRational> bb;
075
076
077    GroebnerBaseAbstract<BigRational> bbw;
078
079
080    @Override
081    protected void setUp() {
082        //BigRational cf = new BigRational();
083        //bb = new GroebnerBaseSeq<BigRational>(cf);
084        bb = GBFactory.<BigRational> getImplementation(/*cf*/);
085        bbw = new GroebnerBaseWalk<BigRational>(bb);
086    }
087
088
089    @Override
090    protected void tearDown() {
091        bb.terminate();
092        bbw.terminate();
093        bb = null;
094        bbw = null;
095    }
096
097
098    /**
099     * Test FJLT GBase. Example from the FJLT paper.
100     */
101    @SuppressWarnings({ "unchecked", "cast" })
102    public void testFJLTGBase() { // (y,x)
103        String exam = "(y,x) L " // REVILEX REVITDG
104                        + "( (x**2 - y**3), (x**3 - y**2 - x) )";
105
106        Reader source = new StringReader(exam);
107        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
108        try {
109            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
110        } catch (ClassCastException e) {
111            fail("" + e);
112        } catch (IOException e) {
113            fail("" + e);
114        }
115        //System.out.println("F = " + F);
116
117        G = bb.GB(F.list);
118        PolynomialList<BigRational> seq = new PolynomialList<BigRational>(F.ring, G);
119        //System.out.println("seq G = " + seq);
120        assertTrue("isGB( GB(FJLT) )", bb.isGB(G));
121        assertTrue("isMinimalGB( GB(FJLT) )", bb.isMinimalGB(G));
122        assertEquals("#GB(FJLT) == 2", 2, G.size());
123        //assertEquals("#GB(FJLT) == 3", 3, G.size());
124        Gp = bbw.GB(F.list);
125        PolynomialList<BigRational> fjlt = new PolynomialList<BigRational>(F.ring, Gp);
126        //System.out.println("walk G = " + fjlt);
127        assertTrue("isGB( GB(FJLT) )", bb.isGB(Gp));
128        assertTrue("isMinimalGB( GB(FJLT) )", bb.isMinimalGB(Gp));
129        assertEquals("#GB(FJLT) == 2", 2, Gp.size());
130        //assertEquals("#GB(FJLT) == 3", 3, Gp.size());
131        //Collections.reverse(G); // now in minimal
132        assertEquals("G == Gp: ", seq, fjlt);
133    }
134
135
136    /**
137     * Test example GBase.
138     */
139    @SuppressWarnings({ "unchecked", "cast" })
140    public void testFGLMGBase() { //(z,y,x)
141        String exam = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )"
142                        + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )";
143
144        Reader source = new StringReader(exam);
145        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
146        try {
147            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
148        } catch (ClassCastException e) {
149            fail("" + e);
150        } catch (IOException e) {
151            fail("" + e);
152        }
153        //System.out.println("F = " + F);
154
155        G = bb.GB(F.list);
156        PolynomialList<BigRational> P = new PolynomialList<BigRational>(F.ring, G);
157        //System.out.println("G = " + P);
158        assertTrue("isGB( GB(P) )", bb.isGB(G));
159        assertEquals("#GB(P) == 3", 3, G.size());
160
161        Gp = bbw.GB(F.list);
162        PolynomialList<BigRational> P2 = new PolynomialList<BigRational>(F.ring, Gp);
163        //System.out.println("G = " + P2);
164        assertTrue("isGB( GB(P2) )", bb.isGB(Gp));
165        assertEquals("#GB(P2) == 3", 3, Gp.size());
166        assertEquals("GB == FGLM", P, P2);
167    }
168
169
170    /**
171     * Test Trinks GBase.
172     */
173    @SuppressWarnings({ "unchecked", "cast" })
174    public void testTrinksGBase() {
175        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
176                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
177                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
178                        + "( 99 W - 11 B S + 3 B**2 ) " /*+ ", ( 10000 B**2 + 6600 B + 2673 )"*/ + ") ";
179
180        Reader source = new StringReader(exam);
181        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
182        try {
183            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
184        } catch (ClassCastException e) {
185            fail("" + e);
186        } catch (IOException e) {
187            fail("" + e);
188        }
189        //System.out.println("F = " + F);
190
191        long t = System.currentTimeMillis();
192        G = bb.GB(F.list);
193        t = System.currentTimeMillis() - t;
194        PolynomialList<BigRational> seq = new PolynomialList<BigRational>(F.ring, G);
195        //System.out.println("seq G = " + seq);
196        assertTrue("isGB( GB(Trinks) )", bb.isGB(G));
197        assertTrue("isMinimalGB( GB(Trinks) )", bb.isMinimalGB(G));
198        assertEquals("#GB(Trinks) == 6", 6, G.size());
199        long w = System.currentTimeMillis();
200        Gp = bbw.GB(F.list);
201        w = System.currentTimeMillis() - w;
202        PolynomialList<BigRational> tri = new PolynomialList<BigRational>(F.ring, Gp);
203        //System.out.println("walk G = " + tri);
204        //System.out.println("lex = " + t + ", walk = " + w + " in milliseconds");
205        assertTrue("findbugs ", t + w >= 0L);
206        assertTrue("isGB( GB(Trinks) )", bb.isGB(Gp));
207        assertTrue("isMinimalGB( GB(Trinks) )", bb.isMinimalGB(Gp));
208        //assertEquals("#GB(Trinks) == 6", 6, Gp.size());
209        //Collections.reverse(G); // now in minimal
210        assertEquals("G == Gp: ", seq, tri); // ideal.equals
211    }
212
213
214    /**
215     * Test Trinks GBase with different t1 and t2.
216     */
217    @SuppressWarnings({ "unchecked", "cast" })
218    public void testTrinksGBaseT1T2() { // t2 = G|4| = IGRLEX.blockOrder(4)
219        String exam = "(B,S,T,Z,P,W) G|4| " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
220                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
221                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
222                        + "( 99 W - 11 B S + 3 B**2 ) " /*+ ", ( 10000 B**2 + 6600 B + 2673 )"*/ + ") ";
223
224        Reader source = new StringReader(exam);
225        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
226        try {
227            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
228        } catch (ClassCastException e) {
229            fail("" + e);
230        } catch (IOException e) {
231            fail("" + e);
232        }
233        //System.out.println("F = " + F);
234        // set t1, t2 = TermOrderNyName.IGRLEX.blockOrder(4)
235        bbw = new GroebnerBaseWalk<BigRational>(bb, TermOrderByName.IGRLEX.blockOrder(2));
236        // Test wrong way from INVLEX to IGRLEX.
237        //bbw = new GroebnerBaseWalk<BigRational>(bb, TermOrderByName.INVLEX);
238        //System.out.println("bbw = " + bbw);
239
240        Gp = bbw.GB(F.list);
241        //PolynomialList<BigRational> tri = new PolynomialList<BigRational>(F.ring, Gp);
242        //System.out.println("walk G = " + tri);
243        assertTrue("isGB( GB(Trinks) )", bb.isGB(Gp));
244        assertTrue("isMinimalGB( GB(Trinks) )", bb.isMinimalGB(Gp));
245        //assertEquals("#GB(Trinks) == 6", 6, Gp.size());
246        //Collections.reverse(G); // now in minimal
247    }
248
249
250    /**
251     * Test ISSAC GBase. Example from the FGLM paper.
252     */
253    @SuppressWarnings({ "unchecked", "cast" })
254    public void testFGLMissacGBase() { // 
255        String exam = "Mod 32003 (w,z,y,x) L " // Mod 9223372036854775783 536870909 32003 (z,y,x,w)
256                        + "( "
257                        + " (8*w^2 + 5*w*x - 4*w*y + 2*w*z + 3*w + 5*x^2 + 2*x*y - 7*x*z - 7*x + 7*y^2 -8*y*z - 7*y + 7*z^2 - 8*z + 8),"
258                        + "(3*w^2 - 5*w*x - 3*w*y - 6*w*z + 9*w + 4*x^2 + 2*x*y - 2*x*z + 7*x + 9*y^2 + 6*y*z + 5*y + 7*z^2 + 7*z + 5),"
259                        + "(-2*w^2 + 9*w*x + 9*w*y - 7*w*z - 4*w + 8*x^2 + 9*x*y - 3*x*z + 8*x + 6*y^2 - 7*y*z + 4*y - 6*z^2 + 8*z + 2),"
260                        + "(7*w^2 + 5*w*x + 3*w*y - 5*w*z - 5*w + 2*x^2 + 9*x*y - 7*x*z + 4*x -4*y^2 - 5*y*z + 6*y - 4*z^2 - 9*z + 2)"
261                        + " )";
262
263        Reader source = new StringReader(exam);
264        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
265        try {
266            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
267        } catch (ClassCastException e) {
268            fail("" + e);
269        } catch (IOException e) {
270            fail("" + e);
271        }
272        //System.out.println("F = " + F);
273
274        //G = bb.GB(F.list);
275        long t = System.currentTimeMillis();
276        G = bb.GB(F.list);
277        t = System.currentTimeMillis() - t;
278        //PolynomialList<BigRational> seq = new PolynomialList<BigRational>(F.ring, G);
279        //System.out.println("seq G = " + seq);
280        assertTrue("isGB( GB() )", bb.isGB(G));
281        assertTrue("isMinimalGB( GB() )", bb.isMinimalGB(G));
282        //assertEquals("#GB(FJLT) == 2", 2, G.size());
283
284        long w = System.currentTimeMillis();
285        Gp = bbw.GB(F.list);
286        w = System.currentTimeMillis() - w;
287        //PolynomialList<BigRational> fjlt = new PolynomialList<BigRational>(F.ring, Gp);
288        //System.out.println("walk G = " + fjlt);
289        //System.out.println("lex = " + t + ", walk = " + w + " in milliseconds");
290        assertTrue("findbugs ", t + w >= 0L);
291        assertTrue("isGB( GB() )", bb.isGB(Gp));
292        assertTrue("isMinimalGB( GB() )", bb.isMinimalGB(Gp));
293        //assertEquals("#GB(FJLT) == 2", 2, Gp.size());
294        //Collections.reverse(G); // now in minimal
295        assertEquals("G == Gp: ", G, Gp);
296    }
297}