001/*
002 * $Id$
003 */
004
005package edu.jas.application;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.ArrayList;
012import java.util.List;
013
014import edu.jas.arith.BigComplex;
015import edu.jas.arith.BigDecimal;
016import edu.jas.arith.BigInteger;
017import edu.jas.arith.BigQuaternion;
018import edu.jas.arith.BigQuaternionRing;
019import edu.jas.arith.BigRational;
020import edu.jas.arith.ModInt;
021import edu.jas.arith.ModIntRing;
022import edu.jas.arith.ModInteger;
023import edu.jas.arith.ModIntegerRing;
024import edu.jas.arith.ModLong;
025import edu.jas.arith.ModLongRing;
026import edu.jas.poly.GenPolynomial;
027import edu.jas.poly.GenPolynomialRing;
028import edu.jas.poly.GenSolvablePolynomial;
029import edu.jas.poly.GenSolvablePolynomialRing;
030import edu.jas.poly.TermOrder;
031import edu.jas.structure.RingFactory;
032import edu.jas.ufd.Quotient;
033import edu.jas.ufd.QuotientRing;
034
035import junit.framework.Test;
036import junit.framework.TestCase;
037import junit.framework.TestSuite;
038
039
040/**
041 * RingFactoryTokenizer tests with JUnit.
042 * @author Heinz Kredel
043 */
044
045public class RingFactoryTokenizerTest extends TestCase {
046
047
048    /**
049     * main.
050     */
051    public static void main(String[] args) {
052        junit.textui.TestRunner.run(suite());
053    }
054
055
056    /**
057     * Constructs a <CODE>RingFactoryTokenizerTest</CODE> object.
058     * @param name String.
059     */
060    public RingFactoryTokenizerTest(String name) {
061        super(name);
062    }
063
064
065    /**
066     * suite.
067     */
068    public static Test suite() {
069        TestSuite suite = new TestSuite(RingFactoryTokenizerTest.class);
070        return suite;
071    }
072
073
074    RingFactory fac; // unused
075
076
077    GenPolynomialRing pfac;
078
079
080    GenSolvablePolynomialRing spfac;
081
082
083    RingFactoryTokenizer parser;
084
085
086    Reader source;
087
088
089    @Override
090    protected void setUp() {
091        fac = null;
092        pfac = null;
093        parser = null;
094        source = null;
095    }
096
097
098    @Override
099    protected void tearDown() {
100        fac = null;
101        pfac = null;
102        parser = null;
103        source = null;
104    }
105
106
107    /**
108     * Test rational polynomial.
109     */
110    @SuppressWarnings("unchecked")
111    public void testBigRational() {
112        String exam = "Rat(x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 3/4 - 6/8 ), "
113                        + "( 1 x + x^3 + 1/3 y z - x^3 ) " + " )";
114        source = new StringReader(exam);
115        parser = new RingFactoryTokenizer(source);
116        GenPolynomialRing<BigRational> f = null;
117        try {
118            f = (GenPolynomialRing<BigRational>) parser.nextPolynomialRing();
119        } catch (IOException e) {
120            fail("" + e);
121        } catch (ClassCastException e) {
122            fail("" + e);
123        }
124
125        BigRational fac = new BigRational(0);
126        TermOrder tord = new TermOrder(TermOrder.INVLEX);
127        String[] vars = new String[] { "x", "y", "z" };
128        int nvar = vars.length;
129        pfac = new GenPolynomialRing<BigRational>(fac, nvar, tord, vars);
130        assertEquals("pfac == f", pfac, f);
131    }
132
133
134    /**
135     * Test integer polynomial.
136     */
137    @SuppressWarnings("unchecked")
138    public void testBigInteger() {
139        String exam = "Int(x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 3 2 - 6 ), "
140                        + "( 1 x + x^3 + 3 y z - x^3 ) " + " )";
141        source = new StringReader(exam);
142        parser = new RingFactoryTokenizer(source);
143        GenPolynomialRing<BigInteger> f = null;
144        try {
145            f = (GenPolynomialRing<BigInteger>) parser.nextPolynomialRing();
146        } catch (IOException e) {
147            fail("" + e);
148        } catch (ClassCastException e) {
149            fail("" + e);
150        }
151
152        BigInteger fac = new BigInteger(0);
153        TermOrder tord = new TermOrder(TermOrder.INVLEX);
154        String[] vars = new String[] { "x", "y", "z" };
155        int nvar = vars.length;
156        pfac = new GenPolynomialRing<BigInteger>(fac, nvar, tord, vars);
157        assertEquals("pfac == f", pfac, f);
158    }
159
160
161    /**
162     * Test modular integer polynomial.
163     */
164    @SuppressWarnings("unchecked")
165    public void testModInteger() {
166        String exam = "Mod 19 (x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 3 2 - 6 + 19 ), "
167                        + "( 1 x + x^3 + 3 y z - x^3 ) " + " )";
168        source = new StringReader(exam);
169        parser = new RingFactoryTokenizer(source);
170        GenPolynomialRing<ModInteger> f = null;
171        try {
172            f = (GenPolynomialRing<ModInteger>) parser.nextPolynomialRing();
173        } catch (IOException e) {
174            fail("" + e);
175        } catch (ClassCastException e) {
176            fail("" + e);
177        }
178
179        ModIntRing fac = new ModIntRing(19);
180        TermOrder tord = new TermOrder(TermOrder.INVLEX);
181        String[] vars = new String[] { "x", "y", "z" };
182        int nvar = vars.length;
183        pfac = new GenPolynomialRing<ModInt>(fac, nvar, tord, vars);
184        //System.out.println("pfac   = " + pfac);
185        //System.out.println("f.ring = " + f.ring);
186        assertEquals("pfac == f", pfac, f);
187
188        ModLongRing lfac = new ModLongRing(19);
189        assertFalse("fac != lfac", fac.equals(lfac));
190        assertFalse("lfac != f.coFac", lfac.equals(f.coFac));
191    }
192
193
194    /**
195     * Test complex polynomial.
196     */
197    @SuppressWarnings("unchecked")
198    public void testBigComplex() {
199        String exam = "Complex(x,y,z) L " + "( " + "( 1i0 ), " + "( 0i0 ), " + "( 3/4i2 - 6/8i2 ), "
200                        + "( 1i0 x + x^3 + 1i3 y z - x^3 ) " + " )";
201        source = new StringReader(exam);
202        parser = new RingFactoryTokenizer(source);
203        GenPolynomialRing<BigComplex> f = null;
204        try {
205            f = (GenPolynomialRing<BigComplex>) parser.nextPolynomialRing();
206        } catch (IOException e) {
207            fail("" + e);
208        } catch (ClassCastException e) {
209            fail("" + e);
210        }
211
212        BigComplex fac = new BigComplex(0);
213        TermOrder tord = new TermOrder(TermOrder.INVLEX);
214        String[] vars = new String[] { "x", "y", "z" };
215        int nvar = vars.length;
216        pfac = new GenPolynomialRing<BigComplex>(fac, nvar, tord, vars);
217        assertEquals("pfac == f", pfac, f);
218    }
219
220
221    /**
222     * Test decimal polynomial.
223     */
224    @SuppressWarnings("unchecked")
225    public void testBigDecimal() {
226        String exam = "D(x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 0.25 * 0.25 - 0.25^2 ), "
227                        + "( 1 x + x^3 + 0.3333333333333333333333 y z - x^3 ) " + " )";
228        source = new StringReader(exam);
229        parser = new RingFactoryTokenizer(source);
230        GenPolynomialRing<BigDecimal> f = null;
231        try {
232            f = (GenPolynomialRing<BigDecimal>) parser.nextPolynomialRing();
233        } catch (IOException e) {
234            fail("" + e);
235        } catch (ClassCastException e) {
236            fail("" + e);
237        }
238
239        BigDecimal fac = new BigDecimal(0);
240        TermOrder tord = new TermOrder(TermOrder.INVLEX);
241        String[] vars = new String[] { "x", "y", "z" };
242        int nvar = vars.length;
243        pfac = new GenPolynomialRing<BigDecimal>(fac, nvar, tord, vars);
244        assertEquals("pfac == f", pfac, f);
245    }
246
247
248    /**
249     * Test quaternion polynomial.
250     */
251    @SuppressWarnings("unchecked")
252    public void testBigQuaternion() {
253        String exam = "Quat(x,y,z) L " + "( " + "( 1i0j0k0 ), " + "( 0i0j0k0 ), "
254                        + "( 3/4i2j1k3 - 6/8i2j1k3 ), " + "( 1 x + x^3 + 1i2j3k4 y z - x^3 ) " + " )";
255        source = new StringReader(exam);
256        parser = new RingFactoryTokenizer(source);
257        GenPolynomialRing<BigQuaternion> f = null;
258        try {
259            f = (GenPolynomialRing<BigQuaternion>) parser.nextPolynomialRing();
260        } catch (IOException e) {
261            fail("" + e);
262        } catch (ClassCastException e) {
263            fail("" + e);
264        }
265
266        BigQuaternionRing fac = new BigQuaternionRing();
267        TermOrder tord = new TermOrder(TermOrder.INVLEX);
268        String[] vars = new String[] { "x", "y", "z" };
269        int nvar = vars.length;
270        pfac = new GenPolynomialRing<BigQuaternion>(fac, nvar, tord, vars);
271        assertEquals("pfac == f", pfac, f);
272    }
273
274
275    /**
276     * Test rational function coefficients.
277     */
278    @SuppressWarnings("unchecked")
279    public void testRationalFunction() {
280        String exam = "RatFunc(x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 3/4 - 6/8 ), "
281                        + "( 1 x + x^3 + 1/3 y z - x^3 ) " + " )";
282        source = new StringReader(exam);
283        parser = new RingFactoryTokenizer(source);
284        GenPolynomialRing<Quotient<BigInteger>> f = null;
285        try {
286            f = (GenPolynomialRing<Quotient<BigInteger>>) parser.nextPolynomialRing();
287        } catch (IOException e) {
288            fail("" + e);
289        } catch (ClassCastException e) {
290            fail("" + e);
291        }
292
293        BigInteger fac = new BigInteger(0);
294        TermOrder tord = new TermOrder(TermOrder.INVLEX);
295        String[] vars = new String[] { "x", "y", "z" };
296        int nvar = vars.length;
297        pfac = new GenPolynomialRing<BigInteger>(fac, nvar, tord, vars);
298
299        QuotientRing<BigInteger> qfac = new QuotientRing<BigInteger>(pfac);
300        //System.out.println("qfac = " + qfac.toScript());
301        //System.out.println("f.coFac = " + f.ring.coFac.toScript());
302        assertEquals("qfac == f.coFac", qfac, f.coFac);
303    }
304
305
306    /**
307     * Test integral function coefficients.
308     */
309    @SuppressWarnings("unchecked")
310    public void testIntegralFunction() {
311        String exam = "IntFunc(x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 3/4 - 6/8 ), "
312                        + "( 1 x + x^3 + 1/3 y z - x^3 ) " + " )";
313        source = new StringReader(exam);
314        parser = new RingFactoryTokenizer(source);
315        GenPolynomialRing<GenPolynomial<BigRational>> f = null;
316        try {
317            f = (GenPolynomialRing<GenPolynomial<BigRational>>) parser.nextPolynomialRing();
318        } catch (IOException e) {
319            fail("" + e);
320        } catch (ClassCastException e) {
321            fail("" + e);
322        }
323
324        BigRational fac = new BigRational(0);
325        TermOrder tord = new TermOrder(TermOrder.INVLEX);
326        String[] vars = new String[] { "x", "y", "z" };
327        int nvar = vars.length;
328        pfac = new GenPolynomialRing<BigRational>(fac, nvar, tord, vars);
329
330        //System.out.println("pfac = " + pfac.toScript());
331        //System.out.println("f.coFac = " + f.coFac.toScript());
332        assertEquals("pfac == f.coFac", pfac, f.coFac);
333    }
334
335
336    /**
337     * Test modular function coefficients.
338     */
339    @SuppressWarnings("unchecked")
340    public void testModularFunction() {
341        String exam = "ModFunc 32003 (x,y,z) L " + "( " + "( 1 ), " + "( 0 ), " + "( 3/4 - 6/8 ), "
342                        + "( 1 x + x^3 + 1/3 y z - x^3 ) " + " )";
343        source = new StringReader(exam);
344        parser = new RingFactoryTokenizer(source);
345        GenPolynomialRing<Quotient<ModInteger>> f = null;
346        try {
347            f = (GenPolynomialRing<Quotient<ModInteger>>) parser.nextPolynomialRing();
348        } catch (IOException e) {
349            fail("" + e);
350        } catch (ClassCastException e) {
351            fail("" + e);
352        }
353
354        ModIntegerRing fac = new ModIntegerRing(32003);
355        TermOrder tord = new TermOrder(TermOrder.INVLEX);
356        String[] vars = new String[] { "x", "y", "z" };
357        int nvar = vars.length;
358        pfac = new GenPolynomialRing<ModInteger>(fac, nvar, tord, vars);
359
360        QuotientRing<ModInteger> qfac = new QuotientRing<ModInteger>(pfac);
361        //System.out.println("qfac = " + qfac.toScript());
362        //System.out.println("f.coFac = " + f.ring.toScript());
363        assertEquals("qfac == f.coFac", qfac, f.coFac);
364    }
365
366
367    /**
368     * Test rational solvable polynomial.
369     */
370    @SuppressWarnings("unchecked")
371    public void testSolvableBigRational() {
372        String exam = "Rat(x,y,z) L " + "RelationTable " + "( " + " ( z ), ( y ), ( y z - 1 ) " + ") " + "( "
373                        + " ( 1 ), " + " ( 0 ), " + " ( 3/4 - 6/8 ), " + " ( 1 x + x^3 + 1/3 y z - x^3 ) "
374                        + " )";
375        source = new StringReader(exam);
376        parser = new RingFactoryTokenizer(source);
377        GenSolvablePolynomialRing<BigRational> f = null;
378        try {
379            f = (GenSolvablePolynomialRing<BigRational>) parser.nextSolvablePolynomialRing();
380        } catch (IOException e) {
381            fail("" + e);
382        } catch (ClassCastException e) {
383            fail("" + e);
384        }
385
386        BigRational fac = new BigRational(0);
387        TermOrder tord = new TermOrder(TermOrder.INVLEX);
388        String[] vars = new String[] { "x", "y", "z" };
389        int nvar = vars.length;
390        spfac = new GenSolvablePolynomialRing<BigRational>(fac, nvar, tord, vars);
391        List<GenSolvablePolynomial<BigRational>> rel = new ArrayList<GenSolvablePolynomial<BigRational>>(3);
392        rel.add(spfac.parse("z"));
393        rel.add(spfac.parse("y"));
394        rel.add(spfac.parse("y z - 1"));
395        spfac.addSolvRelations(rel);
396        assertEquals("spfac == f", spfac, f);
397        //System.out.println("spfac = " + spfac);
398        //System.out.println("spfac.table = " + spfac.table);
399    }
400
401
402    /**
403     * Test mod integer solvable polynomial.
404     */
405    @SuppressWarnings("unchecked")
406    public void testSolvableModInteger() {
407        String exam = "Mod 19 (x,y,z) L " + "RelationTable " + "( " + " ( z ), ( y ), ( y z - 1 ) " + ") "
408                        + "( " + "( 1 ), " + "( 0 ), " + "( 3 2 - 6 + 19 ), " + "( 1 x + x^3 + 3 y z - x^3 ) "
409                        + " )";
410        source = new StringReader(exam);
411        parser = new RingFactoryTokenizer(source);
412        GenSolvablePolynomialRing<ModInteger> f = null;
413        try {
414            f = (GenSolvablePolynomialRing<ModInteger>) parser.nextSolvablePolynomialRing();
415        } catch (IOException e) {
416            fail("" + e);
417        } catch (ClassCastException e) {
418            fail("" + e);
419        }
420
421        ModIntRing fac = new ModIntRing(19);
422        TermOrder tord = new TermOrder(TermOrder.INVLEX);
423        String[] vars = new String[] { "x", "y", "z" };
424        int nvar = vars.length;
425        spfac = new GenSolvablePolynomialRing<ModInt>(fac, nvar, tord, vars);
426        List<GenSolvablePolynomial<ModLong>> rel = new ArrayList<GenSolvablePolynomial<ModLong>>(3);
427        rel.add(spfac.parse("z"));
428        rel.add(spfac.parse("y"));
429        rel.add(spfac.parse("y z - 1"));
430        spfac.addSolvRelations(rel);
431        assertEquals("spfac == f", spfac, f);
432        //System.out.println("spfac = " + spfac);
433        //System.out.println("spfac.table = " + spfac.table);
434    }
435
436
437    /**
438     * Test integer polynomial module.
439     */
440    @SuppressWarnings("unchecked")
441    public void testBigIntegerModule() {
442        String exam = "Int(x,y,z) L " + "( " + " ( " + "  ( 1 ), " + "  ( 0 ), " + "  ( 3 2 - 6 ), "
443                        + "  ( 1 x + x^3 + 3 y z - x^3 ) " + " ), " + " ( ( 1 ), ( 0 ) ) " + ")";
444        source = new StringReader(exam);
445        parser = new RingFactoryTokenizer(source);
446        GenPolynomialRing<BigInteger> m = null;
447        try {
448            m = (GenPolynomialRing<BigInteger>) parser.nextSubModuleRing();
449        } catch (IOException e) {
450            fail("" + e);
451        } catch (ClassCastException e) {
452            fail("" + e);
453        }
454
455        BigInteger fac = new BigInteger(0);
456        TermOrder tord = new TermOrder(TermOrder.INVLEX);
457        String[] vars = new String[] { "x", "y", "z" };
458        int nvar = vars.length;
459        pfac = new GenPolynomialRing<BigInteger>(fac, nvar, tord, vars);
460        assertEquals("pfac == m.ring", pfac, m);
461    }
462
463
464    /**
465     * Test rational solvable polynomial module.
466     */
467    @SuppressWarnings("unchecked")
468    public void testBigRationalSolvableModule() {
469        String exam = "Rat(x,y,z) L " + "RelationTable " + "( " + " ( z ), ( y ), ( y z - 1 ) " + ") " + "( "
470                        + " ( " + "  ( 1 ), " + "  ( 0 ), " + "  ( 3/4 - 6/8 ), "
471                        + "  ( 1 x + x^3 + 1/3 y z - x^3 ) " + " ), " + " ( ( x ), ( 1 ), ( 0 ) ) " + " )";
472        source = new StringReader(exam);
473        parser = new RingFactoryTokenizer(source);
474        GenSolvablePolynomialRing<BigRational> m = null;
475        try {
476            m = (GenSolvablePolynomialRing<BigRational>) parser.nextSolvableSubModuleRing();
477        } catch (IOException e) {
478            fail("" + e);
479        } catch (ClassCastException e) {
480            fail("" + e);
481        }
482
483        BigRational fac = new BigRational(0);
484        TermOrder tord = new TermOrder(TermOrder.INVLEX);
485        String[] vars = new String[] { "x", "y", "z" };
486        int nvar = vars.length;
487        spfac = new GenSolvablePolynomialRing<BigRational>(fac, nvar, tord, vars);
488        List<GenSolvablePolynomial<ModLong>> rel = new ArrayList<GenSolvablePolynomial<ModLong>>(3);
489        rel.add(spfac.parse("z"));
490        rel.add(spfac.parse("y"));
491        rel.add(spfac.parse("y z - 1"));
492        spfac.addSolvRelations(rel);
493        assertEquals("spfac == m.ring", spfac, m);
494    }
495
496
497    /**
498     * Test rational polynomial with generic coefficients.
499     */
500    @SuppressWarnings("unchecked")
501    public void testBigRationalGeneric() {
502        String exam = "Rat(x,y,z) L " + "( " + "( 1^3 ), " + "( 0^3 ), " + "( { 3/4 }^2 - 6/8^2 ), "
503                        + "( { 1 }^2 x + x^3 + 1/3 y z - x^3 ), "
504                        + "( 1.0001 - 0.0001 + { 0.25 }**2 - 1/4^2 ) " + " )";
505        source = new StringReader(exam);
506        parser = new RingFactoryTokenizer(source);
507        GenPolynomialRing<BigRational> f = null;
508        try {
509            f = (GenPolynomialRing<BigRational>) parser.nextPolynomialRing();
510        } catch (IOException e) {
511            fail("" + e);
512        } catch (ClassCastException e) {
513            fail("" + e);
514        }
515
516        BigRational fac = new BigRational(0);
517        TermOrder tord = new TermOrder(TermOrder.INVLEX);
518        String[] vars = new String[] { "x", "y", "z" };
519        int nvar = vars.length;
520        pfac = new GenPolynomialRing<BigRational>(fac, nvar, tord, vars);
521        assertEquals("pfac == f", pfac, f);
522    }
523
524}