001 /* 002 * $Id: FactorMoreTest.java 3742 2011-08-20 20:14:04Z kredel $ 003 */ 004 005 package edu.jas.ufd; 006 007 008 import java.util.SortedMap; 009 010 import org.apache.log4j.BasicConfigurator; 011 012 import junit.framework.Test; 013 import junit.framework.TestCase; 014 import junit.framework.TestSuite; 015 016 import edu.jas.arith.BigInteger; 017 import edu.jas.arith.BigRational; 018 import edu.jas.arith.ModInteger; 019 import edu.jas.arith.ModIntegerRing; 020 import edu.jas.kern.ComputerThreads; 021 import edu.jas.poly.GenPolynomial; 022 import edu.jas.poly.GenPolynomialRing; 023 import edu.jas.poly.TermOrder; 024 025 026 /** 027 * Factor tests with JUnit. 028 * @author Heinz Kredel. 029 */ 030 031 public 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 //System.out.println("\na = " + a); 203 //System.out.println("b = " + b); 204 //System.out.println("c = " + c); 205 206 SortedMap<GenPolynomial<GenPolynomial<BigInteger>>, Long> sm = fac.recursiveFactors(a); 207 //System.out.println("\na = " + a); 208 //System.out.println("sm = " + sm); 209 210 if (sm.size() >= facs) { 211 assertTrue("#facs < " + facs, sm.size() >= facs); 212 } else { 213 long sf = 0; 214 for (Long e : sm.values()) { 215 sf += e; 216 } 217 assertTrue("#facs < " + facs + ", sm = " + sm, sf >= facs); 218 } 219 220 boolean tt = fac.isRecursiveFactorization(a, sm); 221 //System.out.println("t = " + tt); 222 assertTrue("prod(factor(a)) = a", tt); 223 } 224 ComputerThreads.terminate(); 225 } 226 227 228 /** 229 * Test rational function factorization. 230 */ 231 public void testRationalFunctionFactorization() { 232 233 TermOrder to = new TermOrder(TermOrder.INVLEX); 234 BigRational cfac = new BigRational(1); 235 String[] qvars = new String[] { "t" }; 236 GenPolynomialRing<BigRational> pfac = new GenPolynomialRing<BigRational>(cfac, 1, to, qvars); 237 QuotientRing<BigRational> qfac = new QuotientRing<BigRational>(pfac); 238 Quotient<BigRational> t = qfac.generators().get(1); 239 240 FactorQuotient<BigRational> fac = new FactorQuotient<BigRational>(qfac); 241 242 String[] vars = new String[] { "x" }; 243 GenPolynomialRing<Quotient<BigRational>> pqfac = new GenPolynomialRing<Quotient<BigRational>>(qfac, 244 1, to, vars); 245 GenPolynomial<Quotient<BigRational>> x = pqfac.univariate(0); 246 GenPolynomial<Quotient<BigRational>> x2 = pqfac.univariate(0, 2); 247 248 for (int i = 1; i < 3; i++) { 249 int facs = 0; 250 GenPolynomial<Quotient<BigRational>> a; 251 GenPolynomial<Quotient<BigRational>> b = pqfac.random(2, 3, el, q); 252 //b = b.monic(); 253 //b = b.multiply(b); 254 GenPolynomial<Quotient<BigRational>> c = pqfac.random(2, 3, el, q); 255 //c = c.monic(); 256 if (c.degree() < 1) { 257 c = x2.subtract(pqfac.getONE().multiply(t)); 258 } 259 if (b.degree() < 1) { 260 b = x.sum(pqfac.getONE()); 261 } 262 263 if (c.degree() > 0) { 264 facs++; 265 } 266 if (b.degree() > 0) { 267 facs++; 268 } 269 a = c.multiply(b); 270 //System.out.println("\na = " + a); 271 //System.out.println("b = " + b); 272 //System.out.println("c = " + c); 273 274 SortedMap<GenPolynomial<Quotient<BigRational>>, Long> sm = fac.factors(a); 275 //System.out.println("\na = " + a); 276 //System.out.println("sm = " + sm); 277 278 if (sm.size() >= facs) { 279 assertTrue("#facs < " + facs, sm.size() >= facs); 280 } else { 281 long sf = 0; 282 for (Long e : sm.values()) { 283 sf += e; 284 } 285 assertTrue("#facs < " + facs, sf >= facs); 286 } 287 288 boolean tt = fac.isFactorization(a, sm); 289 //System.out.println("t = " + tt); 290 assertTrue("prod(factor(a)) = a", tt); 291 } 292 ComputerThreads.terminate(); 293 } 294 295 296 /** 297 * Test modular rational function factorization. 298 */ 299 public void testModularRationalFunctionFactorization() { 300 301 TermOrder to = new TermOrder(TermOrder.INVLEX); 302 ModIntegerRing cfac = new ModIntegerRing(19, true); 303 String[] qvars = new String[] { "t" }; 304 GenPolynomialRing<ModInteger> pfac = new GenPolynomialRing<ModInteger>(cfac, 1, to, qvars); 305 QuotientRing<ModInteger> qfac = new QuotientRing<ModInteger>(pfac); 306 Quotient<ModInteger> t = qfac.generators().get(1); 307 308 FactorQuotient<ModInteger> fac = new FactorQuotient<ModInteger>(qfac); 309 310 String[] vars = new String[] { "x" }; 311 GenPolynomialRing<Quotient<ModInteger>> pqfac = new GenPolynomialRing<Quotient<ModInteger>>(qfac, 1, 312 to, vars); 313 GenPolynomial<Quotient<ModInteger>> x = pqfac.univariate(0); 314 GenPolynomial<Quotient<ModInteger>> x2 = pqfac.univariate(0, 2); 315 316 for (int i = 1; i < 3; i++) { 317 int facs = 0; 318 GenPolynomial<Quotient<ModInteger>> a; 319 GenPolynomial<Quotient<ModInteger>> b = pqfac.random(2, 3, el, q); 320 //b = b.monic(); 321 //b = b.multiply(b); 322 GenPolynomial<Quotient<ModInteger>> c = pqfac.random(2, 3, el, q); 323 //c = c.monic(); 324 if (c.degree() < 1) { 325 c = x2.subtract(pqfac.getONE().multiply(t)); 326 } 327 if (b.degree() < 1) { 328 b = x.sum(pqfac.getONE()); 329 } 330 331 if (c.degree() > 0) { 332 facs++; 333 } 334 if (b.degree() > 0) { 335 facs++; 336 } 337 a = c.multiply(b); 338 //System.out.println("\na = " + a); 339 //System.out.println("b = " + b); 340 //System.out.println("c = " + c); 341 342 SortedMap<GenPolynomial<Quotient<ModInteger>>, Long> sm = fac.factors(a); 343 //System.out.println("\na = " + a); 344 //System.out.println("sm = " + sm); 345 346 if (sm.size() >= facs) { 347 assertTrue("#facs < " + facs, sm.size() >= facs); 348 } else { 349 long sf = 0; 350 for (Long e : sm.values()) { 351 sf += e; 352 } 353 assertTrue("#facs < " + facs, sf >= facs); 354 } 355 356 boolean tt = fac.isFactorization(a, sm); 357 //System.out.println("t = " + tt); 358 assertTrue("prod(factor(a)) = a", tt); 359 } 360 ComputerThreads.terminate(); 361 } 362 363 }