001 002/* 003 * $Id: ComplexTest.java 5864 2018-07-20 14:28:52Z kredel $ 004 */ 005 006package edu.jas.poly; 007 008//import java.util.ArrayList; 009//import java.util.List; 010 011import junit.framework.Test; 012import junit.framework.TestCase; 013import junit.framework.TestSuite; 014 015 016import edu.jas.arith.BigRational; 017import edu.jas.arith.BigInteger; 018 019//import edu.jas.structure.RingElem; 020 021 022 023/** 024 * Complex test with JUnit. 025 * @author Heinz Kredel 026 */ 027 028public class ComplexTest extends TestCase { 029 030/** 031 * main. 032 */ 033 public static void main (String[] args) { 034 junit.textui.TestRunner.run( suite() ); 035 } 036 037/** 038 * Constructs a <CODE>ComplexTest</CODE> object. 039 * @param name String. 040 */ 041 public ComplexTest(String name) { 042 super(name); 043 } 044 045/** 046 * suite. 047 */ 048 public static Test suite() { 049 TestSuite suite= new TestSuite(ComplexTest.class); 050 return suite; 051 } 052 053 ComplexRing<BigInteger> fac; 054 GenPolynomialRing<BigRational> pfac; 055 ComplexRing< GenPolynomial<BigRational> > mfac; 056 057 Complex< BigInteger > a; 058 Complex< BigInteger > b; 059 Complex< BigInteger > c; 060 Complex< BigInteger > d; 061 Complex< BigInteger > e; 062 063 Complex< GenPolynomial<BigRational> > ap; 064 Complex< GenPolynomial<BigRational> > bp; 065 Complex< GenPolynomial<BigRational> > cp; 066 Complex< GenPolynomial<BigRational> > dp; 067 Complex< GenPolynomial<BigRational> > ep; 068 069 070 int rl = 1; 071 int kl = 13; 072 int ll = 7; 073 int el = 3; 074 float q = 0.4f; 075 int il = 2; 076 long p = 1152921504606846883L; // 2^60-93; 077 078 @Override 079 protected void setUp() { 080 a = b = c = d = e = null; 081 ap = bp = cp = dp = ep = null; 082 BigInteger cfac = new BigInteger(1); 083 fac = new ComplexRing<BigInteger>( cfac ); 084 pfac = new GenPolynomialRing<BigRational>( new BigRational(1), 1 ); 085 GenPolynomial<BigRational> mo = pfac.random(kl,ll,el,q); 086 while ( mo.isConstant() ) { 087 mo = pfac.random(kl,ll,el,q); 088 } 089 mfac = new ComplexRing<GenPolynomial<BigRational>>( pfac ); 090 } 091 092 @Override 093 protected void tearDown() { 094 a = b = c = d = e = null; 095 ap = bp = cp = dp = ep = null; 096 fac = null; 097 pfac = null; 098 mfac = null; 099 } 100 101 102/** 103 * Test constructor for integer. 104 * 105 */ 106 public void testIntConstruction() { 107 c = fac.getONE(); 108 //System.out.println("c = " + c); 109 assertTrue("isZERO( c )", !c.isZERO() ); 110 assertTrue("isONE( c )", c.isONE() ); 111 112 d = fac.getZERO(); 113 //System.out.println("d = " + d); 114 assertTrue("isZERO( d )", d.isZERO() ); 115 assertTrue("isONE( d )", !d.isONE() ); 116 } 117 118 119/** 120 * Test constructor for polynomial. 121 * 122 */ 123 public void testPolyConstruction() { 124 cp = mfac.getONE(); 125 assertTrue("isZERO( cp )", !cp.isZERO() ); 126 assertTrue("isONE( cp )", cp.isONE() ); 127 128 dp = mfac.getZERO(); 129 assertTrue("isZERO( dp )", dp.isZERO() ); 130 assertTrue("isONE( dp )", !dp.isONE() ); 131 } 132 133 134/** 135 * Test random integer. 136 * 137 */ 138 public void testIntRandom() { 139 for (int i = 0; i < 7; i++) { 140 a = fac.random(kl*(i+1)); 141 if ( a.isZERO() ) { 142 continue; 143 } 144 //a = fac.random(kl*(i+1), ll+2*i, el+i, q ); 145 //System.out.println("a = " + a); 146 assertTrue(" not isZERO( a"+i+" )", !a.isZERO() ); 147 assertTrue(" not isONE( a"+i+" )", !a.isONE() ); 148 } 149 } 150 151 152/** 153 * Test random polynomial. 154 * 155 */ 156 public void testPolyRandom() { 157 for (int i = 0; i < 7; i++) { 158 ap = mfac.random(kl+i); 159 if ( ap.isZERO() ) { 160 continue; 161 } 162 assertTrue(" not isZERO( ap"+i+" )", !ap.isZERO() ); 163 assertTrue(" not isONE( ap"+i+" )", !ap.isONE() ); 164 } 165 } 166 167 168/** 169 * Test integer addition. 170 * 171 */ 172 public void testIntAddition() { 173 174 a = fac.random(kl); 175 b = fac.random(kl); 176 177 c = a.sum(b); 178 d = c.subtract(b); 179 assertEquals("a+b-b = a",a,d); 180 181 c = a.sum(b); 182 d = b.sum(a); 183 assertEquals("a+b = b+a",c,d); 184 185 c = fac.random(kl); 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 191 c = a.sum( fac.getZERO() ); 192 d = a.subtract( fac.getZERO() ); 193 assertEquals("a+0 = a-0",c,d); 194 195 c = fac.getZERO().sum( a ); 196 d = fac.getZERO().subtract( a.negate() ); 197 assertEquals("0+a = 0+(-a)",c,d); 198 } 199 200 201/** 202 * Test polynomial addition. 203 * 204 */ 205 public void testPolyAddition() { 206 207 ap = mfac.random(kl); 208 bp = mfac.random(kl); 209 //System.out.println("a = " + a); 210 //System.out.println("b = " + b); 211 212 cp = ap.sum(bp); 213 dp = cp.subtract(bp); 214 assertEquals("a+b-b = a",ap,dp); 215 216 cp = ap.sum(bp); 217 dp = bp.sum(ap); 218 //System.out.println("c = " + c); 219 //System.out.println("d = " + d); 220 221 assertEquals("a+b = b+a",cp,dp); 222 223 cp = mfac.random(kl); 224 dp = cp.sum( ap.sum(bp) ); 225 ep = cp.sum( ap ).sum(bp); 226 assertEquals("c+(a+b) = (c+a)+b",dp,ep); 227 228 229 cp = ap.sum( mfac.getZERO() ); 230 dp = ap.subtract( mfac.getZERO() ); 231 assertEquals("a+0 = a-0",cp,dp); 232 233 cp = mfac.getZERO().sum( ap ); 234 dp = mfac.getZERO().subtract( ap.negate() ); 235 assertEquals("0+a = 0+(-a)",cp,dp); 236} 237 238 239/** 240 * Test integer multiplication. 241 * 242 */ 243 public void testIntMultiplication() { 244 245 a = fac.random(kl); 246 if ( a.isZERO() ) { 247 return; 248 } 249 assertTrue("not isZERO( a )", !a.isZERO() ); 250 251 b = fac.random(kl); 252 if ( b.isZERO() ) { 253 return; 254 } 255 assertTrue("not isZERO( b )", !b.isZERO() ); 256 257 c = b.multiply(a); 258 d = a.multiply(b); 259 assertTrue("not isZERO( c )", !c.isZERO() ); 260 assertTrue("not isZERO( d )", !d.isZERO() ); 261 262 //System.out.println("a = " + a); 263 //System.out.println("b = " + b); 264 e = d.subtract(c); 265 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() ); 266 267 assertTrue("a*b = b*a", c.equals(d) ); 268 assertEquals("a*b = b*a",c,d); 269 270 c = fac.random(kl); 271 //System.out.println("c = " + c); 272 d = a.multiply( b.multiply(c) ); 273 e = (a.multiply(b)).multiply(c); 274 275 //System.out.println("d = " + d); 276 //System.out.println("e = " + e); 277 278 //System.out.println("d-e = " + d.subtract(c) ); 279 280 assertEquals("a(bc) = (ab)c",d,e); 281 assertTrue("a(bc) = (ab)c", d.equals(e) ); 282 283 c = a.multiply( fac.getONE() ); 284 d = fac.getONE().multiply( a ); 285 assertEquals("a*1 = 1*a",c,d); 286 287 if ( a.isUnit() ) { 288 c = a.inverse(); 289 d = c.multiply(a); 290 //System.out.println("a = " + a); 291 //System.out.println("c = " + c); 292 //System.out.println("d = " + d); 293 assertTrue("a*1/a = 1",d.isONE()); 294 } 295 } 296 297 298/** 299 * Test polynomial multiplication. 300 * 301 */ 302 public void testPolyMultiplication() { 303 304 ap = mfac.random(kl); 305 if ( ap.isZERO() ) { 306 return; 307 } 308 assertTrue("not isZERO( a )", !ap.isZERO() ); 309 310 bp = mfac.random(kl); 311 if ( bp.isZERO() ) { 312 return; 313 } 314 assertTrue("not isZERO( b )", !bp.isZERO() ); 315 316 cp = bp.multiply(ap); 317 dp = ap.multiply(bp); 318 assertTrue("not isZERO( c )", !cp.isZERO() ); 319 assertTrue("not isZERO( d )", !dp.isZERO() ); 320 321 //System.out.println("a = " + a); 322 //System.out.println("b = " + b); 323 ep = dp.subtract(cp); 324 assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO() ); 325 326 assertTrue("a*b = b*a", cp.equals(dp) ); 327 assertEquals("a*b = b*a",cp,dp); 328 329 cp = mfac.random(kl); 330 //System.out.println("c = " + c); 331 dp = ap.multiply( bp.multiply(cp) ); 332 ep = (ap.multiply(bp)).multiply(cp); 333 334 //System.out.println("d = " + d); 335 //System.out.println("e = " + e); 336 337 //System.out.println("d-e = " + d.subtract(c) ); 338 339 assertEquals("a(bc) = (ab)c",dp,ep); 340 assertTrue("a(bc) = (ab)c", dp.equals(ep) ); 341 342 cp = ap.multiply( mfac.getONE() ); 343 dp = mfac.getONE().multiply( ap ); 344 assertEquals("a*1 = 1*a",cp,dp); 345 346 if ( ap.isUnit() ) { 347 cp = ap.inverse(); 348 dp = cp.multiply(ap); 349 //System.out.println("a = " + a); 350 //System.out.println("c = " + c); 351 //System.out.println("d = " + d); 352 assertTrue("a*1/a = 1",dp.isONE()); 353 } 354 } 355 356 357/** 358 * Test integer division. 359 * 360 */ 361 public void testIntDivision() { 362 363 a = fac.random(kl*2); 364 if ( a.isZERO() ) { 365 return; 366 } 367 assertTrue("not isZERO( a )", !a.isZERO() ); 368 369 b = fac.random(kl); 370 if ( b.isZERO() ) { 371 return; 372 } 373 assertTrue("not isZERO( b )", !b.isZERO() ); 374 //System.out.println("a = " + a); 375 //System.out.println("b = " + b); 376 377 c = a.divide(b); 378 //System.out.println("c = " + c); 379 d = a.remainder(b); 380 //System.out.println("d = " + d); 381 382 e = b.multiply(c).sum(d); 383 //System.out.println("e = " + e); 384 assertEquals("a = b (a/b) + a%b ", a, e ); 385 386 c = a.gcd(b); 387 d = a.divide(c); 388 e = b.divide(c); 389 //System.out.println("c = " + c); 390 //System.out.println("d = " + d); 391 //System.out.println("e = " + e); 392 393 d = c.multiply(d); 394 e = c.multiply(e); 395 //System.out.println("d = " + d); 396 //System.out.println("e = " + e); 397 398 assertEquals("a/gcd(a,b)*gcd(a,b) = a ", a, d ); 399 assertEquals("b/gcd(a,b)*gcd(a,b) = b ", b, e ); 400 401 Complex<BigInteger>[] gf = a.egcd(b); 402 c = gf[0]; 403 d = gf[1]; 404 e = gf[2]; 405 //System.out.println("c = " + c); 406 //System.out.println("d = " + d); 407 //System.out.println("e = " + e); 408 d = d.multiply(a); 409 e = e.multiply(b); 410 d = d.sum( e ); 411 //System.out.println("d = " + d); 412 assertEquals("d*a + e*b = c = gcd(a,b)", c, d ); 413 } 414 415 416/** 417 * Test polynomial division. 418 * 419 */ 420 public void testPolyDivision() { 421 422 ap = mfac.random(kl); 423 if ( ap.isZERO() ) { 424 return; 425 } 426 assertTrue("not isZERO( ap )", !ap.isZERO() ); 427 428 bp = mfac.random(kl/2); 429 if ( bp.isZERO() ) { 430 return; 431 } 432 assertTrue("not isZERO( bp )", !bp.isZERO() ); 433 //System.out.println("ap = " + ap); 434 //System.out.println("bp = " + bp); 435 436 cp = ap.divide(bp); 437 //System.out.println("cp = " + cp); 438 dp = ap.remainder(bp); 439 //System.out.println("dp = " + dp); 440 441 ep = bp.multiply(cp).sum(dp); 442 //System.out.println("ep = " + ep); 443 assertEquals("ap = bp (ap/bp) + ap%bp ", ap, ep ); 444 445 // not applicable: 446// cp = ap.gcd(bp); 447// dp = ap.divide(cp); 448// ep = bp.divide(cp); 449// System.out.println("cp = " + cp); 450// System.out.println("dp = " + dp); 451// System.out.println("ep = " + ep); 452 453// dp = cp.multiply(dp); 454// ep = cp.multiply(ep); 455// System.out.println("dp = " + dp); 456// System.out.println("ep = " + ep); 457 458// assertEquals("ap/gcd(ap,bp)*gcd(ap,bp) = ap ", ap, dp ); 459// assertEquals("bp/gcd(ap,bp)*gcd(ap,bp) = bp ", bp, ep ); 460 } 461 462}