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