001 /* 002 * $Id: ComplexAlgebraicTest.java 3611 2011-04-27 21:47:38Z kredel $ 003 */ 004 005 package edu.jas.root; 006 007 008 import junit.framework.Test; 009 import junit.framework.TestCase; 010 import junit.framework.TestSuite; 011 012 import org.apache.log4j.BasicConfigurator; 013 014 import edu.jas.arith.BigRational; 015 import edu.jas.poly.Complex; 016 import edu.jas.poly.ComplexRing; 017 import edu.jas.poly.GenPolynomial; 018 import edu.jas.poly.GenPolynomialRing; 019 import edu.jas.structure.NotInvertibleException; 020 021 022 /** 023 * ComplexAlgebraicNumber Test using JUnit. 024 * @author Heinz Kredel. 025 */ 026 027 public class ComplexAlgebraicTest extends TestCase { 028 029 030 /** 031 * main. 032 */ 033 public static void main(String[] args) { 034 BasicConfigurator.configure(); 035 junit.textui.TestRunner.run(suite()); 036 } 037 038 039 /** 040 * Constructs a <CODE>ComplexAlgebraicTest</CODE> object. 041 * @param name String. 042 */ 043 public ComplexAlgebraicTest(String name) { 044 super(name); 045 } 046 047 048 /** 049 * suite. 050 */ 051 public static Test suite() { 052 TestSuite suite = new TestSuite(ComplexAlgebraicTest.class); 053 return suite; 054 } 055 056 057 //private final static int bitlen = 100; 058 059 ComplexAlgebraicRing<BigRational> fac; 060 061 062 GenPolynomialRing<Complex<BigRational>> mfac; 063 064 065 ComplexAlgebraicNumber<BigRational> a; 066 067 068 ComplexAlgebraicNumber<BigRational> b; 069 070 071 ComplexAlgebraicNumber<BigRational> c; 072 073 074 ComplexAlgebraicNumber<BigRational> d; 075 076 077 ComplexAlgebraicNumber<BigRational> e; 078 079 080 ComplexAlgebraicNumber<BigRational> alpha; 081 082 083 int rl = 1; 084 085 086 int kl = 10; 087 088 089 int ll = 10; 090 091 092 int el = ll; 093 094 095 float q = 0.5f; 096 097 098 @Override 099 protected void setUp() { 100 a = b = c = d = e = null; 101 ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(new BigRational(1)); 102 Complex<BigRational> im = cfac.getIMAG(); 103 BigRational rfac = new BigRational(); 104 BigRational two = new BigRational(2); 105 Complex<BigRational> nw = new Complex<BigRational>(cfac, rfac.getZERO(), two); 106 Complex<BigRational> sw = new Complex<BigRational>(cfac, rfac.getZERO(), rfac.getZERO()); 107 Complex<BigRational> se = new Complex<BigRational>(cfac, two, rfac.getZERO()); 108 Complex<BigRational> ne = new Complex<BigRational>(cfac, two, two); 109 Rectangle<BigRational> positiv = new Rectangle<BigRational>(nw, sw, se, ne); 110 //System.out.println("postiv = " + positiv); 111 String[] vars = new String[] { "alpha" }; 112 mfac = new GenPolynomialRing<Complex<BigRational>>(cfac, rl, vars); 113 Complex<BigRational> r1 = cfac.fromInteger(1).sum(im); 114 Complex<BigRational> r2 = r1.conjugate(); 115 GenPolynomial<Complex<BigRational>> mo = mfac.univariate(0, 1); 116 mo = mo.subtract(r1).multiply(mo.subtract(r2)); // (x - (1+i))((x - (1-i))) 117 fac = new ComplexAlgebraicRing<BigRational>(mo, positiv); 118 alpha = fac.getGenerator(); 119 //System.out.println("fac = " + fac); 120 } 121 122 123 @Override 124 protected void tearDown() { 125 a = b = c = d = e = null; 126 fac = null; 127 alpha = null; 128 } 129 130 131 /** 132 * Test constructor and toString. 133 * 134 */ 135 public void testConstruction() { 136 c = fac.getONE(); 137 //System.out.println("c = " + c); 138 //System.out.println("c.getVal() = " + c.getVal()); 139 assertTrue("length( c ) = 1", c.number.getVal().length() == 1); 140 assertTrue("isZERO( c )", !c.isZERO()); 141 assertTrue("isONE( c )", c.isONE()); 142 143 d = fac.getZERO(); 144 //System.out.println("d = " + d); 145 //System.out.println("d.getVal() = " + d.getVal()); 146 assertTrue("length( d ) = 0", d.number.getVal().length() == 0); 147 assertTrue("isZERO( d )", d.isZERO()); 148 assertTrue("isONE( d )", !d.isONE()); 149 } 150 151 152 /** 153 * Test random polynomial. 154 * 155 */ 156 public void testRandom() { 157 for (int i = 0; i < 7; i++) { 158 a = fac.random(el); 159 //System.out.println("a = " + a); 160 if (a.isZERO() || a.isONE()) { 161 continue; 162 } 163 // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q ); 164 assertTrue("length( a" + i + " ) <> 0", a.number.getVal().length() >= 0); 165 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 166 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 167 } 168 } 169 170 171 /** 172 * Test addition. 173 * 174 */ 175 public void testAddition() { 176 a = fac.random(ll); 177 b = fac.random(ll); 178 179 c = a.sum(b); 180 d = c.subtract(b); 181 assertEquals("a+b-b = a", a, d); 182 183 c = a.sum(b); 184 d = b.sum(a); 185 assertEquals("a+b = b+a", c, d); 186 187 c = fac.random(ll); 188 d = c.sum(a.sum(b)); 189 e = c.sum(a).sum(b); 190 assertEquals("c+(a+b) = (c+a)+b", d, e); 191 192 193 c = a.sum(fac.getZERO()); 194 d = a.subtract(fac.getZERO()); 195 assertEquals("a+0 = a-0", c, d); 196 197 c = fac.getZERO().sum(a); 198 d = fac.getZERO().subtract(a.negate()); 199 assertEquals("0+a = 0+(-a)", c, d); 200 } 201 202 203 /** 204 * Test object multiplication. 205 * 206 */ 207 public void testMultiplication() { 208 a = fac.random(ll); 209 assertTrue("not isZERO( a )", !a.isZERO()); 210 211 b = fac.random(ll); 212 assertTrue("not isZERO( b )", !b.isZERO()); 213 214 c = b.multiply(a); 215 d = a.multiply(b); 216 assertTrue("not isZERO( c )", !c.isZERO()); 217 assertTrue("not isZERO( d )", !d.isZERO()); 218 219 //System.out.println("a = " + a); 220 //System.out.println("b = " + b); 221 e = d.subtract(c); 222 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 223 224 assertTrue("a*b = b*a", c.equals(d)); 225 assertEquals("a*b = b*a", c, d); 226 227 c = fac.random(ll); 228 //System.out.println("c = " + c); 229 d = a.multiply(b.multiply(c)); 230 e = (a.multiply(b)).multiply(c); 231 232 //System.out.println("d = " + d); 233 //System.out.println("e = " + e); 234 235 //System.out.println("d-e = " + d.subtract(c) ); 236 237 assertEquals("a(bc) = (ab)c", d, e); 238 assertTrue("a(bc) = (ab)c", d.equals(e)); 239 240 c = a.multiply(fac.getONE()); 241 d = fac.getONE().multiply(a); 242 assertEquals("a*1 = 1*a", c, d); 243 244 245 c = a.inverse(); 246 d = c.multiply(a); 247 //System.out.println("a = " + a); 248 //System.out.println("c = " + c); 249 //System.out.println("d = " + d); 250 assertEquals("a*1/a = 1", fac.getONE(), d); 251 252 try { 253 a = fac.getZERO().inverse(); 254 } catch (NotInvertibleException expected) { 255 return; 256 } 257 fail("0 invertible"); 258 } 259 260 261 /** 262 * Test distributive law. 263 * 264 */ 265 public void testDistributive() { 266 a = fac.random(ll); 267 b = fac.random(ll); 268 c = fac.random(ll); 269 270 d = a.multiply(b.sum(c)); 271 e = a.multiply(b).sum(a.multiply(c)); 272 273 assertEquals("a(b+c) = ab+ac", d, e); 274 } 275 276 277 /** 278 * Test compareTo of complex algebraic numbers. 279 * 280 */ 281 public void testCompare() { 282 a = fac.random(ll).abs(); 283 b = a.sum(fac.getONE()); 284 c = b.sum(fac.getONE()); 285 286 int ab = a.compareTo(b); 287 int bc = b.compareTo(c); 288 int ac = a.compareTo(c); 289 290 assertTrue("a < a+1 ", ab < 0); 291 assertTrue("a+1 < a+2 ", bc < 0); 292 assertTrue("a < a+2 ", ac < 0); 293 294 a = a.negate(); 295 b = a.sum(fac.getONE()); 296 c = b.sum(fac.getONE()); 297 298 ab = a.compareTo(b); 299 bc = b.compareTo(c); 300 ac = a.compareTo(c); 301 302 assertTrue("a < a+1 ", ab < 0); 303 assertTrue("a+1 < a+2 ", bc < 0); 304 assertTrue("a < a+2 ", ac < 0); 305 } 306 307 }