001/* 002 * $Id: ANumGenPolynomialTest.java 4071 2012-07-27 19:59:38Z kredel $ 003 */ 004 005package edu.jas.poly; 006 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012import edu.jas.arith.BigRational; 013 014 015/** 016 * AlgebraicNumber coefficients GenPolynomial tests with JUnit. 017 * @author Heinz Kredel. 018 */ 019 020public class ANumGenPolynomialTest extends TestCase { 021 022 023 /** 024 * main 025 */ 026 public static void main(String[] args) { 027 junit.textui.TestRunner.run(suite()); 028 } 029 030 031 /** 032 * Constructs a <CODE>ANumGenPolynomialTest</CODE> object. 033 * @param name String. 034 */ 035 public ANumGenPolynomialTest(String name) { 036 super(name); 037 } 038 039 040 /** 041 * suite. 042 */ 043 public static Test suite() { 044 TestSuite suite = new TestSuite(ANumGenPolynomialTest.class); 045 return suite; 046 } 047 048 049 //private final static int bitlen = 100; 050 051 GenPolynomialRing<AlgebraicNumber<BigRational>> fac; 052 053 054 AlgebraicNumberRing<BigRational> cfac; 055 056 057 GenPolynomial<AlgebraicNumber<BigRational>> a; 058 059 060 GenPolynomial<AlgebraicNumber<BigRational>> b; 061 062 063 GenPolynomial<AlgebraicNumber<BigRational>> c; 064 065 066 GenPolynomial<AlgebraicNumber<BigRational>> d; 067 068 069 GenPolynomial<AlgebraicNumber<BigRational>> e; 070 071 072 int rl = 3; 073 074 075 int kl = 10; 076 077 078 int ll = 5; 079 080 081 int el = 5; 082 083 084 float q = 0.5f; 085 086 087 @Override 088 protected void setUp() { 089 a = b = c = d = e = null; 090 BigRational r = new BigRational(1); 091 // univariate minimal polynomial 092 GenPolynomialRing<BigRational> mfac = new GenPolynomialRing<BigRational>(r, 1); 093 GenPolynomial<BigRational> modul = mfac.random(3); 094 while (modul.isZERO() || modul.isUnit() || modul.isConstant()) { 095 modul = mfac.random(3); 096 } 097 modul = modul.sum(mfac.fromInteger(3L)); 098 099 cfac = new AlgebraicNumberRing<BigRational>(modul.monic()); 100 fac = new GenPolynomialRing<AlgebraicNumber<BigRational>>(cfac, rl); 101 } 102 103 104 @Override 105 protected void tearDown() { 106 a = b = c = d = e = null; 107 fac = null; 108 } 109 110 111 /** 112 * Test constructor //and toString 113 * 114 */ 115 public void testConstruction() { 116 c = fac.getONE(); 117 //System.out.println("c = " + c); 118 assertTrue("length( c ) = 1", c.length() == 1); 119 assertTrue("isZERO( c )c" + c, !c.isZERO()); 120 assertTrue("isONE( c ) " + c, c.isONE()); 121 122 d = fac.getZERO(); 123 //System.out.println("d = " + d); 124 assertTrue("length( d ) = 0", d.length() == 0); 125 assertTrue("isZERO( d )", d.isZERO()); 126 assertTrue("isONE( d )", !d.isONE()); 127 } 128 129 130 /** 131 * Test random polynomial. 132 * 133 */ 134 public void testRandom() { 135 for (int i = 0; i < 7; i++) { 136 a = fac.random(ll + i); 137 //System.out.println("a = " + a); 138 139 // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q ); 140 assertTrue("length( a" + i + " ) <> 0", a.length() >= 0); 141 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 142 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 143 } 144 } 145 146 147 /** 148 * Test addition. 149 * 150 */ 151 public void testAddition() { 152 153 a = fac.random(ll); 154 b = fac.random(ll); 155 156 c = a.sum(b); 157 d = c.subtract(b); 158 assertEquals("a+b-b = a", a, d); 159 160 c = fac.random(ll); 161 162 ExpVector u = ExpVector.EVRAND(rl, el, q); 163 AlgebraicNumber<BigRational> x = cfac.random(kl); 164 165 b = new GenPolynomial<AlgebraicNumber<BigRational>>(fac, x, u); 166 c = a.sum(b); 167 d = a.sum(x, u); 168 assertEquals("a+p(x,u) = a+(x,u)", c, d); 169 170 c = a.subtract(b); 171 d = a.subtract(x, u); 172 assertEquals("a-p(x,u) = a-(x,u)", c, d); 173 174 a = new GenPolynomial<AlgebraicNumber<BigRational>>(fac); 175 b = new GenPolynomial<AlgebraicNumber<BigRational>>(fac, x, u); 176 c = b.sum(a); 177 d = a.sum(x, u); 178 assertEquals("a+p(x,u) = a+(x,u)", c, d); 179 180 c = a.subtract(b); 181 d = a.subtract(x, u); 182 assertEquals("a-p(x,u) = a-(x,u)", c, d); 183 184 185 c = fac.random(ll); 186 d = c.sum(a.sum(b)); 187 e = c.sum(a).sum(b); 188 assertEquals("c+(a+b) = (c+a)+b", d, e); 189 190 c = a.sum(fac.getZERO()); 191 d = a.subtract(fac.getZERO()); 192 assertEquals("a+0 = a-0", c, d); 193 194 c = fac.getZERO().sum(a); 195 d = fac.getZERO().subtract(a.negate()); 196 assertEquals("0+a = 0+(-a)", c, d); 197 } 198 199 200 /** 201 * Test object multiplication. 202 * 203 */ 204 205 public void testMultiplication() { 206 207 a = fac.random(ll); 208 assertTrue("not isZERO( a )", !a.isZERO()); 209 210 b = fac.random(ll); 211 assertTrue("not isZERO( b )", !b.isZERO()); 212 213 c = b.multiply(a); 214 d = a.multiply(b); 215 assertTrue("not isZERO( c )", !c.isZERO()); 216 assertTrue("not isZERO( d )", !d.isZERO()); 217 218 //System.out.println("a = " + a); 219 //System.out.println("b = " + b); 220 e = d.subtract(c); 221 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 222 223 assertTrue("a*b = b*a", c.equals(d)); 224 assertEquals("a*b = b*a", c, d); 225 226 c = fac.random(ll); 227 //System.out.println("c = " + c); 228 d = a.multiply(b.multiply(c)); 229 e = (a.multiply(b)).multiply(c); 230 231 //System.out.println("d = " + d); 232 //System.out.println("e = " + e); 233 234 //System.out.println("d-e = " + d.subtract(c) ); 235 236 assertEquals("a(bc) = (ab)c", d, e); 237 assertTrue("a(bc) = (ab)c", d.equals(e)); 238 239 AlgebraicNumber<BigRational> z = a.leadingBaseCoefficient(); 240 //System.out.println("z = " + z); 241 if (z.isUnit()) { 242 AlgebraicNumber<BigRational> x = z.inverse(); 243 //System.out.println("x = " + x); 244 //System.out.println("a = " + a); 245 c = a.monic(); 246 //System.out.println("c = " + c); 247 d = a.multiply(x); 248 //System.out.println("d = " + d); 249 assertEquals("a.monic() = a(1/ldcf(a))", c, d); 250 } 251 252 AlgebraicNumber<BigRational> y = b.leadingBaseCoefficient(); 253 if (y.isUnit()) { 254 y = y.inverse(); 255 c = b.monic(); 256 d = b.multiply(y); 257 assertEquals("b.monic() = b(1/ldcf(b))", c, d); 258 259 e = new GenPolynomial<AlgebraicNumber<BigRational>>(fac, y); 260 d = b.multiply(e); 261 assertEquals("b.monic() = b(1/ldcf(b))", c, d); 262 263 d = e.multiply(b); 264 assertEquals("b.monic() = (1/ldcf(b))*b", c, d); 265 } 266 } 267 268 269 /** 270 * Test distributive law. 271 * 272 */ 273 public void testDistributive() { 274 a = fac.random(ll); 275 b = fac.random(ll); 276 c = fac.random(ll); 277 278 d = a.multiply(b.sum(c)); 279 e = a.multiply(b).sum(a.multiply(c)); 280 281 assertEquals("a(b+c) = ab+ac", d, e); 282 } 283 284 285 /** 286 * Test object quotient and remainder. 287 * 288 */ 289 290 public void testQuotRem1() { 291 292 fac = new GenPolynomialRing<AlgebraicNumber<BigRational>>(cfac, 1); 293 294 a = fac.random(2); //.monic(); 295 if (a.isZERO()) { 296 return; 297 } 298 assertTrue("not isZERO( a )", !a.isZERO()); 299 300 b = fac.random(2); //.monic(); 301 if (b.isZERO()) { 302 return; 303 } 304 assertTrue("not isZERO( b )", !b.isZERO()); 305 306 GenPolynomial<AlgebraicNumber<BigRational>> g = fac.random(1).monic(); 307 if (g.isZERO()) { 308 return; 309 } 310 assertTrue("not isZERO( g )", !g.isZERO()); 311 //g = fac.getONE(); 312 a = a.multiply(g); 313 b = b.multiply(g); 314 //System.out.println("ta = " + a); 315 //System.out.println("tb = " + b); 316 //System.out.println("tg = " + g); 317 318 GenPolynomial<AlgebraicNumber<BigRational>>[] qr; 319 qr = b.divideAndRemainder(a); 320 c = qr[0]; 321 d = qr[1]; 322 //System.out.println("q = " + c); 323 //System.out.println("r = " + d); 324 e = c.multiply(a).sum(d); 325 assertEquals("b = q a + r", b, e); 326 327 qr = a.divideAndRemainder(b); 328 c = qr[0]; 329 d = qr[1]; 330 //System.out.println("q = " + c); 331 //System.out.println("r = " + d); 332 e = c.multiply(b).sum(d); 333 assertEquals("a = q b + r", a, e); 334 335 336 // gcd tests ------------------------------- 337 c = a.gcd(b); 338 //System.out.println("gcd = " + c); 339 assertTrue("a mod gcd(a,b) = 0", a.remainder(c).isZERO()); 340 assertTrue("b mod gcd(a,b) = 0", b.remainder(c).isZERO()); 341 //assertEquals("g = gcd(a,b)", c, g ); 342 343 GenPolynomial<AlgebraicNumber<BigRational>>[] gst; 344 gst = a.egcd(b); 345 //System.out.println("egcd = " + gst[0]); 346 //System.out.println(", s = " + gst[1] + ", t = " + gst[2]); 347 c = gst[0]; 348 d = gst[1]; 349 e = gst[2]; 350 assertTrue("a mod gcd(a,b) = 0", a.remainder(c).isZERO()); 351 assertTrue("b mod gcd(a,b) = 0", b.remainder(c).isZERO()); 352 //assertEquals("g = gcd(a,b)", c, g ); 353 354 GenPolynomial<AlgebraicNumber<BigRational>> x; 355 x = a.multiply(d).sum(b.multiply(e)).monic(); 356 //System.out.println("x = " + x); 357 assertEquals("gcd(a,b) = a s + b t", c, x); 358 359 //System.out.println("a = " + a); 360 //System.out.println("b = " + b); 361 if (a.isZERO() || b.isZERO()) { 362 return; 363 } 364 try { 365 c = a.modInverse(b); 366 //System.out.println("c = " + c); 367 x = c.multiply(a).remainder(b).monic(); 368 //System.out.println("x = " + x); 369 assertTrue("a invertible mod b " + x, x.isUnit()); 370 } catch (RuntimeException e) { 371 // dann halt nicht 372 // not invertible 373 } 374 } 375 376}