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