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