001/* 002 * $Id$ 003 */ 004 005package edu.jas.poly; 006 007 008import java.util.HashSet; 009import java.util.List; 010import java.util.Set; 011 012import edu.jas.arith.BigInteger; 013 014import junit.framework.Test; 015import junit.framework.TestCase; 016import junit.framework.TestSuite; 017 018 019/** 020 * BigInteger coefficients GenPolynomial tests with JUnit. 021 * @author Heinz Kredel 022 */ 023 024public class IntGenPolynomialTest extends TestCase { 025 026 027 /** 028 * main. 029 */ 030 public static void main(String[] args) { 031 junit.textui.TestRunner.run(suite()); 032 } 033 034 035 /** 036 * Constructs a <CODE>IntGenPolynomialTest</CODE> object. 037 * @param name String. 038 */ 039 public IntGenPolynomialTest(String name) { 040 super(name); 041 } 042 043 044 /** 045 */ 046 public static Test suite() { 047 TestSuite suite = new TestSuite(IntGenPolynomialTest.class); 048 return suite; 049 } 050 051 GenPolynomialRing<BigInteger> fac; 052 053 054 GenPolynomial<BigInteger> a, b, c, d, e; 055 056 057 int rl = 7; 058 059 060 int kl = 10; 061 062 063 int ll = 10; 064 065 066 int el = 5; 067 068 069 float q = 0.3f; 070 071 @Override 072 protected void setUp() { 073 a = b = c = d = e = null; 074 fac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl); 075 } 076 077 078 @Override 079 protected void tearDown() { 080 a = b = c = d = e = null; 081 fac = null; 082 } 083 084 085 /** 086 * Test generate. 087 */ 088 public void testGenerate() { 089 String s = fac.toScript(); 090 //System.out.println("fac.toScript: " + s + ", " + s.length()); 091 assertTrue("#s == 50: " + s, s.length() == 50); 092 093 List<GenPolynomial<BigInteger>> gens = fac.generators(); 094 assertFalse("#gens != () ", gens.isEmpty()); 095 //System.out.println("generators: " + gens); 096 097 // test equals 098 Set<GenPolynomial<BigInteger>> set = new HashSet<GenPolynomial<BigInteger>>(gens); 099 //System.out.println("gen set: " + set); 100 assertEquals("#gens == #set: ", gens.size(), set.size()); 101 102 // test for elements 0, 1 103 a = fac.getZERO(); 104 b = fac.getONE(); 105 assertFalse("0 not in #set: ", set.contains(a)); 106 assertTrue("1 in #set: ", set.contains(b)); 107 108 // specific tests 109 assertEquals("#gens == rl+1 ", rl + 1, gens.size()); 110 Set<Integer> iset = new HashSet<Integer>(set.size()); 111 for (GenPolynomial<BigInteger> p : gens) { 112 //System.out.println("p = " + p.toScript() + ", # = " + p.hashCode() + ", red = " + p.reductum()); 113 assertTrue("red(p) == 0 ", p.reductum().isZERO()); 114 iset.add(p.hashCode()); 115 } 116 assertEquals("#gens == #iset: ", gens.size(), iset.size()); 117 } 118 119 120 /** 121 * Test constructor and toString. 122 */ 123 public void testConstruction() { 124 c = fac.getONE(); 125 assertTrue("length( c ) = 1", c.length() == 1); 126 assertTrue("isZERO( c )", !c.isZERO()); 127 assertTrue("isONE( c )", c.isONE()); 128 129 d = fac.getZERO(); 130 assertTrue("length( d ) = 0", d.length() == 0); 131 assertTrue("isZERO( d )", d.isZERO()); 132 assertTrue("isONE( d )", !d.isONE()); 133 } 134 135 136 /** 137 * Test random polynomial. 138 */ 139 public void testRandom() { 140 for (int i = 0; i < 7; i++) { 141 a = fac.random(kl * (i + 2), ll + 2 * i, el + i, q); 142 assertTrue("length( a" + i + " ) <> 0", a.length() >= 0); 143 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 144 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 145 } 146 } 147 148 149 /** 150 * Test addition. 151 */ 152 public void testAddition() { 153 a = fac.random(kl, ll, el, q); 154 b = fac.random(kl, ll, el, q); 155 156 c = a.sum(b); 157 d = c.subtract(b); 158 assertEquals("a+b-b = a", a, d); 159 160 c = fac.random(kl, ll, el, q); 161 162 ExpVector u = ExpVector.random(rl, el, q); 163 BigInteger x = BigInteger.IRAND(kl); 164 165 b = new GenPolynomial<BigInteger>(fac, x, u); 166 c = a.sum(b); 167 d = a.sum(x, u); 168 assertEquals("a+p(x,u) = a+(x,u)", c, d); 169 170 c = a.subtract(b); 171 d = a.subtract(x, u); 172 assertEquals("a-p(x,u) = a-(x,u)", c, d); 173 174 a = new GenPolynomial<BigInteger>(fac); 175 b = new GenPolynomial<BigInteger>(fac, x, u); 176 c = b.sum(a); 177 d = a.sum(x, u); 178 assertEquals("a+p(x,u) = a+(x,u)", c, d); 179 180 c = a.subtract(b); 181 d = a.subtract(x, u); 182 assertEquals("a-p(x,u) = a-(x,u)", c, d); 183 } 184 185 186 /** 187 * Test object multiplication. 188 */ 189 public void testMultiplication() { 190 a = fac.random(kl, ll, el, q); 191 assertTrue("not isZERO( a )", !a.isZERO()); 192 193 b = fac.random(kl, ll, el, q); 194 assertTrue("not isZERO( b )", !b.isZERO()); 195 196 c = b.multiply(a); 197 d = a.multiply(b); 198 assertTrue("not isZERO( c )", !c.isZERO()); 199 assertTrue("not isZERO( d )", !d.isZERO()); 200 201 //System.out.println("a = " + a); 202 //System.out.println("b = " + b); 203 e = d.subtract(c); 204 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 205 206 assertTrue("a*b = b*a", c.equals(d)); 207 assertEquals("a*b = b*a", c, d); 208 209 c = fac.random(kl, ll, el, q); 210 //System.out.println("c = " + c); 211 d = a.multiply(b.multiply(c)); 212 e = (a.multiply(b)).multiply(c); 213 //System.out.println("d = " + d); 214 //System.out.println("e = " + e); 215 //System.out.println("d-e = " + d.subtract(c) ); 216 assertEquals("a(bc) = (ab)c", d, e); 217 assertTrue("a(bc) = (ab)c", d.equals(e)); 218 219 //BigInteger x = a.leadingBaseCoefficient().inverse(); 220 //c = a.monic(); 221 //d = a.multiply(x); 222 //assertEquals("a.monic() = a(1/ldcf(a))",c,d); 223 224 BigInteger y = b.leadingBaseCoefficient(); 225 //c = b.monic(); 226 //d = b.multiply(y); 227 //assertEquals("b.monic() = b(1/ldcf(b))",c,d); 228 229 e = new GenPolynomial<BigInteger>(fac, y); 230 c = b.multiply(e); 231 // assertEquals("b.monic() = b(1/ldcf(b))",c,d); 232 233 d = e.multiply(b); 234 assertEquals("b*p(y,u) = p(y,u)*b", c, d); 235 } 236 237 238 /** 239 * Test BLAS level 1. 240 */ 241 public void testBLAS1() { 242 a = fac.random(kl, ll, el, q); 243 b = fac.random(kl, ll, el, q); 244 ExpVector ev = ExpVector.random(rl, el, q); 245 BigInteger lc = BigInteger.IRAND(kl); 246 247 d = a.subtractMultiple(lc, b); 248 e = a.subtract(b.multiply(lc)); 249 assertEquals("a - (lc) b == a - ((lc) b)", d, e); 250 251 d = a.subtractMultiple(lc, ev, b); 252 e = a.subtract(b.multiply(lc, ev)); 253 assertEquals("a - (lc ev) b == a - ((lc ev) b)", d, e); 254 255 ExpVector fv = ExpVector.random(rl, el, q); 256 BigInteger tc = BigInteger.IRAND(kl); 257 258 d = a.scaleSubtractMultiple(tc, lc, ev, b); 259 e = a.multiply(tc).subtract(b.multiply(lc, ev)); 260 assertEquals("(tc) a - (lc ev) b == ((tc) a - ((lc ev) b))", d, e); 261 262 d = a.scaleSubtractMultiple(tc, fv, lc, ev, b); 263 e = a.multiply(tc, fv).subtract(b.multiply(lc, ev)); 264 assertEquals("(tc fv) a - (lc ev) b == ((tc fv) a - ((lc ev) b))", d, e); 265 266 d = a.scaleSubtractMultiple(tc, lc, b); 267 e = a.multiply(tc).subtract(b.multiply(lc)); 268 assertEquals("tc a - lc b == (tc a) - (lc b)", d, e); 269 } 270 271 272 /** 273 * Test distributive law. 274 */ 275 public void testDistributive() { 276 a = fac.random(kl, ll, el, q); 277 b = fac.random(kl, ll, el, q); 278 c = fac.random(kl, ll, el, q); 279 280 d = a.multiply(b.sum(c)); 281 e = a.multiply(b).sum(a.multiply(c)); 282 283 assertEquals("a(b+c) = ab+ac", d, e); 284 } 285 286 287 /** 288 * Test absolute norm. 289 */ 290 public void testAbsNorm() { 291 BigInteger r, ar; 292 a = fac.getONE().negate(); 293 //System.out.println("a = " + a); 294 295 r = PolyUtil.<BigInteger> absNorm(a); 296 //System.out.println("r = " + r); 297 assertTrue("isONE( absNorm(-1) )", r.isONE()); 298 299 a = fac.random(kl * 2, ll + 2, el, q); 300 //System.out.println("a = " + a); 301 302 r = PolyUtil.<BigInteger> absNorm(a); 303 //System.out.println("r = " + r); 304 assertTrue(" not isZERO( absNorm(a) )", !r.isZERO() || a.isZERO()); 305 } 306 307 308 /** 309 * Test max norm. 310 */ 311 public void testMaxNorm() { 312 BigInteger r, s; 313 a = fac.getZERO(); 314 //System.out.println("a = " + a); 315 316 r = a.maxNorm(); 317 //System.out.println("r = " + r); 318 assertTrue("isONE( maxNorm(0) )", r.isZERO()); 319 320 r = a.sumNorm(); 321 //System.out.println("r = " + r); 322 assertTrue("isONE( sumNorm(0) )", r.isZERO()); 323 324 a = fac.getONE().negate(); 325 //System.out.println("a = " + a); 326 327 r = a.maxNorm(); 328 //System.out.println("r = " + r); 329 assertTrue("isONE( maxNorm(-1) )", r.isONE()); 330 331 r = a.sumNorm(); 332 //System.out.println("r = " + r); 333 assertTrue("isONE( sumNorm(-1) )", r.isONE()); 334 335 a = fac.random(kl * 2, ll + 2, el, q); 336 //System.out.println("a = " + a); 337 338 r = a.maxNorm(); 339 //System.out.println("r = " + r); 340 assertTrue("not isZERO( maxNorm(a) )", !r.isZERO() || a.isZERO()); 341 342 //s = a.multiply(a).maxNorm(); 343 //System.out.println("s = " + s + ", r*r = " + r.multiply(r)); 344 //assertEquals("s*s == maxNorm(a*a) )", r.multiply(r), s ); 345 346 r = a.sumNorm(); 347 //System.out.println("r = " + r); 348 assertTrue("not isZERO( maxNorm(a) )", !r.isZERO() || a.isZERO()); 349 350 //s = a.multiply(a).sumNorm(); 351 //System.out.println("s = " + s + ", r*r = " + r.multiply(r)); 352 //assertEquals("s*s == sumNorm(a*a) ): ", r.multiply(r), s ); 353 } 354 355}