001    /*
002     * $Id: GroebnerBaseSeqPairSeqTest.java 2412 2009-02-07 12:17:54Z kredel $
003     */
004    
005    package edu.jas.gb;
006    
007    
008    import java.util.List;
009    import java.util.ArrayList;
010    import java.io.IOException;
011    import java.io.Reader;
012    import java.io.StringReader;
013    
014    import junit.framework.Test;
015    import junit.framework.TestCase;
016    import junit.framework.TestSuite;
017    
018    import org.apache.log4j.BasicConfigurator;
019    //import org.apache.log4j.Logger;
020    
021    import edu.jas.arith.BigRational;
022    import edu.jas.gb.ExtendedGB;
023    import edu.jas.gb.GroebnerBase;
024    import edu.jas.gb.GroebnerBaseSeqPairSeq;
025    import edu.jas.poly.GenPolynomial;
026    import edu.jas.poly.GenPolynomialRing;
027    import edu.jas.poly.GenPolynomialTokenizer;
028    import edu.jas.poly.PolynomialList;
029    
030    
031    
032    
033    /**
034     * Groebner base sequential, sequential pair list, tests with JUnit.
035     * @author Heinz Kredel.
036     */
037    
038    public class GroebnerBaseSeqPairSeqTest extends TestCase {
039    
040        //private static final Logger logger = Logger.getLogger(GroebnerBaseSeqPairSeqTest.class);
041    
042    /**
043     * main
044     */
045       public static void main (String[] args) {
046              BasicConfigurator.configure();
047              junit.textui.TestRunner.run( suite() );
048       }
049    
050    /**
051     * Constructs a <CODE>GroebnerBaseSeqPairSeqTest</CODE> object.
052     * @param name String.
053     */
054       public GroebnerBaseSeqPairSeqTest(String name) {
055              super(name);
056       }
057    
058    /**
059     * suite.
060     */ 
061     public static Test suite() {
062         TestSuite suite= new TestSuite(GroebnerBaseSeqPairSeqTest.class);
063         return suite;
064       }
065    
066       GenPolynomialRing<BigRational> fac;
067    
068       List<GenPolynomial<BigRational>> L;
069       PolynomialList<BigRational> F;
070       List<GenPolynomial<BigRational>> G;
071    
072       GroebnerBase<BigRational> bb; // do interface
073    
074       GenPolynomial<BigRational> a;
075       GenPolynomial<BigRational> b;
076       GenPolynomial<BigRational> c;
077       GenPolynomial<BigRational> d;
078       GenPolynomial<BigRational> e;
079    
080       int rl = 3; //4; //3; 
081       int kl = 2; // 10;
082       int ll = 5; //7;
083       int el = 3;
084       float q = 0.3f; //0.2f; //0.4f
085    
086       protected void setUp() {
087           BigRational coeff = new BigRational(9);
088           fac = new GenPolynomialRing<BigRational>(coeff,rl);
089           a = b = c = d = e = null;
090           bb = new GroebnerBaseSeqPairSeq<BigRational>();
091       }
092    
093       protected void tearDown() {
094           a = b = c = d = e = null;
095           fac = null;
096           bb = null;
097       }
098    
099    
100    /**
101     * Test sequential GBase.
102     * 
103     */
104     public void testSeqPairSequentialGBase() {
105    
106         L = new ArrayList<GenPolynomial<BigRational>>();
107    
108         a = fac.random(kl, ll, el, q );
109         b = fac.random(kl, ll, el, q );
110         c = fac.random(kl, ll, el, q );
111         d = fac.random(kl, ll, el, q );
112         e = d; //fac.random(kl, ll, el, q );
113    
114         if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
115            return;
116         }
117    
118         assertTrue("not isZERO( a )", !a.isZERO() );
119         L.add(a);
120    
121         L = bb.GB( L );
122         assertTrue("isGB( { a } )", bb.isGB(L) );
123    
124         assertTrue("not isZERO( b )", !b.isZERO() );
125         L.add(b);
126         //System.out.println("L = " + L.size() );
127    
128         L = bb.GB( L );
129         assertTrue("isGB( { a, b } )", bb.isGB(L) );
130    
131         assertTrue("not isZERO( c )", !c.isZERO() );
132         L.add(c);
133    
134         L = bb.GB( L );
135         assertTrue("isGB( { a, b, c } )", bb.isGB(L) );
136    
137         assertTrue("not isZERO( d )", !d.isZERO() );
138         L.add(d);
139    
140         L = bb.GB( L );
141         assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) );
142    
143         assertTrue("not isZERO( e )", !e.isZERO() );
144         L.add(e);
145    
146         L = bb.GB( L );
147         assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L) );
148     }
149    
150    
151    /**
152     * Test Trinks7 GBase.
153     * 
154     */
155     @SuppressWarnings("unchecked") // not jet working
156     public void testTrinks7GBase() {
157         String exam = "(B,S,T,Z,P,W) L "
158                     + "( "  
159                     + "( 45 P + 35 S - 165 B - 36 ), " 
160                     + "( 35 P + 40 Z + 25 T - 27 S ), "
161                     + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
162                     + "( - 9 W + 15 T P + 20 S Z ), "
163                     + "( P W + 2 T Z - 11 B**3 ), "
164                     + "( 99 W - 11 B S + 3 B**2 ), "
165                     + "( B**2 + 33/50 B + 2673/10000 ) "
166                     + ") ";
167         Reader source = new StringReader( exam );
168         GenPolynomialTokenizer parser
169                      = new GenPolynomialTokenizer( source );
170         try {
171             F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
172         } catch(ClassCastException e) {
173             fail(""+e);
174         } catch(IOException e) {
175             fail(""+e);
176         }
177         //System.out.println("F = " + F);
178    
179         G = bb.GB(F.list);
180         assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
181         assertEquals("#GB(Trinks7) == 6", 6, G.size() );
182         PolynomialList<BigRational> trinks 
183               = new PolynomialList<BigRational>(F.ring,G);
184         //System.out.println("G = " + trinks);
185    
186     }
187    
188    
189    /**
190     * Test sequential extended GBase.
191     * 
192     */
193     public void testSeqPairSequentialExtendedGBase() {
194    
195         L = new ArrayList<GenPolynomial<BigRational>>();
196    
197         ExtendedGB<BigRational> exgb;
198    
199         a = fac.random(kl, ll, el, q );
200         b = fac.random(kl, ll, el, q );
201         c = fac.random(kl, ll, el, q );
202         d = fac.random(kl, ll, el, q );
203         e = d; //fac.random(kl, ll, el, q );
204    
205         if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
206            return;
207         }
208    
209         assertTrue("not isZERO( a )", !a.isZERO() );
210         L.add(a);
211         //System.out.println("L = " + L );
212    
213         exgb = bb.extGB( L );
214         // System.out.println("exgb = " + exgb );
215         assertTrue("isGB( { a } )", bb.isGB(exgb.G) );
216         assertTrue("isRmat( { a } )", bb.isReductionMatrix(exgb) );
217    
218         assertTrue("not isZERO( b )", !b.isZERO() );
219         L.add(b);
220         //System.out.println("L = " + L );
221    
222         exgb = bb.extGB( L );
223         //System.out.println("exgb = " + exgb );
224         assertTrue("isGB( { a, b } )", bb.isGB(exgb.G) );
225         assertTrue("isRmat( { a, b } )", bb.isReductionMatrix(exgb) );
226    
227         assertTrue("not isZERO( c )", !c.isZERO() );
228         L.add(c);
229    
230         exgb = bb.extGB( L );
231         //System.out.println("exgb = " + exgb );
232         assertTrue("isGB( { a, b, c } )", bb.isGB(exgb.G) );
233         assertTrue("isRmat( { a, b, c } )", bb.isReductionMatrix(exgb) );
234    
235         assertTrue("not isZERO( d )", !d.isZERO() );
236         L.add(d);
237    
238         exgb = bb.extGB( L );
239         //System.out.println("exgb = " + exgb );
240         assertTrue("isGB( { a, b, c, d } )", bb.isGB(exgb.G) );
241         assertTrue("isRmat( { a, b, c, d } )", bb.isReductionMatrix(exgb) );
242    
243    
244         assertTrue("not isZERO( e )", !e.isZERO() );
245         L.add(e);
246    
247         exgb = bb.extGB( L );
248         //System.out.println("exgb = " + exgb );
249         assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(exgb.G) );
250         assertTrue("isRmat( { a, b, c, d, e } )", bb.isReductionMatrix(exgb) );
251     }
252    
253    
254    /**
255     * Test Trinks7 GBase.
256     * 
257     */
258     @SuppressWarnings("unchecked") // not jet working
259     public void testTrinks7ExtendedGBase() {
260         String exam = "(B,S,T,Z,P,W) L "
261                     + "( "  
262                     + "( 45 P + 35 S - 165 B - 36 ), " 
263                     + "( 35 P + 40 Z + 25 T - 27 S ), "
264                     + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
265                     + "( - 9 W + 15 T P + 20 S Z ), "
266                     + "( P W + 2 T Z - 11 B**3 ), "
267                     + "( 99 W - 11 B S + 3 B**2 ), "
268                     + "( B**2 + 33/50 B + 2673/10000 ) "
269                     + ") ";
270         Reader source = new StringReader( exam );
271         GenPolynomialTokenizer parser
272                      = 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    
283         ExtendedGB<BigRational> exgb;
284         
285         exgb = bb.extGB(F.list);
286         //System.out.println("exgb = " + exgb );
287         assertTrue("isGB( GB(Trinks7) )", bb.isGB(exgb.G) );
288         //assertEquals("#GB(Trinks7) == 6", 6, exgb.G.size() );
289         assertTrue("isRmat( GB(Trinks7) )", bb.isReductionMatrix(exgb) );
290         PolynomialList<BigRational> trinks 
291               = new PolynomialList<BigRational>(F.ring,G);
292         //System.out.println("G = " + trinks);
293    
294     }
295    
296    }