001/*
002 * $Id$
003 */
004
005package edu.jas.application;
006
007
008import java.util.ArrayList;
009import java.util.List;
010
011import junit.framework.Test;
012import junit.framework.TestCase;
013import junit.framework.TestSuite;
014
015
016
017import edu.jas.arith.BigRational;
018import edu.jas.poly.GenWordPolynomial;
019import edu.jas.poly.GenWordPolynomialRing;
020import edu.jas.structure.NotDivisibleException;
021import edu.jas.structure.NotInvertibleException;
022
023
024/**
025 * WordResidue tests with JUnit.
026 * @author Heinz Kredel
027 */
028
029public class WordResidueTest extends TestCase {
030
031
032    /**
033     * main.
034     */
035    public static void main(String[] args) {
036        
037        junit.textui.TestRunner.run(suite());
038    }
039
040
041    /**
042     * Constructs a <CODE>WordResidueTest</CODE> object.
043     * @param name String.
044     */
045    public WordResidueTest(String name) {
046        super(name);
047    }
048
049
050    /**
051     * suite.
052     */
053    public static Test suite() {
054        TestSuite suite = new TestSuite(WordResidueTest.class);
055        return suite;
056    }
057
058
059    WordIdeal<BigRational> id;
060
061
062    WordResidueRing<BigRational> fac;
063
064
065    GenWordPolynomialRing<BigRational> mfac;
066
067
068    List<GenWordPolynomial<BigRational>> F;
069
070
071    WordResidue<BigRational> a, b, c, d, e, f;
072
073
074    int rl = 4;
075
076
077    int kl = 3;
078
079
080    int ll = 4;
081
082
083    int el = 2;
084
085
086    int il = (rl == 1 ? 1 : 2);
087
088
089    @Override
090    protected void setUp() {
091        a = b = c = d = e = null;
092        String[] vars = new String[] { "w", "x", "y", "z" };
093        mfac = new GenWordPolynomialRing<BigRational>(new BigRational(1), vars);
094        //System.out.println("mfac = " + mfac.toScript());
095        //GenWordPolynomial<BigRational> p1 = mfac.parse("w - x^2");
096        //GenWordPolynomial<BigRational> p1 = mfac.parse("x + 49/12");
097        //GenWordPolynomial<BigRational> p2 = mfac.parse("y - 5 * w - 65/28");
098        do {
099            F = new ArrayList<GenWordPolynomial<BigRational>>(il);
100            for (int i = 0; i < il; i++) {
101                GenWordPolynomial<BigRational> mo = mfac.random(kl, ll, el);
102                while (mo.isConstant()) {
103                    mo = mfac.random(kl, ll, el);
104                }
105                F.add(mo);
106            }
107            //F.add(p1); F.add(p2);
108            id = new WordIdeal<BigRational>(mfac, F);
109            id.doGB();
110        } while (id.isONE());
111        //System.out.println("id = " + id);
112        assert !id.isONE() : "id = " + id;
113        fac = new WordResidueRing<BigRational>(id);
114        //System.out.println("fac = " + fac.toScript());
115        F = null;
116    }
117
118
119    @Override
120    protected void tearDown() {
121        a = b = c = d = e = null;
122        fac = null;
123        id = null;
124        mfac = null;
125    }
126
127
128    /**
129     * Test constructor and toString.
130     */
131    public void testConstruction() {
132        c = fac.getONE();
133        //System.out.println("c = " + c);
134        //System.out.println("c.val = " + c.val);
135        assertTrue("length( c ) = 1 ", c.val.length() == 1 || id.isONE());
136        assertTrue("isZERO( c )", !c.isZERO() || id.isONE());
137        assertTrue("isONE( c )", c.isONE() || id.isONE());
138
139        d = fac.getZERO();
140        //System.out.println("d = " + d);
141        //System.out.println("d.val = " + d.val);
142        assertTrue("length( d ) = 0", d.val.length() == 0);
143        assertTrue("isZERO( d )", d.isZERO());
144        assertTrue("isONE( d )", !d.isONE());
145
146        for (WordResidue<BigRational> g : fac.generators()) {
147            //System.out.println("g = " + g);
148            assertFalse("not isZERO( g )", g.isZERO());
149        }
150    }
151
152
153    /**
154     * Test random polynomial.
155     */
156    public void testRandom() {
157        for (int i = 1; i < 7; i++) {
158            //a = fac.random(ll+i);
159            a = fac.random(kl * i / 2, ll + i, el + i / 2);
160            //System.out.println("a = " + a);
161            if (a.isZERO() || a.isONE()) {
162                continue;
163            }
164            assertTrue("length( a" + i + " ) <> 0", a.val.length() >= 0);
165            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
166            assertTrue(" not isONE( a" + i + " )", !a.isONE());
167        }
168    }
169
170
171    /**
172     * Test addition.
173     */
174    public void testAddition() {
175        a = fac.random(kl, ll, el + 1);
176        b = fac.random(kl, ll, el + 1);
177        //System.out.println("a = " + a);
178        //System.out.println("b = " + b);
179
180        c = a.sum(b);
181        d = c.subtract(b);
182        assertEquals("a+b-b = a", a, d);
183
184        c = a.sum(b);
185        d = b.sum(a);
186        assertEquals("a+b = b+a", c, d);
187
188        c = fac.random(kl, ll, el);
189        //System.out.println("c = " + c);
190        d = c.sum(a.sum(b));
191        e = c.sum(a).sum(b);
192        assertEquals("c+(a+b) = (c+a)+b", d, e);
193
194        c = a.sum(fac.getZERO());
195        d = a.subtract(fac.getZERO());
196        assertEquals("a+0 = a-0", c, d);
197
198        c = fac.getZERO().sum(a);
199        d = fac.getZERO().subtract(a.negate());
200        assertEquals("0+a = 0+(-a)", c, d);
201    }
202
203
204    /**
205     * Test multiplication.
206     */
207    public void testMultiplication() {
208        List<WordResidue<BigRational>> g = fac.generators();
209        //System.out.println("g = " + g);
210        //a = fac.random(kl,ll,el,q);
211        a = g.get(1);
212        if (a.isZERO()) {
213            a = fac.getONE(); //return;
214        }
215        assertTrue("not isZERO( a )", !a.isZERO());
216
217        b = fac.random(kl, ll, el);
218        //b = g.get(g.size()-1);
219        if (b.isZERO()) {
220            b = fac.getONE(); //return;
221        }
222        assertTrue("not isZERO( b )", !b.isZERO());
223        //System.out.println("a = " + a);
224        //System.out.println("b = " + b);
225
226        c = a.multiply(fac.getONE());
227        d = fac.getONE().multiply(a);
228        assertEquals("a*1 = 1*a", c, d);
229        assertEquals("a*1 = 1*a", c, a);
230
231        c = b.multiply(a);
232        d = a.multiply(b);
233        assertTrue("not isZERO( c )", !c.isZERO());
234        assertTrue("not isZERO( d )", !d.isZERO());
235
236        //System.out.println("a = " + a);
237        //System.out.println("b = " + b);
238        //System.out.println("c = " + c);
239        //System.out.println("d = " + d);
240        //e = d.subtract(c);
241        //non-com: assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
242        //non-com: assertEquals("a*b = b*a",c,d);
243
244        c = fac.random(kl, ll + 1, el);
245        //System.out.println("c = " + c);
246        d = a.multiply(b.multiply(c));
247        e = a.multiply(b).multiply(c);
248        //System.out.println("d = " + d);
249        //System.out.println("e = " + e);
250        //System.out.println("d-e = " + d.subtract(e) );
251        assertEquals("a(bc) = (ab)c", d, e);
252        //assertTrue("a(bc) = (ab)c", d.equals(e) );
253
254        if (a.isUnit()) { // !a.isZERO() isUnit()
255            try {
256                c = a.inverse();
257                //System.out.println("a = " + a);
258                //System.out.println("c = " + c);
259                d = c.multiply(a);
260                //System.out.println("d = " + d);
261                assertTrue("a*1/a = 1: " + fac, d.isONE()); // || true 
262            } catch (NotInvertibleException e) {
263                // can happen
264            } catch (UnsupportedOperationException e) {
265            }
266        }
267    }
268
269
270    /**
271     * Test division.
272     */
273    public void testDivision() {
274        List<WordResidue<BigRational>> g = fac.generators();
275        //System.out.println("g = " + g);
276        //a = fac.random(kl,ll,el,q);
277        a = g.get(1);
278        if (a.isZERO()) {
279            a = fac.getONE();
280        }
281        assertTrue("not isZERO( a )", !a.isZERO());
282
283        b = fac.random(kl, ll, el + 2).monic();
284        if (b.isZERO() || b.isONE()) {
285            b = g.get(g.size() - 1);
286            //b = fac.getONE();
287        }
288        assertTrue("not isZERO( b )", !b.isZERO());
289        //a = fac.parse("w");
290        //b = fac.parse("w w + 79/360 z + 13/28 w");
291
292        //System.out.println("a = " + a);
293        //System.out.println("b = " + b);
294
295        // left division
296        c = a.multiply(b);
297        //System.out.println("c = " + c);
298        try {
299            d = c.divide(b);
300            //System.out.println("d = " + d);
301            e = c.remainder(b);
302            //System.out.println("e = " + e);
303
304            f = d.multiply(b).sum(e);
305            //System.out.println("f = " + f);
306            assertEquals("c == f: ", c, f);
307        } catch (NotDivisibleException ex) {
308            // pass
309            System.out.println("ex = " + ex);
310        }
311
312        // right division
313        c = b.multiply(a);
314        //System.out.println("c = " + c);
315        try {
316            d = c.rightDivide(b);
317            //System.out.println("d = " + d);
318            e = c.rightRemainder(b);
319            //System.out.println("e = " + e);
320
321            f = b.multiply(d).sum(e);
322            //System.out.println("f = " + f);
323            assertEquals("c == f: ", c, f);
324        } catch (NotDivisibleException ex) {
325            // pass
326            System.out.println("ex = " + ex);
327        }
328
329        // two-sided division
330        c = a.multiply(b.multiply(a));
331        //System.out.println("c = " + c);
332        try {
333            WordResidue<BigRational>[] dd = c.twosidedDivide(b);
334            //System.out.println("dd = " + dd[0] + " // " + dd[1]);
335            e = c.twosidedRemainder(b);
336            //System.out.println("e = " + e);
337
338            f = dd[0].multiply(b).multiply(dd[1]).sum(e);
339            //System.out.println("f = " + f);
340            //System.out.println("c-f = " + c.subtract(f));
341            assertEquals("c == f: ", c, f);
342        } catch (NotDivisibleException ex) {
343            // pass
344            System.out.println("ex = " + ex);
345        }
346    }
347}