001/* 002 * $Id: GBFactoryTest.java 5104 2015-02-07 13:12:43Z kredel $ 003 */ 004 005package edu.jas.gbufd; 006 007 008// import java.util.Map; 009 010import junit.framework.Test; 011import junit.framework.TestCase; 012import junit.framework.TestSuite; 013 014import org.apache.log4j.BasicConfigurator; 015 016import edu.jas.arith.BigComplex; 017import edu.jas.arith.BigInteger; 018import edu.jas.arith.BigRational; 019import edu.jas.arith.ModInteger; 020import edu.jas.arith.ModIntegerRing; 021import edu.jas.arith.Product; 022import edu.jas.arith.ProductRing; 023import edu.jas.gb.GBProxy; 024import edu.jas.gb.GroebnerBase; 025import edu.jas.gb.GroebnerBaseAbstract; 026import edu.jas.gb.GroebnerBaseSeq; 027import edu.jas.kern.ComputerThreads; 028import edu.jas.poly.AlgebraicNumber; 029import edu.jas.poly.AlgebraicNumberRing; 030import edu.jas.poly.GenPolynomial; 031import edu.jas.poly.GenPolynomialRing; 032import edu.jas.poly.TermOrder; 033import edu.jas.structure.RingFactory; 034 035 036/** 037 * Groebner base factory tests with JUnit. 038 * @author Heinz Kredel. 039 */ 040 041public class GBFactoryTest extends TestCase { 042 043 044 /** 045 * main. 046 */ 047 public static void main(String[] args) { 048 BasicConfigurator.configure(); 049 junit.textui.TestRunner.run(suite()); 050 ComputerThreads.terminate(); 051 } 052 053 054 /** 055 * Constructs a <CODE>GBFactoryTest</CODE> object. 056 * @param name String. 057 */ 058 public GBFactoryTest(String name) { 059 super(name); 060 } 061 062 063 /** 064 */ 065 public static Test suite() { 066 TestSuite suite = new TestSuite(GBFactoryTest.class); 067 return suite; 068 } 069 070 071 //private final static int bitlen = 100; 072 073 TermOrder to = new TermOrder(TermOrder.INVLEX); 074 075 076 GenPolynomialRing<BigInteger> dfac; 077 078 079 GenPolynomialRing<BigInteger> cfac; 080 081 082 GenPolynomialRing<GenPolynomial<BigInteger>> rfac; 083 084 085 BigInteger ai, bi, ci, di, ei; 086 087 088 GenPolynomial<BigInteger> a, b, c, d, e; 089 090 091 GenPolynomial<GenPolynomial<BigInteger>> ar, br, cr, dr, er; 092 093 094 int rl = 5; 095 096 097 int kl = 4; 098 099 100 int ll = 5; 101 102 103 int el = 3; 104 105 106 float q = 0.3f; 107 108 109 @Override 110 protected void setUp() { 111 a = b = c = d = e = null; 112 ai = bi = ci = di = ei = null; 113 ar = br = cr = dr = er = null; 114 dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to); 115 cfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl - 1, to); 116 rfac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to); 117 } 118 119 120 @Override 121 protected void tearDown() { 122 a = b = c = d = e = null; 123 ai = bi = ci = di = ei = null; 124 ar = br = cr = dr = er = null; 125 dfac = null; 126 cfac = null; 127 rfac = null; 128 } 129 130 131 /** 132 * Test get BigInteger implementation. 133 */ 134 public void testBigInteger() { 135 BigInteger bi = new BigInteger(); 136 GroebnerBase<BigInteger> bba; 137 138 bba = GBFactory.getImplementation(bi); 139 //System.out.println("bba = " + bba); 140 assertTrue("bba integer " + bba, bba instanceof GroebnerBasePseudoSeq); 141 } 142 143 144 /** 145 * Test get ModInteger implementation. 146 */ 147 public void testModInteger() { 148 ModIntegerRing mi = new ModIntegerRing(19, true); 149 GroebnerBase<ModInteger> bba; 150 151 bba = GBFactory.getImplementation(mi); 152 //System.out.println("bba = " + bba); 153 assertTrue("bba modular field " + bba, bba instanceof GroebnerBaseSeq); 154 155 mi = new ModIntegerRing(30); 156 bba = GBFactory.getImplementation(mi); 157 //System.out.println("bba = " + bba); 158 assertTrue("bba modular ring " + bba, bba instanceof GroebnerBasePseudoSeq); 159 } 160 161 162 /** 163 * Test get BigRational implementation. 164 */ 165 public void testBigRational() { 166 BigRational b = new BigRational(); 167 GroebnerBase<BigRational> bba; 168 169 bba = GBFactory.getImplementation(b); 170 //System.out.println("bba = " + bba); 171 assertTrue("bba field " + bba, bba instanceof GroebnerBaseSeq); 172 } 173 174 175 /** 176 * Test get BigComplex implementation. 177 */ 178 public void testBigComplex() { 179 BigComplex b = new BigComplex(); 180 GroebnerBase<BigComplex> bba; 181 182 bba = GBFactory.<BigComplex> getImplementation(b); 183 //System.out.println("bba = " + bba); 184 assertTrue("bba field " + bba, bba instanceof GroebnerBaseSeq); 185 } 186 187 188 /** 189 * Test get AlgebraicNumber<BigRational> implementation. 190 */ 191 public void testAlgebraicNumberBigRational() { 192 BigRational b = new BigRational(); 193 GenPolynomialRing<BigRational> fac; 194 fac = new GenPolynomialRing<BigRational>(b, 1); 195 GenPolynomial<BigRational> mo = fac.random(kl, ll, el, q); 196 while (mo.isZERO() || mo.isONE() || mo.isConstant()) { 197 mo = fac.random(kl, ll, el, q); 198 } 199 200 AlgebraicNumberRing<BigRational> afac; 201 afac = new AlgebraicNumberRing<BigRational>(mo); 202 203 GroebnerBase<AlgebraicNumber<BigRational>> bba; 204 205 bba = GBFactory.<AlgebraicNumber<BigRational>> getImplementation(afac); 206 //System.out.println("bba1 = " + bba); 207 assertTrue("bba algebraic ring " + bba, bba instanceof GroebnerBasePseudoSeq); 208 209 mo = fac.univariate(0).subtract(fac.getONE()); 210 afac = new AlgebraicNumberRing<BigRational>(mo, true); 211 212 bba = GBFactory.<AlgebraicNumber<BigRational>> getImplementation(afac); 213 //System.out.println("bba1 = " + bba); 214 assertTrue("bba algebraic field " + bba, bba instanceof GroebnerBaseSeq); 215 } 216 217 218 /** 219 * Test get AlgebraicNumber<ModInteger> implementation. 220 */ 221 public void testAlgebraicNumberModInteger() { 222 ModIntegerRing b = new ModIntegerRing(19, true); 223 GenPolynomialRing<ModInteger> fac; 224 fac = new GenPolynomialRing<ModInteger>(b, 1); 225 GenPolynomial<ModInteger> mo = fac.random(kl, ll, el, q); 226 while (mo.isZERO() || mo.isONE() || mo.isConstant()) { 227 mo = fac.random(kl, ll, el, q); 228 } 229 230 AlgebraicNumberRing<ModInteger> afac; 231 afac = new AlgebraicNumberRing<ModInteger>(mo); 232 233 AlgebraicNumber<ModInteger> a = afac.getONE(); 234 assertTrue("a == 1 " + a, a.isONE()); 235 GroebnerBase<AlgebraicNumber<ModInteger>> bba; 236 237 bba = GBFactory.<AlgebraicNumber<ModInteger>> getImplementation(afac); 238 //System.out.println("bba2 = " + bba); 239 assertTrue("bba algebraic ring " + bba, bba instanceof GroebnerBasePseudoSeq); 240 } 241 242 243 /** 244 * Test get GenPolynomial implementation. 245 */ 246 @SuppressWarnings("unchecked") 247 public void testGenPolynomial() { 248 BigRational b = new BigRational(); 249 GenPolynomialRing<BigRational> fac; 250 fac = new GenPolynomialRing<BigRational>(b, rl, to); 251 RingFactory<GenPolynomial<BigRational>> rf = fac; 252 253 GroebnerBase<GenPolynomial<BigRational>> bba; 254 255 bba = GBFactory.getImplementation(fac); 256 //System.out.println("bba = " + bba); 257 assertTrue("bba recursive polynomial " + bba, bba instanceof GroebnerBasePseudoRecSeq); 258 259 GroebnerBase<BigRational> bb; 260 bb = GBFactory.<BigRational> getImplementation((RingFactory) rf); 261 //System.out.println("bb = " + bb); 262 assertTrue("bba recursive polynomial " + bb, bb instanceof GroebnerBasePseudoRecSeq); 263 } 264 265 266 /** 267 * Test get Product implementation. 268 */ 269 @SuppressWarnings({ "cast", "unchecked" }) 270 public void testProduct() { 271 ModIntegerRing mi = new ModIntegerRing(19, true); 272 ProductRing<ModInteger> fac; 273 fac = new ProductRing<ModInteger>(mi, 3); 274 RingFactory<Product<ModInteger>> rf = fac; 275 276 GroebnerBase<Product<ModInteger>> bba; 277 278 bba = GBFactory.getImplementation(fac); 279 //System.out.println("bba = " + bba); 280 assertTrue("bba product " + bba, bba instanceof RGroebnerBaseSeq); 281 282 mi = new ModIntegerRing(30); 283 fac = new ProductRing<ModInteger>(mi, 3); 284 rf = fac; 285 286 GroebnerBase<Product<ModInteger>> bb; 287 bb = GBFactory.<Product<ModInteger>> getImplementation((RingFactory) rf); 288 //System.out.println("bb = " + bb); 289 assertTrue("bba product " + bb, bb instanceof RGroebnerBasePseudoSeq); 290 } 291 292 293 /** 294 * Test get proxy implementation. 295 */ 296 public void testProxy() { 297 BigRational b = new BigRational(); 298 GroebnerBaseAbstract<BigRational> bba; 299 300 bba = GBFactory.getProxy(b); 301 //System.out.println("bba = " + bba); 302 assertTrue("bba field " + bba, bba instanceof GBProxy); 303 bba.terminate(); 304 305 306 ModIntegerRing m = new ModIntegerRing(2 * 3); 307 GroebnerBaseAbstract<ModInteger> bbm; 308 309 bbm = GBFactory.getProxy(m); 310 //System.out.println("bba = " + bba); 311 assertTrue("bbm ! field " + bbm, !(bbm instanceof GBProxy)); 312 bbm.terminate(); 313 } 314 315}