001/* 002 * $Id: RatGenSolvablePolynomialTest.java 5905 2018-08-24 10:23:58Z kredel $ 003 */ 004 005package edu.jas.poly; 006 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012 013import edu.jas.arith.BigRational; 014 015 016/** 017 * BigRational coefficients GenSolvablePolynomial tests with JUnit. 018 * @author Heinz Kredel 019 */ 020 021public class RatGenSolvablePolynomialTest extends TestCase { 022 023 024 /** 025 * main. 026 */ 027 public static void main(String[] args) { 028 junit.textui.TestRunner.run(suite()); 029 } 030 031 032 /** 033 * Constructs a <CODE>RatGenSolvablePolynomialTest</CODE> object. 034 * @param name String. 035 */ 036 public RatGenSolvablePolynomialTest(String name) { 037 super(name); 038 } 039 040 041 /** 042 */ 043 public static Test suite() { 044 TestSuite suite = new TestSuite(RatGenSolvablePolynomialTest.class); 045 return suite; 046 } 047 048 049 GenSolvablePolynomial<BigRational> a, b, c, d, e, f, x1, x2; 050 051 052 int rl = 5; 053 054 055 int kl = 10; 056 057 058 int ll = 5; 059 060 061 int el = 3; 062 063 064 float q = 0.5f; 065 066 067 RelationTable<BigRational> table; 068 069 070 GenSolvablePolynomialRing<BigRational> ring; 071 072 073 BigRational cfac; 074 075 076 @Override 077 protected void setUp() { 078 cfac = new BigRational(1); 079 ring = new GenSolvablePolynomialRing<BigRational>(cfac, rl); 080 table = ring.table; 081 a = b = c = d = e = null; 082 } 083 084 085 @Override 086 protected void tearDown() { 087 table = null; 088 ring = null; 089 a = b = c = d = e = null; 090 } 091 092 093 /** 094 * Test constructor and toString. 095 */ 096 public void testConstructor() { 097 a = new GenSolvablePolynomial<BigRational>(ring); 098 assertTrue("length( a ) = 0", a.length() == 0); 099 assertTrue("isZERO( a )", a.isZERO()); 100 assertTrue("isONE( a )", !a.isONE()); 101 102 c = ring.getONE(); 103 assertTrue("length( c ) = 1", c.length() == 1); 104 assertTrue("isZERO( c )", !c.isZERO()); 105 assertTrue("isONE( c )", c.isONE()); 106 107 d = ring.getZERO(); 108 assertTrue("length( d ) = 0", d.length() == 0); 109 assertTrue("isZERO( d )", d.isZERO()); 110 assertTrue("isONE( d )", !d.isONE()); 111 } 112 113 114 /** 115 * Test random polynomial. 116 */ 117 public void testRandom() { 118 assertTrue("isCommutative()", ring.isCommutative()); 119 120 for (int i = 0; i < 2; i++) { 121 // a = ring.random(ll+2*i); 122 a = ring.random(kl * (i + 1), ll + 2 * i, el + i, q); 123 assertTrue("length( a" + i + " ) <> 0", a.length() >= 0); 124 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 125 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 126 } 127 } 128 129 130 /** 131 * Test addition. 132 */ 133 public void testAddition() { 134 a = ring.random(kl, ll, el, q); 135 136 c = (GenSolvablePolynomial<BigRational>) a.subtract(a); 137 assertTrue("a-a = 0", c.isZERO()); 138 139 b = (GenSolvablePolynomial<BigRational>) a.sum(a); 140 c = (GenSolvablePolynomial<BigRational>) b.subtract(a); 141 142 assertEquals("a+a-a = a", c, a); 143 assertTrue("a+a-a = a", c.equals(a)); 144 145 b = ring.random(kl, ll, el, q); 146 c = (GenSolvablePolynomial<BigRational>) b.sum(a); 147 d = (GenSolvablePolynomial<BigRational>) a.sum(b); 148 149 assertEquals("a+b = b+a", c, d); 150 assertTrue("a+b = b+a", c.equals(d)); 151 152 c = ring.random(kl, ll, el, q); 153 d = (GenSolvablePolynomial<BigRational>) a.sum(b.sum(c)); 154 e = (GenSolvablePolynomial<BigRational>) a.sum(b).sum(c); 155 156 assertEquals("a+(b+c) = (a+b)+c", d, e); 157 assertTrue("a+(b+c) = (a+b)+c", d.equals(e)); 158 159 ExpVector u = ExpVector.random(rl, el, q); 160 BigRational x = cfac.random(kl); 161 162 b = ring.getONE().multiply(x, u); 163 c = (GenSolvablePolynomial<BigRational>) a.sum(b); 164 d = (GenSolvablePolynomial<BigRational>) a.sum(x, u); 165 assertEquals("a+p(x,u) = a+(x,u)", c, d); 166 167 c = (GenSolvablePolynomial<BigRational>) a.subtract(b); 168 d = (GenSolvablePolynomial<BigRational>) a.subtract(x, u); 169 assertEquals("a-p(x,u) = a-(x,u)", c, d); 170 171 a = ring.getZERO(); 172 b = ring.getONE().multiply(x, u); 173 c = (GenSolvablePolynomial<BigRational>) b.sum(a); 174 d = (GenSolvablePolynomial<BigRational>) a.sum(x, u); 175 assertEquals("a+p(x,u) = a+(x,u)", c, d); 176 177 c = (GenSolvablePolynomial<BigRational>) a.subtract(b); 178 d = (GenSolvablePolynomial<BigRational>) a.subtract(x, u); 179 assertEquals("a-p(x,u) = a-(x,u)", c, d); 180 } 181 182 183 /** 184 * Test object multiplication. 185 */ 186 @SuppressWarnings("cast") 187 public void testMultiplication() { 188 a = ring.random(kl, ll, el, q); 189 assertTrue("not isZERO( a )", !a.isZERO()); 190 //a = RatGenSolvablePolynomial.DIRRAS(1, kl, 4, el, q ); 191 192 b = ring.random(kl, ll, el, q); 193 assertTrue("not isZERO( b )", !b.isZERO()); 194 195 c = b.multiply(a); 196 d = a.multiply(b); 197 assertTrue("not isZERO( c )", !c.isZERO()); 198 assertTrue("not isZERO( d )", !d.isZERO()); 199 200 e = (GenSolvablePolynomial<BigRational>) d.subtract(c); 201 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 202 203 assertEquals("a*b = b*a", c, d); 204 assertTrue("a*b = b*a", c.equals(d)); 205 206 c = ring.random(kl, ll, el, q); 207 d = a.multiply(b.multiply(c)); 208 e = (a.multiply(b)).multiply(c); 209 210 assertEquals("a(bc) = (ab)c", d, e); 211 assertTrue("a(bc) = (ab)c", d.equals(e)); 212 213 BigRational x = a.leadingBaseCoefficient().inverse(); 214 c = (GenSolvablePolynomial<BigRational>) a.monic(); 215 d = a.multiply(x); 216 assertEquals("a.monic() = a(1/ldcf(a))", c, d); 217 218 ExpVector u = ring.evzero; 219 BigRational y = b.leadingBaseCoefficient().inverse(); 220 c = (GenSolvablePolynomial<BigRational>) b.monic(); 221 d = b.multiply(y, u); 222 assertEquals("b.monic() = b(1/ldcf(b))", c, d); 223 224 e = ring.getONE().multiply(y, u); 225 d = b.multiply(e); 226 assertEquals("b.monic() = b(1/ldcf(b))", c, d); 227 228 d = e.multiply(b); 229 assertEquals("b.monic() = (1/ldcf(b) (0))*b", c, d); 230 231 d = a.monic(); 232 assertTrue("a.monic(): ", d.leadingBaseCoefficient().isONE()); 233 } 234 235 236 /** 237 * Test Weyl polynomials. 238 */ 239 public void testWeyl() { 240 int rloc = 4; 241 ring = new GenSolvablePolynomialRing<BigRational>(cfac, rloc); 242 243 RelationGenerator<BigRational> wl = new WeylRelations<BigRational>(); 244 wl.generate(ring); 245 table = ring.table; 246 //System.out.println("table = " + table); 247 //System.out.println("ring = " + ring); 248 249 assertFalse("isCommutative()", ring.isCommutative()); 250 assertTrue("isAssociative()", ring.isAssociative()); 251 252 a = ring.random(kl, ll, el, q); 253 assertTrue("not isZERO( a )", !a.isZERO()); 254 //System.out.println("a = " + a); 255 256 b = ring.random(kl, ll, el, q); 257 assertTrue("not isZERO( b )", !b.isZERO()); 258 //System.out.println("b = " + b); 259 260 261 // non commutative 262 c = b.multiply(a); 263 d = a.multiply(b); 264 //System.out.println("c = " + c); 265 //System.out.println("d = " + d); 266 assertTrue("not isZERO( c )", !c.isZERO()); 267 assertTrue("not isZERO( d )", !d.isZERO()); 268 269 e = (GenSolvablePolynomial<BigRational>) d.subtract(c); 270 assertTrue("!isZERO( a*b-b*a ) " + e, !e.isZERO()); 271 assertTrue("a*b != b*a", c.equals(d) || c.leadingExpVector().equals(d.leadingExpVector())); 272 273 c = ring.random(kl, ll, el, q); 274 //System.out.println("\na = " + a); 275 //System.out.println("\nb = " + b); 276 //System.out.println("\nc = " + c); 277 278 // associative 279 //x1 = b.multiply(c); 280 //System.out.println("\nx1 = " + x1); 281 d = a.multiply(b.multiply(c)); 282 283 //x2 = a.multiply(b); 284 //System.out.println("\nx2 = " + x2); 285 e = a.multiply(b).multiply(c); 286 287 //System.out.println("\nd = " + d); 288 //System.out.println("\ne = " + e); 289 290 //f = (GenSolvablePolynomial<BigRational>)d.subtract(e); 291 //System.out.println("\nf = " + f); 292 293 assertEquals("a(bc) = (ab)c", d, e); 294 assertTrue("a(bc) = (ab)c", d.equals(e)); 295 } 296 297 298 /** 299 * Test division of polynomials. 300 */ 301 public void testDivide() { 302 int rloc = 4; 303 ring = new GenSolvablePolynomialRing<BigRational>(cfac, rloc); 304 305 RelationGenerator<BigRational> wl = new WeylRelations<BigRational>(); 306 wl.generate(ring); 307 //System.out.println("ring = " + ring.toScript()); 308 309 assertFalse("isCommutative()", ring.isCommutative()); 310 assertTrue("isAssociative()", ring.isAssociative()); 311 312 do { 313 a = ring.random(kl, ll, el, q); 314 } while(a.isZERO()); 315 //System.out.println("a = " + a); 316 317 do { 318 b = ring.random(kl, ll, el, q); 319 } while(b.isZERO()); 320 //System.out.println("b = " + b); 321 322 // non commutative 323 c = b.multiply(a); 324 d = a.multiply(b); 325 //System.out.println("c = " + c); 326 //System.out.println("d = " + d); 327 assertTrue("not isZERO( c )", !c.isZERO()); 328 assertTrue("not isZERO( d )", !d.isZERO()); 329 330 e = (GenSolvablePolynomial<BigRational>) d.subtract(c); 331 assertTrue("a*b != b*a", !c.equals(d) || c.leadingExpVector().equals(d.leadingExpVector())); 332 333 // divide 334 e = c.divide(a); 335 //System.out.println("e = " + e); 336 f = c.rightDivide(b); 337 //System.out.println("f = " + f); 338 assertEquals("b == b*a/a: " + e, e, b); 339 assertEquals("a == b*a/b: " + e, f, a); 340 341 e = d.rightDivide(a); 342 //System.out.println("e = " + e); 343 f = d.divide(b); 344 //System.out.println("f = " + f); 345 assertEquals("b == a*b/a: " + e, e, b); 346 assertEquals("a == a*b/b: " + e, f, a); 347 } 348 349 350 /** 351 * Test distributive law. 352 */ 353 public void testDistributive() { 354 int rloc = 4; 355 ring = new GenSolvablePolynomialRing<BigRational>(cfac, rloc); 356 357 RelationGenerator<BigRational> wl = new WeylRelations<BigRational>(); 358 wl.generate(ring); 359 //table = ring.table; 360 //System.out.println("table = " + table); 361 //System.out.println("ring = " + ring); 362 363 a = ring.random(kl, ll, el, q); 364 b = ring.random(kl, ll, el, q); 365 c = ring.random(kl, ll, el, q); 366 367 d = a.multiply((GenSolvablePolynomial<BigRational>) b.sum(c)); 368 e = (GenSolvablePolynomial<BigRational>) a.multiply(b).sum(a.multiply(c)); 369 370 assertEquals("a(b+c) = ab+ac", d, e); 371 } 372}