001/* 002 * $Id$ 003 */ 004 005package edu.jas.application; 006 007 008import java.util.ArrayList; 009import java.util.List; 010 011import edu.jas.arith.BigRational; 012import edu.jas.kern.ComputerThreads; 013import edu.jas.poly.GenPolynomial; 014import edu.jas.poly.GenPolynomialRing; 015 016import junit.framework.Test; 017import junit.framework.TestCase; 018import junit.framework.TestSuite; 019 020 021/** 022 * Local tests with JUnit. 023 * @author Heinz Kredel 024 */ 025 026public class LocalTest extends TestCase { 027 028 029 /** 030 * main. 031 */ 032 public static void main(String[] args) { 033 // 034 junit.textui.TestRunner.run(suite()); 035 } 036 037 038 /** 039 * Constructs a <CODE>LocalTest</CODE> object. 040 * @param name String. 041 */ 042 public LocalTest(String name) { 043 super(name); 044 } 045 046 047 /** 048 * suite. 049 */ 050 public static Test suite() { 051 TestSuite suite = new TestSuite(LocalTest.class); 052 return suite; 053 } 054 055 056 //private final static int bitlen = 100; 057 058 Ideal<BigRational> id; 059 060 061 LocalRing<BigRational> fac; 062 063 064 GenPolynomialRing<BigRational> mfac; 065 066 067 List<GenPolynomial<BigRational>> F; 068 069 070 Local<BigRational> a, b, c, d, e; 071 072 073 int rl = 3; 074 075 076 int kl = 3; 077 078 079 int ll = 5; 080 081 082 int el = 2; 083 084 085 float q = 0.3f; 086 087 088 int il = 2; 089 090 091 @Override 092 protected void setUp() { 093 a = b = c = d = e = null; 094 BigRational cfac = new BigRational(1); 095 String[] vars = new String[] { "x", "y", "z" }; 096 mfac = new GenPolynomialRing<BigRational>(cfac, rl, vars); 097 id = null; 098 while (id == null || id.isONE()) { 099 F = new ArrayList<GenPolynomial<BigRational>>(il); 100 for (int i = 0; i < rl; i++) { 101 //GenPolynomial<BigRational> mo = mfac.random(kl,ll,el,q); 102 GenPolynomial<BigRational> mo = mfac.univariate(i); 103 mo = mo.sum(mfac.fromInteger(cfac.random(7).denominator())); 104 while (mo.isConstant()) { 105 mo = mfac.random(kl, ll, el, q); 106 } 107 F.add(mo); 108 } 109 id = new Ideal<BigRational>(mfac, F); 110 id = id.GB(); 111 } 112 //System.out.println("id = " + id); 113 fac = new LocalRing<BigRational>(id); 114 //System.out.println("fac = " + fac); 115 F = null; 116 } 117 118 119 @Override 120 protected void tearDown() { 121 a = b = c = d = e = null; 122 fac = null; 123 id = null; 124 mfac = null; 125 ComputerThreads.terminate(); 126 } 127 128 129 /** 130 * Test factory. 131 */ 132 public void testRing() { 133 assertFalse("#ring infinite", fac.isFinite()); 134 assertTrue("associative ring", fac.isAssociative()); 135 assertTrue("commutative ring", fac.isCommutative()); 136 assertTrue("characteristic zero", fac.characteristic().signum() == 0); 137 assertTrue("no field", fac.isField()); 138 } 139 140 141 /** 142 * Test constructor and toString. 143 * 144 */ 145 public void testConstruction() { 146 c = fac.getONE(); 147 //System.out.println("c = " + c); 148 //System.out.println("c.num = " + c.num); 149 assertTrue("length( c ) = 1", c.num.length() == 1); 150 assertTrue("isZERO( c )", !c.isZERO()); 151 assertTrue("isONE( c )", c.isONE()); 152 153 d = fac.getZERO(); 154 //System.out.println("d = " + d); 155 //System.out.println("d.num = " + d.num); 156 assertTrue("length( d ) = 0", d.num.length() == 0); 157 assertTrue("isZERO( d )", d.isZERO()); 158 assertTrue("isONE( d )", !d.isONE()); 159 160 List<Local<BigRational>> gens = fac.generators(); 161 //System.out.println("gens = " + gens); 162 assertTrue("#gens == 7: ", gens.size() == 7); 163 for (Local<BigRational> v : gens) { 164 a = fac.parse(v.toString()); 165 assertEquals("a == v", a, v); 166 } 167 } 168 169 170 /** 171 * Test random polynomial. 172 * 173 */ 174 public void testRandom() { 175 //System.out.println("fac = " + fac); 176 for (int i = 0; i < 4; i++) { 177 //a = fac.random(ll+i); 178 a = fac.random(kl * (i + 1), ll + i, el, q); 179 //System.out.println("a = " + a); 180 assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0); 181 if (a.isZERO() || a.isONE()) { 182 continue; 183 } 184 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 185 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 186 } 187 } 188 189 190 /** 191 * Test addition. Not jet working because of monic GBs. 192 */ 193 public void testAddition() { 194 //System.out.println("fac = " + fac); 195 196 a = fac.random(kl, ll, el, q); 197 b = fac.random(kl, ll, el, q); 198 //System.out.println("a = " + a); 199 //System.out.println("b = " + b); 200 201 c = a.sum(b); 202 d = c.subtract(b); 203 //System.out.println("c = " + c); 204 //System.out.println("d = " + d); 205 assertEquals("a+b-b = a", a, d); 206 207 c = a.sum(b); 208 d = b.sum(a); 209 assertEquals("a+b = b+a", c, d); 210 211 212 c = fac.random(kl, ll, el, q); 213 d = c.sum(a.sum(b)); 214 e = c.sum(a).sum(b); 215 assertEquals("c+(a+b) = (c+a)+b", d, e); 216 217 c = a.sum(fac.getZERO()); 218 d = a.subtract(fac.getZERO()); 219 assertEquals("a+0 = a-0", c, d); 220 221 c = fac.getZERO().sum(a); 222 d = fac.getZERO().subtract(a.negate()); 223 assertEquals("0+a = 0+(-a)", c, d); 224 } 225 226 227 /** 228 * Test object multiplication. Not jet working because of monic GBs 229 */ 230 231 public void testMultiplication() { 232 //System.out.println("fac = " + fac); 233 234 a = fac.random(kl, ll, el, q); 235 b = fac.random(kl, ll, el, q); 236 237 if (a.isZERO() || b.isZERO()) { 238 return; 239 } 240 //System.out.println("a = " + a); 241 //System.out.println("b = " + b); 242 assertTrue("not isZERO( a )", !a.isZERO()); 243 assertTrue("not isZERO( b )", !b.isZERO()); 244 245 c = b.multiply(a); 246 d = a.multiply(b); 247 //System.out.println("c = " + c); 248 //System.out.println("d = " + d); 249 assertTrue("not isZERO( c )", !c.isZERO()); 250 assertTrue("not isZERO( d )", !d.isZERO()); 251 252 e = d.subtract(c); 253 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 254 255 assertTrue("a*b = b*a", c.equals(d)); 256 assertEquals("a*b = b*a", c, d); 257 258 c = fac.random(kl, ll, el, q); 259 d = a.multiply(b.multiply(c)); 260 e = (a.multiply(b)).multiply(c); 261 //System.out.println("c = " + c); 262 //System.out.println("d = " + d); 263 //System.out.println("e = " + e); 264 265 assertEquals("a(bc) = (ab)c", d, e); 266 assertTrue("a(bc) = (ab)c", d.equals(e)); 267 268 c = a.multiply(fac.getONE()); 269 d = fac.getONE().multiply(a); 270 assertEquals("a*1 = 1*a", c, d); 271 272 if (a.isUnit()) { 273 c = a.inverse(); 274 d = c.multiply(a); 275 //System.out.println("a = " + a); 276 //System.out.println("c = " + c); 277 //System.out.println("d = " + d); 278 assertTrue("a*1/a = 1", d.isONE()); 279 d = c.inverse(); 280 //System.out.println("d = " + d); 281 assertTrue("1/(1/a) = a", d.equals(a)); 282 } 283 } 284 285}