001    /*
002     * $Id: GenPolynomialTokenizerTest.java 3585 2011-03-26 22:40:36Z kredel $
003     */
004    
005    package edu.jas.poly;
006    
007    import junit.framework.Test;
008    import junit.framework.TestCase;
009    import junit.framework.TestSuite;
010    
011    import org.apache.log4j.BasicConfigurator;
012    
013    import java.io.IOException;
014    import java.io.Reader;
015    import java.io.StringReader;
016    
017    import java.util.List;
018    import java.util.Arrays;
019    
020    //import edu.jas.structure.RingElem;
021    import edu.jas.structure.RingFactory;
022    
023    import edu.jas.arith.BigRational;
024    import edu.jas.arith.BigInteger;
025    import edu.jas.arith.ModInteger;
026    import edu.jas.arith.ModIntegerRing;
027    import edu.jas.arith.ModLong;
028    import edu.jas.arith.ModLongRing;
029    import edu.jas.arith.BigComplex;
030    import edu.jas.arith.BigDecimal;
031    import edu.jas.arith.BigQuaternion;
032    
033    import edu.jas.poly.AlgebraicNumber;
034    import edu.jas.poly.AlgebraicNumberRing;
035    import edu.jas.poly.GenPolynomial;
036    import edu.jas.poly.GenSolvablePolynomial;
037    import edu.jas.poly.GenPolynomialRing;
038    import edu.jas.poly.GenSolvablePolynomialRing;
039    import edu.jas.poly.GenPolynomialTokenizer;
040    import edu.jas.poly.InvalidExpressionException;
041    import edu.jas.poly.PolynomialList;
042    
043    
044    /**
045     * GenPolynomialTokenizer tests with JUnit.
046     * @author Heinz Kredel
047     */
048    
049    public class GenPolynomialTokenizerTest extends TestCase {
050    
051        /**
052         * main.
053         */
054        public static void main (String[] args) {
055            if ( false ) BasicConfigurator.configure();
056            junit.textui.TestRunner.run( suite() );
057        }
058    
059        /**
060         * Constructs a <CODE>GenPolynomialTokenizerTest</CODE> object.
061         * @param name String.
062         */
063        public GenPolynomialTokenizerTest(String name) {
064            super(name);
065        }
066    
067        /**
068         * suite.
069         */ 
070        public static Test suite() {
071            TestSuite suite= new TestSuite(GenPolynomialTokenizerTest.class);
072            return suite;
073        }
074    
075        RingFactory fac; // unused
076        GenPolynomialRing pfac;
077        GenSolvablePolynomialRing spfac;
078        GenPolynomialTokenizer parser;
079        Reader source;
080    
081    
082        protected void setUp() {
083            fac = null;
084            pfac = null;
085            parser = null;
086            source = null;
087        }
088    
089        protected void tearDown() {
090            fac = null;
091            pfac = null;
092            parser = null;
093            source = null;
094        }
095    
096    
097        /**
098         * Test rational polynomial.
099         */
100        @SuppressWarnings("unchecked")
101        public void testBigRational() {
102            String exam = "Rat(x,y,z) L "  
103                + "( "
104                + "( 1 ), "
105                + "( 0 ), "
106                + "( 3/4 - 6/8 ), "
107                + "( 1 x + x^3 + 1/3 y z - x^3 ) "
108                + " )";
109            source = new StringReader( exam );
110            parser = new GenPolynomialTokenizer( source );
111            PolynomialList<BigRational> f = null;
112            try {
113                f = (PolynomialList<BigRational>)parser.nextPolynomialSet();
114            } catch(IOException e) {
115                fail(""+e);
116            } catch (ClassCastException e) {
117                fail(""+e);
118            }
119            //System.out.println("f = " + f);
120            assertTrue("f != null", f.list != null);
121            assertTrue("length( f ) = 4", f.list.size() == 4);
122    
123            BigRational fac = new BigRational(0);
124            TermOrder tord = new TermOrder(TermOrder.INVLEX);
125            String[] vars = new String[]{ "x", "y", "z" };
126            int nvar = vars.length;
127            pfac = new GenPolynomialRing<BigRational>(fac,nvar,tord,vars);
128            assertEquals("pfac == f.ring", pfac, f.ring );
129    
130            GenPolynomial<BigRational> a = f.list.get(0);
131            //System.out.println("a = " + a);
132            assertTrue("isONE( f.get(0) )", a.isONE() );
133    
134            GenPolynomial<BigRational> b = f.list.get(1);
135            //System.out.println("b = " + b);
136            assertTrue("isZERO( f.get(1) )", b.isZERO() );
137    
138            GenPolynomial<BigRational> c = f.list.get(2);
139            //System.out.println("c = " + c);
140            assertTrue("isZERO( f.get(2) )", c.isZERO() );
141    
142            GenPolynomial<BigRational> d = f.list.get(3);
143            //System.out.println("d = " + d);
144            assertEquals("f.get(3).length() == 2", 2, d.length() );
145        }
146    
147    
148        /**
149         * Test integer polynomial.
150         */
151        @SuppressWarnings("unchecked")
152        public void testBigInteger() {
153            String exam = "Int(x,y,z) L "  
154                + "( "
155                + "( 1 ), "
156                + "( 0 ), "
157                + "( 3 2 - 6 ), "
158                + "( 1 x + x^3 + 3 y z - x^3 ) "
159                + " )";
160            source = new StringReader( exam );
161            parser = new GenPolynomialTokenizer( source );
162            PolynomialList<BigInteger> f = null;
163            try {
164                f = (PolynomialList<BigInteger>)parser.nextPolynomialSet();
165            } catch(IOException e) {
166                fail(""+e);
167            } catch (ClassCastException e) {
168                fail(""+e);
169            }
170            //System.out.println("f = " + f);
171            assertTrue("f != null", f.list != null);
172            assertTrue("length( f ) = 4", f.list.size() == 4);
173    
174            BigInteger fac = new BigInteger(0);
175            TermOrder tord = new TermOrder(TermOrder.INVLEX);
176            String[] vars = new String[]{ "x", "y", "z" };
177            int nvar = vars.length;
178            pfac = new GenPolynomialRing<BigInteger>(fac,nvar,tord,vars);
179            assertEquals("pfac == f.ring", pfac, f.ring );
180    
181    
182            GenPolynomial<BigInteger> a = f.list.get(0);
183            //System.out.println("a = " + a);
184            assertTrue("isONE( f.get(0) )", a.isONE() );
185    
186            GenPolynomial<BigInteger> b = f.list.get(1);
187            //System.out.println("b = " + b);
188            assertTrue("isZERO( f.get(1) )", b.isZERO() );
189    
190            GenPolynomial<BigInteger> c = f.list.get(2);
191            //System.out.println("c = " + c);
192            assertTrue("isZERO( f.get(2) )", c.isZERO() );
193    
194            GenPolynomial<BigInteger> d = f.list.get(3);
195            //System.out.println("d = " + d);
196            assertEquals("f.get(3).length() == 2", 2, d.length() );
197        }
198    
199    
200        /**
201         * Test modular integer polynomial.
202         */
203        @SuppressWarnings("unchecked")
204        public void testModInteger() {
205            String exam = "Mod 19 (x,y,z) L "  
206                + "( "
207                + "( 1 ), "
208                + "( 0 ), "
209                + "( 3 2 - 6 + 19 ), "
210                + "( 1 x + x^3 + 3 y z - x^3 ) "
211                + " )";
212            source = new StringReader( exam );
213            parser = new GenPolynomialTokenizer( source );
214            PolynomialList<ModInteger> f = null;
215            try {
216                f = (PolynomialList<ModInteger>)parser.nextPolynomialSet();
217            } catch(IOException e) {
218                fail(""+e);
219            } catch (ClassCastException e) {
220                fail(""+e);
221            }
222            //System.out.println("f = " + f);
223            assertTrue("f != null", f.list != null);
224            assertTrue("length( f ) = 4", f.list.size() == 4);
225    
226            ModLongRing fac = new ModLongRing(19);
227            TermOrder tord = new TermOrder(TermOrder.INVLEX);
228            String[] vars = new String[]{ "x", "y", "z" };
229            int nvar = vars.length;
230            pfac = new GenPolynomialRing<ModLong>(fac,nvar,tord,vars);
231            assertEquals("pfac == f.ring", pfac, f.ring );
232    
233            GenPolynomial<ModInteger> a = f.list.get(0);
234            //System.out.println("a = " + a);
235            assertTrue("isONE( f.get(0) )", a.isONE() );
236    
237            GenPolynomial<ModInteger> b = f.list.get(1);
238            //System.out.println("b = " + b);
239            assertTrue("isZERO( f.get(1) )", b.isZERO() );
240    
241            GenPolynomial<ModInteger> c = f.list.get(2);
242            //System.out.println("c = " + c);
243            assertTrue("isZERO( f.get(2) )", c.isZERO() );
244    
245            GenPolynomial<ModInteger> d = f.list.get(3);
246            //System.out.println("d = " + d);
247            assertEquals("f.get(3).length() == 2", 2, d.length() );
248        }
249    
250    
251        /**
252         * Test complex polynomial.
253         */
254        @SuppressWarnings("unchecked")
255        public void testBigComplex() {
256            String exam = "Complex(x,y,z) L "  
257                + "( "
258                + "( 1i0 ), "
259                + "( 0i0 ), "
260                + "( 3/4i2 - 6/8i2 ), "
261                + "( 1i0 x + x^3 + 1i3 y z - x^3 ) "
262                + " )";
263            source = new StringReader( exam );
264            parser = new GenPolynomialTokenizer( source );
265            PolynomialList<BigComplex> f = null;
266            try {
267                f = (PolynomialList<BigComplex>)parser.nextPolynomialSet();
268            } catch(IOException e) {
269                fail(""+e);
270            } catch (ClassCastException e) {
271                fail(""+e);
272            }
273            //System.out.println("f = " + f);
274            assertTrue("f != null", f.list != null);
275            assertTrue("length( f ) = 4", f.list.size() == 4);
276    
277            BigComplex fac = new BigComplex(0);
278            TermOrder tord = new TermOrder(TermOrder.INVLEX);
279            String[] vars = new String[]{ "x", "y", "z" };
280            int nvar = vars.length;
281            pfac = new GenPolynomialRing<BigComplex>(fac,nvar,tord,vars);
282            assertEquals("pfac == f.ring", pfac, f.ring );
283    
284    
285            GenPolynomial<BigComplex> a = f.list.get(0);
286            //System.out.println("a = " + a);
287            assertTrue("isONE( f.get(0) )", a.isONE() );
288    
289            GenPolynomial<BigComplex> b = f.list.get(1);
290            //System.out.println("b = " + b);
291            assertTrue("isZERO( f.get(1) )", b.isZERO() );
292    
293            GenPolynomial<BigComplex> c = f.list.get(2);
294            //System.out.println("c = " + c);
295            assertTrue("isZERO( f.get(2) )", c.isZERO() );
296    
297            GenPolynomial<BigComplex> d = f.list.get(3);
298            //System.out.println("d = " + d);
299            assertEquals("f.get(3).length() == 2", 2, d.length() );
300        }
301    
302    
303        /**
304         * Test decimal polynomial.
305         */
306        @SuppressWarnings("unchecked")
307        public void testBigDecimal() {
308            String exam = "D(x,y,z) L "  
309                + "( "
310                + "( 1 ), "
311                + "( 0 ), "
312                + "( 0.25 * 0.25 - 0.25^2 ), "
313                + "( 1 x + x^3 + 0.3333333333333333333333 y z - x^3 ) "
314                + " )";
315            source = new StringReader( exam );
316            parser = new GenPolynomialTokenizer( source );
317            PolynomialList<BigDecimal> f = null;
318            try {
319                f = (PolynomialList<BigDecimal>)parser.nextPolynomialSet();
320            } catch(IOException e) {
321                fail(""+e);
322            } catch (ClassCastException e) {
323                fail(""+e);
324            }
325            //System.out.println("f = " + f);
326            assertTrue("f != null", f.list != null);
327            assertTrue("length( f ) = 4", f.list.size() == 4);
328    
329            BigDecimal fac = new BigDecimal(0);
330            TermOrder tord = new TermOrder(TermOrder.INVLEX);
331            String[] vars = new String[]{ "x", "y", "z" };
332            int nvar = vars.length;
333            pfac = new GenPolynomialRing<BigDecimal>(fac,nvar,tord,vars);
334            assertEquals("pfac == f.ring", pfac, f.ring );
335    
336            GenPolynomial<BigDecimal> a = f.list.get(0);
337            //System.out.println("a = " + a);
338            assertTrue("isONE( f.get(0) )", a.isONE() );
339    
340            GenPolynomial<BigDecimal> b = f.list.get(1);
341            //System.out.println("b = " + b);
342            assertTrue("isZERO( f.get(1) )", b.isZERO() );
343    
344            GenPolynomial<BigDecimal> c = f.list.get(2);
345            //System.out.println("c = " + c);
346            assertTrue("isZERO( f.get(2) )", c.isZERO() );
347    
348            GenPolynomial<BigDecimal> d = f.list.get(3);
349            //System.out.println("d = " + d);
350            assertEquals("f.get(3).length() == 2", 2, d.length() );
351        }
352    
353    
354        /**
355         * Test quaternion polynomial.
356         */
357        @SuppressWarnings("unchecked")
358        public void testBigQuaternion() {
359            String exam = "Quat(x,y,z) L "  
360                + "( "
361                + "( 1i0j0k0 ), "
362                + "( 0i0j0k0 ), "
363                + "( 3/4i2j1k3 - 6/8i2j1k3 ), "
364                + "( 1 x + x^3 + 1i2j3k4 y z - x^3 ) "
365                + " )";
366            source = new StringReader( exam );
367            parser = new GenPolynomialTokenizer( source );
368            PolynomialList<BigQuaternion> f = null;
369            try {
370                f = (PolynomialList<BigQuaternion>)parser.nextPolynomialSet();
371            } catch(IOException e) {
372                fail(""+e);
373            } catch (ClassCastException e) {
374                fail(""+e);
375            }
376            //System.out.println("f = " + f);
377            assertTrue("f != null", f.list != null);
378            assertTrue("length( f ) = 4", f.list.size() == 4);
379    
380            BigQuaternion fac = new BigQuaternion(0);
381            TermOrder tord = new TermOrder(TermOrder.INVLEX);
382            String[] vars = new String[]{ "x", "y", "z" };
383            int nvar = vars.length;
384            pfac = new GenPolynomialRing<BigQuaternion>(fac,nvar,tord,vars);
385            assertEquals("pfac == f.ring", pfac, f.ring );
386    
387    
388            GenPolynomial<BigQuaternion> a = f.list.get(0);
389            //System.out.println("a = " + a);
390            assertTrue("isONE( f.get(0) )", a.isONE() );
391    
392            GenPolynomial<BigQuaternion> b = f.list.get(1);
393            //System.out.println("b = " + b);
394            assertTrue("isZERO( f.get(1) )", b.isZERO() );
395    
396            GenPolynomial<BigQuaternion> c = f.list.get(2);
397            //System.out.println("c = " + c);
398            assertTrue("isZERO( f.get(2) )", c.isZERO() );
399    
400            GenPolynomial<BigQuaternion> d = f.list.get(3);
401            //System.out.println("d = " + d);
402            assertEquals("f.get(3).length() == 2", 2, d.length() );
403        }
404    
405    
406        /**
407         * Test rational solvable polynomial.
408         */
409        @SuppressWarnings("unchecked")
410        public void testSolvableBigRational() {
411            String exam = "Rat(x,y,z) L "  
412                + "RelationTable "
413                + "( "
414                + " ( z ), ( y ), ( y z -1 ) "
415                + ") "
416                + "( "
417                + " ( 1 ), "
418                + " ( 0 ), "
419                + " ( 3/4 - 6/8 ), "
420                + " ( 1 x + x^3 + 1/3 y z - x^3 ) "
421                + " )";
422            source = new StringReader( exam );
423            parser = new GenPolynomialTokenizer( source );
424            PolynomialList<BigRational> f = null;
425            try {
426                f = (PolynomialList<BigRational>)parser.nextSolvablePolynomialSet();
427            } catch(IOException e) {
428                fail(""+e);
429            } catch (ClassCastException e) {
430                fail(""+e);
431            }
432            //System.out.println("f = " + f);
433            //System.out.println("f.ring.table = " + ((GenSolvablePolynomialRing)f.ring).table);
434            assertTrue("f != null", f.list != null);
435            assertTrue("length( f ) = 4", f.list.size() == 4);
436    
437            BigRational fac = new BigRational(0);
438            TermOrder tord = new TermOrder(TermOrder.INVLEX);
439            String[] vars = new String[]{ "x", "y", "z" };
440            int nvar = vars.length;
441            spfac = new GenSolvablePolynomialRing<BigRational>(fac,nvar,tord,vars);
442            assertEquals("spfac == f.ring", spfac, f.ring );
443            //System.out.println("spfac = " + spfac);
444            //System.out.println("spfac.table = " + spfac.table);
445    
446    
447            GenSolvablePolynomial<BigRational> a = f.castToSolvableList().get(0);
448            //System.out.println("a = " + a);
449            assertTrue("isZERO( f.get(0) )", a.isONE() );
450    
451            GenSolvablePolynomial<BigRational> b = f.castToSolvableList().get(1);
452            //System.out.println("b = " + b);
453            assertTrue("isZERO( f.get(1) )", b.isZERO() );
454    
455            GenSolvablePolynomial<BigRational> c = f.castToSolvableList().get(2);
456            //System.out.println("c = " + c);
457            assertTrue("isONE( f.get(2) )", c.isZERO() );
458    
459            GenSolvablePolynomial<BigRational> d = f.castToSolvableList().get(3);
460            //System.out.println("d = " + d);
461            assertEquals("f.get(3).length() == 2", 2, d.length() );
462        }
463    
464    
465        /**
466         * Test mod integer solvable polynomial.
467         */
468        @SuppressWarnings("unchecked")
469        public void testSolvableModInteger() {
470            String exam = "Mod 19 (x,y,z) L "  
471                + "RelationTable "
472                + "( "
473                + " ( z ), ( y ), ( y z -1 ) "
474                + ") "
475                + "( "
476                + "( 1 ), "
477                + "( 0 ), "
478                + "( 3 2 - 6 + 19 ), "
479                + "( 1 x + x^3 + 3 y z - x^3 ) "
480                + " )";
481            source = new StringReader( exam );
482            parser = new GenPolynomialTokenizer( source );
483            PolynomialList<ModInteger> f = null;
484            try {
485                f = (PolynomialList<ModInteger>)parser.nextSolvablePolynomialSet();
486            } catch(IOException e) {
487                fail(""+e);
488            } catch (ClassCastException e) {
489                fail(""+e);
490            }
491            //System.out.println("f = " + f);
492            //System.out.println("f.ring.table = " + ((GenSolvablePolynomialRing)f.ring).table);
493            assertTrue("f != null", f.list != null);
494            assertTrue("length( f ) = 4", f.list.size() == 4);
495    
496            ModLongRing fac = new ModLongRing(19);
497            TermOrder tord = new TermOrder(TermOrder.INVLEX);
498            String[] vars = new String[]{ "x", "y", "z" };
499            int nvar = vars.length;
500            spfac = new GenSolvablePolynomialRing<ModLong>(fac,nvar,tord,vars);
501            assertEquals("spfac == f.ring", spfac, f.ring );
502            //System.out.println("spfac = " + spfac);
503            //System.out.println("spfac.table = " + spfac.table);
504    
505    
506            GenSolvablePolynomial<ModInteger> a = f.castToSolvableList().get(0);
507            //System.out.println("a = " + a);
508            assertTrue("isZERO( f.get(0) )", a.isONE() );
509    
510            GenSolvablePolynomial<ModInteger> b = f.castToSolvableList().get(1);
511            //System.out.println("b = " + b);
512            assertTrue("isZERO( f.get(1) )", b.isZERO() );
513    
514            GenSolvablePolynomial<ModInteger> c = f.castToSolvableList().get(2);
515            //System.out.println("c = " + c);
516            assertTrue("isONE( f.get(2) )", c.isZERO() );
517    
518            GenSolvablePolynomial<ModInteger> d = f.castToSolvableList().get(3);
519            //System.out.println("d = " + d);
520            assertEquals("f.get(3).length() == 2", 2, d.length() );
521        }
522    
523    
524        /**
525         * Test integer polynomial module.
526         */
527        @SuppressWarnings("unchecked")
528        public void testBigIntegerModule() {
529            String exam = "Int(x,y,z) L "  
530                + "( "
531                + " ( "
532                + "  ( 1 ), "
533                + "  ( 0 ), "
534                + "  ( 3 2 - 6 ), "
535                + "  ( 1 x + x^3 + 3 y z - x^3 ) "
536                + " ), "
537                + " ( ( 1 ), ( 0 ) ) "
538                + ")";
539            source = new StringReader( exam );
540            parser = new GenPolynomialTokenizer( source );
541            ModuleList<BigInteger> m = null;
542            try {
543                m = (ModuleList<BigInteger>)parser.nextSubModuleSet();
544            } catch(IOException e) {
545                fail(""+e);
546            } catch (ClassCastException e) {
547                fail(""+e);
548            }
549            //System.out.println("m = " + m);
550            assertTrue("m != null", m.list != null);
551            assertTrue("length( m ) = 2", m.list.size() == 2);
552            assertTrue("length( m[0] ) = 4", ((List)m.list.get(0)).size() == 4);
553    
554    
555            BigInteger fac = new BigInteger(0);
556            TermOrder tord = new TermOrder(TermOrder.INVLEX);
557            String[] vars = new String[]{ "x", "y", "z" };
558            int nvar = vars.length;
559            pfac = new GenPolynomialRing<BigInteger>(fac,nvar,tord,vars);
560            assertEquals("pfac == m.ring", pfac, m.ring );
561    
562            List<List<GenPolynomial<BigInteger>>> rows = m.list;
563            List<GenPolynomial<BigInteger>> f;
564    
565            f = rows.get(0);
566            GenPolynomial<BigInteger> a = f.get(0);
567            //System.out.println("a = " + a);
568            assertTrue("isONE( f.get(0) )", a.isONE() );
569    
570            GenPolynomial<BigInteger> b = f.get(1);
571            //System.out.println("b = " + b);
572            assertTrue("isZERO( f.get(1) )", b.isZERO() );
573    
574            GenPolynomial<BigInteger> c = f.get(2);
575            //System.out.println("c = " + c);
576            assertTrue("isZERO( f.get(2) )", c.isZERO() );
577    
578            GenPolynomial<BigInteger> d = f.get(3);
579            //System.out.println("d = " + d);
580            assertEquals("f.get(3).length() == 2", 2, d.length() );
581    
582            f = rows.get(1);
583            assertTrue("length( f ) = 4", f.size() == 4);
584    
585            a = f.get(0);
586            //System.out.println("a = " + a);
587            assertTrue("isONE( f.get(0) )", a.isONE() );
588    
589            b = f.get(1);
590            //System.out.println("b = " + b);
591            assertTrue("isZERO( f.get(1) )", b.isZERO() );
592    
593            c = f.get(2);
594            //System.out.println("c = " + c);
595            assertTrue("isZERO( f.get(2) )", c.isZERO() );
596    
597            d = f.get(3);
598            //System.out.println("c = " + d);
599            assertTrue("isZERO( f.get(3) )", d.isZERO() );
600        }
601    
602    
603        /**
604         * Test rational solvable polynomial module.
605         */
606        @SuppressWarnings("unchecked")
607        public void testBigRationalSolvableModule() {
608            String exam = "Rat(x,y,z) L "  
609                + "RelationTable "
610                + "( "
611                + " ( z ), ( y ), ( y z -1 ) "
612                + ") "
613                + "( "
614                + " ( "
615                + "  ( 1 ), "
616                + "  ( 0 ), "
617                + "  ( 3/4 - 6/8 ), "
618                + "  ( 1 x + x^3 + 1/3 y z - x^3 ) "
619                + " ), "
620                + " ( ( x ), ( 1 ), ( 0 ) ) "
621                + " )";
622            source = new StringReader( exam );
623            parser = new GenPolynomialTokenizer( source );
624            ModuleList<BigRational> m = null;
625            try {
626                m = (ModuleList<BigRational>)parser.nextSolvableSubModuleSet();
627            } catch(IOException e) {
628                fail(""+e);
629            } catch (ClassCastException e) {
630                fail(""+e);
631            }
632            //System.out.println("m = " + m);
633            //System.out.println("m.ring = " + m.ring);
634            assertTrue("m != null", m.list != null);
635            assertTrue("length( m ) = 2", m.list.size() == 2);
636            assertTrue("length( m[0] ) = 4", ((List)m.list.get(0)).size() == 4);
637    
638            BigRational fac = new BigRational(0);
639            TermOrder tord = new TermOrder(TermOrder.INVLEX);
640            String[] vars = new String[]{ "x", "y", "z" };
641            int nvar = vars.length;
642            spfac = new GenSolvablePolynomialRing<BigRational>(fac,nvar,tord,vars);
643            assertEquals("spfac == m.ring", spfac, m.ring );
644    
645            List<List<GenSolvablePolynomial<BigRational>>> rows = m.castToSolvableList();
646            List<GenSolvablePolynomial<BigRational>> f;
647    
648            f = rows.get(0);
649            GenSolvablePolynomial<BigRational> a = f.get(0);
650            //System.out.println("a = " + a);
651            assertTrue("isONE( f.get(0) )", a.isONE() );
652    
653            GenSolvablePolynomial<BigRational> b = f.get(1);
654            //System.out.println("b = " + b);
655            assertTrue("isZERO( f.get(1) )", b.isZERO() );
656    
657            GenSolvablePolynomial<BigRational> c = f.get(2);
658            //System.out.println("c = " + c);
659            assertTrue("isZERO( f.get(2) )", c.isZERO() );
660    
661            GenSolvablePolynomial<BigRational> d = f.get(3);
662            //System.out.println("d = " + d);
663            assertEquals("f.get(3).length() == 2", 2, d.length() );
664    
665            f = rows.get(1);
666            assertTrue("length( f ) = 4", f.size() == 4);
667    
668            a = f.get(0);
669            //System.out.println("a = " + a);
670            assertTrue("!isONE( f.get(0) )", !a.isONE() );
671    
672            b = f.get(1);
673            //System.out.println("b = " + b);
674            assertTrue("isONE( f.get(1) )", b.isONE() );
675    
676            c = f.get(2);
677            //System.out.println("c = " + c);
678            assertTrue("isZERO( f.get(2) )", c.isZERO() );
679    
680            d = f.get(3);
681            //System.out.println("d = " + d);
682            assertTrue("isZERO( f.get(3) )", d.isZERO() );
683    
684        }
685    
686    
687        /**
688         * Test algebraic number polynomial.
689         * <b>Note: </b> Syntax no more supported.
690         */
691        @SuppressWarnings("unchecked")
692        public void removedTestAlgebraicNumber() {
693            String exam = "AN[ (i) ( i^2 + 1 ) ] (x,y,z) L "  
694                + "( "
695                + "( 1 ), "
696                + "( _i_ ), "
697                + "( 0 ), "
698                + "( _i^2_ + 1 ), "
699                + "( 1 x + x^3 + _3 i_ y z - x^3 ) "
700                + " )";
701            source = new StringReader( exam );
702            parser = new GenPolynomialTokenizer( source );
703            PolynomialList<AlgebraicNumber<BigRational>> f = null;
704            AlgebraicNumberRing<BigRational> fac = null;
705            try {
706                f = (PolynomialList<AlgebraicNumber<BigRational>>)parser.nextPolynomialSet();
707                fac = (AlgebraicNumberRing<BigRational>)f.ring.coFac;
708            } catch(IOException e) {
709                fail(""+e);
710            } catch (ClassCastException e) {
711                fail(""+e);
712            }
713            //System.out.println("f = " + f);
714            assertTrue("f != null", f.list != null);
715            assertTrue("length( f ) = 5", f.list.size() == 5);
716    
717            TermOrder tord = new TermOrder(TermOrder.INVLEX);
718            String[] vars = new String[]{ "x", "y", "z" };
719            int nvar = vars.length;
720            pfac = new GenPolynomialRing<AlgebraicNumber<BigRational>>(fac,nvar,tord,vars);
721            assertEquals("pfac == f.ring", pfac, f.ring );
722    
723            GenPolynomial<AlgebraicNumber<BigRational>> a = f.list.get(0);
724            //System.out.println("a = " + a);
725            assertTrue("isONE( f.get(0) )", a.isONE() );
726    
727            GenPolynomial<AlgebraicNumber<BigRational>> b = f.list.get(1);
728            //System.out.println("b = " + b);
729            assertTrue("isUnit( f.get(1) )", b.isUnit() );
730    
731            b = b.monic();
732            //System.out.println("b = " + b);
733            assertTrue("isUnit( f.get(1) )", b.isONE() );
734    
735            GenPolynomial<AlgebraicNumber<BigRational>> c = f.list.get(2);
736            //System.out.println("c = " + c);
737            assertTrue("isZERO( f.get(1) )", c.isZERO() );
738    
739            GenPolynomial<AlgebraicNumber<BigRational>> d = f.list.get(3);
740            //System.out.println("d = " + d);
741            assertTrue("isZERO( f.get(2) )", d.isZERO() );
742    
743            GenPolynomial<AlgebraicNumber<BigRational>> e = f.list.get(4);
744            //System.out.println("e = " + e);
745            assertEquals("f.get(3).length() == 2", 2, e.length() );
746        }
747    
748    
749        /**
750         * Test Galois field coefficient polynomial.
751         * <b>Note: </b> Syntax no more supported.
752         */
753        @SuppressWarnings("unchecked")
754        public void removedTestGaloisField() {
755            String exam = "AN[ 19 (i) ( i^2 + 1 ) ] (x,y,z) L "  
756                + "( "
757                + "( 20 ), "
758                + "( _i_ ), "
759                + "( 0 ), "
760                + "( _i^2_ + 20 ), "
761                + "( 1 x + x^3 + _3 i_ y z - x^3 ) "
762                + " )";
763            source = new StringReader( exam );
764            parser = new GenPolynomialTokenizer( source );
765            PolynomialList<AlgebraicNumber<ModInteger>> f = null;
766            AlgebraicNumberRing<ModInteger> fac = null;
767            try {
768                f = (PolynomialList<AlgebraicNumber<ModInteger>>)parser.nextPolynomialSet();
769                fac = (AlgebraicNumberRing<ModInteger>)f.ring.coFac;
770            } catch(IOException e) {
771                fail(""+e);
772            } catch (ClassCastException e) {
773                fail(""+e);
774            }
775            //System.out.println("f = " + f);
776            assertTrue("f != null", f.list != null);
777            assertTrue("length( f ) = 5", f.list.size() == 5);
778    
779            TermOrder tord = new TermOrder(TermOrder.INVLEX);
780            String[] vars = new String[]{ "x", "y", "z" };
781            int nvar = vars.length;
782            pfac = new GenPolynomialRing<AlgebraicNumber<ModInteger>>(fac,nvar,tord,vars);
783            assertEquals("pfac == f.ring", pfac, f.ring );
784    
785            GenPolynomial<AlgebraicNumber<ModInteger>> a = f.list.get(0);
786            //System.out.println("a = " + a);
787            assertTrue("isONE( f.get(0) )", a.isONE() );
788    
789            GenPolynomial<AlgebraicNumber<ModInteger>> b = f.list.get(1);
790            //System.out.println("b = " + b);
791            assertTrue("isUnit( f.get(1) )", b.isUnit() );
792    
793            b = b.monic();
794            //System.out.println("b = " + b);
795            assertTrue("isUnit( f.get(1) )", b.isONE() );
796    
797            GenPolynomial<AlgebraicNumber<ModInteger>> c = f.list.get(2);
798            //System.out.println("c = " + c);
799            assertTrue("isZERO( f.get(1) )", c.isZERO() );
800    
801            GenPolynomial<AlgebraicNumber<ModInteger>> d = f.list.get(3);
802            //System.out.println("d = " + d);
803            assertTrue("isZERO( f.get(2) )", d.isZERO() );
804    
805            GenPolynomial<AlgebraicNumber<ModInteger>> e = f.list.get(4);
806            //System.out.println("e = " + e);
807            assertEquals("f.get(3).length() == 2", 2, e.length() );
808        }
809    
810    
811        /**
812         * Test algebraic number polynomial with braces.
813         */
814        @SuppressWarnings("unchecked")
815        public void testAlgebraicNumberBrace() {
816            String exam = "AN[ (i) ( i^2 + 1 ) ] (x,y,z) L "  
817                + "( "
818                + "( 1 ), "
819                + "( { i } ), "
820                + "( 0 ), "
821                + "( { i^2 } + 1 ), "
822                + "( 1 x + x^3 + { 3 i }^2  y z - x^3 ) "
823                + " )";
824            source = new StringReader( exam );
825            parser = new GenPolynomialTokenizer( source );
826            PolynomialList<AlgebraicNumber<BigRational>> f = null;
827            AlgebraicNumberRing<BigRational> fac = null;
828            try {
829                f = (PolynomialList<AlgebraicNumber<BigRational>>)parser.nextPolynomialSet();
830                fac = (AlgebraicNumberRing<BigRational>)f.ring.coFac;
831            } catch(IOException e) {
832                fail(""+e);
833            } catch (ClassCastException e) {
834                fail(""+e);
835            }
836            //System.out.println("f = " + f);
837            assertTrue("f != null", f.list != null);
838            assertTrue("length( f ) = 5", f.list.size() == 5);
839    
840            TermOrder tord = new TermOrder(TermOrder.INVLEX);
841            String[] vars = new String[]{ "x", "y", "z" };
842            int nvar = vars.length;
843            pfac = new GenPolynomialRing<AlgebraicNumber<BigRational>>(fac,nvar,tord,vars);
844            assertEquals("pfac == f.ring", pfac, f.ring );
845    
846            GenPolynomial<AlgebraicNumber<BigRational>> a = f.list.get(0);
847            //System.out.println("a = " + a);
848            assertTrue("isONE( f.get(0) )", a.isONE() );
849    
850            GenPolynomial<AlgebraicNumber<BigRational>> b = f.list.get(1);
851            //System.out.println("b = " + b);
852            assertTrue("isUnit( f.get(1) )", b.isUnit() );
853    
854            b = b.monic();
855            //System.out.println("b = " + b);
856            assertTrue("isUnit( f.get(1) )", b.isONE() );
857    
858            GenPolynomial<AlgebraicNumber<BigRational>> c = f.list.get(2);
859            //System.out.println("c = " + c);
860            assertTrue("isZERO( f.get(1) )", c.isZERO() );
861    
862            GenPolynomial<AlgebraicNumber<BigRational>> d = f.list.get(3);
863            //System.out.println("d = " + d);
864            assertTrue("isZERO( f.get(2) )", d.isZERO() );
865    
866            GenPolynomial<AlgebraicNumber<BigRational>> e = f.list.get(4);
867            //System.out.println("e = " + e);
868            assertEquals("f.get(3).length() == 2", 2, e.length() );
869        }
870    
871    
872        /**
873         * Test Galois field coefficient polynomial with braces.
874         */
875        @SuppressWarnings("unchecked")
876        public void testGaloisFieldBrace() {
877            String exam = "AN[ 19 (i) ( i^2 + 1 ) ] (x,y,z) L "  
878                + "( "
879                + "( 20 ), "
880                + "( { i } ), "
881                + "( 0 ), "
882                + "( { i^2 } + 20 ), "
883                + "( 1 x + x^3 + { 3 i }^3 y z + { -1 }^3 x^3 ) "
884                + " )";
885            source = new StringReader( exam );
886            parser = new GenPolynomialTokenizer( source );
887            PolynomialList<AlgebraicNumber<ModInteger>> f = null;
888            AlgebraicNumberRing<ModInteger> fac = null;
889            try {
890                f = (PolynomialList<AlgebraicNumber<ModInteger>>)parser.nextPolynomialSet();
891                fac = (AlgebraicNumberRing<ModInteger>)f.ring.coFac;
892            } catch(IOException e) {
893                fail(""+e);
894            } catch (ClassCastException e) {
895                fail(""+e);
896            }
897            //System.out.println("f = " + f);
898            assertTrue("f != null", f.list != null);
899            assertTrue("length( f ) = 5", f.list.size() == 5);
900    
901            TermOrder tord = new TermOrder(TermOrder.INVLEX);
902            String[] vars = new String[]{ "x", "y", "z" };
903            int nvar = vars.length;
904            pfac = new GenPolynomialRing<AlgebraicNumber<ModInteger>>(fac,nvar,tord,vars);
905            assertEquals("pfac == f.ring", pfac, f.ring );
906    
907            GenPolynomial<AlgebraicNumber<ModInteger>> a = f.list.get(0);
908            //System.out.println("a = " + a);
909            assertTrue("isONE( f.get(0) )", a.isONE() );
910    
911            GenPolynomial<AlgebraicNumber<ModInteger>> b = f.list.get(1);
912            //System.out.println("b = " + b);
913            assertTrue("isUnit( f.get(1) )", b.isUnit() );
914    
915            b = b.monic();
916            //System.out.println("b = " + b);
917            assertTrue("isUnit( f.get(1) )", b.isONE() );
918    
919            GenPolynomial<AlgebraicNumber<ModInteger>> c = f.list.get(2);
920            //System.out.println("c = " + c);
921            assertTrue("isZERO( f.get(1) )", c.isZERO() );
922    
923            GenPolynomial<AlgebraicNumber<ModInteger>> d = f.list.get(3);
924            //System.out.println("d = " + d);
925            assertTrue("isZERO( f.get(2) )", d.isZERO() );
926    
927            GenPolynomial<AlgebraicNumber<ModInteger>> e = f.list.get(4);
928            //System.out.println("e = " + e);
929            assertEquals("f.get(3).length() == 2", 2, e.length() );
930        }
931    
932    
933        /**
934         * Test Galois field coefficient polynomial without braces.
935         */
936        @SuppressWarnings("unchecked")
937        public void testGaloisFieldWoBrace() {
938            String exam = "AN[ 19 (i) ( i^2 + 1 ) ] (x,y,z) L "  
939                + "( "
940                + "( 20 ), "
941                + "( i ), "
942                + "( 0 ), "
943                + "( i^2 + 20 ), "
944                + "( 1 x + x^3 + 3^3 i^3 y z - (x)^3 ) "
945                + " )";
946            source = new StringReader( exam );
947            parser = new GenPolynomialTokenizer( source );
948            PolynomialList<AlgebraicNumber<ModInteger>> f = null;
949            AlgebraicNumberRing<ModInteger> fac = null;
950            try {
951                f = (PolynomialList<AlgebraicNumber<ModInteger>>)parser.nextPolynomialSet();
952                fac = (AlgebraicNumberRing<ModInteger>)f.ring.coFac;
953            } catch(IOException e) {
954                fail(""+e);
955            } catch (ClassCastException e) {
956                fail(""+e);
957            }
958            //System.out.println("f = " + f);
959            assertTrue("f != null", f.list != null);
960            assertTrue("length( f ) = 5", f.list.size() == 5);
961    
962            TermOrder tord = new TermOrder(TermOrder.INVLEX);
963            String[] vars = new String[]{ "x", "y", "z" };
964            int nvar = vars.length;
965            pfac = new GenPolynomialRing<AlgebraicNumber<ModInteger>>(fac,nvar,tord,vars);
966            assertEquals("pfac == f.ring", pfac, f.ring );
967    
968            GenPolynomial<AlgebraicNumber<ModInteger>> a = f.list.get(0);
969            //System.out.println("a = " + a);
970            assertTrue("isONE( f.get(0) )", a.isONE() );
971    
972            GenPolynomial<AlgebraicNumber<ModInteger>> b = f.list.get(1);
973            //System.out.println("b = " + b);
974            assertTrue("isUnit( f.get(1) )", b.isUnit() );
975    
976            b = b.monic();
977            //System.out.println("b = " + b);
978            assertTrue("isUnit( f.get(1) )", b.isONE() );
979    
980            GenPolynomial<AlgebraicNumber<ModInteger>> c = f.list.get(2);
981            //System.out.println("c = " + c);
982            assertTrue("isZERO( f.get(1) )", c.isZERO() );
983    
984            GenPolynomial<AlgebraicNumber<ModInteger>> d = f.list.get(3);
985            //System.out.println("d = " + d);
986            assertTrue("isZERO( f.get(2) )", d.isZERO() );
987    
988            GenPolynomial<AlgebraicNumber<ModInteger>> e = f.list.get(4);
989            //System.out.println("e = " + e);
990            assertEquals("f.get(3).length() == 2", 2, e.length() );
991        }
992    
993    
994        /**
995         * Test rational polynomial with generic coefficients.
996         */
997        @SuppressWarnings("unchecked")
998        public void testBigRationalGeneric() {
999            String exam = "Rat(x,y,z) L "  
1000                + "( "
1001                + "( 1^3 ), "
1002                + "( 0^3 ), "
1003                + "( { 3/4 }^2 - 6/8^2 ), "
1004                + "( { 1 }^2 x + x^3 + 1/3 y z - x^3 ), "
1005                + "( 1.0001 - 0.0001 + { 0.25 }**2 - 1/4^2 ) "
1006                + " )";
1007            source = new StringReader( exam );
1008            parser = new GenPolynomialTokenizer( source );
1009            PolynomialList<BigRational> f = null;
1010            try {
1011                f = (PolynomialList<BigRational>)parser.nextPolynomialSet();
1012            } catch(IOException e) {
1013                fail(""+e);
1014            } catch (ClassCastException e) {
1015                fail(""+e);
1016            }
1017            //System.out.println("f = " + f);
1018            assertTrue("f != null", f.list != null);
1019            assertTrue("length( f ) = 5", f.list.size() == 5);
1020    
1021            BigRational fac = new BigRational(0);
1022            TermOrder tord = new TermOrder(TermOrder.INVLEX);
1023            String[] vars = new String[]{ "x", "y", "z" };
1024            int nvar = vars.length;
1025            pfac = new GenPolynomialRing<BigRational>(fac,nvar,tord,vars);
1026            assertEquals("pfac == f.ring", pfac, f.ring );
1027    
1028    
1029            GenPolynomial<BigRational> a = f.list.get(0);
1030            //System.out.println("a = " + a);
1031            assertTrue("isONE( f.get(0) )", a.isONE() );
1032    
1033            GenPolynomial<BigRational> b = f.list.get(1);
1034            //System.out.println("b = " + b);
1035            assertTrue("isZERO( f.get(1) )", b.isZERO() );
1036    
1037            GenPolynomial<BigRational> c = f.list.get(2);
1038            //System.out.println("c = " + c);
1039            assertTrue("isZERO( f.get(2) )", c.isZERO() );
1040    
1041            GenPolynomial<BigRational> d = f.list.get(3);
1042            //System.out.println("d = " + d);
1043            assertEquals("f.get(3).length() == 2", 2, d.length() );
1044    
1045            GenPolynomial<BigRational> e = f.list.get(4);
1046            //System.out.println("e = " + e);
1047            assertTrue("isONE( f.get(4) )", e.isONE() );
1048        }
1049    
1050    
1051        /**
1052         * Test rational polynomial with errors.
1053         */
1054        @SuppressWarnings("unchecked")
1055        public void testBigRationalErorr() {
1056            // brace mismatch
1057            String exam = "Rat(x,y,z) L "  
1058                + "( "
1059                + "( { 3 ), "
1060                + " )";
1061            source = new StringReader( exam );
1062            parser = new GenPolynomialTokenizer( source );
1063            PolynomialList<BigRational> f = null;
1064            try {
1065                f = (PolynomialList<BigRational>)parser.nextPolynomialSet();
1066            } catch(IOException e) {
1067                fail(""+e);
1068            } catch (ClassCastException e) {
1069                fail(""+e);
1070            } catch (InvalidExpressionException e) {
1071                // pass
1072            }
1073    
1074            // brace mismatch
1075            exam = "Rat(x,y,z) L "  
1076                + "( "
1077                + "( 3 } ), "
1078                + " )";
1079            source = new StringReader( exam );
1080            parser = new GenPolynomialTokenizer( source );
1081            f = null;
1082            try {
1083                f = (PolynomialList<BigRational>)parser.nextPolynomialSet();
1084            } catch(IOException e) {
1085                fail(""+e);
1086            } catch (ClassCastException e) {
1087                fail(""+e);
1088            } catch (InvalidExpressionException e) {
1089                // pass
1090            }
1091    
1092            // invalid nesting
1093            exam = "Rat(x,y,z) L "  
1094                + "( "
1095                + "( { x } ), "
1096                + " )";
1097            source = new StringReader( exam );
1098            parser = new GenPolynomialTokenizer( source );
1099            f = null;
1100            try {
1101                f = (PolynomialList<BigRational>)parser.nextPolynomialSet();
1102            } catch(IOException e) {
1103                fail(""+e);
1104            } catch (ClassCastException e) {
1105                fail(""+e);
1106            } catch (InvalidExpressionException e) {
1107                // pass
1108            }
1109    
1110            // unknown varaible
1111            exam = "Rat(x,y,z) L "  
1112                + "( "
1113                + "( w ), "
1114                + " )";
1115            source = new StringReader( exam );
1116            parser = new GenPolynomialTokenizer( source );
1117            f = null;
1118            try {
1119                f = (PolynomialList<BigRational>)parser.nextPolynomialSet();
1120            } catch(IOException e) {
1121                fail(""+e);
1122            } catch (ClassCastException e) {
1123                fail(""+e);
1124            } catch (InvalidExpressionException e) {
1125                // pass
1126            }
1127        }
1128    
1129    
1130        /**
1131         * Test variables.
1132         */
1133        public void testVariables() {
1134            String vars = "a,b,c,d,e";
1135            String[] variables = GenPolynomialTokenizer.variableList(vars);
1136            assertTrue("len == 5: ", variables.length == 5);
1137    
1138            String expr = "a,b,c,d,e";
1139            variables = GenPolynomialTokenizer.expressionVariables(expr);
1140            //System.out.println("variables = " + Arrays.toString(variables) + ", len = " + variables.length);
1141            assertTrue("len == 5: ", variables.length == 5);
1142    
1143            expr = "b,c,d,e*a,b,c,d";
1144            variables = GenPolynomialTokenizer.expressionVariables(expr);
1145            //System.out.println("variables = " + Arrays.toString(variables) + ", len = " + variables.length);
1146            assertTrue("len == 5: ", variables.length == 5);
1147    
1148            expr = "b + c^3 - d + e*a - b/c +d";
1149            variables = GenPolynomialTokenizer.expressionVariables(expr);
1150            //System.out.println("variables = " + Arrays.toString(variables) + ", len = " + variables.length);
1151            assertTrue("len == 5: ", variables.length == 5);
1152    
1153            expr = "(b + c)^3 - { d + e*a } / [ b/c + d ] + (b + 3f + f*3 + f3)";
1154            variables = GenPolynomialTokenizer.expressionVariables(expr);
1155            //System.out.println("variables = " + Arrays.toString(variables) + ", len = " + variables.length);
1156            assertTrue("len == 7: ", variables.length == 7);
1157        }
1158    
1159    }