001 002/* 003 * $Id$ 004 */ 005 006package edu.jas.poly; 007 008 009import java.util.List; 010 011import edu.jas.arith.BigInteger; 012import edu.jas.arith.BigRational; 013 014import junit.framework.Test; 015import junit.framework.TestCase; 016import junit.framework.TestSuite; 017 018 019/** 020 * Local tests with JUnit. 021 * @author Heinz Kredel 022 */ 023 024public class LocalTest extends TestCase { 025 026 027 /** 028 * main. 029 */ 030 public static void main(String[] args) { 031 junit.textui.TestRunner.run(suite()); 032 } 033 034 035 /** 036 * Constructs a <CODE>LocalTest</CODE> object. 037 * @param name String. 038 */ 039 public LocalTest(String name) { 040 super(name); 041 } 042 043 044 /** 045 * suite. 046 */ 047 public static Test suite() { 048 TestSuite suite = new TestSuite(LocalTest.class); 049 return suite; 050 } 051 052 LocalRing<BigInteger> fac; 053 054 055 GenPolynomialRing<BigRational> pfac; 056 057 058 LocalRing<GenPolynomial<BigRational>> mfac; 059 060 061 Local<BigInteger> a, b, c, d, e; 062 063 064 Local<GenPolynomial<BigRational>> ap, bp, cp, dp, ep; 065 066 067 int rl = 1; 068 069 070 int kl = 13; 071 072 073 int ll = 5; 074 075 076 int el = 2; 077 078 079 float q = 0.3f; 080 081 082 int il = 2; 083 084 085 long p = 1152921504606846883L; // 2^60-93; 086 087 @Override 088 protected void setUp() { 089 a = b = c = d = e = null; 090 ap = bp = cp = dp = ep = null; 091 BigInteger cfac = new BigInteger(1); 092 fac = new LocalRing<BigInteger>(cfac, new BigInteger(p)); 093 094 pfac = new GenPolynomialRing<BigRational>(new BigRational(1), 1); 095 GenPolynomial<BigRational> mo = pfac.random(kl, ll, el, q); 096 while (mo.isConstant()) { 097 mo = pfac.random(kl, ll, el, q); 098 } 099 mfac = new LocalRing<GenPolynomial<BigRational>>(pfac, mo); 100 } 101 102 103 @Override 104 protected void tearDown() { 105 a = b = c = d = e = null; 106 ap = bp = cp = dp = ep = null; 107 fac = null; 108 pfac = null; 109 mfac = null; 110 } 111 112 113 /** 114 * Test factory for integer. 115 */ 116 public void testIntRing() { 117 assertFalse("#ring infinite", fac.isFinite()); 118 assertTrue("associative ring", fac.isAssociative()); 119 assertTrue("commutative ring", fac.isCommutative()); 120 assertTrue("characteristic zero", fac.characteristic().signum() == 0); 121 assertFalse("no field", fac.isField()); 122 } 123 124 125 /** 126 * Test factory for polynomial. 127 */ 128 public void testPolyRing() { 129 assertFalse("#ring infinite", mfac.isFinite()); 130 assertTrue("associative ring", mfac.isAssociative()); 131 assertTrue("commutative ring", mfac.isCommutative()); 132 assertTrue("characteristic zero", mfac.characteristic().signum() == 0); 133 assertFalse("no field", mfac.isField()); 134 } 135 136 137 /** 138 * Test constructor for integer. 139 */ 140 public void testIntConstruction() { 141 c = fac.getONE(); 142 //System.out.println("c = " + c); 143 assertTrue("isZERO( c )", !c.isZERO()); 144 assertTrue("isONE( c )", c.isONE()); 145 146 d = fac.getZERO(); 147 //System.out.println("d = " + d); 148 assertTrue("isZERO( d )", d.isZERO()); 149 assertTrue("isONE( d )", !d.isONE()); 150 151 List<Local<BigInteger>> gens = fac.generators(); 152 //System.out.println("gens = " + gens); 153 assertTrue("#gens == 1: ", gens.size() == 1); 154 for (Local<BigInteger> v : gens) { 155 a = fac.parse(v.toString()); 156 assertEquals("a == v", a, v); 157 } 158 } 159 160 161 /** 162 * Test constructor for polynomial. 163 */ 164 public void testPolyConstruction() { 165 cp = mfac.getONE(); 166 assertTrue("isZERO( cp )", !cp.isZERO()); 167 assertTrue("isONE( cp )", cp.isONE()); 168 169 dp = mfac.getZERO(); 170 assertTrue("isZERO( dp )", dp.isZERO()); 171 assertTrue("isONE( dp )", !dp.isONE()); 172 173 List<Local<GenPolynomial<BigRational>>> gens = mfac.generators(); 174 //System.out.println("gens = " + gens); 175 assertTrue("#gens == 3: ", gens.size() == 3); 176 for (Local<GenPolynomial<BigRational>> v : gens) { 177 ap = mfac.parse(v.toString()); 178 assertEquals("ap == v", ap, v); 179 } 180 } 181 182 183 /** 184 * Test random integer. 185 */ 186 public void testRandom() { 187 for (int i = 0; i < 4; i++) { 188 a = fac.random(kl * (i + 1)); 189 //a = fac.random(kl*(i+1), ll+i, el, q ); 190 //System.out.println("a = " + a); 191 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 192 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 193 } 194 } 195 196 197 /** 198 * Test random polynomial. 199 */ 200 public void testPolyRandom() { 201 for (int i = 0; i < 7; i++) { 202 ap = mfac.random(kl + i); 203 assertTrue(" not isZERO( ap" + i + " )", !ap.isZERO()); 204 assertTrue(" not isONE( ap" + i + " )", !ap.isONE()); 205 } 206 } 207 208 209 /** 210 * Test integer addition. 211 */ 212 public void testIntAddition() { 213 a = fac.random(kl); 214 b = fac.random(kl); 215 //System.out.println("a = " + a); 216 //System.out.println("b = " + b); 217 218 c = a.sum(b); 219 d = c.subtract(b); 220 assertEquals("a+b-b = a", a, d); 221 222 c = a.sum(b); 223 d = b.sum(a); 224 assertEquals("a+b = b+a", c, d); 225 226 c = fac.random(kl); 227 d = c.sum(a.sum(b)); 228 e = c.sum(a).sum(b); 229 assertEquals("c+(a+b) = (c+a)+b", d, e); 230 231 c = a.sum(fac.getZERO()); 232 d = a.subtract(fac.getZERO()); 233 assertEquals("a+0 = a-0", c, d); 234 235 c = fac.getZERO().sum(a); 236 d = fac.getZERO().subtract(a.negate()); 237 assertEquals("0+a = 0+(-a)", c, d); 238 } 239 240 241 /** 242 * Test polynomial addition. 243 */ 244 public void testPolyAddition() { 245 ap = mfac.random(kl); 246 bp = mfac.random(kl); 247 //System.out.println("a = " + a); 248 //System.out.println("b = " + b); 249 250 cp = ap.sum(bp); 251 dp = cp.subtract(bp); 252 assertEquals("a+b-b = a", ap, dp); 253 254 cp = ap.sum(bp); 255 dp = bp.sum(ap); 256 //System.out.println("c = " + c); 257 //System.out.println("d = " + d); 258 259 assertEquals("a+b = b+a", cp, dp); 260 261 cp = mfac.random(kl); 262 dp = cp.sum(ap.sum(bp)); 263 ep = cp.sum(ap).sum(bp); 264 assertEquals("c+(a+b) = (c+a)+b", dp, ep); 265 266 267 cp = ap.sum(mfac.getZERO()); 268 dp = ap.subtract(mfac.getZERO()); 269 assertEquals("a+0 = a-0", cp, dp); 270 271 cp = mfac.getZERO().sum(ap); 272 dp = mfac.getZERO().subtract(ap.negate()); 273 assertEquals("0+a = 0+(-a)", cp, dp); 274 } 275 276 277 /** 278 * Test integer multiplication. 279 */ 280 public void testIntMultiplication() { 281 a = fac.random(kl); 282 b = fac.random(kl); 283 if (a.isZERO() || b.isZERO()) { 284 return; 285 } 286 assertTrue("not isZERO( a )", !a.isZERO()); 287 assertTrue("not isZERO( b )", !b.isZERO()); 288 289 c = b.multiply(a); 290 d = a.multiply(b); 291 assertTrue("not isZERO( c )", !c.isZERO()); 292 assertTrue("not isZERO( d )", !d.isZERO()); 293 294 //System.out.println("a = " + a); 295 //System.out.println("b = " + b); 296 e = d.subtract(c); 297 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 298 299 assertTrue("a*b = b*a", c.equals(d)); 300 assertEquals("a*b = b*a", c, d); 301 302 c = fac.random(kl); 303 d = a.multiply(b.multiply(c)); 304 e = a.multiply(b).multiply(c); 305 306 assertEquals("a(bc) = (ab)c", d, e); 307 assertTrue("a(bc) = (ab)c", d.equals(e)); 308 309 c = a.multiply(fac.getONE()); 310 d = fac.getONE().multiply(a); 311 assertEquals("a*1 = 1*a", c, d); 312 313 if (a.isUnit()) { 314 c = a.inverse(); 315 d = c.multiply(a); 316 //System.out.println("a = " + a); 317 //System.out.println("c = " + c); 318 //System.out.println("d = " + d); 319 assertTrue("a*1/a = 1", d.isONE()); 320 } 321 322 Local<BigInteger>[] qr; 323 c = b.multiply(a); 324 qr = c.quotientRemainder(a); 325 assertEquals("b*a / a == b", qr[0], b); 326 assertTrue("b*a rem a == 0", qr[1].isZERO()); 327 } 328 329 330 /** 331 * Test polynomial multiplication. 332 */ 333 public void testPolyMultiplication() { 334 ap = mfac.random(kl); 335 bp = mfac.random(kl); 336 if (ap.isZERO() || bp.isZERO()) { 337 return; 338 } 339 340 assertTrue("not isZERO( a )", !ap.isZERO()); 341 assertTrue("not isZERO( b )", !bp.isZERO()); 342 343 cp = bp.multiply(ap); 344 dp = ap.multiply(bp); 345 assertTrue("not isZERO( c )", !cp.isZERO()); 346 assertTrue("not isZERO( d )", !dp.isZERO()); 347 348 //System.out.println("a = " + a); 349 //System.out.println("b = " + b); 350 ep = dp.subtract(cp); 351 assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO()); 352 353 assertTrue("a*b = b*a", cp.equals(dp)); 354 assertEquals("a*b = b*a", cp, dp); 355 356 cp = mfac.random(kl); 357 //System.out.println("c = " + c); 358 dp = ap.multiply(bp.multiply(cp)); 359 ep = (ap.multiply(bp)).multiply(cp); 360 361 //System.out.println("d = " + d); 362 //System.out.println("e = " + e); 363 364 //System.out.println("d-e = " + d.subtract(c) ); 365 366 assertEquals("a(bc) = (ab)c", dp, ep); 367 assertTrue("a(bc) = (ab)c", dp.equals(ep)); 368 369 cp = ap.multiply(mfac.getONE()); 370 dp = mfac.getONE().multiply(ap); 371 assertEquals("a*1 = 1*a", cp, dp); 372 373 if (ap.isUnit()) { 374 cp = ap.inverse(); 375 dp = cp.multiply(ap); 376 //System.out.println("a = " + a); 377 //System.out.println("c = " + c); 378 //System.out.println("d = " + d); 379 assertTrue("a*1/a = 1", dp.isONE()); 380 } 381 382 Local<GenPolynomial<BigRational>>[] qr; 383 cp = bp.multiply(ap); 384 qr = cp.quotientRemainder(ap); 385 assertEquals("b*a / a == b", qr[0], bp); 386 assertTrue("b*a rem a == 0", qr[1].isZERO()); 387 } 388 389}