001 002/* 003 * $Id$ 004 */ 005 006package edu.jas.ufd; 007 008 009import java.io.IOException; 010import java.io.StringReader; 011import java.util.Arrays; 012import java.util.List; 013 014import edu.jas.arith.BigInteger; 015import edu.jas.kern.ComputerThreads; 016import edu.jas.kern.PrettyPrint; 017import edu.jas.poly.GenExteriorPolynomial; 018import edu.jas.poly.GenExteriorPolynomialRing; 019import edu.jas.poly.GenPolynomial; 020import edu.jas.poly.GenPolynomialRing; 021import edu.jas.poly.GenPolynomialTokenizer; 022import edu.jas.poly.IndexFactory; 023import edu.jas.poly.TermOrder; 024import edu.jas.structure.RingElem; 025 026import junit.framework.Test; 027import junit.framework.TestCase; 028import junit.framework.TestSuite; 029 030 031/** 032 * Quotient BigInteger coefficient GenPolynomial tests with JUnit. 033 * @author Heinz Kredel 034 */ 035 036public class QuotIntPolynomialTest extends TestCase { 037 038 039 /** 040 * main. 041 */ 042 public static void main(String[] args) { 043 junit.textui.TestRunner.run(suite()); 044 ComputerThreads.terminate(); 045 } 046 047 048 /** 049 * Constructs a <CODE>QoutIntPolynomialTest</CODE> object. 050 * @param name String. 051 */ 052 public QuotIntPolynomialTest(String name) { 053 super(name); 054 } 055 056 057 /** 058 * suite. 059 */ 060 public static Test suite() { 061 TestSuite suite = new TestSuite(QuotIntPolynomialTest.class); 062 return suite; 063 } 064 065 066 QuotientRing<BigInteger> eFac; 067 068 069 GenPolynomialRing<BigInteger> mfac; 070 071 072 GenPolynomialRing<Quotient<BigInteger>> qfac; 073 074 075 GenPolynomial<Quotient<BigInteger>> a, b, c, d, e; 076 077 078 int rl = 3; 079 080 081 int kl = 2; // degree of coefficient polynomials!!! 082 083 084 int ll = 4; //6; 085 086 087 int el = 3; 088 089 090 float q = 0.3f; 091 092 093 @Override 094 protected void setUp() { 095 a = b = c = d = e = null; 096 TermOrder to = new TermOrder(TermOrder.INVLEX); 097 String[] cv = new String[] { "a", "b", "c" }; 098 BigInteger cfac = new BigInteger(1); 099 mfac = new GenPolynomialRing<BigInteger>(cfac, rl, to, cv); 100 eFac = new QuotientRing<BigInteger>(mfac); 101 String[] v = new String[] { "w", "x", "y", "z" }; 102 qfac = new GenPolynomialRing<Quotient<BigInteger>>(eFac, rl + 1, v); 103 } 104 105 106 @Override 107 protected void tearDown() { 108 a = b = c = d = e = null; 109 //eFac.terminate(); 110 eFac = null; 111 mfac = null; 112 qfac = null; 113 ComputerThreads.terminate(); 114 } 115 116 117 /** 118 * Test constructor and toString. 119 */ 120 public void testConstruction() { 121 c = qfac.getONE(); 122 //System.out.println("c = " + c); 123 //System.out.println("c.val = " + c.val); 124 assertTrue("length( c ) = 1", c.length() == 1); 125 assertTrue("isZERO( c )", !c.isZERO()); 126 assertTrue("isONE( c )", c.isONE()); 127 128 d = qfac.getZERO(); 129 //System.out.println("d = " + d); 130 //System.out.println("d.val = " + d.val); 131 assertTrue("length( d ) = 0", d.length() == 0); 132 assertTrue("isZERO( d )", d.isZERO()); 133 assertTrue("isONE( d )", !d.isONE()); 134 } 135 136 137 /** 138 * Test random polynomial. 139 */ 140 public void testRandom() { 141 for (int i = 0; i < 3; i++) { 142 //a = qfac.random(ll+i); 143 a = qfac.random(kl, ll + i, el, q); 144 //System.out.println("a["+i+"] = " + a); 145 if (a.isZERO() || a.isONE()) { 146 continue; 147 } 148 assertTrue("length( a" + i + " ) <> 0", a.length() >= 0); 149 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 150 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 151 152 b = a.monic(); 153 Quotient<BigInteger> ldbcf = b.leadingBaseCoefficient(); 154 //System.out.println("b["+i+"] = " + b); 155 assertTrue("ldbcf( b" + i + " ) == 1 " + b + ", a = " + a, ldbcf.isONE()); 156 } 157 } 158 159 160 /** 161 * Test addition. 162 */ 163 public void testAddition() { 164 a = qfac.random(kl, ll, el, q); 165 b = qfac.random(kl, ll, el, q); 166 //System.out.println("a = " + a); 167 //System.out.println("b = " + b); 168 //System.out.println("a = " + a.toString( qfac.getVars() )); 169 //System.out.println("b = " + b.toString( qfac.getVars() )); 170 171 c = a.sum(b); 172 d = c.subtract(b); 173 assertEquals("a+b-b = a", a, d); 174 175 c = a.sum(b); 176 d = b.sum(a); 177 //System.out.println("c = " + c.toString( qfac.getVars() )); 178 //System.out.println("c = " + c); 179 //System.out.println("d = " + d); 180 181 assertEquals("a+b = b+a", c, d); 182 183 c = qfac.random(kl, ll, el, q); 184 d = c.sum(a.sum(b)); 185 e = c.sum(a).sum(b); 186 assertEquals("c+(a+b) = (c+a)+b", d, e); 187 188 c = a.sum(qfac.getZERO()); 189 d = a.subtract(qfac.getZERO()); 190 assertEquals("a+0 = a-0", c, d); 191 192 c = qfac.getZERO().sum(a); 193 d = qfac.getZERO().subtract(a.negate()); 194 assertEquals("0+a = 0+(-a)", c, d); 195 } 196 197 198 /** 199 * Test object multiplication. 200 */ 201 public void testMultiplication() { 202 a = qfac.random(kl, ll, el, q); 203 //assertTrue("not isZERO( a )", !a.isZERO() ); 204 205 b = qfac.random(kl, ll, el, q); 206 //assertTrue("not isZERO( b )", !b.isZERO() ); 207 208 c = b.multiply(a); 209 d = a.multiply(b); 210 if (!a.isZERO() && !b.isZERO()) { 211 assertTrue("not isZERO( c )", !c.isZERO()); 212 assertTrue("not isZERO( d )", !d.isZERO()); 213 } 214 215 //System.out.println("a = " + a); 216 //System.out.println("b = " + b); 217 e = d.subtract(c); 218 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 219 220 assertTrue("a*b = b*a", c.equals(d)); 221 assertEquals("a*b = b*a", c, d); 222 223 c = qfac.random(kl, ll, el, q); 224 //System.out.println("c = " + c); 225 d = a.multiply(b.multiply(c)); 226 e = (a.multiply(b)).multiply(c); 227 228 //System.out.println("d = " + d); 229 //System.out.println("e = " + e); 230 231 //System.out.println("d-e = " + d.subtract(c) ); 232 233 assertEquals("a(bc) = (ab)c", d, e); 234 assertTrue("a(bc) = (ab)c", d.equals(e)); 235 236 c = a.multiply(qfac.getONE()); 237 d = qfac.getONE().multiply(a); 238 assertEquals("a*1 = 1*a", c, d); 239 240 if (a.isUnit()) { 241 c = a.inverse(); 242 d = c.multiply(a); 243 //System.out.println("a = " + a); 244 //System.out.println("c = " + c); 245 //System.out.println("d = " + d); 246 assertTrue("a*1/a = 1", d.isONE()); 247 } 248 } 249 250 251 /** 252 * Test parse(). 253 */ 254 public void testParse() { 255 a = qfac.random(kl, ll * 2, el * 2, q * 2); 256 //assertTrue("not isZERO( a )", !a.isZERO() ); 257 258 //PrettyPrint.setInternal(); 259 //System.out.println("a = " + a); 260 PrettyPrint.setPretty(); 261 //System.out.println("a = " + a); 262 String p = a.toString(qfac.getVars()); 263 //System.out.println("p = " + p); 264 b = qfac.parse(p); 265 //System.out.println("b = " + b.toString( qfac.getVars() ) ); 266 267 //c = a.subtract(b); 268 //System.out.println("c = " + c); 269 assertEquals("parse(a.toSting()) = a", a, b); 270 } 271 272 273 /** 274 * Test exterior derivation. 275 */ 276 @SuppressWarnings("unchecked") 277 public void testDerivation() { 278 // integers 279 BigInteger rf = new BigInteger(); 280 //System.out.println("rf = " + rf.toScriptFactory()); 281 282 // 3/6 commuting vars 283 String[] vars = new String[] { "x1", "x2", "x3" }; 284 //System.out.println("vars = " + Arrays.toString(vars)); 285 286 // polynomials over integers 287 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, vars); 288 //System.out.println("pf = " + pf.toScript()); 289 290 QuotientRing<BigInteger> qf = new QuotientRing<BigInteger>(pf); 291 //System.out.println("qf = " + qf.toScript()); 292 293 assertTrue("commutative", qf.isCommutative()); 294 assertTrue("associative", qf.isAssociative()); 295 assertTrue("not field", qf.isField()); 296 assertEquals("qf == qf: ", qf, qf); 297 298 String s = qf.toScript(); 299 //System.out.println("qf.toScript: " + s + ", " + s.length()); 300 assertEquals("#s == 42: " + s, s.length(), 42); 301 302 s = qf.toString(); 303 //System.out.println("qf.toString: " + s + ", " + s.length()); 304 assertEquals("#s == 41: " + s, s.length(), 41); 305 306 Quotient<BigInteger> p = qf.getONE(); 307 //System.out.println("p = " + p); 308 assertTrue("p == 1", p.isONE()); 309 p = qf.getZERO(); 310 assertTrue("p == 0", p.isZERO()); 311 //System.out.println("p = " + p); 312 313 List<Quotient<BigInteger>> gens = qf.generators(); 314 //System.out.println("gens = " + gens); 315 assertTrue("#gens == 4", gens.size() == 4); 316 317 RingElem<Quotient<BigInteger>> qe = new Quotient<BigInteger>(qf); 318 //System.out.println("qe = " + qe); 319 assertTrue("p.equals(qe) = ", p.equals(qe)); 320 assertTrue("p.equals(p) = ", p.equals(p)); 321 322 qe = qe.sum(p); 323 //System.out.println("qe = " + qe); 324 assertTrue("qe.isZERO() = ", qe.isZERO()); 325 //p = pf.random(9); 326 p = qf.random(kl, ll, el, q); 327 p = p.subtract(p); 328 //System.out.println("p = " + p); 329 //System.out.println("p.isZERO() = " + p.isZERO()); 330 assertTrue("p.isZERO() = ", p.isZERO()); 331 332 333 // exterior polynomials over (polynomials over integers) 334 // 3 non-commuting vars 335 IndexFactory wf2 = new IndexFactory(3); 336 //System.out.println("wf2 = " + wf2); 337 338 GenExteriorPolynomialRing<Quotient<BigInteger>> ppf; 339 ppf = new GenExteriorPolynomialRing<Quotient<BigInteger>>(qf, wf2); 340 //System.out.println("ppf = " + ppf.toScript()); 341 342 GenExteriorPolynomial<Quotient<BigInteger>> pp = ppf.getONE(); 343 //System.out.println("pp = " + pp); 344 assertTrue("pp == 1", pp.isONE()); 345 //pp = ppf.random(2); 346 pp = ppf.random(kl, ll, el); 347 //System.out.println("pp = " + pp); 348 pp = ppf.getZERO(); 349 //System.out.println("pp = " + pp); 350 assertTrue("pp == 0", pp.isZERO()); 351 352 List<GenExteriorPolynomial<Quotient<BigInteger>>> pgens = ppf.generators(); 353 //System.out.println("pgens = " + pgens); 354 assertTrue("#pgens == 4+3", pgens.size() == 4 + 3); 355 356 357 //pp = ppf.random(2); 358 pp = ppf.random(kl, ll, el); 359 //System.out.println("\npp = " + pp); 360 assertFalse("pp.isZERO() = ", pp.isZERO()); 361 long deg = pp.degree(); 362 //System.out.println("deg = " + deg); 363 364 GenExteriorPolynomial<Quotient<BigInteger>> der; 365 der = PolyUfdUtil.<BigInteger> exteriorDerivativeQuot(pp); 366 //System.out.println("der = " + der); 367 //System.out.println("deg = " + deg + ", deg(der) = " + der.degree()); 368 assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0); 369 deg = der.degree(); 370 der = PolyUfdUtil.<BigInteger> exteriorDerivativeQuot(der); 371 //System.out.println("der(der) = " + der); 372 //System.out.println("deg = " + deg + ", deg(der) = " + der.degree()); 373 assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0); 374 375 376 StringReader sr = new StringReader("{x1 x2 | x3 } E(1) E(2)"); 377 //System.out.println("sr = " + sr); 378 GenPolynomialTokenizer tok = new GenPolynomialTokenizer(sr); 379 //System.out.println("ppf = " + ppf.toScript()); 380 381 // parse with tokenizer 382 try { 383 pp = (GenExteriorPolynomial<Quotient<BigInteger>>) tok.nextExteriorPolynomial(ppf); 384 } catch (IOException e) { 385 e.printStackTrace(); 386 return; 387 } 388 //System.out.println("\npp = " + pp); 389 deg = pp.degree(); 390 der = PolyUfdUtil.<BigInteger> exteriorDerivativeQuot(pp); 391 //System.out.println("der = " + der); 392 assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0); 393 394 395 sr = new StringReader("{x3 | x2} E(1) + x3 E(2)"); 396 //sr = new StringReader("x1 E(1) + x2 E(2)"); 397 //sr = new StringReader("x2 E(1) + x1 E(2)"); 398 //System.out.println("sr = " + sr); 399 tok = new GenPolynomialTokenizer(sr); 400 //System.out.println("ppf = " + ppf.toScript()); 401 402 // parse with tokenizer 403 try { 404 pp = (GenExteriorPolynomial<Quotient<BigInteger>>) tok.nextExteriorPolynomial(ppf); 405 } catch (IOException e) { 406 e.printStackTrace(); 407 return; 408 } 409 //System.out.println("\npp = " + pp); 410 deg = pp.degree(); 411 der = PolyUfdUtil.<BigInteger> exteriorDerivativeQuot(pp); 412 //System.out.println("der = " + der); 413 assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0); 414 deg = der.degree(); 415 der = PolyUfdUtil.<BigInteger> exteriorDerivativeQuot(der); 416 //System.out.println("der(der) = " + der); 417 assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0); 418 } 419 420}