001/* 002 * $Id: UnivPowerSeriesTest.java 4071 2012-07-27 19:59:38Z kredel $ 003 */ 004 005package edu.jas.ps; 006 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012import edu.jas.arith.BigRational; 013import edu.jas.poly.GenPolynomial; 014import edu.jas.poly.GenPolynomialRing; 015 016 017/** 018 * Univariate power series tests with JUnit. 019 * @author Heinz Kredel. 020 */ 021 022public class UnivPowerSeriesTest extends TestCase { 023 024 025 /** 026 * main. 027 */ 028 public static void main(String[] args) { 029 junit.textui.TestRunner.run(suite()); 030 } 031 032 033 /** 034 * Constructs a <CODE>UnivPowerSeriesTest</CODE> object. 035 * @param name String. 036 */ 037 public UnivPowerSeriesTest(String name) { 038 super(name); 039 } 040 041 042 /** 043 */ 044 public static Test suite() { 045 TestSuite suite = new TestSuite(UnivPowerSeriesTest.class); 046 return suite; 047 } 048 049 050 //private final static int bitlen = 100; 051 052 UnivPowerSeriesRing<BigRational> fac; 053 054 055 UnivPowerSeries<BigRational> a; 056 057 058 UnivPowerSeries<BigRational> b; 059 060 061 UnivPowerSeries<BigRational> c; 062 063 064 UnivPowerSeries<BigRational> d; 065 066 067 UnivPowerSeries<BigRational> e; 068 069 070 int kl = 10; 071 072 073 float q = 0.5f; 074 075 076 @Override 077 protected void setUp() { 078 a = b = c = d = e = null; 079 fac = new UnivPowerSeriesRing<BigRational>(new BigRational(1)); 080 } 081 082 083 @Override 084 protected void tearDown() { 085 a = b = c = d = e = null; 086 fac = null; 087 } 088 089 090 /** 091 * Test constructor and toString. 092 * 093 */ 094 public void testConstruction() { 095 c = fac.getONE(); 096 assertTrue("isZERO( c )", !c.isZERO()); 097 assertTrue("isONE( c )", c.isONE()); 098 099 d = fac.getZERO(); 100 assertTrue("isZERO( d )", d.isZERO()); 101 assertTrue("isONE( d )", !d.isONE()); 102 } 103 104 105 /** 106 * Test random polynomial. 107 */ 108 public void testRandom() { 109 for (int i = 0; i < 5; i++) { 110 a = fac.random(i + 2); 111 //System.out.println("a = " + a); 112 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 113 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 114 } 115 } 116 117 118 /** 119 * Test addition. 120 * 121 */ 122 public void testAddition() { 123 a = fac.random(kl); 124 b = fac.random(kl); 125 126 c = a.sum(b); 127 d = b.sum(a); 128 assertEquals("a+b = b+a", c, d); 129 130 d = c.subtract(b); 131 assertEquals("a+b-b = a", a, d); 132 133 c = fac.random(kl); 134 d = a.sum(b.sum(c)); 135 e = a.sum(b).sum(c); 136 assertEquals("a+(b+c) = (a+b)+c", d, e); 137 } 138 139 140 /** 141 * Test multiplication. 142 * 143 */ 144 public void testMultiplication() { 145 a = fac.random(kl); 146 assertTrue("not isZERO( a )", !a.isZERO()); 147 148 b = fac.random(kl); 149 assertTrue("not isZERO( b )", !b.isZERO()); 150 151 c = b.multiply(a); 152 d = a.multiply(b); 153 assertTrue("not isZERO( c )", !c.isZERO()); 154 assertTrue("not isZERO( d )", !d.isZERO()); 155 156 //System.out.println("a = " + a); 157 //System.out.println("b = " + b); 158 e = d.subtract(c); 159 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 160 161 assertTrue("a*b = b*a", c.equals(d)); 162 assertEquals("a*b = b*a", c, d); 163 164 c = fac.random(kl); 165 //System.out.println("c = " + c); 166 d = a.multiply(b.multiply(c)); 167 e = (a.multiply(b)).multiply(c); 168 169 //System.out.println("d = " + d); 170 //System.out.println("e = " + e); 171 172 //System.out.println("d-e = " + d.subtract(c) ); 173 174 assertEquals("a(bc) = (ab)c", d, e); 175 assertTrue("a(bc) = (ab)c", d.equals(e)); 176 } 177 178 179 /** 180 * Test distributive law. 181 * 182 */ 183 public void testDistributive() { 184 a = fac.random(kl, q); 185 b = fac.random(kl, q); 186 c = fac.random(kl, q); 187 188 d = a.multiply(b.sum(c)); 189 e = a.multiply(b).sum(a.multiply(c)); 190 191 assertEquals("a(b+c) = ab+ac", d, e); 192 } 193 194 195 /** 196 * Test object quotient and remainder. 197 * 198 */ 199 public void testQuotRem() { 200 fac = new UnivPowerSeriesRing<BigRational>(new BigRational(1)); 201 202 a = fac.random(kl); 203 assertTrue("not isZERO( a )", !a.isZERO()); 204 205 b = fac.random(kl); 206 assertTrue("not isZERO( b )", !b.isZERO()); 207 208 UnivPowerSeries<BigRational> g = fac.random(kl); 209 assertTrue("not isZERO( g )", !g.isZERO()); 210 a = a.multiply(g); 211 b = b.multiply(g); 212 //System.out.println("a = " + a); 213 //System.out.println("b = " + b); 214 //System.out.println("g = " + g); 215 216 /* 217 UnivPowerSeries<BigRational>[] qr; 218 qr = b.divideAndRemainder(a); 219 c = qr[0]; 220 d = qr[1]; 221 //System.out.println("q = " + c); 222 //System.out.println("r = " + d); 223 e = c.multiply(a).sum(d); 224 assertEquals("b = q a + r", b, e ); 225 */ 226 227 // gcd tests ------------------------------- 228 c = a.gcd(b); 229 //System.out.println("gcd = " + c); 230 assertTrue("a mod gcd(a,b) = 0", a.remainder(c).isZERO()); 231 assertTrue("b mod gcd(a,b) = 0", b.remainder(c).isZERO()); 232 //assertEquals("g = gcd(a,b)", c, g ); 233 } 234 235 236 /** 237 * Test evaluation. 238 * 239 */ 240 public void testEvaluation() { 241 a = fac.random(kl, q); 242 b = fac.random(kl, q); 243 BigRational fv = new BigRational(0); 244 BigRational v = fv.random(kl); 245 246 BigRational av = a.evaluate(v); 247 BigRational bv = b.evaluate(v); 248 249 c = a.sum(b); 250 BigRational cv = c.evaluate(v); 251 BigRational dv = av.sum(bv); 252 assertEquals("a(v)+b(v) = (a+b)(v) ", cv, dv); 253 254 c = fac.getZERO(); 255 cv = c.evaluate(v); 256 dv = fv.getZERO(); 257 assertEquals("0(v) = 0 ", cv, dv); 258 259 c = fac.getONE(); 260 cv = c.evaluate(v); 261 dv = fv.getONE(); 262 assertEquals("1(v) = 1 ", cv, dv); 263 264 // not true: 265 //c = a.multiply(b); 266 //cv = c.evaluate(v); 267 //dv = av.multiply(bv); 268 //assertEquals("a(v)*b(v) = (a*b)(v) ", cv, dv); 269 } 270 271 272 /** 273 * Test Taylor series. 274 * 275 */ 276 public void testTaylor() { 277 BigRational br = new BigRational(0); 278 GenPolynomialRing<BigRational> pr = fac.polyRing(); 279 //System.out.println("pr = " + pr); 280 281 GenPolynomial<BigRational> p = pr.random(kl, 3, 3, q + q); 282 //System.out.println("p = " + p); 283 284 TaylorFunction<BigRational> F = new PolynomialTaylorFunction<BigRational>(p); 285 286 UnivPowerSeries<BigRational> ps = fac.seriesOfTaylor(F, br); 287 //System.out.println("ps = " + ps); 288 UnivPowerSeries<BigRational> pps = fac.fromPolynomial(p); 289 //System.out.println("pps = " + pps); 290 assertEquals("taylor(p) == p", ps, pps); 291 292 for (GenPolynomial<BigRational> g : pr.generators()) { 293 F = new PolynomialTaylorFunction<BigRational>(g); 294 ps = fac.seriesOfTaylor(F, br); 295 //System.out.println("g = " + g); 296 //System.out.println("ps = " + ps); 297 pps = fac.fromPolynomial(g); 298 //System.out.println("pps = " + pps); 299 assertEquals("taylor(p) == p", ps, pps); 300 } 301 } 302}