001    /*
002     * $Id: EGroebnerBaseSeqTest.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     * EGroebner base sequential tests with JUnit.
036     * @author Heinz Kredel.
037     */
038    
039    public class EGroebnerBaseSeqTest extends TestCase {
040    
041        private static final Logger logger = Logger.getLogger(EGroebnerBaseSeqTest.class);
042    
043    /**
044     * main
045     */
046       public static void main (String[] args) {
047           BasicConfigurator.configure();
048           junit.textui.TestRunner.run( suite() );
049       }
050    
051    /**
052     * Constructs a <CODE>EGroebnerBaseSeqTest</CODE> object.
053     * @param name String.
054     */
055       public EGroebnerBaseSeqTest(String name) {
056              super(name);
057       }
058    
059    /**
060     * suite.
061     */ 
062     public static Test suite() {
063         TestSuite suite= new TestSuite(EGroebnerBaseSeqTest.class);
064         return suite;
065       }
066    
067       GenPolynomialRing<BigInteger> fac;
068    
069       List<GenPolynomial<BigInteger>> L;
070       PolynomialList<BigInteger> F;
071       List<GenPolynomial<BigInteger>> G;
072    
073       GroebnerBase<BigInteger> bb;
074    
075       GenPolynomial<BigInteger> a;
076       GenPolynomial<BigInteger> b;
077       GenPolynomial<BigInteger> c;
078       GenPolynomial<BigInteger> d;
079       GenPolynomial<BigInteger> e;
080    
081       int rl = 3; //4; //3; 
082       int kl = 4; //4; 10
083       int ll = 4;
084       int el = 3;
085       float q = 0.2f; //0.4f
086    
087       protected void setUp() {
088           BigInteger coeff = new BigInteger(9);
089           fac = new GenPolynomialRing<BigInteger>(coeff,rl);
090           a = b = c = d = e = null;
091           bb = new EGroebnerBaseSeq<BigInteger>();
092       }
093    
094       protected void tearDown() {
095           a = b = c = d = e = null;
096           fac = null;
097           bb = null;
098       }
099    
100    
101    /**
102     * Test sequential GBase.
103     * 
104     */
105     public void testSequentialGBase() {
106    
107         L = new ArrayList<GenPolynomial<BigInteger>>();
108    
109         a = fac.random(kl, ll, el, q ).abs();
110         b = fac.random(kl, ll, el, q ).abs();
111         c = fac.random(kl, ll/2, el, q ).abs();
112         d = fac.random(kl, ll/2, el, q ).abs();
113         e = d; //fac.random(kl, ll, el, q );
114    
115         if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
116            return;
117         }
118    
119         L.add(a);
120         //System.out.println("    L  = " + L );
121         L = bb.GB( L );
122         //System.out.println("eGB(L) = " + L );
123         assertTrue("isGB( { a } )", bb.isGB(L) );
124    
125         L.add(b);
126         //System.out.println("    L  = " + L );
127         L = bb.GB( L );
128         //System.out.println("eGB(L) = " + L );
129         assertTrue("isGB( { a, b } )", bb.isGB(L) );
130    
131         L.add(c);
132         //System.out.println("    L  = " + L );
133         L = bb.GB( L );
134         //System.out.println("eGB(L) = " + L );
135         assertTrue("isGB( { a, b, c } )", bb.isGB(L) );
136    
137         L.add(d);
138         //System.out.println("    L  = " + L );
139         L = bb.GB( L );
140         //System.out.println("eGB(L) = " + L );
141         assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) );
142    
143         L.add(e);
144         //System.out.println("    L  = " + L );
145         L = bb.GB( L );
146         //System.out.println("eGB(L) = " + L );
147         assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L) );
148     }
149    
150    /**
151     * Test Trinks7 GBase over Z.
152     * 
153     */ 
154     @SuppressWarnings("unchecked") 
155         public void xtestTrinks7GBaseZ() { // needs 20 sec
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( eGB(Trinks7) )", bb.isGB(G) );
184         //assertEquals("#GB(Trinks7) == 44", 44, 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         EGroebnerBaseSeq<GenPolynomial<BigRational>> bb 
208             = new EGroebnerBaseSeq<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() = " + G.size());
240         ComputerThreads.terminate();
241    
242         assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
243         //assertEquals("#GB(Trinks7) == 1", 1, G.size() );
244     }
245    
246    }