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