001/* 002 * $Id: FactorMoreTest.java 3888 2012-02-11 18:32:34Z kredel $ 003 */ 004 005package edu.jas.ufd; 006 007 008import java.util.SortedMap; 009 010import org.apache.log4j.BasicConfigurator; 011 012import junit.framework.Test; 013import junit.framework.TestCase; 014import junit.framework.TestSuite; 015 016import edu.jas.arith.BigInteger; 017import edu.jas.arith.BigRational; 018import edu.jas.arith.ModInteger; 019import edu.jas.arith.ModIntegerRing; 020import edu.jas.kern.ComputerThreads; 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 FactorMoreTest 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>FactorTest</CODE> object. 045 * @param name String. 046 */ 047 public FactorMoreTest(String name) { 048 super(name); 049 } 050 051 052 /** 053 */ 054 public static Test suite() { 055 TestSuite suite = new TestSuite(FactorMoreTest.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 = 3; 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 */ 090 public void testDummy() { 091 } 092 093 094 /** 095 * Test integral function factorization. 096 */ 097 public void testIntegralFunctionFactorization() { 098 099 TermOrder to = new TermOrder(TermOrder.INVLEX); 100 BigRational cfac = new BigRational(1); 101 String[] qvars = new String[] { "t" }; 102 GenPolynomialRing<BigRational> pfac = new GenPolynomialRing<BigRational>(cfac, 1, to, qvars); 103 GenPolynomial<BigRational> t = pfac.univariate(0); 104 105 FactorAbstract<BigRational> fac = new FactorRational(); 106 107 String[] vars = new String[] { "x" }; 108 GenPolynomialRing<GenPolynomial<BigRational>> pqfac = new GenPolynomialRing<GenPolynomial<BigRational>>( 109 pfac, 1, to, vars); 110 GenPolynomial<GenPolynomial<BigRational>> x = pqfac.univariate(0); 111 GenPolynomial<GenPolynomial<BigRational>> x2 = pqfac.univariate(0, 2); 112 113 for (int i = 1; i < 3; i++) { 114 int facs = 0; 115 GenPolynomial<GenPolynomial<BigRational>> a; 116 GenPolynomial<GenPolynomial<BigRational>> b = pqfac.random(2, 3, el, q); 117 //b = b.monic(); 118 //b = b.multiply(b); 119 GenPolynomial<GenPolynomial<BigRational>> c = pqfac.random(2, 3, el, q); 120 //c = c.monic(); 121 if (c.degree() < 1) { 122 c = x2.subtract(pqfac.getONE().multiply(t)); 123 } 124 if (b.degree() < 1) { 125 b = x.sum(pqfac.getONE()); 126 } 127 128 if (c.degree() > 0) { 129 facs++; 130 } 131 if (b.degree() > 0) { 132 facs++; 133 } 134 a = c.multiply(b); 135 //System.out.println("\na = " + a); 136 //System.out.println("b = " + b); 137 //System.out.println("c = " + c); 138 139 SortedMap<GenPolynomial<GenPolynomial<BigRational>>, Long> sm = fac.recursiveFactors(a); 140 //System.out.println("\na = " + a); 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, sf >= facs); 151 } 152 153 boolean tt = fac.isRecursiveFactorization(a, sm); 154 //System.out.println("t = " + tt); 155 assertTrue("prod(factor(a)) = a", tt); 156 } 157 ComputerThreads.terminate(); 158 } 159 160 161 /** 162 * Test integer integral function factorization. 163 */ 164 public void testIntegerIntegralFunctionFactorization() { 165 166 TermOrder to = new TermOrder(TermOrder.INVLEX); 167 BigInteger cfac = new BigInteger(1); 168 String[] qvars = new String[] { "t" }; 169 GenPolynomialRing<BigInteger> pfac = new GenPolynomialRing<BigInteger>(cfac, 1, to, qvars); 170 GenPolynomial<BigInteger> t = pfac.univariate(0); 171 172 FactorAbstract<BigInteger> fac = new FactorInteger<ModInteger>(); 173 174 String[] vars = new String[] { "x" }; 175 GenPolynomialRing<GenPolynomial<BigInteger>> pqfac = new GenPolynomialRing<GenPolynomial<BigInteger>>( 176 pfac, 1, to, vars); 177 GenPolynomial<GenPolynomial<BigInteger>> x = pqfac.univariate(0); 178 GenPolynomial<GenPolynomial<BigInteger>> x2 = pqfac.univariate(0, 2); 179 180 for (int i = 1; i < 3; i++) { 181 int facs = 0; 182 GenPolynomial<GenPolynomial<BigInteger>> a; 183 GenPolynomial<GenPolynomial<BigInteger>> b = pqfac.random(2, 3, el, q); 184 //b = b.monic(); 185 //b = b.multiply(b); 186 GenPolynomial<GenPolynomial<BigInteger>> c = pqfac.random(2, 3, el, q); 187 //c = c.monic(); 188 if (c.degree() < 1) { 189 c = x2.subtract(pqfac.getONE().multiply(t)); 190 } 191 if (b.degree() < 1) { 192 b = x.sum(pqfac.getONE()); 193 } 194 195 if (c.degree() > 0) { 196 facs++; 197 } 198 if (b.degree() > 0) { 199 facs++; 200 } 201 a = c.multiply(b); 202 //a = pqfac.parse("( ( -26 t - 91 ) x^3 - ( 13 t + 26 ) x^2 + ( 6 t^2 + 21 t ) x + ( 3 t^2 + 6 t ) )"); 203 //a = pqfac.parse("( -3 x^3 + ( t - 1 ) x^2 + ( 3 t ) x - ( t^2 - t ) )"); 204 //System.out.println("\na = " + a); 205 //System.out.println("b = " + b); 206 //System.out.println("c = " + c); 207 208 SortedMap<GenPolynomial<GenPolynomial<BigInteger>>, Long> sm = fac.recursiveFactors(a); 209 //System.out.println("\na = " + a); 210 //System.out.println("sm = " + sm); 211 212 if (sm.size() >= facs) { 213 assertTrue("#facs < " + facs, sm.size() >= facs); 214 } else { 215 long sf = 0; 216 for (Long e : sm.values()) { 217 sf += e; 218 } 219 assertTrue("#facs < " + facs + ", sm = " + sm, sf >= facs); 220 } 221 222 boolean tt = fac.isRecursiveFactorization(a, sm); 223 //System.out.println("t = " + tt); 224 assertTrue("prod(factor(a)) = a", tt); 225 } 226 ComputerThreads.terminate(); 227 } 228 229 230 /** 231 * Test rational function factorization. 232 */ 233 public void testRationalFunctionFactorization() { 234 235 TermOrder to = new TermOrder(TermOrder.INVLEX); 236 BigRational cfac = new BigRational(1); 237 String[] qvars = new String[] { "t" }; 238 GenPolynomialRing<BigRational> pfac = new GenPolynomialRing<BigRational>(cfac, 1, to, qvars); 239 QuotientRing<BigRational> qfac = new QuotientRing<BigRational>(pfac); 240 Quotient<BigRational> t = qfac.generators().get(1); 241 242 FactorQuotient<BigRational> fac = new FactorQuotient<BigRational>(qfac); 243 244 String[] vars = new String[] { "x" }; 245 GenPolynomialRing<Quotient<BigRational>> pqfac = new GenPolynomialRing<Quotient<BigRational>>(qfac, 246 1, to, vars); 247 GenPolynomial<Quotient<BigRational>> x = pqfac.univariate(0); 248 GenPolynomial<Quotient<BigRational>> x2 = pqfac.univariate(0, 2); 249 250 for (int i = 1; i < 3; i++) { 251 int facs = 0; 252 GenPolynomial<Quotient<BigRational>> a; 253 GenPolynomial<Quotient<BigRational>> b = pqfac.random(2, 3, el, q); 254 //b = b.monic(); 255 //b = b.multiply(b); 256 GenPolynomial<Quotient<BigRational>> c = pqfac.random(2, 3, el, q); 257 //c = c.monic(); 258 if (c.degree() < 1) { 259 c = x2.subtract(pqfac.getONE().multiply(t)); 260 } 261 if (b.degree() < 1) { 262 b = x.sum(pqfac.getONE()); 263 } 264 265 if (c.degree() > 0) { 266 facs++; 267 } 268 if (b.degree() > 0) { 269 facs++; 270 } 271 a = c.multiply(b); 272 //System.out.println("\na = " + a); 273 //System.out.println("b = " + b); 274 //System.out.println("c = " + c); 275 276 SortedMap<GenPolynomial<Quotient<BigRational>>, Long> sm = fac.factors(a); 277 //System.out.println("\na = " + a); 278 //System.out.println("sm = " + sm); 279 280 if (sm.size() >= facs) { 281 assertTrue("#facs < " + facs, sm.size() >= facs); 282 } else { 283 long sf = 0; 284 for (Long e : sm.values()) { 285 sf += e; 286 } 287 assertTrue("#facs < " + facs, sf >= facs); 288 } 289 290 boolean tt = fac.isFactorization(a, sm); 291 //System.out.println("t = " + tt); 292 assertTrue("prod(factor(a)) = a", tt); 293 } 294 ComputerThreads.terminate(); 295 } 296 297 298 /** 299 * Test modular rational function factorization. 300 */ 301 public void testModularRationalFunctionFactorization() { 302 303 TermOrder to = new TermOrder(TermOrder.INVLEX); 304 ModIntegerRing cfac = new ModIntegerRing(19, true); 305 String[] qvars = new String[] { "t" }; 306 GenPolynomialRing<ModInteger> pfac = new GenPolynomialRing<ModInteger>(cfac, 1, to, qvars); 307 QuotientRing<ModInteger> qfac = new QuotientRing<ModInteger>(pfac); 308 Quotient<ModInteger> t = qfac.generators().get(1); 309 310 FactorQuotient<ModInteger> fac = new FactorQuotient<ModInteger>(qfac); 311 312 String[] vars = new String[] { "x" }; 313 GenPolynomialRing<Quotient<ModInteger>> pqfac = new GenPolynomialRing<Quotient<ModInteger>>(qfac, 1, 314 to, vars); 315 GenPolynomial<Quotient<ModInteger>> x = pqfac.univariate(0); 316 GenPolynomial<Quotient<ModInteger>> x2 = pqfac.univariate(0, 2); 317 318 for (int i = 1; i < 3; i++) { 319 int facs = 0; 320 GenPolynomial<Quotient<ModInteger>> a; 321 GenPolynomial<Quotient<ModInteger>> b = pqfac.random(2, 3, el, q); 322 //b = b.monic(); 323 //b = b.multiply(b); 324 GenPolynomial<Quotient<ModInteger>> c = pqfac.random(2, 3, el, q); 325 //c = c.monic(); 326 if (c.degree() < 1) { 327 c = x2.subtract(pqfac.getONE().multiply(t)); 328 } 329 if (b.degree() < 1) { 330 b = x.sum(pqfac.getONE()); 331 } 332 333 if (c.degree() > 0) { 334 facs++; 335 } 336 if (b.degree() > 0) { 337 facs++; 338 } 339 a = c.multiply(b); 340 //System.out.println("\na = " + a); 341 //System.out.println("b = " + b); 342 //System.out.println("c = " + c); 343 344 SortedMap<GenPolynomial<Quotient<ModInteger>>, Long> sm = fac.factors(a); 345 //System.out.println("\na = " + a); 346 //System.out.println("sm = " + sm); 347 348 if (sm.size() >= facs) { 349 assertTrue("#facs < " + facs, sm.size() >= facs); 350 } else { 351 long sf = 0; 352 for (Long e : sm.values()) { 353 sf += e; 354 } 355 assertTrue("#facs < " + facs, sf >= facs); 356 } 357 358 boolean tt = fac.isFactorization(a, sm); 359 //System.out.println("t = " + tt); 360 assertTrue("prod(factor(a)) = a", tt); 361 } 362 ComputerThreads.terminate(); 363 } 364 365}