001/*
002 * $Id: LocalTest.java 4534 2013-07-28 15:08:42Z kredel $
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
015import edu.jas.arith.BigRational;
016import edu.jas.kern.ComputerThreads;
017import edu.jas.poly.GenPolynomial;
018import edu.jas.poly.GenPolynomialRing;
019
020
021/**
022 * Local tests with JUnit.
023 * @author Heinz Kredel.
024 */
025
026public class LocalTest 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>LocalTest</CODE> object.
040     * @param name String.
041     */
042    public LocalTest(String name) {
043        super(name);
044    }
045
046
047    /**
048     * suite.
049     */
050    public static Test suite() {
051        TestSuite suite = new TestSuite(LocalTest.class);
052        return suite;
053    }
054
055
056    //private final static int bitlen = 100;
057
058    Ideal<BigRational> id;
059
060
061    LocalRing<BigRational> fac;
062
063
064    GenPolynomialRing<BigRational> mfac;
065
066
067    List<GenPolynomial<BigRational>> F;
068
069
070    Local<BigRational> a;
071
072
073    Local<BigRational> b;
074
075
076    Local<BigRational> c;
077
078
079    Local<BigRational> d;
080
081
082    Local<BigRational> e;
083
084
085    int rl = 3;
086
087
088    int kl = 3;
089
090
091    int ll = 5;
092
093
094    int el = 2;
095
096
097    float q = 0.3f;
098
099
100    int il = 2;
101
102
103    @Override
104    protected void setUp() {
105        a = b = c = d = e = null;
106        BigRational cfac = new BigRational(1);
107        String[] vars = new String[] { "x", "y", "z" };
108        mfac = new GenPolynomialRing<BigRational>(cfac, rl, vars);
109        id = null;
110        while (id == null || id.isONE()) {
111            F = new ArrayList<GenPolynomial<BigRational>>(il);
112            for (int i = 0; i < rl; i++) {
113                //GenPolynomial<BigRational> mo = mfac.random(kl,ll,el,q);
114                GenPolynomial<BigRational> mo = mfac.univariate(i);
115                mo = mo.sum(mfac.fromInteger(cfac.random(7).denominator()));
116                while (mo.isConstant()) {
117                    mo = mfac.random(kl, ll, el, q);
118                }
119                F.add(mo);
120            }
121            id = new Ideal<BigRational>(mfac, F);
122            id = id.GB();
123        }
124        //System.out.println("id = " + id);
125        fac = new LocalRing<BigRational>(id);
126        //System.out.println("fac = " + fac);
127        F = null;
128    }
129
130
131    @Override
132    protected void tearDown() {
133        a = b = c = d = e = null;
134        fac = null;
135        id = null;
136        mfac = null;
137        ComputerThreads.terminate();
138    }
139
140
141    /**
142     * Test constructor and toString.
143     * 
144     */
145    public void testConstruction() {
146        c = fac.getONE();
147        //System.out.println("c = " + c);
148        //System.out.println("c.num = " + c.num);
149        assertTrue("length( c ) = 1", c.num.length() == 1);
150        assertTrue("isZERO( c )", !c.isZERO());
151        assertTrue("isONE( c )", c.isONE());
152
153        d = fac.getZERO();
154        //System.out.println("d = " + d);
155        //System.out.println("d.num = " + d.num);
156        assertTrue("length( d ) = 0", d.num.length() == 0);
157        assertTrue("isZERO( d )", d.isZERO());
158        assertTrue("isONE( d )", !d.isONE());
159    }
160
161
162    /**
163     * Test random polynomial.
164     * 
165     */
166    public void testRandom() {
167        //System.out.println("fac = " + fac);
168        for (int i = 0; i < 4; i++) {
169            //a = fac.random(ll+i);
170            a = fac.random(kl * (i + 1), ll + i, el, q);
171            //System.out.println("a = " + a);
172            assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0);
173            if (a.isZERO() || a.isONE()) {
174                continue;
175            }
176            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
177            assertTrue(" not isONE( a" + i + " )", !a.isONE());
178        }
179    }
180
181
182    /**
183     * Test addition. Not jet working because of monic GBs.
184     */
185    public void testAddition() {
186        //System.out.println("fac = " + fac);
187
188        a = fac.random(kl, ll, el, q);
189        b = fac.random(kl, ll, el, q);
190        //System.out.println("a = " + a);
191        //System.out.println("b = " + b);
192
193        c = a.sum(b);
194        d = c.subtract(b);
195        //System.out.println("c = " + c);
196        //System.out.println("d = " + d);
197        assertEquals("a+b-b = a", a, d);
198
199        c = a.sum(b);
200        d = b.sum(a);
201        assertEquals("a+b = b+a", c, d);
202
203
204        c = fac.random(kl, ll, el, q);
205        d = c.sum(a.sum(b));
206        e = c.sum(a).sum(b);
207        assertEquals("c+(a+b) = (c+a)+b", d, e);
208
209        c = a.sum(fac.getZERO());
210        d = a.subtract(fac.getZERO());
211        assertEquals("a+0 = a-0", c, d);
212
213        c = fac.getZERO().sum(a);
214        d = fac.getZERO().subtract(a.negate());
215        assertEquals("0+a = 0+(-a)", c, d);
216    }
217
218
219    /**
220     * Test object multiplication. Not jet working because of monic GBs
221     */
222
223    public void testMultiplication() {
224        //System.out.println("fac = " + fac);
225
226        a = fac.random(kl, ll, el, q);
227        b = fac.random(kl, ll, el, q);
228
229        if (a.isZERO() || b.isZERO()) {
230            return;
231        }
232        //System.out.println("a = " + a);
233        //System.out.println("b = " + b);
234        assertTrue("not isZERO( a )", !a.isZERO());
235        assertTrue("not isZERO( b )", !b.isZERO());
236
237        c = b.multiply(a);
238        d = a.multiply(b);
239        //System.out.println("c = " + c);
240        //System.out.println("d = " + d);
241        assertTrue("not isZERO( c )", !c.isZERO());
242        assertTrue("not isZERO( d )", !d.isZERO());
243
244        e = d.subtract(c);
245        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
246
247        assertTrue("a*b = b*a", c.equals(d));
248        assertEquals("a*b = b*a", c, d);
249
250        c = fac.random(kl, ll, el, q);
251        d = a.multiply(b.multiply(c));
252        e = (a.multiply(b)).multiply(c);
253        //System.out.println("c = " + c);
254        //System.out.println("d = " + d);
255        //System.out.println("e = " + e);
256
257        assertEquals("a(bc) = (ab)c", d, e);
258        assertTrue("a(bc) = (ab)c", d.equals(e));
259
260        c = a.multiply(fac.getONE());
261        d = fac.getONE().multiply(a);
262        assertEquals("a*1 = 1*a", c, d);
263
264        if (a.isUnit()) {
265            c = a.inverse();
266            d = c.multiply(a);
267            //System.out.println("a = " + a);
268            //System.out.println("c = " + c);
269            //System.out.println("d = " + d);
270            assertTrue("a*1/a = 1", d.isONE());
271            d = c.inverse();
272            //System.out.println("d = " + d);
273            assertTrue("1/(1/a) = a", d.equals(a));
274        }
275    }
276
277}