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