001 002/* 003 * $Id: QuotientRatTest.java 3789 2011-10-01 18:54:43Z kredel $ 004 */ 005 006package edu.jas.ufd; 007 008 009import junit.framework.Test; 010import junit.framework.TestCase; 011import junit.framework.TestSuite; 012 013import org.apache.log4j.BasicConfigurator; 014//import org.apache.log4j.Logger; 015 016import edu.jas.arith.BigRational; 017 018import edu.jas.poly.GenPolynomialRing; 019import edu.jas.poly.TermOrder; 020 021import edu.jas.kern.PrettyPrint; 022import edu.jas.kern.ComputerThreads; 023 024 025/** 026 * Quotient over BigRational GenPolynomial tests with JUnit. 027 * @author Heinz Kredel. 028 */ 029 030public class QuotientRatTest extends TestCase { 031 032/** 033 * main. 034 */ 035 public static void main (String[] args) { 036 BasicConfigurator.configure(); 037 junit.textui.TestRunner.run( suite() ); 038 } 039 040/** 041 * Constructs a <CODE>QuotientRatTest</CODE> object. 042 * @param name String. 043 */ 044 public QuotientRatTest(String name) { 045 super(name); 046 } 047 048/** 049 * suite. 050 */ 051 public static Test suite() { 052 TestSuite suite= new TestSuite(QuotientRatTest.class); 053 return suite; 054 } 055 056 //private final static int bitlen = 100; 057 058 QuotientRing<BigRational> zFac; 059 QuotientRing<BigRational> efac; 060 GenPolynomialRing<BigRational> mfac; 061 062 Quotient< BigRational > a; 063 Quotient< BigRational > b; 064 Quotient< BigRational > c; 065 Quotient< BigRational > d; 066 Quotient< BigRational > e; 067 Quotient< BigRational > az; 068 Quotient< BigRational > bz; 069 Quotient< BigRational > cz; 070 Quotient< BigRational > dz; 071 Quotient< BigRational > ez; 072 073 int rl = 3; 074 int kl = 5; 075 int ll = 3; //6; 076 int el = 2; 077 float q = 0.4f; 078 079 protected void setUp() { 080 a = b = c = d = e = null; 081 TermOrder to = new TermOrder( TermOrder.INVLEX ); 082 mfac = new GenPolynomialRing<BigRational>( new BigRational(1), rl, to ); 083 efac = new QuotientRing<BigRational>( mfac ); 084 zFac = new QuotientRing<BigRational>( mfac, false ); 085 } 086 087 protected void tearDown() { 088 a = b = c = d = e = null; 089 //efac.terminate(); 090 efac = null; 091 zFac = null; 092 ComputerThreads.terminate(); 093 } 094 095 096/** 097 * Test constructor and toString. 098 * 099 */ 100 public void testConstruction() { 101 c = efac.getONE(); 102 //System.out.println("c = " + c); 103 //System.out.println("c.val = " + c.val); 104 assertTrue("length( c ) = 1", c.num.length() == 1); 105 assertTrue("isZERO( c )", !c.isZERO() ); 106 assertTrue("isONE( c )", c.isONE() ); 107 108 d = efac.getZERO(); 109 //System.out.println("d = " + d); 110 //System.out.println("d.val = " + d.val); 111 assertTrue("length( d ) = 0", d.num.length() == 0); 112 assertTrue("isZERO( d )", d.isZERO() ); 113 assertTrue("isONE( d )", !d.isONE() ); 114 } 115 116 117/** 118 * Test random polynomial. 119 * 120 */ 121 public void testRandom() { 122 for (int i = 0; i < 7; i++) { 123 //a = efac.random(ll+i); 124 a = efac.random(kl*(i+1), ll+2+2*i, el, q ); 125 //System.out.println("a = " + a); 126 if ( a.isZERO() || a.isONE() ) { 127 continue; 128 } 129 assertTrue("length( a"+i+" ) <> 0", a.num.length() >= 0); 130 assertTrue(" not isZERO( a"+i+" )", !a.isZERO() ); 131 assertTrue(" not isONE( a"+i+" )", !a.isONE() ); 132 } 133 } 134 135 136/** 137 * Test addition. 138 * 139 */ 140 public void testAddition() { 141 142 a = efac.random(kl,ll,el,q); 143 b = efac.random(kl,ll,el,q); 144 //System.out.println("a = " + a); 145 //System.out.println("b = " + b); 146 147 c = a.sum(b); 148 d = c.subtract(b); 149 //System.out.println("c = " + c); 150 //System.out.println("d = " + d); 151 d = d.monic(); 152 //System.out.println("d = " + d); 153 assertEquals("a+b-b = a",a,d); 154 155 c = a.sum(b); 156 d = b.sum(a); 157 //System.out.println("c = " + c); 158 //System.out.println("d = " + d); 159 assertEquals("a+b = b+a",c,d); 160 161 //System.out.println("monic(d) = " + d.monic()); 162 163 c = efac.random(kl,ll,el,q); 164 //System.out.println("c = " + c); 165 d = c.sum( a.sum(b) ); 166 e = c.sum( a ).sum(b); 167 //System.out.println("d = " + d); 168 //System.out.println("e = " + e); 169 assertEquals("c+(a+b) = (c+a)+b",d,e); 170 171 172 c = a.sum( efac.getZERO() ); 173 d = a.subtract( efac.getZERO() ); 174 assertEquals("a+0 = a-0",c,d); 175 176 c = efac.getZERO().sum( a ); 177 d = efac.getZERO().subtract( a.negate() ); 178 assertEquals("0+a = 0+(-a)",c,d); 179 } 180 181 182/** 183 * Test object multiplication. 184 * 185 */ 186 public void testMultiplication() { 187 188 a = efac.random(kl,ll,el,q); 189 //assertTrue("not isZERO( a )", !a.isZERO() ); 190 191 b = efac.random(kl,ll,el,q); 192 //assertTrue("not isZERO( b )", !b.isZERO() ); 193 194 c = b.multiply(a); 195 d = a.multiply(b); 196 //assertTrue("not isZERO( c )", !c.isZERO() ); 197 //assertTrue("not isZERO( d )", !d.isZERO() ); 198 199 //System.out.println("a = " + a); 200 //System.out.println("b = " + b); 201 e = d.subtract(c); 202 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() ); 203 204 assertTrue("a*b = b*a", c.equals(d) ); 205 assertEquals("a*b = b*a",c,d); 206 207 c = efac.random(kl,ll,el,q); 208 //System.out.println("c = " + c); 209 d = a.multiply( b.multiply(c) ); 210 e = (a.multiply(b)).multiply(c); 211 212 //System.out.println("d = " + d); 213 //System.out.println("e = " + e); 214 215 //System.out.println("d-e = " + d.subtract(c) ); 216 217 assertEquals("a(bc) = (ab)c",d,e); 218 assertTrue("a(bc) = (ab)c", d.equals(e) ); 219 220 c = a.multiply( efac.getONE() ); 221 d = efac.getONE().multiply( a ); 222 assertEquals("a*1 = 1*a",c,d); 223 224 if ( a.isUnit() ) { 225 c = a.inverse(); 226 d = c.multiply(a); 227 //System.out.println("a = " + a); 228 //System.out.println("c = " + c); 229 //System.out.println("d = " + d); 230 assertTrue("a*1/a = 1",d.isONE()); 231 } 232 } 233 234 235/** 236 * Test addition with syzygy gcd and euclids gcd. 237 * 238 */ 239 public void xtestAdditionGcd() { 240 241 long te, tz; 242 243 a = efac.random(kl,ll,el,q); 244 b = efac.random(kl,ll,el,q); 245 //System.out.println("a = " + a); 246 //System.out.println("b = " + b); 247 248 az = new Quotient<BigRational>(zFac,a.num,a.den,true); 249 bz = new Quotient<BigRational>(zFac,b.num,b.den,true); 250 251 te = System.currentTimeMillis(); 252 c = a.sum(b); 253 d = c.subtract(b); 254 d = d.monic(); 255 te = System.currentTimeMillis() - te; 256 assertEquals("a+b-b = a",a,d); 257 258 tz = System.currentTimeMillis(); 259 cz = az.sum(bz); 260 dz = cz.subtract(bz); 261 dz = dz.monic(); 262 tz = System.currentTimeMillis() - tz; 263 assertEquals("a+b-b = a",az,dz); 264 265 System.out.println("te = " + te); 266 System.out.println("tz = " + tz); 267 268 c = a.sum(b); 269 d = b.sum(a); 270 //System.out.println("c = " + c); 271 //System.out.println("d = " + d); 272 assertEquals("a+b = b+a",c,d); 273 274 c = efac.random(kl,ll,el,q); 275 cz = new Quotient<BigRational>(zFac,c.num,c.den,true); 276 277 278 te = System.currentTimeMillis(); 279 d = c.sum( a.sum(b) ); 280 e = c.sum( a ).sum(b); 281 te = System.currentTimeMillis() - te; 282 assertEquals("c+(a+b) = (c+a)+b",d,e); 283 284 tz = System.currentTimeMillis(); 285 dz = cz.sum( az.sum(bz) ); 286 ez = cz.sum( az ).sum(bz); 287 tz = System.currentTimeMillis() - tz; 288 assertEquals("c+(a+b) = (c+a)+b",dz,ez); 289 290 System.out.println("te = " + te); 291 System.out.println("tz = " + tz); 292 293 c = a.sum( efac.getZERO() ); 294 d = a.subtract( efac.getZERO() ); 295 assertEquals("a+0 = a-0",c,d); 296 297 c = efac.getZERO().sum( a ); 298 d = efac.getZERO().subtract( a.negate() ); 299 assertEquals("0+a = 0+(-a)",c,d); 300 } 301 302 303/** 304 * Test parse(). 305 * 306 */ 307 public void testParse() { 308 a = efac.random(kl*2,ll*2,el*2,q*2); 309 //assertTrue("not isZERO( a )", !a.isZERO() ); 310 311 //PrettyPrint.setInternal(); 312 //System.out.println("a = " + a); 313 PrettyPrint.setPretty(); 314 //System.out.println("a = " + a); 315 String p = a.toString(); 316 //System.out.println("p = " + p); 317 b = efac.parse(p); 318 //System.out.println("b = " + b); 319 320 //c = a.subtract(b); 321 //System.out.println("c = " + c); 322 assertEquals("parse(a.toSting()) = a",a,b); 323 } 324 325}