001    /*
002     * $Id: RatPolyGenPolynomialTest.java 1888 2008-07-12 13:37:34Z kredel $
003     */
004    
005    package edu.jas.poly;
006    
007    import junit.framework.Test;
008    import junit.framework.TestCase;
009    import junit.framework.TestSuite;
010    import edu.jas.arith.BigRational;
011    
012    
013    /**
014     * BigRational coefficients GenPolynomial coefficients GenPolynomial tests with JUnit.
015     * @author Heinz Kredel.
016     */
017    
018    public class RatPolyGenPolynomialTest extends TestCase {
019    
020    /**
021     * main
022     */
023       public static void main (String[] args) {
024              junit.textui.TestRunner.run( suite() );
025       }
026    
027    /**
028     * Constructs a <CODE>RatPolyGenPolynomialTest</CODE> object.
029     * @param name String.
030     */
031       public RatPolyGenPolynomialTest(String name) {
032              super(name);
033       }
034    
035    /**
036     */ 
037     public static Test suite() {
038         TestSuite suite= new TestSuite(RatPolyGenPolynomialTest.class);
039         return suite;
040       }
041    
042       //private final static int bitlen = 100;
043    
044       GenPolynomialRing<BigRational> cf;
045       GenPolynomialRing<GenPolynomial<BigRational>> fac;
046    
047       GenPolynomial<GenPolynomial<BigRational>> a;
048       GenPolynomial<GenPolynomial<BigRational>> b;
049       GenPolynomial<GenPolynomial<BigRational>> c;
050       GenPolynomial<GenPolynomial<BigRational>> d;
051       GenPolynomial<GenPolynomial<BigRational>> e;
052    
053       int rl = 7; 
054       int kl = 10;
055       int ll = 10;
056       int el = 5;
057       float q = 0.5f;
058    
059       protected void setUp() {
060           a = b = c = d = e = null;
061           cf  = new GenPolynomialRing<BigRational>( new BigRational(1), 1 );
062           fac = new GenPolynomialRing<GenPolynomial<BigRational>>(cf,rl);
063       }
064    
065       protected void tearDown() {
066           a = b = c = d = e = null;
067           fac = null;
068       }
069    
070    
071    /**
072     * Test constructor and toString.
073     * 
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     */
092     public void testRandom() {
093         for (int i = 0; i < 7; i++) {
094             a = fac.random(ll);
095                 // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q );
096             assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
097             assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
098             assertTrue(" not isONE( a"+i+" )", !a.isONE() );
099         }
100     }
101    
102    
103    /**
104     * Test addition.
105     * 
106     */
107     public void testAddition() {
108    
109         a = fac.random(ll);
110         b = fac.random(ll);
111    
112         c = a.sum(b);
113         d = c.subtract(b);
114         assertEquals("a+b-b = a",a,d);
115    
116         c = fac.random(ll);
117    
118         ExpVector u = ExpVector.EVRAND(rl,el,q);
119         GenPolynomial<BigRational> x = cf.random(kl);
120    
121         b = new GenPolynomial<GenPolynomial<BigRational>>(fac,x, u);
122         c = a.sum(b);
123         d = a.sum(x,u);
124         assertEquals("a+p(x,u) = a+(x,u)",c,d);
125    
126         c = a.subtract(b);
127         d = a.subtract(x,u);
128         assertEquals("a-p(x,u) = a-(x,u)",c,d);
129    
130         a = new GenPolynomial<GenPolynomial<BigRational>>(fac);
131         b = new GenPolynomial<GenPolynomial<BigRational>>(fac,x, u);
132         c = b.sum(a);
133         d = a.sum(x,u);
134         assertEquals("a+p(x,u) = a+(x,u)",c,d);
135    
136         c = a.subtract(b);
137         d = a.subtract(x,u);
138         assertEquals("a-p(x,u) = a-(x,u)",c,d);
139     }
140    
141    
142    /**
143     * Test object multiplication.
144     * 
145     */
146    
147     public void testMultiplication() {
148    
149         a = fac.random(ll);
150         assertTrue("not isZERO( a )", !a.isZERO() );
151    
152         b = fac.random(ll);
153         assertTrue("not isZERO( b )", !b.isZERO() );
154    
155         c = b.multiply(a);
156         d = a.multiply(b);
157         assertTrue("not isZERO( c )", !c.isZERO() );
158         assertTrue("not isZERO( d )", !d.isZERO() );
159    
160         //System.out.println("a = " + a);
161         //System.out.println("b = " + b);
162         e = d.subtract(c);
163         assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
164    
165         assertTrue("a*b = b*a", c.equals(d) );
166         assertEquals("a*b = b*a",c,d);
167    
168         c = fac.random(ll);
169         //System.out.println("c = " + c);
170         d = a.multiply( b.multiply(c) );
171         e = (a.multiply(b)).multiply(c);
172    
173         //System.out.println("d = " + d);
174         //System.out.println("e = " + e);
175    
176         //System.out.println("d-e = " + d.subtract(c) );
177    
178         assertEquals("a(bc) = (ab)c",d,e);
179         assertTrue("a(bc) = (ab)c", d.equals(e) );
180    
181         //GenPolynomial<BigRational> x = a.leadingBaseCoefficient().inverse();
182         //c = a.monic();
183         //d = a.multiply(x);
184         //assertEquals("a.monic() = a(1/ldcf(a))",c,d);
185    
186         GenPolynomial<BigRational> y = b.leadingBaseCoefficient();
187         //c = b.monic();
188         //d = b.multiply(y);
189         //assertEquals("b.monic() = b(1/ldcf(b))",c,d);
190    
191         e = new GenPolynomial<GenPolynomial<BigRational>>(fac,y);
192         c = b.multiply(e);
193         // assertEquals("b.monic() = b(1/ldcf(b))",c,d);
194    
195         d = e.multiply(b);
196         assertEquals("b*p(y,u) = p(y,u)*b",c,d);
197     }
198    
199    }