001 002/* 003 * $Id$ 004 */ 005 006package edu.jas.ufd; 007 008 009import edu.jas.arith.BigInteger; 010import edu.jas.kern.ComputerThreads; 011import edu.jas.kern.PrettyPrint; 012import edu.jas.poly.GenPolynomial; 013import edu.jas.poly.GenPolynomialRing; 014import edu.jas.poly.TermOrder; 015 016import junit.framework.Test; 017import junit.framework.TestCase; 018import junit.framework.TestSuite; 019 020 021/** 022 * Quotient BigInteger coefficient GenPolynomial tests with JUnit. 023 * @author Heinz Kredel 024 */ 025 026public class QuotIntPolynomialTest extends TestCase { 027 028 029 /** 030 * main. 031 */ 032 public static void main(String[] args) { 033 junit.textui.TestRunner.run(suite()); 034 ComputerThreads.terminate(); 035 } 036 037 038 /** 039 * Constructs a <CODE>QoutIntPolynomialTest</CODE> object. 040 * @param name String. 041 */ 042 public QuotIntPolynomialTest(String name) { 043 super(name); 044 } 045 046 047 /** 048 * suite. 049 */ 050 public static Test suite() { 051 TestSuite suite = new TestSuite(QuotIntPolynomialTest.class); 052 return suite; 053 } 054 055 056 QuotientRing<BigInteger> eFac; 057 058 059 GenPolynomialRing<BigInteger> mfac; 060 061 062 GenPolynomialRing<Quotient<BigInteger>> qfac; 063 064 065 GenPolynomial<Quotient<BigInteger>> a, b, c, d, e; 066 067 068 int rl = 3; 069 070 071 int kl = 2; // degree of coefficient polynomials!!! 072 073 074 int ll = 4; //6; 075 076 077 int el = 3; 078 079 080 float q = 0.3f; 081 082 083 @Override 084 protected void setUp() { 085 a = b = c = d = e = null; 086 TermOrder to = new TermOrder(TermOrder.INVLEX); 087 String[] cv = new String[] { "a", "b", "c" }; 088 BigInteger cfac = new BigInteger(1); 089 mfac = new GenPolynomialRing<BigInteger>(cfac, rl, to, cv); 090 eFac = new QuotientRing<BigInteger>(mfac); 091 String[] v = new String[] { "w", "x", "y", "z" }; 092 qfac = new GenPolynomialRing<Quotient<BigInteger>>(eFac, rl + 1, v); 093 } 094 095 096 @Override 097 protected void tearDown() { 098 a = b = c = d = e = null; 099 //eFac.terminate(); 100 eFac = null; 101 mfac = null; 102 qfac = null; 103 ComputerThreads.terminate(); 104 } 105 106 107 /** 108 * Test constructor and toString. 109 */ 110 public void testConstruction() { 111 c = qfac.getONE(); 112 //System.out.println("c = " + c); 113 //System.out.println("c.val = " + c.val); 114 assertTrue("length( c ) = 1", c.length() == 1); 115 assertTrue("isZERO( c )", !c.isZERO()); 116 assertTrue("isONE( c )", c.isONE()); 117 118 d = qfac.getZERO(); 119 //System.out.println("d = " + d); 120 //System.out.println("d.val = " + d.val); 121 assertTrue("length( d ) = 0", d.length() == 0); 122 assertTrue("isZERO( d )", d.isZERO()); 123 assertTrue("isONE( d )", !d.isONE()); 124 } 125 126 127 /** 128 * Test random polynomial. 129 */ 130 public void testRandom() { 131 for (int i = 0; i < 3; i++) { 132 //a = qfac.random(ll+i); 133 a = qfac.random(kl, ll + i, el, q); 134 //System.out.println("a["+i+"] = " + a); 135 if (a.isZERO() || a.isONE()) { 136 continue; 137 } 138 assertTrue("length( a" + i + " ) <> 0", a.length() >= 0); 139 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 140 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 141 142 b = a.monic(); 143 Quotient<BigInteger> ldbcf = b.leadingBaseCoefficient(); 144 //System.out.println("b["+i+"] = " + b); 145 assertTrue("ldbcf( b" + i + " ) == 1 " + b + ", a = " + a, ldbcf.isONE()); 146 } 147 } 148 149 150 /** 151 * Test addition. 152 */ 153 public void testAddition() { 154 a = qfac.random(kl, ll, el, q); 155 b = qfac.random(kl, ll, el, q); 156 //System.out.println("a = " + a); 157 //System.out.println("b = " + b); 158 //System.out.println("a = " + a.toString( qfac.getVars() )); 159 //System.out.println("b = " + b.toString( qfac.getVars() )); 160 161 c = a.sum(b); 162 d = c.subtract(b); 163 assertEquals("a+b-b = a", a, d); 164 165 c = a.sum(b); 166 d = b.sum(a); 167 //System.out.println("c = " + c.toString( qfac.getVars() )); 168 //System.out.println("c = " + c); 169 //System.out.println("d = " + d); 170 171 assertEquals("a+b = b+a", c, d); 172 173 c = qfac.random(kl, ll, el, q); 174 d = c.sum(a.sum(b)); 175 e = c.sum(a).sum(b); 176 assertEquals("c+(a+b) = (c+a)+b", d, e); 177 178 c = a.sum(qfac.getZERO()); 179 d = a.subtract(qfac.getZERO()); 180 assertEquals("a+0 = a-0", c, d); 181 182 c = qfac.getZERO().sum(a); 183 d = qfac.getZERO().subtract(a.negate()); 184 assertEquals("0+a = 0+(-a)", c, d); 185 } 186 187 188 /** 189 * Test object multiplication. 190 */ 191 public void testMultiplication() { 192 a = qfac.random(kl, ll, el, q); 193 //assertTrue("not isZERO( a )", !a.isZERO() ); 194 195 b = qfac.random(kl, ll, el, q); 196 //assertTrue("not isZERO( b )", !b.isZERO() ); 197 198 c = b.multiply(a); 199 d = a.multiply(b); 200 if (!a.isZERO() && !b.isZERO()) { 201 assertTrue("not isZERO( c )", !c.isZERO()); 202 assertTrue("not isZERO( d )", !d.isZERO()); 203 } 204 205 //System.out.println("a = " + a); 206 //System.out.println("b = " + b); 207 e = d.subtract(c); 208 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 209 210 assertTrue("a*b = b*a", c.equals(d)); 211 assertEquals("a*b = b*a", c, d); 212 213 c = qfac.random(kl, ll, el, q); 214 //System.out.println("c = " + c); 215 d = a.multiply(b.multiply(c)); 216 e = (a.multiply(b)).multiply(c); 217 218 //System.out.println("d = " + d); 219 //System.out.println("e = " + e); 220 221 //System.out.println("d-e = " + d.subtract(c) ); 222 223 assertEquals("a(bc) = (ab)c", d, e); 224 assertTrue("a(bc) = (ab)c", d.equals(e)); 225 226 c = a.multiply(qfac.getONE()); 227 d = qfac.getONE().multiply(a); 228 assertEquals("a*1 = 1*a", c, d); 229 230 if (a.isUnit()) { 231 c = a.inverse(); 232 d = c.multiply(a); 233 //System.out.println("a = " + a); 234 //System.out.println("c = " + c); 235 //System.out.println("d = " + d); 236 assertTrue("a*1/a = 1", d.isONE()); 237 } 238 } 239 240 241 /** 242 * Test parse(). 243 */ 244 public void testParse() { 245 a = qfac.random(kl, ll * 2, el * 2, q * 2); 246 //assertTrue("not isZERO( a )", !a.isZERO() ); 247 248 //PrettyPrint.setInternal(); 249 //System.out.println("a = " + a); 250 PrettyPrint.setPretty(); 251 //System.out.println("a = " + a); 252 String p = a.toString(qfac.getVars()); 253 //System.out.println("p = " + p); 254 b = qfac.parse(p); 255 //System.out.println("b = " + b.toString( qfac.getVars() ) ); 256 257 //c = a.subtract(b); 258 //System.out.println("c = " + c); 259 assertEquals("parse(a.toSting()) = a", a, b); 260 } 261 262}