001/* 002 * $Id: MultiplicativeSetTest.java 5866 2018-07-20 15:02:16Z kredel $ 003 */ 004 005package edu.jas.gbufd; 006 007 008import java.util.List; 009 010import junit.framework.Test; 011import junit.framework.TestCase; 012import junit.framework.TestSuite; 013 014 015import edu.jas.arith.BigRational; 016import edu.jas.kern.ComputerThreads; 017import edu.jas.poly.GenPolynomial; 018import edu.jas.poly.GenPolynomialRing; 019import edu.jas.poly.PolynomialList; 020import edu.jas.poly.TermOrder; 021 022 023/** 024 * MultiplicativeSet tests with JUnit. 025 * @author Heinz Kredel 026 */ 027public class MultiplicativeSetTest extends TestCase { 028 029 030 031 /** 032 * main 033 */ 034 public static void main(String[] args) { 035 junit.textui.TestRunner.run(suite()); 036 } 037 038 039 /** 040 * Constructs a <CODE>MultiplicativeSetTest</CODE> object. 041 * @param name String. 042 */ 043 public MultiplicativeSetTest(String name) { 044 super(name); 045 } 046 047 048 /** 049 * suite. 050 */ 051 public static Test suite() { 052 TestSuite suite = new TestSuite(MultiplicativeSetTest.class); 053 return suite; 054 } 055 056 057 TermOrder to; 058 059 060 GenPolynomialRing<BigRational> fac; 061 062 063 List<GenPolynomial<BigRational>> L; 064 065 066 PolynomialList<BigRational> F; 067 068 069 List<GenPolynomial<BigRational>> G; 070 071 072 List<? extends GenPolynomial<BigRational>> M; 073 074 075 GenPolynomial<BigRational> a; 076 077 078 GenPolynomial<BigRational> b; 079 080 081 GenPolynomial<BigRational> c; 082 083 084 GenPolynomial<BigRational> d; 085 086 087 GenPolynomial<BigRational> e; 088 089 090 int rl = 3; //4; //3; 091 092 093 int kl = 4; //10 094 095 096 int ll = 5; //7 097 098 099 int el = 3; 100 101 102 float q = 0.2f; //0.4f 103 104 105 @Override 106 protected void setUp() { 107 BigRational coeff = new BigRational(17, 1); 108 to = new TermOrder( /*TermOrder.INVLEX*/); 109 fac = new GenPolynomialRing<BigRational>(coeff, rl, to); 110 a = b = c = d = e = null; 111 } 112 113 114 @Override 115 protected void tearDown() { 116 a = b = c = d = e = null; 117 fac = null; 118 ComputerThreads.terminate(); 119 } 120 121 122 /** 123 * Test multiplicative set contained. 124 * 125 */ 126 public void testContaines() { 127 128 a = fac.random(kl, ll, el, q); 129 b = fac.random(kl, ll, el, q); 130 c = fac.random(kl, ll, el, q); 131 d = fac.random(kl, ll, el, q); 132 e = d; //fac.random(kl, ll, el, q ); 133 134 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 135 return; 136 } 137 138 MultiplicativeSet<BigRational> ms = new MultiplicativeSet<BigRational>(fac); 139 //System.out.println("ms = " + ms); 140 //System.out.println("a = " + a); 141 //System.out.println("b = " + b); 142 //System.out.println("c = " + c); 143 144 assertTrue("isEmpty ", ms.isEmpty()); 145 146 if (!a.isConstant()) { 147 assertFalse("not contained ", ms.contains(a)); 148 } 149 150 MultiplicativeSet<BigRational> ms2 = ms.add(a); 151 //System.out.println("ms2 = " + ms2); 152 153 if (!a.isConstant()) { 154 assertFalse("not isEmpty ", ms2.isEmpty()); 155 assertTrue("contained ", ms2.contains(a)); 156 } 157 158 if (!a.equals(b) && !b.isConstant()) { 159 assertFalse("not contained ", ms2.contains(b)); 160 } 161 162 MultiplicativeSet<BigRational> ms3 = ms2.add(b); 163 //System.out.println("ms3 = " + ms3); 164 165 if (!b.isConstant()) { 166 assertFalse("not isEmpty ", ms3.isEmpty()); 167 } 168 assertTrue("contained ", ms3.contains(a)); 169 assertTrue("contained ", ms3.contains(b)); 170 171 if (!a.equals(c) && !b.equals(c) && !c.isConstant()) { 172 assertFalse("not contained ", ms3.contains(c)); 173 } 174 175 e = a.multiply(b); 176 //System.out.println("e = " + e); 177 if (!e.isConstant()) { 178 assertTrue("contained ", ms3.contains(e)); 179 } 180 181 MultiplicativeSet<BigRational> ms4 = ms3.add(e); 182 //System.out.println("ms4 = " + ms4); 183 184 assertTrue("m3 == m4 ", ms3.equals(ms4)); 185 186 } 187 188 189 /** 190 * Test multiplicative set removeFactors. 191 * 192 */ 193 public void testRemoveFactors() { 194 195 a = fac.random(kl, ll, el, q); 196 b = fac.random(kl, ll, el, q); 197 c = fac.random(kl, ll, el, q); 198 d = fac.random(kl, ll, el, q); 199 e = d; //fac.random(kl, ll, el, q ); 200 201 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 202 return; 203 } 204 205 MultiplicativeSet<BigRational> ms = new MultiplicativeSet<BigRational>(fac); 206 //System.out.println("ms = " + ms); 207 //System.out.println("a = " + a); 208 //System.out.println("b = " + b); 209 //System.out.println("c = " + c); 210 211 assertTrue("isEmpty ", ms.isEmpty()); 212 213 e = ms.removeFactors(a); 214 //System.out.println("e = " + e); 215 assertEquals("a == remove(a) ", a, e); 216 217 MultiplicativeSet<BigRational> ms2 = ms.add(a); 218 //System.out.println("ms2 = " + ms2); 219 220 if (!a.isConstant()) { 221 assertFalse("not isEmpty ", ms2.isEmpty()); 222 assertTrue("contained ", ms2.contains(a)); 223 224 e = ms2.removeFactors(a); 225 //System.out.println("e = " + e); 226 assertTrue("1 == remove(a) ", e.isConstant()); 227 228 if (!b.isConstant()) { 229 e = ms2.removeFactors(b); 230 //System.out.println("e = " + e); 231 assertEquals("b == remove(b) ", e, b); 232 } 233 } 234 235 d = a.multiply(b); 236 MultiplicativeSet<BigRational> ms3 = ms2.add(d); 237 //System.out.println("ms3 = " + ms3); 238 239 if (!d.isConstant()) { 240 assertFalse("not isEmpty ", ms3.isEmpty()); 241 242 e = ms3.removeFactors(a); 243 //System.out.println("e = " + e); 244 assertTrue("1 == remove(a) ", e.isConstant()); 245 246 e = ms3.removeFactors(b); 247 //System.out.println("e = " + e); 248 assertTrue("1 == remove(b) ", e.isConstant()); 249 250 e = ms3.removeFactors(d); 251 //System.out.println("e = " + e); 252 assertTrue("1 == remove(a*b) ", e.isConstant()); 253 254 if (!c.isConstant()) { 255 e = ms3.removeFactors(c); 256 //System.out.println("e = " + e); 257 assertEquals("c == remove(c) ", e, c); 258 } 259 } 260 } 261 262 263 /** 264 * Test co-prime multiplicative set contained. 265 * 266 */ 267 public void testCoPrimeContaines() { 268 269 a = fac.random(kl, ll, el, q); 270 b = fac.random(kl, ll, el, q); 271 c = fac.random(kl, ll, el, q); 272 d = fac.random(kl, ll, el, q); 273 e = d; //fac.random(kl, ll, el, q ); 274 275 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 276 return; 277 } 278 279 MultiplicativeSet<BigRational> ms = new MultiplicativeSetCoPrime<BigRational>(fac); 280 //System.out.println("ms = " + ms); 281 //System.out.println("a = " + a); 282 //System.out.println("b = " + b); 283 //System.out.println("c = " + c); 284 285 assertTrue("isEmpty ", ms.isEmpty()); 286 287 if (!a.isConstant()) { 288 assertFalse("not contained ", ms.contains(a)); 289 } 290 291 MultiplicativeSet<BigRational> ms2 = ms.add(a); 292 //System.out.println("ms2 = " + ms2); 293 294 if (!a.isConstant()) { 295 assertFalse("not isEmpty ", ms2.isEmpty()); 296 assertTrue("contained ", ms2.contains(a)); 297 } 298 299 if (!a.equals(b) && !b.isConstant()) { 300 assertFalse("not contained " + ms2, ms2.contains(b)); 301 } 302 303 MultiplicativeSet<BigRational> ms3 = ms2.add(b); 304 //System.out.println("ms3 = " + ms3); 305 306 if (!b.isConstant()) { 307 assertFalse("not isEmpty ", ms3.isEmpty()); 308 } 309 assertTrue("contained ", ms3.contains(a)); 310 assertTrue("contained ", ms3.contains(b)); 311 312 if (!a.equals(c) && !b.equals(c) && !c.isConstant()) { 313 assertFalse("not contained " + ms3, ms3.contains(c)); 314 } 315 316 e = a.multiply(b); 317 //System.out.println("e = " + e); 318 if (!e.isConstant()) { 319 assertTrue("contained ", ms3.contains(e)); 320 } 321 322 MultiplicativeSet<BigRational> ms4 = ms3.add(e); 323 //System.out.println("ms4 = " + ms4); 324 325 assertTrue("m3 == m4 ", ms3.equals(ms4)); 326 } 327 328 329 /** 330 * Test co-prime multiplicative set removeFactors. 331 * 332 */ 333 public void testCoPrimeRemoveFactors() { 334 335 a = fac.random(kl, ll, el, q); 336 b = fac.random(kl, ll, el, q); 337 c = fac.random(kl, ll, el, q); 338 d = fac.random(kl, ll, el, q); 339 e = d; //fac.random(kl, ll, el, q ); 340 341 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 342 return; 343 } 344 345 MultiplicativeSet<BigRational> ms = new MultiplicativeSetCoPrime<BigRational>(fac); 346 //System.out.println("ms = " + ms); 347 //System.out.println("a = " + a); 348 //System.out.println("b = " + b); 349 //System.out.println("c = " + c); 350 351 assertTrue("isEmpty ", ms.isEmpty()); 352 353 e = ms.removeFactors(a); 354 //System.out.println("e = " + e); 355 assertEquals("a == remove(a) ", a, e); 356 357 MultiplicativeSet<BigRational> ms2 = ms.add(a); 358 //System.out.println("ms2 = " + ms2); 359 360 if (!a.isConstant()) { 361 assertFalse("not isEmpty ", ms2.isEmpty()); 362 assertTrue("contained ", ms2.contains(a)); 363 364 e = ms2.removeFactors(a); 365 //System.out.println("e = " + e); 366 assertTrue("1 == remove(a) ", e.isConstant()); 367 368 if (!b.isConstant()) { 369 e = ms2.removeFactors(b); 370 //System.out.println("e = " + e); 371 assertEquals("b == remove(b) ", e, b); 372 } 373 } 374 375 d = a.multiply(b); 376 MultiplicativeSet<BigRational> ms3 = ms2.add(d); 377 //System.out.println("ms3 = " + ms3); 378 379 if (!d.isConstant()) { 380 assertFalse("not isEmpty ", ms3.isEmpty()); 381 382 e = ms3.removeFactors(a); 383 //System.out.println("e = " + e); 384 assertTrue("1 == remove(a) ", e.isConstant()); 385 386 e = ms3.removeFactors(b); 387 //System.out.println("e = " + e); 388 assertTrue("1 == remove(b) ", e.isConstant()); 389 390 e = ms3.removeFactors(d); 391 //System.out.println("e = " + e); 392 assertTrue("1 == remove(a*b) ", e.isConstant()); 393 394 if (!c.isConstant()) { 395 e = ms3.removeFactors(c); 396 //System.out.println("e = " + e); 397 assertEquals("c == remove(c) ", e, c); 398 } 399 } 400 } 401 402 403 /** 404 * Test squarefree multiplicative set contained. 405 * 406 */ 407 public void testSquarefreeContaines() { 408 409 a = fac.random(kl, ll, el, q); 410 b = fac.random(kl, ll, el, q); 411 c = fac.random(kl, ll, el, q); 412 d = fac.random(kl, ll, el, q); 413 e = d; //fac.random(kl, ll, el, q ); 414 415 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 416 return; 417 } 418 419 MultiplicativeSet<BigRational> ms = new MultiplicativeSetSquarefree<BigRational>(fac); 420 //System.out.println("ms = " + ms); 421 //System.out.println("a = " + a); 422 //System.out.println("b = " + b); 423 //System.out.println("c = " + c); 424 425 assertTrue("isEmpty ", ms.isEmpty()); 426 427 if (!a.isConstant()) { 428 assertFalse("not contained ", ms.contains(a)); 429 } 430 431 MultiplicativeSet<BigRational> ms2 = ms.add(a); 432 //System.out.println("ms2 = " + ms2); 433 434 if (!a.isConstant()) { 435 assertFalse("not isEmpty ", ms2.isEmpty()); 436 assertTrue("contained ", ms2.contains(a)); 437 } 438 439 if (!a.equals(b) && !b.isConstant()) { 440 assertFalse("not contained " + ms2, ms2.contains(b)); 441 } 442 443 MultiplicativeSet<BigRational> ms3 = ms2.add(b); 444 //System.out.println("ms3 = " + ms3); 445 446 if (!b.isConstant()) { 447 assertFalse("not isEmpty ", ms3.isEmpty()); 448 } 449 assertTrue("contained ", ms3.contains(a)); 450 assertTrue("contained ", ms3.contains(b)); 451 452 if (!a.equals(c) && !b.equals(c) && !c.isConstant()) { 453 assertFalse("not contained " + ms3, ms3.contains(c)); 454 } 455 456 e = a.multiply(b); 457 //System.out.println("e = " + e); 458 if (!e.isConstant()) { 459 assertTrue("contained ", ms3.contains(e)); 460 } 461 462 MultiplicativeSet<BigRational> ms4 = ms3.add(e); 463 //System.out.println("ms4 = " + ms4); 464 465 assertTrue("m3 == m4 ", ms3.equals(ms4)); 466 } 467 468 469 /** 470 * Test squarefree multiplicative set removeFactors. 471 * 472 */ 473 public void testSquarefreeRemoveFactors() { 474 475 a = fac.random(kl, ll, el, q); 476 b = fac.random(kl, ll, el, q); 477 c = fac.random(kl, ll, el, q); 478 d = fac.random(kl, ll, el, q); 479 e = d; //fac.random(kl, ll, el, q ); 480 481 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 482 return; 483 } 484 485 MultiplicativeSet<BigRational> ms = new MultiplicativeSetSquarefree<BigRational>(fac); 486 //System.out.println("ms = " + ms); 487 //System.out.println("a = " + a); 488 //System.out.println("b = " + b); 489 //System.out.println("c = " + c); 490 491 assertTrue("isEmpty ", ms.isEmpty()); 492 493 e = ms.removeFactors(a); 494 //System.out.println("e = " + e); 495 assertEquals("a == remove(a) ", a, e); 496 497 MultiplicativeSet<BigRational> ms2 = ms.add(a); 498 //System.out.println("ms2 = " + ms2); 499 500 if (!a.isConstant()) { 501 assertFalse("not isEmpty ", ms2.isEmpty()); 502 assertTrue("contained ", ms2.contains(a)); 503 504 e = ms2.removeFactors(a); 505 //System.out.println("e = " + e); 506 assertTrue("1 == remove(a) ", e.isConstant()); 507 508 if (!b.isConstant()) { 509 e = ms2.removeFactors(b); 510 //System.out.println("e = " + e); 511 assertEquals("b == remove(b) ", e, b); 512 } 513 } 514 515 d = a.multiply(b); 516 MultiplicativeSet<BigRational> ms3 = ms2.add(d); 517 //System.out.println("ms3 = " + ms3); 518 519 if (!d.isConstant()) { 520 assertFalse("not isEmpty ", ms3.isEmpty()); 521 522 e = ms3.removeFactors(a); 523 //System.out.println("e = " + e); 524 assertTrue("1 == remove(a) ", e.isConstant()); 525 526 e = ms3.removeFactors(b); 527 //System.out.println("e = " + e); 528 assertTrue("1 == remove(b) ", e.isConstant()); 529 530 e = ms3.removeFactors(d); 531 //System.out.println("e = " + e); 532 assertTrue("1 == remove(a*b) ", e.isConstant()); 533 534 if (!c.isConstant()) { 535 e = ms3.removeFactors(c); 536 //System.out.println("e = " + e); 537 assertEquals("c == remove(c) ", e, c); 538 } 539 } 540 } 541 542 543 /** 544 * Test irreducible multiplicative set contained. 545 * 546 */ 547 public void testFactorsContaines() { 548 549 a = fac.random(kl - 2, ll - 2, el, q); 550 b = fac.random(kl - 2, ll - 2, el, q); 551 c = fac.random(kl - 2, ll - 2, el, q).monic(); 552 d = fac.random(kl, ll, el, q); 553 e = d; //fac.random(kl, ll, el, q ); 554 555 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 556 return; 557 } 558 559 MultiplicativeSet<BigRational> ms = new MultiplicativeSetFactors<BigRational>(fac); 560 //System.out.println("ms = " + ms); 561 //System.out.println("a = " + a); 562 //System.out.println("b = " + b); 563 //System.out.println("c = " + c); 564 565 assertTrue("isEmpty ", ms.isEmpty()); 566 567 if (!a.isConstant()) { 568 assertFalse("not contained ", ms.contains(a)); 569 } 570 571 MultiplicativeSet<BigRational> ms2 = ms.add(a); 572 //System.out.println("ms2 = " + ms2); 573 574 if (!a.isConstant()) { 575 assertFalse("not isEmpty ", ms2.isEmpty()); 576 assertTrue("contained ", ms2.contains(a)); 577 578 if (!a.equals(b) && !b.isConstant()) { 579 assertFalse("not contained " + ms2 + ", " + b, ms2.contains(b)); 580 } 581 } 582 583 MultiplicativeSet<BigRational> ms3 = ms2.add(b); 584 //System.out.println("ms3 = " + ms3); 585 586 assertTrue("contained ", ms3.contains(a)); 587 assertTrue("contained ", ms3.contains(b)); 588 589 if (!b.isConstant()) { 590 assertFalse("not isEmpty ", ms3.isEmpty()); 591 592 if (!a.monic().equals(c) && !b.monic().equals(c) && !c.isConstant() && !ms3.mset.contains(c)) { 593 assertFalse("not contained " + ms3 + ", " + c, ms3.contains(c)); 594 } 595 } 596 597 e = a.multiply(b); 598 //System.out.println("e = " + e); 599 if (!e.isConstant()) { 600 assertTrue("contained ", ms3.contains(e)); 601 } 602 603 MultiplicativeSet<BigRational> ms4 = ms3.add(e); 604 //System.out.println("ms4 = " + ms4); 605 606 assertTrue("m3 == m4 ", ms3.equals(ms4)); 607 } 608 609 610 /** 611 * Test irreducible multiplicative set removeFactors. 612 * 613 */ 614 public void testFactorsRemoveFactors() { 615 616 a = fac.random(kl - 2, ll - 2, el, q); 617 b = fac.random(kl - 2, ll - 2, el, q); 618 c = fac.random(kl - 2, ll - 2, el, q); 619 d = fac.random(kl - 2, ll - 2, el, q); 620 e = d; //fac.random(kl, ll, el, q ); 621 622 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 623 return; 624 } 625 626 MultiplicativeSet<BigRational> ms = new MultiplicativeSetFactors<BigRational>(fac); 627 //System.out.println("ms = " + ms); 628 //System.out.println("a = " + a); 629 //System.out.println("b = " + b); 630 //System.out.println("c = " + c); 631 632 assertTrue("isEmpty ", ms.isEmpty()); 633 634 e = ms.removeFactors(a); 635 //System.out.println("e = " + e); 636 assertEquals("a == remove(a) ", a, e); 637 638 MultiplicativeSet<BigRational> ms2 = ms.add(a); 639 //System.out.println("ms2 = " + ms2); 640 641 if (!a.isConstant()) { 642 assertFalse("not isEmpty ", ms2.isEmpty()); 643 assertTrue("contained ", ms2.contains(a)); 644 645 e = ms2.removeFactors(a); 646 //System.out.println("e = " + e); 647 assertTrue("1 == remove(a) ", e.isConstant()); 648 649 if (!b.isConstant()) { 650 e = ms2.removeFactors(b); 651 //System.out.println("e = " + e); 652 assertTrue("remove(b) | b ", b.remainder(e).isZERO()); 653 } 654 } 655 656 d = a.multiply(b); 657 MultiplicativeSet<BigRational> ms3 = ms2.add(d); 658 //System.out.println("ms3 = " + ms3); 659 660 if (!d.isConstant()) { 661 assertFalse("not isEmpty ", ms3.isEmpty()); 662 663 e = ms3.removeFactors(a); 664 //System.out.println("e = " + e); 665 assertTrue("1 == remove(a) ", e.isConstant()); 666 667 e = ms3.removeFactors(b); 668 //System.out.println("e = " + e); 669 assertTrue("1 == remove(b) ", e.isConstant()); 670 671 e = ms3.removeFactors(d); 672 //System.out.println("e = " + e); 673 assertTrue("1 == remove(a*b) ", e.isConstant()); 674 675 if (!c.isConstant()) { 676 e = ms3.removeFactors(c); 677 //System.out.println("e = " + e); 678 assertTrue("remove(c) | c ", c.remainder(e).isZERO()); 679 } 680 } 681 } 682 683}