001/* 002 * $Id: PolyGBUtilTest.java 4071 2012-07-27 19:59:38Z kredel $ 003 */ 004 005package edu.jas.gbufd; 006 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012import org.apache.log4j.BasicConfigurator; 013 014import edu.jas.arith.BigInteger; 015import edu.jas.arith.BigRational; 016import edu.jas.arith.ModInteger; 017import edu.jas.arith.ModIntegerRing; 018import edu.jas.arith.PrimeList; 019import edu.jas.kern.ComputerThreads; 020import edu.jas.poly.GenPolynomial; 021import edu.jas.poly.GenPolynomialRing; 022import edu.jas.poly.TermOrder; 023import edu.jas.ufd.GCDProxy; 024import edu.jas.ufd.GreatestCommonDivisorAbstract; 025import edu.jas.ufd.GreatestCommonDivisorModEval; 026import edu.jas.ufd.GreatestCommonDivisorModular; 027import edu.jas.ufd.GreatestCommonDivisorSimple; 028import edu.jas.ufd.GreatestCommonDivisorSubres; 029 030 031/** 032 * PolyGBUtil tests with JUnit. 033 * @author Heinz Kredel. 034 */ 035public class PolyGBUtilTest extends TestCase { 036 037 038 /** 039 * main. 040 */ 041 public static void main(String[] args) { 042 BasicConfigurator.configure(); 043 junit.textui.TestRunner.run(suite()); 044 ComputerThreads.terminate(); 045 } 046 047 048 /** 049 * Constructs a <CODE>PolyUtilTest</CODE> object. 050 * @param name String. 051 */ 052 public PolyGBUtilTest(String name) { 053 super(name); 054 } 055 056 057 /** 058 */ 059 public static Test suite() { 060 TestSuite suite = new TestSuite(PolyGBUtilTest.class); 061 return suite; 062 } 063 064 065 //TermOrder to = new TermOrder(TermOrder.INVLEX); 066 TermOrder to = new TermOrder(TermOrder.IGRLEX); 067 068 069 int rl = 3; 070 071 072 int kl = 3; 073 074 075 int ll = 4; 076 077 078 int el = 3; 079 080 081 float q = 0.29f; 082 083 084 @Override 085 protected void setUp() { 086 } 087 088 089 @Override 090 protected void tearDown() { 091 } 092 093 094 /** 095 * Test resultant modular. 096 */ 097 public void testResultantModular() { 098 GenPolynomialRing<ModInteger> dfac; 099 ModIntegerRing mi; 100 101 PrimeList primes = new PrimeList(); 102 mi = new ModIntegerRing(primes.get(1)); // 17, 19, 23, 41, 163, 103 dfac = new GenPolynomialRing<ModInteger>(mi, rl, to); 104 //System.out.println("dfac = " + dfac); 105 106 GreatestCommonDivisorAbstract<ModInteger> ufds = new GreatestCommonDivisorSimple<ModInteger>(); 107 GreatestCommonDivisorAbstract<ModInteger> sres = new GreatestCommonDivisorSubres<ModInteger>(); 108 GreatestCommonDivisorAbstract<ModInteger> ufdm = new GreatestCommonDivisorModEval<ModInteger>(); 109 GenPolynomial<ModInteger> a, b, c, d, e; 110 111 for (int i = 0; i < 1; i++) { 112 a = dfac.random(kl, ll, el, q); 113 b = dfac.random(kl, ll + 2, el, q); 114 //a = dfac.parse("6 x0^4 - 17"); 115 //b = dfac.parse("6 x1^2 - 7 x0^2 - 5 x1 - 14"); 116 //a = dfac.parse("5 x1^4 * x2^4 + 2 x1^2 + x0^2"); 117 //b = dfac.parse("5 x0^2 * x1^2 + 2 x2^2 + 5 x2 + 15"); 118 //a = dfac.parse("x0^3 * x2^2 + 6 x0^4 + 6 x0 + 7"); 119 //b = dfac.parse("7 x0^2 * x2^2 + 3 x2^2 + 4 x1^2 * x2 + 4 x2 + 4 x1^2 + x1 + 7"); 120 //System.out.println("a = " + a); 121 //System.out.println("b = " + b); 122 123 c = ufds.resultant(a, b); 124 d = sres.resultant(a, b); 125 e = ufdm.resultant(a, b); 126 127 boolean t1 = PolyGBUtil.<ModInteger> isResultant(a, b, c); 128 //System.out.println("t1 = " + t1); 129 boolean t2 = PolyGBUtil.<ModInteger> isResultant(a, b, d); 130 //System.out.println("t2 = " + t2); 131 boolean t3 = PolyGBUtil.<ModInteger> isResultant(a, b, e); 132 //System.out.println("t3 = " + t3); 133 134 //System.out.println("c = " + c); 135 //System.out.println("d = " + d); 136 //System.out.println("e = " + e); 137 138 assertTrue("isResultant(a,b,c): " + c, t1); 139 assertTrue("isResultant(a,b,d): " + d, t2); 140 assertTrue("isResultant(a,b,e): " + e, t3); 141 } 142 } 143 144 145 /** 146 * Test resultant integer. 147 */ 148 public void testResultantInteger() { 149 GenPolynomialRing<BigInteger> dfac; 150 dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to); 151 //System.out.println("dfac = " + dfac); 152 153 GreatestCommonDivisorAbstract<BigInteger> ufds = new GreatestCommonDivisorSimple<BigInteger>(); 154 GreatestCommonDivisorAbstract<BigInteger> sres = new GreatestCommonDivisorSubres<BigInteger>(); 155 GreatestCommonDivisorAbstract<BigInteger> ufdm = new GreatestCommonDivisorModular<ModInteger>(); //true); 156 GenPolynomial<BigInteger> a, b, c, d, e; 157 158 for (int i = 0; i < 1; i++) { 159 a = dfac.random(kl, ll, el, q); 160 b = dfac.random(kl, ll + 2, el, q); 161 //a = dfac.parse("6 x0^4 - 17"); 162 //b = dfac.parse("6 x1^2 - 7 x0^2 - 5 x1 - 14"); 163 //System.out.println("a = " + a); 164 //System.out.println("b = " + b); 165 166 c = ufds.resultant(a, b); 167 d = sres.resultant(a, b); 168 e = ufdm.resultant(a, b); 169 170 boolean t1 = PolyGBUtil.<BigInteger> isResultant(a, b, c); 171 //System.out.println("t1 = " + t1); 172 boolean t2 = PolyGBUtil.<BigInteger> isResultant(a, b, d); 173 //System.out.println("t2 = " + t2); 174 boolean t3 = PolyGBUtil.<BigInteger> isResultant(a, b, e); 175 //System.out.println("t3 = " + t3); 176 177 //System.out.println("c = " + c); 178 //System.out.println("d = " + d); 179 //System.out.println("e = " + e); 180 181 assertTrue("isResultant(a,b,d): " + d, t2); 182 assertTrue("isResultant(a,b,e): " + e, t3); 183 assertTrue("isResultant(a,b,c): " + c, t1); 184 } 185 } 186 187 188 /** 189 * Test resultant modular parallel proxy. 190 */ 191 public void testResultantModularParallel() { 192 GenPolynomialRing<ModInteger> dfac; 193 ModIntegerRing mi; 194 195 PrimeList primes = new PrimeList(); 196 mi = new ModIntegerRing(primes.get(1)); // 17, 19, 23, 41, 163, 197 dfac = new GenPolynomialRing<ModInteger>(mi, rl, to); 198 //System.out.println("dfac = " + dfac); 199 200 GreatestCommonDivisorAbstract<ModInteger> ufds = new GreatestCommonDivisorSimple<ModInteger>(); 201 GreatestCommonDivisorAbstract<ModInteger> sres = new GreatestCommonDivisorSubres<ModInteger>(); 202 GreatestCommonDivisorAbstract<ModInteger> ufdm = new GreatestCommonDivisorModEval<ModInteger>(); 203 204 GreatestCommonDivisorAbstract<ModInteger> pufds = new GCDProxy<ModInteger>(sres, ufds); 205 GreatestCommonDivisorAbstract<ModInteger> pufdm = new GCDProxy<ModInteger>(ufdm, sres); 206 207 GenPolynomial<ModInteger> a, b, c, d; 208 209 for (int i = 0; i < 1; i++) { 210 a = dfac.random(kl, ll, el, q); 211 b = dfac.random(kl, ll + 2, el, q); 212 //System.out.println("a = " + a); 213 //System.out.println("b = " + b); 214 215 c = pufds.resultant(a, b); 216 d = pufdm.resultant(a, b); 217 218 boolean t1 = PolyGBUtil.<ModInteger> isResultant(a, b, c); 219 //System.out.println("t1 = " + t1); 220 boolean t2 = PolyGBUtil.<ModInteger> isResultant(a, b, d); 221 //System.out.println("t2 = " + t2); 222 223 //System.out.println("c = " + c); 224 //System.out.println("d = " + d); 225 226 assertTrue("isResultant(a,b,c): " + c, t1); 227 assertTrue("isResultant(a,b,d): " + d, t2); 228 } 229 } 230 231 232 /** 233 * Test resultant integer parallel proxy. 234 */ 235 public void testResultantIntegerProxy() { 236 GenPolynomialRing<BigInteger> dfac; 237 dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to); 238 //System.out.println("dfac = " + dfac); 239 240 GreatestCommonDivisorAbstract<BigInteger> ufds = new GreatestCommonDivisorSimple<BigInteger>(); 241 GreatestCommonDivisorAbstract<BigInteger> sres = new GreatestCommonDivisorSubres<BigInteger>(); 242 GreatestCommonDivisorAbstract<BigInteger> ufdm = new GreatestCommonDivisorModular<ModInteger>(); //true); 243 244 GreatestCommonDivisorAbstract<BigInteger> pufds = new GCDProxy<BigInteger>(sres, ufds); 245 GreatestCommonDivisorAbstract<BigInteger> pufdm = new GCDProxy<BigInteger>(ufdm, sres); 246 247 GenPolynomial<BigInteger> a, b, c, d; 248 249 for (int i = 0; i < 1; i++) { 250 a = dfac.random(kl, ll, el, q); 251 b = dfac.random(kl, ll + 1, el, q); 252 //System.out.println("a = " + a); 253 //System.out.println("b = " + b); 254 255 c = pufds.resultant(a, b); 256 d = pufdm.resultant(a, b); 257 258 boolean t1 = PolyGBUtil.<BigInteger> isResultant(a, b, c); 259 //System.out.println("t1 = " + t1); 260 boolean t2 = PolyGBUtil.<BigInteger> isResultant(a, b, d); 261 //System.out.println("t2 = " + t2); 262 263 //System.out.println("c = " + c); 264 //System.out.println("d = " + d); 265 266 assertTrue("isResultant(a,b,d): " + d, t2); 267 assertTrue("isResultant(a,b,c): " + c, t1); 268 } 269 } 270 271 272 /** 273 * Test coefficient base pseudo remainder. 274 */ 275 public void testCoefficientBasePseudoRemainder() { 276 GenPolynomialRing<BigRational> dfac; 277 BigRational br = new BigRational(); 278 to = new TermOrder(TermOrder.INVLEX); 279 //String[] vars = new String[] { "x1", "x2", "x3" }; 280 String[] vars = new String[] { "x1", "x2" }; 281 dfac = new GenPolynomialRing<BigRational>(br, to, vars); 282 //System.out.println("dfac = " + dfac); 283 GenPolynomialRing<GenPolynomial<BigRational>> rfac = dfac.recursive(1); 284 //System.out.println("rfac = " + rfac); 285 GenPolynomialRing<BigRational> cfac = (GenPolynomialRing<BigRational>) rfac.coFac; 286 //System.out.println("cfac = " + cfac); 287 GenPolynomial<GenPolynomial<BigRational>> ar, cr, dr, er; 288 GenPolynomial<BigRational> b; 289 290 ar = rfac.random(kl, ll, el, q * 1.1f); 291 b = cfac.random(kl, ll + 2, el * 2, q); 292 //System.out.println("ar = " + ar); 293 //System.out.println("b = " + b); 294 295 cr = PolyGBUtil.<BigRational> coefficientPseudoRemainderBase(ar, b); 296 //System.out.println("cr = " + cr); 297 assertTrue("deg(c) < deg(a): ", cr.degree(0) <= ar.degree(0) || ar.degree(0) == 0); 298 assertTrue("deg(lfcd(c)) < deg(b): ", 299 cr.leadingBaseCoefficient().degree(0) < b.degree(0) || b.degree(0) == 0); 300 301 dr = ar.multiply(b); 302 //System.out.println("dr = " + dr); 303 cr = PolyGBUtil.<BigRational> coefficientPseudoRemainderBase(dr, b); 304 //System.out.println("cr = " + cr); 305 assertTrue("c == 0: ", cr.isZERO()); 306 307 long s = ar.degree(0); 308 er = rfac.univariate(0, s + 1); 309 //System.out.println("er = " + er); 310 er = er.multiply(b.multiply(b)); 311 er = er.sum(ar); 312 //System.out.println("er = " + er); 313 cr = PolyGBUtil.<BigRational> coefficientPseudoRemainderBase(er, b); 314 //System.out.println("cr = " + cr); 315 assertTrue("deg(c) < deg(a): ", cr.degree(0) < er.degree(0)); 316 } 317 318 319 /** 320 * Test coefficient recursive pseudo remainder. 321 */ 322 public void testCoefficientRecursivePseudoRemainder() { 323 GenPolynomialRing<BigRational> dfac; 324 BigRational br = new BigRational(); 325 to = new TermOrder(TermOrder.INVLEX); 326 String[] vars = new String[] { "x1", "x2", "x3" }; 327 //String[] vars = new String[] { "x1", "x2" }; 328 dfac = new GenPolynomialRing<BigRational>(br, to, vars); 329 //System.out.println("dfac = " + dfac); 330 GenPolynomialRing<GenPolynomial<BigRational>> r1fac = dfac.recursive(2); 331 //System.out.println("r1fac = " + r1fac); 332 GenPolynomialRing<GenPolynomial<GenPolynomial<BigRational>>> rfac = r1fac.recursive(1); 333 //System.out.println("rfac = " + rfac); 334 GenPolynomialRing<GenPolynomial<BigRational>> cfac = (GenPolynomialRing<GenPolynomial<BigRational>>) rfac.coFac; 335 //System.out.println("cfac = " + cfac); 336 GenPolynomial<GenPolynomial<GenPolynomial<BigRational>>> ar, cr, dr, er; 337 GenPolynomial<GenPolynomial<BigRational>> b; 338 339 ar = rfac.random(kl, ll, el, q); 340 b = cfac.random(kl, ll + 2, el, q); 341 //System.out.println("ar = " + ar); 342 //System.out.println("b = " + b); 343 344 cr = PolyGBUtil.<BigRational> coefficientPseudoRemainder(ar, b); 345 //System.out.println("cr = " + cr); 346 assertTrue("deg(c) < deg(a): ", cr.degree(0) <= ar.degree(0) || ar.degree(0) == 0); 347 assertTrue("deg(lfcd(c)) < deg(b): ", 348 cr.leadingBaseCoefficient().degree(0) < b.degree(0) || b.degree(0) == 0); 349 350 dr = ar.multiply(b); 351 //System.out.println("dr = " + dr); 352 cr = PolyGBUtil.<BigRational> coefficientPseudoRemainder(dr, b); 353 //System.out.println("cr = " + cr); 354 assertTrue("c == 0: ", cr.isZERO()); 355 356 long s = ar.degree(0); 357 er = rfac.univariate(0, s + 1); 358 ////System.out.println("er = " + er); 359 er = er.multiply(b.multiply(cfac.fromInteger(100))); 360 er = er.sum(ar); 361 //System.out.println("er = " + er); 362 cr = PolyGBUtil.<BigRational> coefficientPseudoRemainder(er, b); 363 //System.out.println("cr = " + cr); 364 assertTrue("deg(c) < deg(a): ", cr.degree(0) < er.degree(0)); 365 } 366 367}