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