001/*
002 * $Id: ModGenPolynomialTest.java 5931 2018-09-23 17:21:33Z 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.ModInteger;
013import edu.jas.arith.ModIntegerRing;
014//import edu.jas.structure.RingElem;
015
016
017/**
018 * ModInteger coefficients GenPolynomial tests with JUnit.
019 * @author Heinz Kredel
020 */
021
022public class ModGenPolynomialTest extends TestCase {
023
024    /**
025     * main.
026     */
027    public static void main (String[] args) {
028        junit.textui.TestRunner.run( suite() );
029    }
030
031    /**
032     * Constructs a <CODE>ModGenPolynomialTest</CODE> object.
033     * @param name String.
034     */
035    public ModGenPolynomialTest(String name) {
036        super(name);
037    }
038
039    /**
040     * suite.
041     */ 
042    public static Test suite() {
043        TestSuite suite= new TestSuite(ModGenPolynomialTest.class);
044        return suite;
045    }
046
047    GenPolynomialRing<ModInteger> fac;
048
049    GenPolynomial<ModInteger> a, b, c, d, e;
050
051    int ml = 19; // modul 
052    int rl = 7; 
053    int kl = 10;
054    int ll = 10;
055    int el = 5;
056    float q = 0.5f;
057
058    
059    protected void setUp() {
060        a = b = c = d = e = null;
061        fac = new GenPolynomialRing<ModInteger>(new ModIntegerRing(ml),rl);
062    }
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    public void testConstruction() {
075        c = fac.getONE();
076        assertTrue("length( c ) = 1", c.length() == 1);
077        assertTrue("isZERO( c )", !c.isZERO() );
078        assertTrue("isONE( c )", c.isONE() );
079
080        d = fac.getZERO();
081        assertTrue("length( d ) = 0", d.length() == 0);
082        assertTrue("isZERO( d )", d.isZERO() );
083        assertTrue("isONE( d )", !d.isONE() );
084    }
085
086
087    /**
088     * Test random polynomial.
089     */
090    public void testRandom() {
091        for (int i = 0; i < 7; i++) {
092            a = fac.random(ll);
093            //fac.random(kl*(i+1), 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(ll);
106        b = fac.random(ll);
107
108        c = a.sum(b);
109        d = c.subtract(b);
110        assertEquals("a+b-b = a",a,d);
111
112        c = fac.random(ll);
113
114        ExpVector u = ExpVector.random(rl,el,q);
115        ModInteger x = c.leadingBaseCoefficient().ring.random(kl);
116
117        b = new GenPolynomial<ModInteger>(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<ModInteger>(fac);
127        b = new GenPolynomial<ModInteger>(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(ll);
143        assertTrue("not isZERO( a )", !a.isZERO() );
144
145        b = fac.random(ll);
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(ll);
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        ModInteger 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        ModInteger y = b.leadingBaseCoefficient().inverse();
180        c = b.monic();
181        d = b.multiply(y);
182        assertEquals("b.monic() = b(1/ldcf(b))",c,d);
183
184        e = new GenPolynomial<ModInteger>(fac,y);
185        d = b.multiply(e);
186        assertEquals("b.monic() = b(1/ldcf(b))",c,d);
187
188        d = e.multiply(b);
189        assertEquals("b.monic() = (1/ldcf(b) (0))*b",c,d);
190    }
191
192
193    /**
194     * Test distributive law.
195     */
196    public void testDistributive() {
197        a = fac.random(kl,ll,el,q);
198        b = fac.random(kl,ll,el,q);
199        c = fac.random(kl,ll,el,q);
200
201        d = a.multiply( b.sum(c) );
202        e = a.multiply( b ).sum( a.multiply(c) );
203
204        assertEquals("a(b+c) = ab+ac",d,e);
205    }
206
207}