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.kern.ComputerThreads;
019import edu.jas.kern.PrettyPrint;
020import edu.jas.poly.GenSolvablePolynomial;
021import edu.jas.poly.GenSolvablePolynomialRing;
022import edu.jas.poly.RelationGenerator;
023import edu.jas.poly.TermOrder;
024import edu.jas.poly.WeylRelations;
025
026
027/**
028 * SolvableLocal over BigRational GenSolvablePolynomial tests with JUnit.
029 * @author Heinz Kredel
030 */
031
032public class SolvableLocalTest extends TestCase {
033
034
035    /**
036     * main.
037     */
038    public static void main(String[] args) {
039        
040        junit.textui.TestRunner.run(suite());
041    }
042
043
044    /**
045     * Constructs a <CODE>SolvableLocalTest</CODE> object.
046     * @param name String.
047     */
048    public SolvableLocalTest(String name) {
049        super(name);
050    }
051
052
053    /**
054     * suite.
055     */
056    public static Test suite() {
057        TestSuite suite = new TestSuite(SolvableLocalTest.class);
058        return suite;
059    }
060
061
062    SolvableLocalRing<BigRational> efac;
063
064
065    GenSolvablePolynomialRing<BigRational> mfac;
066
067
068    SolvableIdeal<BigRational> id;
069
070
071    SolvableLocal<BigRational> a, b, c, d, e;
072
073
074    SolvableLocal<BigRational> az, bz, cz, dz, ez;
075
076
077    int rl = 4;
078
079
080    int kl = 2;
081
082
083    int ll = 3; //6;
084
085
086    int el = 2;
087
088
089    float q = 0.15f;
090
091
092    int il = (rl == 1 ? 1 : 2);
093
094
095    @Override
096    protected void setUp() {
097        a = b = c = d = e = null;
098        TermOrder to = new TermOrder(TermOrder.INVLEX);
099        String[] vars = new String[] { "w", "x", "y", "z" };
100        mfac = new GenSolvablePolynomialRing<BigRational>(new BigRational(1), rl, to, vars);
101        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
102        wl.generate(mfac);
103        if (!mfac.isAssociative()) {
104            System.out.println("ring not associative: " + mfac);
105        }
106        //id = genRandomIdeal();
107        id = genIdealA();
108        //System.out.println("id = " + id);
109        assert !id.isONE() : "id = " + id;
110        efac = new SolvableLocalRing<BigRational>(id);
111        //System.out.println("efac = " + efac);
112    }
113
114
115    protected SolvableIdeal<BigRational> genRandomIdeal() {
116        List<GenSolvablePolynomial<BigRational>> F;
117        do {
118            F = new ArrayList<GenSolvablePolynomial<BigRational>>(il);
119            for (int i = 0; i < il; i++) {
120                GenSolvablePolynomial<BigRational> mo = mfac.random(kl, ll, el + 1, q);
121                while (mo.isConstant()) {
122                    mo = mfac.random(kl, ll, el + 1, q);
123                }
124                F.add(mo);
125            }
126            SolvableIdeal<BigRational> id = new SolvableIdeal<BigRational>(mfac, F);
127            id.doGB();
128        } while (id.isONE());
129        return id;
130    }
131
132
133    protected SolvableIdeal<BigRational> genIdealA() {
134        GenSolvablePolynomial<BigRational> p;
135        List<GenSolvablePolynomial<BigRational>> F;
136        F = new ArrayList<GenSolvablePolynomial<BigRational>>(il);
137        p = mfac.parse("y^2 - 42/5");
138        F.add(p);
139        //p = mfac.parse("z^2");
140        p = mfac.parse("x^2");
141        F.add(p);
142        //p = mfac.parse("x^2 - w^2 ");
143        //F.add( p );
144        SolvableIdeal<BigRational> id = new SolvableIdeal<BigRational>(mfac, F);
145        id.doGB();
146        return id;
147    }
148
149
150    @Override
151    protected void tearDown() {
152        a = b = c = d = e = null;
153        //efac.terminate();
154        efac = null;
155        ComputerThreads.terminate();
156    }
157
158
159    /**
160     * Test constructor and toString.
161     */
162    public void testConstruction() {
163        c = efac.getONE();
164        //System.out.println("c = " + c);
165        //System.out.println("c.val = " + c.val);
166        assertTrue("length( c ) = 1", c.num.length() == 1);
167        assertTrue("isZERO( c )", !c.isZERO());
168        assertTrue("isONE( c )", c.isONE());
169
170        d = efac.getZERO();
171        //System.out.println("d = " + d);
172        //System.out.println("d.val = " + d.val);
173        assertTrue("length( d ) = 0", d.num.length() == 0);
174        assertTrue("isZERO( d )", d.isZERO());
175        assertTrue("isONE( d )", !d.isONE());
176
177        for (SolvableLocal<BigRational> g : efac.generators()) {
178            //System.out.println("g = " + g);
179            assertFalse("not isZERO( g )", g.isZERO());
180        }
181        // solved: not all products are defined 
182        assertTrue("isAssociative: ", efac.isAssociative());
183    }
184
185
186    /**
187     * Test random polynomial.
188     */
189    public void testRandom() {
190        for (int i = 0; i < 3; i++) {
191            //a = efac.random(ll+i);
192            a = efac.random(kl + i, ll + 2, el, q);
193            //System.out.println("a = " + a);
194            if (a.isZERO() || a.isONE()) {
195                continue;
196            }
197            assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0);
198            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
199            assertTrue(" not isONE( a" + i + " )", !a.isONE());
200            assertEquals("a == a: ", a, a);
201        }
202    }
203
204
205    /**
206     * Test addition.
207     */
208    public void testAddition() {
209        a = efac.random(kl, ll, el+1, q);
210        b = efac.random(kl, ll, el+1, q);
211        //a = efac.parse("{ 1  | w * x + 25/28  }");
212        //b = efac.parse("{ x - 35/18  | x + 2 w - 6  }");
213        //!a = efac.parse("{ x - 1/7  | y * z + 7/10  }");
214        //!b = efac.parse("{ 1  | w + 3  }");
215        //a = efac.parse("{ 1  | y * z + 7/10 }");
216        //b = efac.parse("{ x - 1/7 | w + 3  }");
217        //a = efac.parse("{ 91/15 | y - 3/14  }");
218        //b = efac.parse("{ 11/12 | w * z^2 + 4/3 }");
219        //System.out.println("a = " + a);
220        //System.out.println("b = " + b);
221
222        c = a.sum(efac.getZERO());
223        d = a.subtract(efac.getZERO());
224        assertEquals("a+0 = a-0", c, d);
225
226        c = efac.getZERO().sum(a);
227        d = efac.getZERO().subtract(a.negate());
228        assertEquals("0+a = 0-(-a)", c, d);
229
230        //c = a.sum(b);
231        //d = b.sum(a);
232        //System.out.println("c = " + c);
233        //System.out.println("d = " + d);
234        //assertEquals("a+b = b+a", c, d);
235
236        c = a.sum(b);
237        //System.out.println("c = " + c);
238        d = c.subtract(b);
239        //d = c.sum(b.negate());
240        //System.out.println("d = " + d);
241        //System.out.println("a = " + a);
242        assertEquals("(a+b)-b == a: " + a + ", " + b, a, d);
243        e = d.subtract(a);
244        //System.out.println("d-a = " + e);
245        assertTrue("((a+b)-b)-a == 0", e.isZERO());
246
247        //c = efac.random(kl,ll,el,q);
248        c = new SolvableLocal<BigRational>(efac, mfac.univariate(1, 2));
249        //System.out.println("c = " + c);
250        d = c.sum(a.sum(b));
251        e = c.sum(a).sum(b);
252        //System.out.println("d = " + d);
253        //System.out.println("e = " + e);
254        assertEquals("c+(a+b) = (c+a)+b", d, e);
255    }
256
257
258    /**
259     * Test multiplication.
260     */
261    public void testMultiplication() {
262        a = efac.random(kl, ll, el + 0, q);
263        b = efac.random(kl, ll, el + 0, q);
264        //System.out.println("a = " + a);
265        //System.out.println("b = " + b);
266
267        c = a.multiply(efac.getONE());
268        d = efac.getONE().multiply(a);
269        //System.out.println("c = " + c);
270        //System.out.println("d = " + d);
271        assertEquals("a*1 = 1*a", c, a);
272        assertEquals("a*1 = 1*a", c, d);
273
274        c = b.multiply(a);
275        d = a.multiply(b);
276        //System.out.println("c = " + c);
277        //System.out.println("d = " + d);
278        //non-com assertFalse("a*b = b*a", c.equals(d) );
279        //e = d.subtract(c);
280        //non-com assertFalse("not isZERO( a*b-b*a ) " + e, e.isZERO() );
281
282        //c = efac.random(kl,ll,el,q);
283        c = new SolvableLocal<BigRational>(efac, mfac.univariate(1, 2));
284        //System.out.println("c = " + c);
285        d = a.multiply(b.multiply(c));
286        //System.out.println("d = " + d);
287        e = (a.multiply(b)).multiply(c);
288        //System.out.println("e = " + e);
289        assertEquals("a(bc) = (ab)c", d, e);
290        if (a.isUnit()) {
291            c = a.inverse();
292            d = c.multiply(a);
293            //System.out.println("a = " + a);
294            //System.out.println("c = " + c);
295            //System.out.println("d = " + d);
296            assertTrue("a*1/a = 1", d.isONE());
297        }
298    }
299
300
301    /**
302     * Test parse.
303     */
304    public void testParse() {
305        a = efac.random(kl, ll, el, q * 2);
306        //PrettyPrint.setInternal();
307        //System.out.println("a = " + a);
308        PrettyPrint.setPretty();
309        //System.out.println("a = " + a);
310        String p = a.toString();
311        //System.out.println("p = " + p);
312        b = efac.parse(p);
313        //System.out.println("b = " + b);
314
315        //c = a.subtract(b);
316        //System.out.println("c = " + c);
317        assertEquals("parse(a.toSting()) = a", a, b);
318    }
319
320}