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