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