001/* 002 * $Id: SolvableQuotientTest.java 5267 2015-07-27 17:57:50Z 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.BigRational; 015import edu.jas.gbufd.SolvableQuotient; 016import edu.jas.gbufd.SolvableQuotientRing; 017import edu.jas.kern.ComputerThreads; 018import edu.jas.kern.PrettyPrint; 019import edu.jas.poly.GenSolvablePolynomialRing; 020import edu.jas.poly.RelationGenerator; 021import edu.jas.poly.TermOrder; 022import edu.jas.poly.WeylRelations; 023 024 025/** 026 * SolvableQuotient over BigRational GenSolvablePolynomial tests with JUnit. 027 * @author Heinz Kredel. 028 */ 029 030public class SolvableQuotientTest extends TestCase { 031 032 033 /** 034 * main. 035 */ 036 public static void main(String[] args) { 037 BasicConfigurator.configure(); 038 junit.textui.TestRunner.run(suite()); 039 } 040 041 042 /** 043 * Constructs a <CODE>SolvableQuotientTest</CODE> object. 044 * @param name String. 045 */ 046 public SolvableQuotientTest(String name) { 047 super(name); 048 } 049 050 051 /** 052 * suite. 053 */ 054 public static Test suite() { 055 TestSuite suite = new TestSuite(SolvableQuotientTest.class); 056 return suite; 057 } 058 059 060 SolvableQuotientRing<BigRational> zFac; 061 062 063 SolvableQuotientRing<BigRational> efac; 064 065 066 GenSolvablePolynomialRing<BigRational> mfac; 067 068 069 SolvableQuotient<BigRational> a, b, c, d, e; 070 071 072 SolvableQuotient<BigRational> az, bz, cz, dz, ez; 073 074 075 int rl = 4; 076 077 078 int kl = 2; 079 080 081 int ll = 3; //6; 082 083 084 int el = 2; 085 086 087 float q = 0.2f; 088 089 090 @Override 091 protected void setUp() { 092 a = b = c = d = e = null; 093 TermOrder to = new TermOrder(TermOrder.INVLEX); 094 String[] vars = new String[] { "w", "x", "y", "z" }; 095 mfac = new GenSolvablePolynomialRing<BigRational>(new BigRational(1), rl, to, vars); 096 RelationGenerator<BigRational> wl = new WeylRelations<BigRational>(); 097 wl.generate(mfac); 098 efac = new SolvableQuotientRing<BigRational>(mfac); 099 zFac = new SolvableQuotientRing<BigRational>(mfac); 100 } 101 102 103 @Override 104 protected void tearDown() { 105 a = b = c = d = e = null; 106 //efac.terminate(); 107 efac = null; 108 zFac = null; 109 ComputerThreads.terminate(); 110 } 111 112 113 /** 114 * Test constructor and toString. 115 */ 116 public void testConstruction() { 117 c = efac.getONE(); 118 //System.out.println("c = " + c); 119 //System.out.println("c.val = " + c.val); 120 assertTrue("length( c ) = 1", c.num.length() == 1); 121 assertTrue("isZERO( c )", !c.isZERO()); 122 assertTrue("isONE( c )", c.isONE()); 123 124 d = efac.getZERO(); 125 //System.out.println("d = " + d); 126 //System.out.println("d.val = " + d.val); 127 assertTrue("length( d ) = 0", d.num.length() == 0); 128 assertTrue("isZERO( d )", d.isZERO()); 129 assertTrue("isONE( d )", !d.isONE()); 130 131 for (SolvableQuotient<BigRational> g : efac.generators()) { 132 //System.out.println("g = " + g); 133 assertFalse("not isZERO( g )", g.isZERO()); 134 } 135 //wrong, solved: 136 assertTrue("isAssociative: ", efac.isAssociative()); 137 } 138 139 140 /** 141 * Test random polynomial. 142 */ 143 public void testRandom() { 144 for (int i = 0; i < 3; i++) { 145 //a = efac.random(ll+i); 146 a = efac.random(kl * (i + 1), ll + i, el, q); 147 //System.out.println("a = " + a); 148 if (a.isZERO() || a.isONE()) { 149 continue; 150 } 151 assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0); 152 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 153 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 154 assertEquals("a == a: ", a, a); 155 } 156 } 157 158 159 /** 160 * Test addition. 161 */ 162 public void testAddition() { 163 a = efac.random(kl, ll, el, q); 164 b = efac.random(kl, ll, el, q); 165 //System.out.println("a = " + a); 166 //System.out.println("b = " + b); 167 168 c = a.sum(efac.getZERO()); 169 d = a.subtract(efac.getZERO()); 170 assertEquals("a+0 = a-0", c, d); 171 172 c = efac.getZERO().sum(a); 173 d = efac.getZERO().subtract(a.negate()); 174 assertEquals("0+a = 0+(-a)", c, d); 175 176 c = a.sum(b); 177 d = c.subtract(b); 178 //System.out.println("c = " + c); 179 //System.out.println("d = " + d); 180 assertEquals("(a+b-b) == a: " + a + ", " + b, a, d); 181 182 c = a.sum(b); 183 d = b.sum(a); 184 //System.out.println("c = " + c); 185 //System.out.println("d = " + d); 186 assertEquals("a+b = b+a", c, d); 187 188 //c = efac.random(kl,ll,el,q); 189 c = new SolvableQuotient<BigRational>(efac, mfac.univariate(1, 2)); //efac.random(kl,ll,el,q); 190 //System.out.println("c = " + c); 191 d = c.sum(a.sum(b)); 192 e = c.sum(a).sum(b); 193 //System.out.println("d = " + d); 194 //System.out.println("e = " + e); 195 assertEquals("c+(a+b) = (c+a)+b", d, e); 196 } 197 198 199 /** 200 * Test multiplication. 201 */ 202 public void testMultiplication() { 203 a = efac.random(kl, ll, el, q); 204 b = efac.random(kl, ll, el, q); 205 //System.out.println("a = " + a); 206 //System.out.println("b = " + b); 207 208 c = a.multiply(efac.getONE()); 209 d = efac.getONE().multiply(a); 210 //System.out.println("c = " + c); 211 //System.out.println("d = " + d); 212 assertEquals("a*1 = 1*a", c, a); 213 assertEquals("a*1 = 1*a", c, d); 214 215 c = b.multiply(a); 216 d = a.multiply(b); 217 //System.out.println("c = " + c); 218 //System.out.println("d = " + d); 219 //non-com assertFalse("a*b = b*a", c.equals(d) ); 220 //e = d.subtract(c); 221 //non-com assertFalse("not isZERO( a*b-b*a ) " + e, e.isZERO() ); 222 223 c = new SolvableQuotient<BigRational>(efac, mfac.univariate(1, 2)); //efac.random(kl,ll,el,q); 224 //System.out.println("c = " + c); 225 d = a.multiply(b.multiply(c)); 226 //System.out.println("d = " + d); 227 e = (a.multiply(b)).multiply(c); 228 //System.out.println("e = " + e); 229 assertEquals("a(bc) = (ab)c", d, e); 230 //assertTrue("a(bc) = (ab)c", d.equals(e) ); 231 if (a.isUnit()) { 232 c = a.inverse(); 233 d = c.multiply(a); 234 //System.out.println("a = " + a); 235 //System.out.println("c = " + c); 236 //System.out.println("d = " + d); 237 assertTrue("a*1/a = 1", d.isONE()); 238 } 239 } 240 241 242 /** 243 * Test addition without syzygy gcd. 244 */ 245 public void testAdditionGcd() { 246 long te, tz; 247 248 a = efac.random(kl, ll, el, q); 249 b = efac.random(kl, ll, el, q); 250 //System.out.println("a = " + a); 251 //System.out.println("b = " + b); 252 253 az = new SolvableQuotient<BigRational>(zFac, a.num, a.den, true); 254 bz = new SolvableQuotient<BigRational>(zFac, b.num, b.den, true); 255 //if (false) { 256 // return; 257 //} 258 259 te = System.currentTimeMillis(); 260 c = a.sum(b); 261 d = c.subtract(b); 262 d = d.monic(); 263 te = System.currentTimeMillis() - te; 264 assertEquals("a+b-b = a", a, d); 265 266 tz = System.currentTimeMillis(); 267 cz = az.sum(bz); 268 dz = cz.subtract(bz); 269 dz = dz.monic(); 270 tz = System.currentTimeMillis() - tz; 271 assertEquals("a+b-b = a", az, dz); 272 273 if (tz >= 0L || te >= 0L) { // true, findbugs 274 return; 275 } 276 System.out.println("te = " + te); 277 System.out.println("tz = " + tz); 278 279 c = a.sum(b); 280 d = b.sum(a); 281 //System.out.println("c = " + c); 282 //System.out.println("d = " + d); 283 assertEquals("a+b = b+a", c, d); 284 285 c = efac.random(kl, ll, el, q); 286 cz = new SolvableQuotient<BigRational>(zFac, c.num, c.den, true); 287 288 289 te = System.currentTimeMillis(); 290 d = c.sum(a.sum(b)); 291 e = c.sum(a).sum(b); 292 te = System.currentTimeMillis() - te; 293 assertEquals("c+(a+b) = (c+a)+b", d, e); 294 295 tz = System.currentTimeMillis(); 296 dz = cz.sum(az.sum(bz)); 297 ez = cz.sum(az).sum(bz); 298 tz = System.currentTimeMillis() - tz; 299 assertEquals("c+(a+b) = (c+a)+b", dz, ez); 300 301 System.out.println("te = " + te); 302 System.out.println("tz = " + tz); 303 304 c = a.sum(efac.getZERO()); 305 d = a.subtract(efac.getZERO()); 306 assertEquals("a+0 = a-0", c, d); 307 308 c = efac.getZERO().sum(a); 309 d = efac.getZERO().subtract(a.negate()); 310 assertEquals("0+a = 0+(-a)", c, d); 311 } 312 313 314 /** 315 * Test parse. 316 */ 317 public void testParse() { 318 a = efac.random(kl, ll, el + 1, q); 319 //PrettyPrint.setInternal(); 320 //System.out.println("a = " + a); 321 PrettyPrint.setPretty(); 322 //System.out.println("a = " + a); 323 String p = a.toString(); 324 //System.out.println("p = " + p); 325 b = efac.parse(p); 326 //System.out.println("b = " + b); 327 328 //c = a.subtract(b); 329 //System.out.println("c = " + c); 330 assertEquals("parse(a.toSting()) = a", a, b); 331 } 332 333}