001    /*
002     * $Id: DGroebnerBaseSeqTest.java 3267 2010-08-15 11:03:00Z 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 org.apache.log4j.Logger;
015    import org.apache.log4j.BasicConfigurator;
016    
017    import junit.framework.Test;
018    import junit.framework.TestCase;
019    import junit.framework.TestSuite;
020    
021    import edu.jas.gb.GroebnerBase;
022    import edu.jas.kern.ComputerThreads;
023    
024    import edu.jas.arith.BigRational;
025    import edu.jas.arith.BigInteger;
026    
027    import edu.jas.poly.GenPolynomial;
028    import edu.jas.poly.GenPolynomialRing;
029    import edu.jas.poly.GenPolynomialTokenizer;
030    import edu.jas.poly.PolynomialList;
031    import edu.jas.poly.PolyUtil;
032    
033    
034    /**
035     * DGroebner base sequential tests with JUnit.
036     * @author Heinz Kredel.
037     */
038    
039    public class DGroebnerBaseSeqTest extends TestCase {
040    
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>DGroebnerBaseSeqTest</CODE> object.
052     * @param name String.
053     */
054       public DGroebnerBaseSeqTest(String name) {
055              super(name);
056       }
057    
058    /**
059     * suite.
060     */ 
061     public static Test suite() {
062         TestSuite suite= new TestSuite(DGroebnerBaseSeqTest.class);
063         return suite;
064       }
065    
066       GenPolynomialRing<BigInteger> fac;
067    
068       List<GenPolynomial<BigInteger>> L;
069       PolynomialList<BigInteger> F;
070       List<GenPolynomial<BigInteger>> G;
071    
072       GroebnerBase<BigInteger> bb;
073    
074       GenPolynomial<BigInteger> a;
075       GenPolynomial<BigInteger> b;
076       GenPolynomial<BigInteger> c;
077       GenPolynomial<BigInteger> d;
078       GenPolynomial<BigInteger> e;
079    
080       int rl = 3; //4; //3; 
081       int kl = 4; //4; 10
082       int ll = 4;
083       int el = 3;
084       float q = 0.2f; //0.4f
085    
086       protected void setUp() {
087           BigInteger coeff = new BigInteger(9);
088           fac = new GenPolynomialRing<BigInteger>(coeff,rl);
089           a = b = c = d = e = null;
090           bb = new DGroebnerBaseSeq<BigInteger>();
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 testSequentialGBase() {
105    
106         L = new ArrayList<GenPolynomial<BigInteger>>();
107    
108         a = fac.random(kl, ll, el, q ); //.abs();
109         b = fac.random(kl, ll, el, q ); //.abs();
110         c = fac.random(kl, ll/2, el, q ); //.abs();
111         d = fac.random(kl, ll/2, el, q ); //.abs();
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         L.add(a);
119         //System.out.println("    L  = " + L );
120         L = bb.GB( L );
121         //System.out.println("dGB(L) = " + L );
122         assertTrue("isGB( { a } )", bb.isGB(L) );
123    
124         L.add(b);
125         //System.out.println("    L  = " + L );
126         L = bb.GB( L );
127         //System.out.println("dGB(L) = " + L );
128         assertTrue("isGB( { a, b } )", bb.isGB(L) );
129    
130         L.add(c);
131         //System.out.println("    L  = " + L );
132         L = bb.GB( L );
133         //System.out.println("dGB(L) = " + L );
134         assertTrue("isGB( { a, b, c } )", bb.isGB(L) );
135    
136         L.add(d);
137         //System.out.println("    L  = " + L );
138         L = bb.GB( L );
139         //System.out.println("dGB(L) = " + L );
140         assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) );
141    
142         L.add(e);
143         //System.out.println("    L  = " + L );
144         L = bb.GB( L );
145         //System.out.println("dGB(L) = " + L );
146         assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L) );
147     }
148    
149    
150    /**
151     * Test Trinks7 GBase over Z.
152     * 
153     */ 
154     @SuppressWarnings("unchecked") // needs to long
155     public void xtestTrinks7GBaseZ() {
156         String exam = "Z(B,S,T,Z,P,W) L "
157                     + "( "  
158                     + "( 45 P + 35 S - 165 B - 36 ), " 
159                     + "( 35 P + 40 Z + 25 T - 27 S ), "
160                     + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
161                     + "( - 9 W + 15 T P + 20 S Z ), "
162                     + "( P W + 2 T Z - 11 B**3 ), "
163                     + "( 99 W - 11 B S + 3 B**2 ), "
164                     + "( 10000 B**2 + 6600 B + 2673 ) "
165                     + ") ";
166         Reader source = new StringReader( exam );
167         GenPolynomialTokenizer parser
168                      = new GenPolynomialTokenizer( source );
169         try {
170             F = (PolynomialList<BigInteger>) parser.nextPolynomialSet();
171         } catch(ClassCastException e) {
172             fail(""+e);
173         } catch(IOException e) {
174             fail(""+e);
175         }
176         System.out.println("F = " + F);
177    
178         G = bb.GB(F.list);
179         PolynomialList<BigInteger> trinks 
180               = new PolynomialList<BigInteger>(F.ring,G);
181         System.out.println("G = " + trinks);
182         System.out.println("G.size() = " + G.size());
183         assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
184         //assertEquals("#GB(Trinks7) == 6", 6, G.size() );
185     }
186    
187    
188    /**
189     * Test Trinks7 GBase over Z(B).
190     * 
191     */ 
192     @SuppressWarnings("unchecked") 
193     public void xtestTrinks7GBaseZ_B() {
194         String exam = "IntFunc{ B } (S,T,Z,P,W) G "
195                     + "( "  
196                     + "( { 45 } P + { 35 } S - { 165 B } - { 36 } ), " 
197                     + "( { 35 } P + { 40 } Z + { 25 } T - { 27 } S ), "
198                     + "( { 15 } W + { 25 } S P + { 30 } Z - { 18 } T - { 165 B**2 } ), "
199                     + "( { - 9 } W + { 15 } T P + { 20 } S Z ), "
200                     + "( P W + { 2 } T Z - { 11 B**3 } ), "
201                     + "( { 99 } W - { 11 B } S + { 3 B**2 } ), "
202                     + "( { 10000 B**2 + 6600 B + 2673 } ) "
203                     + ") ";
204         Reader source = new StringReader( exam );
205         GenPolynomialTokenizer parser
206                      = new GenPolynomialTokenizer( source );
207         DGroebnerBaseSeq<GenPolynomial<BigRational>> bb 
208             = new DGroebnerBaseSeq<GenPolynomial<BigRational>>();
209         PolynomialList<GenPolynomial<BigRational>> F = null;
210         List<GenPolynomial<GenPolynomial<BigRational>>> G = null;
211         try {
212             F = (PolynomialList<GenPolynomial<BigRational>>) parser.nextPolynomialSet();
213         } catch(ClassCastException e) {
214             fail(""+e);
215         } catch(IOException e) {
216             fail(""+e);
217         }
218         System.out.println("F = " + F);
219    
220         List<GenPolynomial<GenPolynomial<BigRational>>> Fp 
221             = new ArrayList<GenPolynomial<GenPolynomial<BigRational>>>( F.list.size() );
222         for ( GenPolynomial<GenPolynomial<BigRational>> p : F.list ) {
223             p = PolyUtil.<BigRational>monic(p);
224             Fp.add( p );
225         }
226         //System.out.println("Fp = " + Fp);
227         G = bb.GB(Fp);
228         //System.out.println("G = " + G);
229    
230         List<GenPolynomial<GenPolynomial<BigRational>>> Gp 
231             = new ArrayList<GenPolynomial<GenPolynomial<BigRational>>>( F.list.size() );
232         for ( GenPolynomial<GenPolynomial<BigRational>> p : G ) {
233             p = PolyUtil.<BigRational>monic(p);
234             Gp.add( p );
235         }
236         PolynomialList<GenPolynomial<BigRational>> trinks 
237               = new PolynomialList<GenPolynomial<BigRational>>(F.ring,Gp);
238         System.out.println("G = " + trinks);
239         System.out.println("G.size() = " + Gp.size());
240         ComputerThreads.terminate();
241         assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
242         //assertEquals("#GB(Trinks7) == 1", 1, G.size() );
243     }
244    
245    }