001/* 002 * $Id: GCDProxyTest.java 5863 2018-07-20 11:13:34Z kredel $ 003 */ 004 005package edu.jas.ufd; 006 007 008//import java.util.Map; 009 010import junit.framework.Test; 011import junit.framework.TestCase; 012import junit.framework.TestSuite; 013 014 015import edu.jas.arith.BigComplex; 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.AlgebraicNumber; 022import edu.jas.poly.AlgebraicNumberRing; 023import edu.jas.poly.GenPolynomial; 024import edu.jas.poly.GenPolynomialRing; 025import edu.jas.poly.PolyUtil; 026import edu.jas.poly.TermOrder; 027 028 029/** 030 * GreatestCommonDivisor proxy tests with JUnit. 031 * @author Heinz Kredel 032 */ 033 034public class GCDProxyTest extends TestCase { 035 036 037 /** 038 * main. 039 */ 040 public static void main(String[] args) { 041 junit.textui.TestRunner.run(suite()); 042 //ComputerThreads.terminate(); 043 //System.out.println("System.exit(0)"); 044 } 045 046 047 /** 048 * Constructs a <CODE>GCDProxyTest</CODE> object. 049 * @param name String. 050 */ 051 public GCDProxyTest(String name) { 052 super(name); 053 } 054 055 056 /** 057 */ 058 public static Test suite() { 059 TestSuite suite = new TestSuite(GCDProxyTest.class); 060 return suite; 061 } 062 063 064 //private final static int bitlen = 100; 065 066 TermOrder to = new TermOrder(TermOrder.INVLEX); 067 068 069 GenPolynomialRing<BigInteger> dfac; 070 071 072 GenPolynomialRing<BigInteger> cfac; 073 074 075 GenPolynomialRing<GenPolynomial<BigInteger>> rfac; 076 077 078 BigInteger ai; 079 080 081 BigInteger bi; 082 083 084 BigInteger ci; 085 086 087 BigInteger di; 088 089 090 BigInteger ei; 091 092 093 GenPolynomial<BigInteger> a; 094 095 096 GenPolynomial<BigInteger> b; 097 098 099 GenPolynomial<BigInteger> c; 100 101 102 GenPolynomial<BigInteger> d; 103 104 105 GenPolynomial<BigInteger> e; 106 107 108 GenPolynomial<GenPolynomial<BigInteger>> ar; 109 110 111 GenPolynomial<GenPolynomial<BigInteger>> br; 112 113 114 GenPolynomial<GenPolynomial<BigInteger>> cr; 115 116 117 GenPolynomial<GenPolynomial<BigInteger>> dr; 118 119 120 GenPolynomial<GenPolynomial<BigInteger>> er; 121 122 123 int rl = 5; 124 125 126 int kl = 5; 127 128 129 int ll = 7; 130 131 132 int el = 3; 133 134 135 float q = 0.3f; 136 137 138 @Override 139 protected void setUp() { 140 a = b = c = d = e = null; 141 ai = bi = ci = di = ei = null; 142 ar = br = cr = dr = er = null; 143 dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to); 144 cfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl - 1, to); 145 rfac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to); 146 } 147 148 149 @Override 150 protected void tearDown() { 151 a = b = c = d = e = null; 152 ai = bi = ci = di = ei = null; 153 ar = br = cr = dr = er = null; 154 dfac = null; 155 cfac = null; 156 rfac = null; 157 ComputerThreads.terminate(); 158 } 159 160 161 /** 162 * Test get BigInteger implementation. 163 * 164 */ 165 public void testBigInteger() { 166 long t; 167 BigInteger bi = new BigInteger(); 168 169 GreatestCommonDivisor<BigInteger> ufd_par; 170 GreatestCommonDivisorAbstract<BigInteger> ufd; 171 172 ufd_par = GCDFactory./*<BigInteger>*/getProxy(bi); 173 //System.out.println("ufd_par = " + ufd_par); 174 assertTrue("ufd_par != null " + ufd_par, ufd_par != null); 175 176 ufd = new GreatestCommonDivisorSubres<BigInteger>(); 177 178 //System.out.println("ufd = " + ufd); 179 assertTrue("ufd != null " + ufd, ufd != null); 180 181 dfac = new GenPolynomialRing<BigInteger>(bi, 4, to); 182 183 for (int i = 0; i < 1; i++) { // 10-50 184 a = dfac.random(kl + i * 10, ll + i, el, q); 185 b = dfac.random(kl + i * 10, ll + i, el, q); 186 c = dfac.random(kl + 2, ll, el, q); 187 //c = dfac.getONE(); 188 //c = c.multiply( dfac.univariate(0) ); 189 c = ufd.primitivePart(c).abs(); 190 //System.out.println("a = " + a); 191 //System.out.println("b = " + b); 192 //System.out.println("c = " + c); 193 194 if (a.isZERO() || b.isZERO() || c.isZERO()) { 195 // skip for this turn 196 continue; 197 } 198 assertTrue("length( c" + i + " ) <> 0", c.length() > 0); 199 //assertTrue(" not isZERO( c"+i+" )", !c.isZERO() ); 200 //assertTrue(" not isONE( c"+i+" )", !c.isONE() ); 201 202 a = a.multiply(c); 203 b = b.multiply(c); 204 //System.out.println("a = " + a); 205 //System.out.println("b = " + b); 206 /* 207 System.out.println("\ni degrees: a = " + a.degree() 208 + ", b = " + b.degree() 209 + ", c = " + c.degree()); 210 */ 211 t = System.currentTimeMillis(); 212 d = ufd_par.gcd(a, b); 213 t = System.currentTimeMillis() - t; 214 //System.out.println("i proxy time = " + t); 215 //System.out.println("c = " + c); 216 //System.out.println("d = " + d); 217 //System.out.println("e = " + e); 218 219 e = PolyUtil.<BigInteger> basePseudoRemainder(d, c); 220 //System.out.println("e = " + e); 221 assertTrue("c | gcd(ac,bc) " + e, e.isZERO()); 222 } 223 // obsolete ((GCDProxy<BigInteger>)ufd_par).terminate(); 224 ComputerThreads.terminate(); 225 } 226 227 228 /** 229 * Test get ModInteger implementation. 230 * 231 */ 232 public void testModInteger() { 233 long t; 234 ModIntegerRing mi = new ModIntegerRing(19,true); 235 //ModIntegerRing mi = new ModIntegerRing(536870909, true); 236 237 GenPolynomial<ModInteger> a, b, c, d, e; 238 239 GreatestCommonDivisor<ModInteger> ufd_par; 240 GreatestCommonDivisorAbstract<ModInteger> ufd; 241 242 ufd_par = GCDFactory.getProxy(mi); 243 //System.out.println("ufd_par = " + ufd_par); 244 assertTrue("ufd_par != null " + ufd_par, ufd_par != null); 245 246 ufd = new GreatestCommonDivisorSubres<ModInteger>(); 247 248 //System.out.println("ufd = " + ufd); 249 assertTrue("ufd != null " + ufd, ufd != null); 250 251 GenPolynomialRing<ModInteger> dfac; 252 dfac = new GenPolynomialRing<ModInteger>(mi, 4, to); 253 254 for (int i = 0; i < 1; i++) { 255 a = dfac.random(kl + i * 2, ll + i, el, q); 256 b = dfac.random(kl + i * 2, ll + i, el, q); 257 c = dfac.random(kl, ll, el, q); 258 //a = dfac.random(kl,ll+i,el,q); 259 //b = dfac.random(kl,ll+i,el,q); 260 //c = dfac.random(kl,ll,el,q); 261 //c = dfac.getONE(); 262 //c = c.multiply( dfac.univariate(0) ); 263 c = ufd.primitivePart(c).abs(); 264 //System.out.println("a = " + a); 265 //System.out.println("b = " + b); 266 //System.out.println("c = " + c); 267 268 if (a.isZERO() || b.isZERO() || c.isZERO()) { 269 // skip for this turn 270 continue; 271 } 272 assertTrue("length( c" + i + " ) <> 0", c.length() > 0); 273 //assertTrue(" not isZERO( c"+i+" )", !c.isZERO() ); 274 //assertTrue(" not isONE( c"+i+" )", !c.isONE() ); 275 276 a = a.multiply(c); 277 b = b.multiply(c); 278 //System.out.println("a = " + a); 279 //System.out.println("b = " + b); 280 /* 281 System.out.println("\nm degrees: a = " + a.degree() 282 + ", b = " + b.degree() 283 + ", c = " + c.degree()); 284 */ 285 t = System.currentTimeMillis(); 286 d = ufd_par.gcd(a, b); 287 t = System.currentTimeMillis() - t; 288 //System.out.println("m proxy time = " + t); 289 //System.out.println("c = " + c); 290 //System.out.println("d = " + d); 291 //System.out.println("e = " + e); 292 293 e = PolyUtil.<ModInteger> basePseudoRemainder(d, c); 294 //System.out.println("e = " + e); 295 assertTrue("c | gcd(ac,bc) " + e, e.isZERO()); 296 } 297 // obsolete ((GCDProxy<ModInteger>)ufd_par).terminate(); 298 ComputerThreads.terminate(); 299 } 300 301 302 /** 303 * Test get BigRational implementation. 304 * 305 */ 306 public void testBigRational() { 307 BigRational b = new BigRational(); 308 GreatestCommonDivisor<BigRational> ufd; 309 310 ufd = GCDFactory./*<BigRational>*/getImplementation(b); 311 //System.out.println("ufd = " + ufd); 312 assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive); 313 } 314 315 316 /** 317 * Test get BigComplex implementation. 318 * 319 */ 320 public void testBigComplex() { 321 BigComplex b = new BigComplex(); 322 GreatestCommonDivisor<BigComplex> ufd; 323 324 ufd = GCDFactory.<BigComplex> getImplementation(b); 325 //System.out.println("ufd = " + ufd); 326 assertTrue("ufd != Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple); 327 } 328 329 330 /** 331 * Test get AlgebraicNumber<BigRational> implementation. 332 * 333 */ 334 public void xtestAlgebraicNumberBigRational() { 335 BigRational b = new BigRational(); 336 GenPolynomialRing<BigRational> fac; 337 fac = new GenPolynomialRing<BigRational>(b, 1); 338 GenPolynomial<BigRational> mo = fac.random(kl, ll, el, q); 339 while (mo.isConstant() || mo.isZERO()) { 340 mo = fac.random(kl, ll, el, q); 341 } 342 343 AlgebraicNumberRing<BigRational> afac; 344 afac = new AlgebraicNumberRing<BigRational>(mo); 345 346 GreatestCommonDivisor<AlgebraicNumber<BigRational>> ufd; 347 348 ufd = GCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac); 349 //System.out.println("ufd = " + ufd); 350 assertTrue("ufd = Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres); 351 352 mo = fac.univariate(0).subtract(fac.getONE()); 353 afac = new AlgebraicNumberRing<BigRational>(mo, true); 354 355 ufd = GCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac); 356 //System.out.println("ufd = " + ufd); 357 assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple); 358 } 359 360 361 /** 362 * Test get AlgebraicNumber<ModInteger&glt; implementation. 363 * 364 */ 365 public void testAlgebraicNumberModInteger() { 366 ModIntegerRing b = new ModIntegerRing(19, true); 367 GenPolynomialRing<ModInteger> fac; 368 fac = new GenPolynomialRing<ModInteger>(b, 1); 369 GenPolynomial<ModInteger> mo = fac.random(kl, ll, el, q); 370 while (mo.isConstant() || mo.isZERO()) { 371 mo = fac.random(kl, ll, el, q); 372 } 373 374 AlgebraicNumberRing<ModInteger> afac; 375 afac = new AlgebraicNumberRing<ModInteger>(mo); 376 377 AlgebraicNumber<ModInteger> a = afac.getONE(); 378 assertTrue("a == 1 " + a, a.isONE()); 379 GreatestCommonDivisor<AlgebraicNumber<ModInteger>> ufd; 380 381 ufd = GCDFactory.<AlgebraicNumber<ModInteger>> getImplementation(afac); 382 //System.out.println("ufd = " + ufd); 383 assertTrue("ufd = Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres); 384 } 385 386}