001 /* 002 * $Id: GroebnerBasePseudoSeqTest.java 3423 2010-12-24 10:56:50Z kredel $ 003 */ 004 005 package edu.jas.gbufd; 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.BigInteger; 022 import edu.jas.arith.BigRational; 023 import edu.jas.gb.GroebnerBase; 024 import edu.jas.gb.GroebnerBaseSeq; 025 026 import edu.jas.poly.GenPolynomial; 027 import edu.jas.poly.GenPolynomialRing; 028 import edu.jas.poly.GenPolynomialTokenizer; 029 import edu.jas.poly.PolynomialList; 030 import edu.jas.poly.PolyUtil; 031 032 033 034 /** 035 * Groebner base pseudo reduction sequential tests with JUnit. 036 * @author Heinz Kredel. 037 */ 038 039 public class GroebnerBasePseudoSeqTest extends TestCase { 040 041 //private static final Logger logger = Logger.getLogger(GroebnerBasePseudoSeqTest.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>GroebnerBasePseudoSeqTest</CODE> object. 053 * @param name String. 054 */ 055 public GroebnerBasePseudoSeqTest(String name) { 056 super(name); 057 } 058 059 /** 060 * suite. 061 */ 062 public static Test suite() { 063 TestSuite suite= new TestSuite(GroebnerBasePseudoSeqTest.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 = 10; 083 int ll = 7; 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 GroebnerBasePseudoSeq<BigInteger>(coeff); 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 ); 110 b = fac.random(kl, ll, el, q ); 111 c = fac.random(kl, ll, el, q ); 112 d = fac.random(kl, ll, el, q ); 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 assertTrue("not isZERO( a )", !a.isZERO() ); 120 L.add(a); 121 122 L = bb.GB( L ); 123 assertTrue("isGB( { a } )", bb.isGB(L) ); 124 125 assertTrue("not isZERO( b )", !b.isZERO() ); 126 L.add(b); 127 //System.out.println("L = " + L.size() ); 128 129 L = bb.GB( L ); 130 assertTrue("isGB( { a, b } )", bb.isGB(L) ); 131 132 assertTrue("not isZERO( c )", !c.isZERO() ); 133 L.add(c); 134 135 L = bb.GB( L ); 136 assertTrue("isGB( { a, b, c } )", bb.isGB(L) ); 137 138 assertTrue("not isZERO( d )", !d.isZERO() ); 139 L.add(d); 140 141 L = bb.GB( L ); 142 assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) ); 143 144 assertTrue("not isZERO( e )", !e.isZERO() ); 145 L.add(e); 146 147 L = bb.GB( L ); 148 assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L) ); 149 } 150 151 /** 152 * Test Trinks7 GBase. 153 * 154 */ 155 @SuppressWarnings("unchecked") 156 public void testTrinks7GBase() { 157 String exam = "Z(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 + "( 10000 B**2 + 6600 B + 2673 ) " 166 + ") "; 167 Reader source = new StringReader( exam ); 168 GenPolynomialTokenizer parser 169 = new GenPolynomialTokenizer( source ); 170 try { 171 F = (PolynomialList<BigInteger>) 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 long s,t; 180 t = System.currentTimeMillis(); 181 G = bb.GB(F.list); 182 t = System.currentTimeMillis() - t; 183 assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) ); 184 assertEquals("#GB(Trinks7) == 6", 6, G.size() ); 185 PolynomialList<BigInteger> trinks 186 = new PolynomialList<BigInteger>(F.ring,G); 187 //System.out.println("G = " + trinks); 188 189 GenPolynomialRing<BigInteger> ifac = F.ring; 190 BigRational cf = new BigRational(); 191 GenPolynomialRing<BigRational> rfac 192 = new GenPolynomialRing<BigRational>(cf,ifac); 193 194 List<GenPolynomial<BigRational>> Gr, Fr, Gir; 195 Fr = PolyUtil.<BigRational>fromIntegerCoefficients(rfac,F.list); 196 GroebnerBaseSeq<BigRational> bbr = new GroebnerBaseSeq<BigRational>(); 197 s = System.currentTimeMillis(); 198 Gr = bbr.GB(Fr); 199 s = System.currentTimeMillis() - s; 200 201 Gir = PolyUtil.<BigRational>fromIntegerCoefficients(rfac,G); 202 Gir = PolyUtil.<BigRational>monic(Gir); 203 204 assertEquals("ratGB == intGB", Gr, Gir ); 205 //System.out.println("time: ratGB = " + s + ", intGB = " + t); 206 } 207 208 }