001/*
002 * $Id: ModGenSolvablePolynomialTest.java 5042 2014-12-29 12:32:59Z kredel $
003 */
004
005package edu.jas.poly;
006
007
008import junit.framework.Test;
009import junit.framework.TestCase;
010import junit.framework.TestSuite;
011
012import org.apache.log4j.BasicConfigurator;
013
014import edu.jas.arith.ModInteger;
015import edu.jas.arith.ModIntegerRing;
016
017
018// import edu.jas.structure.RingElem;
019
020
021/**
022 * ModInteger coefficients GenSolvablePolynomial tests with JUnit.
023 * @author Heinz Kredel.
024 */
025
026public class ModGenSolvablePolynomialTest extends TestCase {
027
028
029    /**
030     * main
031     */
032    public static void main(String[] args) {
033        BasicConfigurator.configure();
034        junit.textui.TestRunner.run(suite());
035    }
036
037
038    /**
039     * Constructs a <CODE>ModGenSolvablePolynomialTest</CODE> object.
040     * @param name String.
041     */
042    public ModGenSolvablePolynomialTest(String name) {
043        super(name);
044    }
045
046
047    /**
048     * suite.
049     */
050    public static Test suite() {
051        TestSuite suite = new TestSuite(ModGenSolvablePolynomialTest.class);
052        return suite;
053    }
054
055
056    GenSolvablePolynomial<ModInteger> a;
057
058
059    GenSolvablePolynomial<ModInteger> b;
060
061
062    GenSolvablePolynomial<ModInteger> c;
063
064
065    GenSolvablePolynomial<ModInteger> d;
066
067
068    GenSolvablePolynomial<ModInteger> e;
069
070
071    GenSolvablePolynomial<ModInteger> f;
072
073
074    GenSolvablePolynomial<ModInteger> x1;
075
076
077    GenSolvablePolynomial<ModInteger> x2;
078
079
080    int ml = 19; // modul 
081
082
083    int rl = 5;
084
085
086    int kl = 10;
087
088
089    int ll = 5;
090
091
092    int el = 3;
093
094
095    float q = 0.5f;
096
097
098    RelationTable<ModInteger> table;
099
100
101    GenSolvablePolynomialRing<ModInteger> ring;
102
103
104    ModIntegerRing cfac;
105
106
107    @Override
108    protected void setUp() {
109        cfac = new ModIntegerRing(ml);
110        ring = new GenSolvablePolynomialRing<ModInteger>(cfac, rl);
111        table = null; //ring.table;
112        a = b = c = d = e = null;
113    }
114
115
116    @Override
117    protected void tearDown() {
118        table = null;
119        ring = null;
120        a = b = c = d = e = null;
121    }
122
123
124    /**
125     * Test constructor and toString.
126     */
127    public void testConstructor() {
128        a = new GenSolvablePolynomial<ModInteger>(ring);
129        assertTrue("length( a ) = 0", a.length() == 0);
130        assertTrue("isZERO( a )", a.isZERO());
131        assertTrue("isONE( a )", !a.isONE());
132
133        c = ring.getONE();
134        assertTrue("length( c ) = 1", c.length() == 1);
135        assertTrue("isZERO( c )", !c.isZERO());
136        assertTrue("isONE( c )", c.isONE());
137
138        d = ring.getZERO();
139        assertTrue("length( d ) = 0", d.length() == 0);
140        assertTrue("isZERO( d )", d.isZERO());
141        assertTrue("isONE( d )", !d.isONE());
142    }
143
144
145    /**
146     * Test random polynomial.
147     */
148    public void testRandom() {
149        assertTrue("isCommutative()", ring.isCommutative());
150
151        for (int i = 0; i < 2; i++) {
152            // a = ring.random(ll+2*i);
153            a = ring.random(kl * (i + 1), ll + 2 * i, el + i, q);
154            assertTrue("length( a" + i + " ) <> 0", a.length() >= 0);
155            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
156            assertTrue(" not isONE( a" + i + " )", !a.isONE());
157        }
158    }
159
160
161    /**
162     * Test addition.
163     */
164    public void testAddition() {
165        a = ring.random(kl, ll, el, q);
166
167        c = (GenSolvablePolynomial<ModInteger>) a.subtract(a);
168        assertTrue("a-a = 0", c.isZERO());
169
170        b = (GenSolvablePolynomial<ModInteger>) a.sum(a);
171        c = (GenSolvablePolynomial<ModInteger>) b.subtract(a);
172
173        assertEquals("a+a-a = a", c, a);
174        assertTrue("a+a-a = a", c.equals(a));
175
176        b = ring.random(kl, ll, el, q);
177        c = (GenSolvablePolynomial<ModInteger>) b.sum(a);
178        d = (GenSolvablePolynomial<ModInteger>) a.sum(b);
179
180        assertEquals("a+b = b+a", c, d);
181        assertTrue("a+b = b+a", c.equals(d));
182
183        c = ring.random(kl, ll, el, q);
184        d = (GenSolvablePolynomial<ModInteger>) a.sum(b.sum(c));
185        e = (GenSolvablePolynomial<ModInteger>) a.sum(b).sum(c);
186
187        assertEquals("a+(b+c) = (a+b)+c", d, e);
188        assertTrue("a+(b+c) = (a+b)+c", d.equals(e));
189
190        ExpVector u = ExpVector.EVRAND(rl, el, q);
191        ModInteger x = cfac.random(kl);
192
193        b = ring.getONE().multiply(x, u);
194        c = (GenSolvablePolynomial<ModInteger>) a.sum(b);
195        d = (GenSolvablePolynomial<ModInteger>) a.sum(x, u);
196        assertEquals("a+p(x,u) = a+(x,u)", c, d);
197
198        c = (GenSolvablePolynomial<ModInteger>) a.subtract(b);
199        d = (GenSolvablePolynomial<ModInteger>) a.subtract(x, u);
200        assertEquals("a-p(x,u) = a-(x,u)", c, d);
201
202        a = ring.getZERO();
203        b = ring.getONE().multiply(x, u);
204        c = (GenSolvablePolynomial<ModInteger>) b.sum(a);
205        d = (GenSolvablePolynomial<ModInteger>) a.sum(x, u);
206        assertEquals("a+p(x,u) = a+(x,u)", c, d);
207
208        c = (GenSolvablePolynomial<ModInteger>) a.subtract(b);
209        d = (GenSolvablePolynomial<ModInteger>) a.subtract(x, u);
210        assertEquals("a-p(x,u) = a-(x,u)", c, d);
211    }
212
213
214    /**
215     * Test multiplication.
216     */
217    @SuppressWarnings("cast")
218    public void testMultiplication() {
219        a = ring.random(kl, ll, el, q);
220        assertTrue("not isZERO( a )", !a.isZERO());
221        //a = ModGenSolvablePolynomial.DIRRAS(1, kl, 4, el, q );
222
223        b = ring.random(kl, ll, el, q);
224        assertTrue("not isZERO( b )", !b.isZERO());
225
226        c = b.multiply(a);
227        d = a.multiply(b);
228        assertTrue("not isZERO( c )", !c.isZERO());
229        assertTrue("not isZERO( d )", !d.isZERO());
230
231        e = (GenSolvablePolynomial<ModInteger>) d.subtract(c);
232        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
233
234        assertEquals("a*b = b*a", c, d);
235        assertTrue("a*b = b*a", c.equals(d));
236
237        c = ring.random(kl, ll, el, q);
238        d = a.multiply(b.multiply(c));
239        e = (a.multiply(b)).multiply(c);
240
241        assertEquals("a(bc) = (ab)c", d, e);
242        assertTrue("a(bc) = (ab)c", d.equals(e));
243
244        ModInteger x = a.leadingBaseCoefficient().inverse();
245        c = (GenSolvablePolynomial<ModInteger>) a.monic();
246        d = a.multiply(x);
247        assertEquals("a.monic() = a(1/ldcf(a))", c, d);
248
249        ExpVector u = ring.evzero;
250        ModInteger y = b.leadingBaseCoefficient().inverse();
251        c = (GenSolvablePolynomial<ModInteger>) b.monic();
252        d = b.multiply(y, u);
253        assertEquals("b.monic() = b(1/ldcf(b))", c, d);
254
255        e = ring.getONE().multiply(y, u);
256        d = b.multiply(e);
257        assertEquals("b.monic() = b(1/ldcf(b))", c, d);
258
259        d = e.multiply(b);
260        assertEquals("b.monic() = (1/ldcf(b) (0))*b", c, d);
261
262        d = a.monic();
263        assertTrue("a.monic(): ", d.leadingBaseCoefficient().isONE());
264    }
265
266
267    /**
268     * Test Weyl polynomials.
269     */
270    public void testWeyl() {
271        int rloc = 4;
272        ring = new GenSolvablePolynomialRing<ModInteger>(cfac, rloc);
273
274        RelationGenerator<ModInteger> wl = new WeylRelations<ModInteger>();
275        wl.generate(ring);
276        //table = ring.table;
277        //System.out.println("table = " + table);
278        //System.out.println("ring = " + ring);
279
280        assertFalse("isCommutative()", ring.isCommutative());
281        assertTrue("isAssociative()", ring.isAssociative());
282
283        a = ring.random(kl, ll, el + 2, q);
284        assertTrue("not isZERO( a )", !a.isZERO());
285        //System.out.println("a = " + a);
286
287        b = ring.random(kl, ll, el + 2, q);
288        assertTrue("not isZERO( b )", !b.isZERO());
289        //System.out.println("b = " + b);
290
291
292        // non commutative
293        c = b.multiply(a);
294        d = a.multiply(b);
295        //System.out.println("c = " + c);
296        //System.out.println("d = " + d);
297        assertTrue("not isZERO( c )", !c.isZERO());
298        assertTrue("not isZERO( d )", !d.isZERO());
299
300        e = (GenSolvablePolynomial<ModInteger>) d.subtract(c);
301        assertTrue("!isZERO( a*b-b*a ) " + e, !e.isZERO());
302        assertTrue("a*b != b*a", c.equals(d) || c.leadingExpVector().equals(d.leadingExpVector()));
303
304        c = ring.random(kl, ll, el, q);
305        //System.out.println("\na = " + a);
306        //System.out.println("\nb = " + b);
307        //System.out.println("\nc = " + c);
308
309        // associative
310        //x1 = b.multiply(c);
311        //System.out.println("\nx1 = " + x1);
312        d = a.multiply(b.multiply(c));
313
314        //x2 = a.multiply(b);
315        //System.out.println("\nx2 = " + x2);
316        e = a.multiply(b).multiply(c);
317
318        //System.out.println("\nd = " + d);
319        //System.out.println("\ne = " + e);
320
321        //f = (GenSolvablePolynomial<ModInteger>)d.subtract(e);
322        //System.out.println("\nf = " + f);
323
324        assertEquals("a(bc) = (ab)c", d, e);
325        assertTrue("a(bc) = (ab)c", d.equals(e));
326    }
327
328
329    /**
330     * Test distributive law.
331     */
332    public void testDistributive() {
333        int rloc = 4;
334        ring = new GenSolvablePolynomialRing<ModInteger>(cfac, rloc);
335
336        RelationGenerator<ModInteger> wl = new WeylRelations<ModInteger>();
337        wl.generate(ring);
338        //table = ring.table;
339        //System.out.println("table = " + table);
340        //System.out.println("ring = " + ring);
341
342        a = ring.random(kl, ll, el, q);
343        b = ring.random(kl, ll, el, q);
344        c = ring.random(kl, ll, el, q);
345
346        d = a.multiply((GenSolvablePolynomial<ModInteger>) b.sum(c));
347        e = (GenSolvablePolynomial<ModInteger>) a.multiply(b).sum(a.multiply(c));
348
349        assertEquals("a(b+c) = ab+ac", d, e);
350    }
351
352}