001/*
002 * $Id$
003 */
004
005package edu.jas.poly;
006
007import junit.framework.Test;
008import junit.framework.TestCase;
009import junit.framework.TestSuite;
010import edu.jas.arith.BigRational;
011
012
013/**
014 * BigRational coefficients GenPolynomial coefficients GenPolynomial tests with JUnit.
015 * @author Heinz Kredel
016 */
017
018public 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    GenPolynomialRing<BigRational> cf;
043    GenPolynomialRing<GenPolynomial<BigRational>> fac;
044
045    GenPolynomial<GenPolynomial<BigRational>> a, b, c, d, e;
046
047    int rl = 7; 
048    int kl = 10;
049    int ll = 10;
050    int el = 5;
051    float q = 0.5f;
052
053    protected void setUp() {
054        a = b = c = d = e = null;
055        cf  = new GenPolynomialRing<BigRational>( new BigRational(1), 1 );
056        fac = new GenPolynomialRing<GenPolynomial<BigRational>>(cf,rl);
057    }
058
059    
060    protected void tearDown() {
061        a = b = c = d = e = null;
062        fac = null;
063    }
064
065
066    /**
067     * Test constructor and toString.
068     */
069    public void testConstruction() {
070        c = fac.getONE();
071        assertTrue("length( c ) = 1", c.length() == 1);
072        assertTrue("isZERO( c )", !c.isZERO() );
073        assertTrue("isONE( c )", c.isONE() );
074
075        d = fac.getZERO();
076        assertTrue("length( d ) = 0", d.length() == 0);
077        assertTrue("isZERO( d )", d.isZERO() );
078        assertTrue("isONE( d )", !d.isONE() );
079    }
080
081
082    /**
083     * Test random polynomial.
084     */
085    public void testRandom() {
086        for (int i = 0; i < 7; i++) {
087            a = fac.random(ll);
088            //fac.random(kl*(i+1), ll+2*i, el+i, q );
089            assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
090            assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
091            assertTrue(" not isONE( a"+i+" )", !a.isONE() );
092        }
093    }
094
095
096    /**
097     * Test addition.
098     */
099    public void testAddition() {
100        a = fac.random(ll);
101        b = fac.random(ll);
102
103        c = a.sum(b);
104        d = c.subtract(b);
105        assertEquals("a+b-b = a",a,d);
106
107        c = fac.random(ll);
108
109        ExpVector u = ExpVector.random(rl,el,q);
110        GenPolynomial<BigRational> x = cf.random(kl);
111
112        b = new GenPolynomial<GenPolynomial<BigRational>>(fac,x, u);
113        c = a.sum(b);
114        d = a.sum(x,u);
115        assertEquals("a+p(x,u) = a+(x,u)",c,d);
116
117        c = a.subtract(b);
118        d = a.subtract(x,u);
119        assertEquals("a-p(x,u) = a-(x,u)",c,d);
120
121        a = new GenPolynomial<GenPolynomial<BigRational>>(fac);
122        b = new GenPolynomial<GenPolynomial<BigRational>>(fac,x, u);
123        c = b.sum(a);
124        d = a.sum(x,u);
125        assertEquals("a+p(x,u) = a+(x,u)",c,d);
126
127        c = a.subtract(b);
128        d = a.subtract(x,u);
129        assertEquals("a-p(x,u) = a-(x,u)",c,d);
130    }
131
132
133    /**
134     * Test object multiplication.
135     */
136    public void testMultiplication() {
137        a = fac.random(ll);
138        assertTrue("not isZERO( a )", !a.isZERO() );
139
140        b = fac.random(ll);
141        assertTrue("not isZERO( b )", !b.isZERO() );
142
143        c = b.multiply(a);
144        d = a.multiply(b);
145        assertTrue("not isZERO( c )", !c.isZERO() );
146        assertTrue("not isZERO( d )", !d.isZERO() );
147
148        //System.out.println("a = " + a);
149        //System.out.println("b = " + b);
150        e = d.subtract(c);
151        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
152
153        assertTrue("a*b = b*a", c.equals(d) );
154        assertEquals("a*b = b*a",c,d);
155
156        c = fac.random(ll);
157        //System.out.println("c = " + c);
158        d = a.multiply( b.multiply(c) );
159        e = (a.multiply(b)).multiply(c);
160
161        //System.out.println("d = " + d);
162        //System.out.println("e = " + e);
163
164        //System.out.println("d-e = " + d.subtract(c) );
165
166        assertEquals("a(bc) = (ab)c",d,e);
167        assertTrue("a(bc) = (ab)c", d.equals(e) );
168
169        //GenPolynomial<BigRational> x = a.leadingBaseCoefficient().inverse();
170        //c = a.monic();
171        //d = a.multiply(x);
172        //assertEquals("a.monic() = a(1/ldcf(a))",c,d);
173
174        GenPolynomial<BigRational> y = b.leadingBaseCoefficient();
175        //c = b.monic();
176        //d = b.multiply(y);
177        //assertEquals("b.monic() = b(1/ldcf(b))",c,d);
178
179        e = new GenPolynomial<GenPolynomial<BigRational>>(fac,y);
180        c = b.multiply(e);
181        // assertEquals("b.monic() = b(1/ldcf(b))",c,d);
182
183        d = e.multiply(b);
184        assertEquals("b*p(y,u) = p(y,u)*b",c,d);
185    }
186
187}