001/*
002 * $Id: IntGenPolynomialTest.java 4284 2012-11-03 18:32:04Z kredel $
003 */
004
005package edu.jas.poly;
006
007import junit.framework.Test;
008import junit.framework.TestCase;
009import junit.framework.TestSuite;
010
011import edu.jas.poly.GenPolynomial;
012import edu.jas.arith.BigInteger;
013//import edu.jas.structure.RingElem;
014
015
016/**
017 * BigInteger coefficients GenPolynomial tests with JUnit.
018 * @author Heinz Kredel.
019 */
020
021public class IntGenPolynomialTest extends TestCase {
022
023/**
024 * main.
025 */
026   public static void main (String[] args) {
027          junit.textui.TestRunner.run( suite() );
028   }
029
030/**
031 * Constructs a <CODE>IntGenPolynomialTest</CODE> object.
032 * @param name String.
033 */
034   public IntGenPolynomialTest(String name) {
035          super(name);
036   }
037
038/**
039 */ 
040 public static Test suite() {
041     TestSuite suite= new TestSuite(IntGenPolynomialTest.class);
042     return suite;
043   }
044
045   //private final static int bitlen = 100;
046
047   GenPolynomialRing<BigInteger> fac;
048
049   GenPolynomial<BigInteger> a;
050   GenPolynomial<BigInteger> b;
051   GenPolynomial<BigInteger> c;
052   GenPolynomial<BigInteger> d;
053   GenPolynomial<BigInteger> e;
054
055   int rl = 7; 
056   int kl = 10;
057   int ll = 10;
058   int el = 5;
059   float q = 0.3f;
060
061   protected void setUp() {
062       a = b = c = d = e = null;
063       fac = new GenPolynomialRing<BigInteger>(new BigInteger(1),rl);
064   }
065
066   protected void tearDown() {
067       a = b = c = d = e = null;
068       fac = null;
069   }
070
071
072/**
073 * Test constructor and toString.
074 */
075 public void testConstruction() {
076     c = fac.getONE();
077     assertTrue("length( c ) = 1", c.length() == 1);
078     assertTrue("isZERO( c )", !c.isZERO() );
079     assertTrue("isONE( c )", c.isONE() );
080
081     d = fac.getZERO();
082     assertTrue("length( d ) = 0", d.length() == 0);
083     assertTrue("isZERO( d )", d.isZERO() );
084     assertTrue("isONE( d )", !d.isONE() );
085 }
086
087
088/**
089 * Test random polynomial.
090 */
091 public void testRandom() {
092     for (int i = 0; i < 7; i++) {
093         a = fac.random(kl*(i+2),ll+2*i,el+i,q);
094         assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
095         assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
096         assertTrue(" not isONE( a"+i+" )", !a.isONE() );
097     }
098 }
099
100
101/**
102 * Test addition.
103 */
104 public void testAddition() {
105     a = fac.random(kl,ll,el,q);
106     b = fac.random(kl,ll,el,q);
107
108     c = a.sum(b);
109     d = c.subtract(b);
110     assertEquals("a+b-b = a",a,d);
111
112     c = fac.random(kl,ll,el,q);
113
114     ExpVector u = ExpVector.EVRAND(rl,el,q);
115     BigInteger x = BigInteger.IRAND(kl);
116
117     b = new GenPolynomial<BigInteger>(fac,x, u);
118     c = a.sum(b);
119     d = a.sum(x,u);
120     assertEquals("a+p(x,u) = a+(x,u)",c,d);
121
122     c = a.subtract(b);
123     d = a.subtract(x,u);
124     assertEquals("a-p(x,u) = a-(x,u)",c,d);
125
126     a = new GenPolynomial<BigInteger>(fac);
127     b = new GenPolynomial<BigInteger>(fac,x, u);
128     c = b.sum(a);
129     d = a.sum(x,u);
130     assertEquals("a+p(x,u) = a+(x,u)",c,d);
131
132     c = a.subtract(b);
133     d = a.subtract(x,u);
134     assertEquals("a-p(x,u) = a-(x,u)",c,d);
135 }
136
137
138/**
139 * Test object multiplication.
140 */
141 public void testMultiplication() {
142     a = fac.random(kl,ll,el,q);
143     assertTrue("not isZERO( a )", !a.isZERO() );
144
145     b = fac.random(kl,ll,el,q);
146     assertTrue("not isZERO( b )", !b.isZERO() );
147
148     c = b.multiply(a);
149     d = a.multiply(b);
150     assertTrue("not isZERO( c )", !c.isZERO() );
151     assertTrue("not isZERO( d )", !d.isZERO() );
152
153     //System.out.println("a = " + a);
154     //System.out.println("b = " + b);
155     e = d.subtract(c);
156     assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
157
158     assertTrue("a*b = b*a", c.equals(d) );
159     assertEquals("a*b = b*a",c,d);
160
161     c = fac.random(kl,ll,el,q);
162     //System.out.println("c = " + c);
163     d = a.multiply( b.multiply(c) );
164     e = (a.multiply(b)).multiply(c);
165
166     //System.out.println("d = " + d);
167     //System.out.println("e = " + e);
168
169     //System.out.println("d-e = " + d.subtract(c) );
170
171     assertEquals("a(bc) = (ab)c",d,e);
172     assertTrue("a(bc) = (ab)c", d.equals(e) );
173
174     //BigInteger x = a.leadingBaseCoefficient().inverse();
175     //c = a.monic();
176     //d = a.multiply(x);
177     //assertEquals("a.monic() = a(1/ldcf(a))",c,d);
178
179     BigInteger y = b.leadingBaseCoefficient();
180     //c = b.monic();
181     //d = b.multiply(y);
182     //assertEquals("b.monic() = b(1/ldcf(b))",c,d);
183
184     e = new GenPolynomial<BigInteger>(fac,y);
185     c = b.multiply(e);
186     // assertEquals("b.monic() = b(1/ldcf(b))",c,d);
187
188     d = e.multiply(b);
189     assertEquals("b*p(y,u) = p(y,u)*b",c,d);
190 }
191
192
193/**
194 * Test BLAS level 1.
195 */
196 public void testBLAS1() {
197     a = fac.random(kl,ll,el,q);
198     b = fac.random(kl,ll,el,q);
199     ExpVector ev = ExpVector.EVRAND(rl,el,q);
200     BigInteger lc = BigInteger.IRAND(kl);
201
202     d = a.subtractMultiple(lc,b);
203     e = a.subtract( b.multiply(lc) );
204     assertEquals("a - (lc) b == a - ((lc) b)",d,e);
205
206     d = a.subtractMultiple(lc,ev,b);
207     e = a.subtract( b.multiply(lc,ev) );
208     assertEquals("a - (lc ev) b == a - ((lc ev) b)",d,e);
209
210     ExpVector fv = ExpVector.EVRAND(rl,el,q);
211     BigInteger tc = BigInteger.IRAND(kl);
212
213     d = a.scaleSubtractMultiple(tc,lc,ev,b);
214     e = a.multiply(tc).subtract( b.multiply(lc,ev) );
215     assertEquals("(tc) a - (lc ev) b == ((tc) a - ((lc ev) b))",d,e);
216
217     d = a.scaleSubtractMultiple(tc,fv,lc,ev,b);
218     e = a.multiply(tc,fv).subtract( b.multiply(lc,ev) );
219     assertEquals("(tc fv) a - (lc ev) b == ((tc fv) a - ((lc ev) b))",d,e);
220 }
221
222
223/**
224 * Test distributive law.
225 */
226 public void testDistributive() {
227     a = fac.random(kl,ll,el,q);
228     b = fac.random(kl,ll,el,q);
229     c = fac.random(kl,ll,el,q);
230
231     d = a.multiply( b.sum(c) );
232     e = a.multiply( b ).sum( a.multiply(c) );
233
234     assertEquals("a(b+c) = ab+ac",d,e);
235 }
236
237}