001/* 002 * $Id: GBAlgorithmBuilderTest.java 4328 2012-12-27 21:41:30Z kredel $ 003 */ 004 005package edu.jas.application; 006 007 008import java.util.List; 009 010import junit.framework.Test; 011import junit.framework.TestCase; 012import junit.framework.TestSuite; 013 014import org.apache.log4j.BasicConfigurator; 015 016import edu.jas.arith.BigInteger; 017import edu.jas.arith.BigRational; 018import edu.jas.gb.DGroebnerBaseSeq; 019import edu.jas.gb.EGroebnerBaseSeq; 020import edu.jas.gb.GBOptimized; 021import edu.jas.gb.GBProxy; 022import edu.jas.gb.GroebnerBaseAbstract; 023import edu.jas.gb.GroebnerBaseParallel; 024import edu.jas.gb.GroebnerBaseSeq; 025import edu.jas.gbufd.GBFactory; 026import edu.jas.gbufd.GroebnerBaseFGLM; 027import edu.jas.gbufd.GroebnerBasePseudoSeq; 028import edu.jas.gbufd.GroebnerBaseRational; 029import edu.jas.kern.ComputerThreads; 030import edu.jas.poly.GenPolynomial; 031import edu.jas.poly.GenPolynomialRing; 032 033 034/** 035 * GBAlgorithmBuilder tests with JUnit. 036 * @author Heinz Kredel. 037 */ 038 039public class GBAlgorithmBuilderTest extends TestCase { 040 041 042 /** 043 * main. 044 */ 045 public static void main(String[] args) { 046 BasicConfigurator.configure(); 047 junit.textui.TestRunner.run(suite()); 048 } 049 050 051 /** 052 * Constructs a <CODE>GBAlgorithmBuilderTest</CODE> object. 053 * @param name String. 054 */ 055 public GBAlgorithmBuilderTest(String name) { 056 super(name); 057 } 058 059 060 /** 061 * suite. 062 */ 063 public static Test suite() { 064 TestSuite suite = new TestSuite(GBAlgorithmBuilderTest.class); 065 return suite; 066 } 067 068 069 GBAlgorithmBuilder builder; 070 071 072 @Override 073 protected void setUp() { 074 builder = null; 075 } 076 077 078 @Override 079 protected void tearDown() { 080 builder = null; 081 ComputerThreads.terminate(); 082 } 083 084 085 /** 086 * Test basic construction for BigRational. 087 */ 088 public void testConstructionRational() { 089 BigRational bf = new BigRational(1); 090 String[] vars = new String[] { "a", "b", "c" }; 091 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 092 093 GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf); 094 //System.out.println("ab = " + ab); 095 096 GroebnerBaseAbstract<BigRational> bb = ab.build(); 097 //System.out.println("bb = " + bb); 098 assertTrue("instance of " + bb, bb instanceof GroebnerBaseSeq); 099 } 100 101 102 /** 103 * Test construction for BigRational and FGLM. 104 */ 105 public void testConstructionRationalFGLM() { 106 BigRational bf = new BigRational(1); 107 String[] vars = new String[] { "a", "b", "c" }; 108 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 109 110 GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf); 111 //System.out.println("ab = " + ab); 112 113 ab = ab.graded(); 114 //System.out.println("ab = " + ab); 115 116 GroebnerBaseAbstract<BigRational> bb = ab.build(); 117 //System.out.println("bb = " + bb); 118 assertTrue("instance of " + bb, bb instanceof GroebnerBaseFGLM); 119 } 120 121 122 /** 123 * Test construction for BigRational and parallel. 124 */ 125 public void testConstructionRationalParallel() { 126 BigRational bf = new BigRational(1); 127 String[] vars = new String[] { "a", "b", "c" }; 128 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 129 130 GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf); 131 //System.out.println("ab = " + ab); 132 133 ab = ab.parallel(); 134 //System.out.println("ab = " + ab); 135 136 GroebnerBaseAbstract<BigRational> bb = ab.build(); 137 //System.out.println("bb = " + bb); 138 assertTrue("instance of " + bb, bb instanceof GBProxy); 139 140 GBProxy<BigRational> bbp = (GBProxy<BigRational>) bb; 141 assertTrue("instance of " + bbp.e1, bbp.e1 instanceof GroebnerBaseSeq); 142 assertTrue("instance of " + bbp.e2, bbp.e2 instanceof GroebnerBaseParallel); 143 } 144 145 146 /** 147 * Test construction for BigRational fraction free and parallel. 148 */ 149 public void testConstructionRationalFFParallel() { 150 BigRational bf = new BigRational(1); 151 String[] vars = new String[] { "a", "b", "c" }; 152 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 153 154 GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf); 155 //System.out.println("ab = " + ab); 156 157 ab = ab.fractionFree(); 158 //System.out.println("ab = " + ab); 159 160 ab = ab.parallel(); 161 //System.out.println("ab = " + ab); 162 163 GroebnerBaseAbstract<BigRational> bb = ab.build(); 164 //System.out.println("bb = " + bb); 165 assertTrue("instance of " + bb, bb instanceof GBProxy); 166 167 GBProxy<BigRational> bbp = (GBProxy<BigRational>) bb; 168 assertTrue("instance of " + bbp.e1, bbp.e1 instanceof GroebnerBaseRational); 169 assertTrue("instance of " + bbp.e2, bbp.e2 instanceof GroebnerBaseRational); 170 } 171 172 173 /** 174 * Test construction for BigRational and optimize. 175 */ 176 public void testConstructionRationalOptimized() { 177 BigRational bf = new BigRational(1); 178 String[] vars = new String[] { "a", "b", "c" }; 179 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 180 181 GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf); 182 //System.out.println("ab = " + ab); 183 184 ab = ab.optimize(); 185 //System.out.println("ab = " + ab); 186 187 GroebnerBaseAbstract<BigRational> bb = ab.build(); 188 //System.out.println("bb = " + bb); 189 assertTrue("instance of " + bb, bb instanceof GBOptimized); 190 } 191 192 193 /** 194 * Test construction for BigRational and fraction free. 195 */ 196 public void testConstructionRationalFF() { 197 BigRational bf = new BigRational(1); 198 String[] vars = new String[] { "a", "b", "c" }; 199 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 200 201 GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf); 202 //System.out.println("ab = " + ab); 203 204 ab = ab.fractionFree(); 205 //System.out.println("ab = " + ab); 206 207 GroebnerBaseAbstract<BigRational> bb = ab.build(); 208 //System.out.println("bb = " + bb); 209 assertTrue("instance of " + bb, bb instanceof GroebnerBaseRational); 210 } 211 212 213 /** 214 * Test basic construction for BigInteger. 215 */ 216 public void testConstructionInteger() { 217 BigInteger bf = new BigInteger(1); 218 String[] vars = new String[] { "a", "b", "c" }; 219 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(bf, vars); 220 221 GBAlgorithmBuilder<BigInteger> ab = GBAlgorithmBuilder.<BigInteger> polynomialRing(pf); 222 //System.out.println("ab = " + ab); 223 224 GroebnerBaseAbstract<BigInteger> bb = ab.build(); 225 //System.out.println("bb = " + bb); 226 assertTrue("instance of " + bb, bb instanceof GroebnerBasePseudoSeq); 227 } 228 229 230 /** 231 * Test construction for d-GB BigInteger. 232 */ 233 public void testConstructionIntegerDGB() { 234 BigInteger bf = new BigInteger(1); 235 String[] vars = new String[] { "a", "b", "c" }; 236 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(bf, vars); 237 238 GBAlgorithmBuilder<BigInteger> ab = GBAlgorithmBuilder.<BigInteger> polynomialRing(pf); 239 //System.out.println("ab = " + ab); 240 241 ab = ab.domainAlgorithm(GBFactory.Algo.dgb); 242 //System.out.println("ab = " + ab); 243 244 GroebnerBaseAbstract<BigInteger> bb = ab.build(); 245 //System.out.println("bb = " + bb); 246 assertTrue("instance of " + bb, bb instanceof DGroebnerBaseSeq); 247 } 248 249 250 /** 251 * Test construction for e-GB BigInteger. 252 */ 253 public void testConstructionIntegerEGB() { 254 BigInteger bf = new BigInteger(1); 255 String[] vars = new String[] { "a", "b", "c" }; 256 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(bf, vars); 257 258 GBAlgorithmBuilder<BigInteger> ab = GBAlgorithmBuilder.<BigInteger> polynomialRing(pf); 259 //System.out.println("ab = " + ab); 260 261 ab = ab.domainAlgorithm(GBFactory.Algo.egb); 262 //System.out.println("ab = " + ab); 263 264 GroebnerBaseAbstract<BigInteger> bb = ab.build(); 265 //System.out.println("bb = " + bb); 266 assertTrue("instance of " + bb, bb instanceof EGroebnerBaseSeq); 267 } 268 269 270 /** 271 * Test construction for BigRational and more. 272 */ 273 public void testConstructionRationalMore() { 274 BigRational bf = new BigRational(1); 275 String[] vars = new String[] { "a", "b", "c" }; 276 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars); 277 278 GroebnerBaseAbstract<BigRational> bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf) 279 .fractionFree().optimize().build(); 280 //System.out.println("bb = " + bb); 281 assertTrue("instance of " + bb, bb instanceof GBOptimized); 282 283 bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().parallel().optimize().build(); 284 //System.out.println("bb = " + bb); 285 assertTrue("instance of " + bb, bb instanceof GBOptimized); 286 287 bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().graded().parallel() 288 .optimize().build(); 289 //System.out.println("bb = " + bb); 290 assertTrue("instance of " + bb, bb instanceof GBOptimized); 291 } 292 293 294 /** 295 * Test construction for BigRational and more and compute. 296 */ 297 public void testConstructionRationalMoreCompute() { 298 List<GenPolynomial<BigRational>> cp = ExamplesGeoTheorems.getExample(); 299 GenPolynomialRing<BigRational> pf = cp.get(0).ring; 300 301 GroebnerBaseAbstract<BigRational> bb; 302 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().parallel().optimize().build(); 303 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).parallel().optimize().build(); 304 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).build(); 305 bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).optimize().build(); 306 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().build(); 307 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).parallel().build(); 308 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().optimize().build(); 309 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).parallel().fractionFree().optimize().build(); 310 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).optimize().fractionFree().build(); 311 //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().optimize().parallel().build(); 312 //System.out.println("bb = " + bb); 313 //assertTrue("instance of " + bb, bb instanceof GBOptimized); 314 315 List<GenPolynomial<BigRational>> gb; 316 long t; 317 t = System.currentTimeMillis(); 318 gb = bb.GB(cp); 319 t = System.currentTimeMillis() - t; 320 //System.out.println("time(gb) = " + t); 321 322 t = System.currentTimeMillis(); 323 gb = bb.GB(cp); 324 t = System.currentTimeMillis() - t; 325 //System.out.println("time(gb) = " + t); 326 327 t = System.currentTimeMillis(); 328 gb = bb.GB(cp); 329 t = System.currentTimeMillis() - t; 330 //System.out.println("time(gb) = " + t); 331 assertTrue("t >= 0: ", t >= 0L); // praise findbugs 332 333 assertTrue("isGB: ", bb.isGB(gb)); 334 bb.terminate(); 335 //System.out.println("gb = " + gb); 336 //System.out.println("bb = " + bb); 337 } 338 339 340 /** 341 * Test construction for BigRational and pairlists and compute. 342 */ 343 public void testConstructionRationalParilistCompute() { 344 List<GenPolynomial<BigRational>> cp = ExamplesGeoTheorems.getExample(); 345 GenPolynomialRing<BigRational> pf = cp.get(0).ring; 346 347 GroebnerBaseAbstract<BigRational> bb; 348 List<GenPolynomial<BigRational>> gb; 349 long t; 350 351 bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).normalPairlist().build(); 352 t = System.currentTimeMillis(); 353 gb = bb.GB(cp); 354 t = System.currentTimeMillis() - t; 355 //System.out.println("time(gb) = " + t); 356 assertTrue("isGB: ", bb.isGB(gb)); 357 358 bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).syzygyPairlist().build(); 359 t = System.currentTimeMillis(); 360 gb = bb.GB(cp); 361 t = System.currentTimeMillis() - t; 362 //System.out.println("time(gb) = " + t); 363 assertTrue("isGB: ", bb.isGB(gb)); 364 365 bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).simplePairlist().build(); 366 t = System.currentTimeMillis(); 367 gb = bb.GB(cp); 368 t = System.currentTimeMillis() - t; 369 //System.out.println("time(gb) = " + t); 370 assertTrue("isGB: ", bb.isGB(gb)); 371 372 assertTrue("t >= 0: ", t >= 0L); // for findbugs 373 } 374 375}