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