001/*
002 * $Id: SolvableReductionTest.java 5042 2014-12-29 12:32:59Z kredel $
003 */
004
005package edu.jas.gb;
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 org.apache.log4j.BasicConfigurator;
016
017import edu.jas.arith.BigRational;
018import edu.jas.poly.GenSolvablePolynomial;
019import edu.jas.poly.GenSolvablePolynomialRing;
020import edu.jas.poly.PolynomialList;
021import edu.jas.poly.RelationGenerator;
022import edu.jas.poly.RelationTable;
023import edu.jas.poly.WeylRelations;
024
025
026/**
027 * Solvable Reduction tests with JUnit.
028 * @author Heinz Kredel.
029 */
030
031public class SolvableReductionTest extends TestCase {
032
033
034    /**
035     * main
036     */
037    public static void main(String[] args) {
038        BasicConfigurator.configure();
039        junit.textui.TestRunner.run(suite());
040    }
041
042
043    /**
044     * Constructs a <CODE>SolvableReductionTest</CODE> object.
045     * @param name String.
046     */
047    public SolvableReductionTest(String name) {
048        super(name);
049    }
050
051
052    /**
053     * suite.
054     */
055    public static Test suite() {
056        TestSuite suite = new TestSuite(SolvableReductionTest.class);
057        return suite;
058    }
059
060
061    //private final static int bitlen = 100;
062
063    GenSolvablePolynomialRing<BigRational> fac;
064
065
066    RelationTable table;
067
068
069    GenSolvablePolynomial<BigRational> a, b, c, d, e;
070
071
072    List<GenSolvablePolynomial<BigRational>> L;
073
074
075    PolynomialList<BigRational> F, G;
076
077
078    SolvableReduction<BigRational> sred;
079
080
081    SolvableReduction<BigRational> sredpar;
082
083
084    int rl = 4;
085
086
087    int kl = 10;
088
089
090    int ll = 5;
091
092
093    int el = 3;
094
095
096    float q = 0.4f;
097
098
099    @Override
100    protected void setUp() {
101        a = b = c = d = e = null;
102        fac = new GenSolvablePolynomialRing<BigRational>(new BigRational(0), rl);
103        sred = new SolvableReductionSeq<BigRational>();
104        sredpar = new SolvableReductionPar<BigRational>();
105    }
106
107
108    @Override
109    protected void tearDown() {
110        a = b = c = d = e = null;
111        fac = null;
112        sred = null;
113        sredpar = null;
114    }
115
116
117    /**
118     * Test constants and empty list reduction.
119     */
120    public void testRatReduction0() {
121        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
122
123        a = fac.random(kl, ll, el, q);
124        c = fac.getONE();
125        d = fac.getZERO();
126
127        e = sred.leftNormalform(L, c);
128        assertTrue("isONE( e )", e.isONE());
129
130        e = sred.leftNormalform(L, d);
131        assertTrue("isZERO( e )", e.isZERO());
132
133
134        L.add(c);
135        e = sred.leftNormalform(L, c);
136        assertTrue("isZERO( e )", e.isZERO());
137
138        // e = Reduction.leftNormalform( L, a );
139        // assertTrue("isZERO( e )", e.isZERO() ); 
140
141        e = sred.leftNormalform(L, d);
142        assertTrue("isZERO( e )", e.isZERO());
143
144
145        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
146        L.add(d);
147        e = sred.leftNormalform(L, c);
148        assertTrue("isONE( e )", e.isONE());
149
150        e = sred.leftNormalform(L, d);
151        assertTrue("isZERO( e )", e.isZERO());
152    }
153
154
155    /**
156     * Test constants and empty list reduction.
157     */
158    public void testWeylRatReduction0() {
159        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
160
161        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
162        wl.generate(fac);
163
164        a = fac.random(kl, ll, el, q);
165        c = fac.getONE();
166        d = fac.getZERO();
167
168        e = sred.leftNormalform(L, c);
169        assertTrue("isONE( e )", e.isONE());
170
171        e = sred.leftNormalform(L, d);
172        assertTrue("isZERO( e )", e.isZERO());
173
174
175        L.add(c);
176        e = sred.leftNormalform(L, c);
177        assertTrue("isZERO( e )", e.isZERO());
178
179        e = sred.leftNormalform(L, a);
180        assertTrue("isZERO( e )", e.isZERO());
181
182        e = sred.leftNormalform(L, d);
183        assertTrue("isZERO( e )", e.isZERO());
184
185
186        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
187        L.add(d);
188        e = sred.leftNormalform(L, c);
189        assertTrue("isONE( e )", e.isONE());
190
191        e = sred.leftNormalform(L, d);
192        assertTrue("isZERO( e )", e.isZERO());
193    }
194
195
196    /**
197     * Test Rat reduction.
198     */
199    public void testRatReduction() {
200        a = fac.random(kl, ll, el, q);
201        b = fac.random(kl, ll, el, q);
202
203        assertTrue("not isZERO( a )", !a.isZERO());
204
205        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
206        L.add(a);
207
208        e = sred.leftNormalform(L, a);
209        assertTrue("isZERO( e )", e.isZERO());
210
211        assertTrue("not isZERO( b )", !b.isZERO());
212
213        L.add(b);
214        e = sred.leftNormalform(L, a);
215        assertTrue("isZERO( e ) some times", e.isZERO());
216    }
217
218
219    /**
220     * Test Rat reduction parallel.
221     */
222    public void testRatReductionPar() {
223        a = fac.random(kl, ll, el, q);
224        b = fac.random(kl, ll, el, q);
225
226        assertTrue("not isZERO( a )", !a.isZERO());
227
228        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
229        L.add(a);
230
231        e = sredpar.leftNormalform(L, a);
232        assertTrue("isZERO( e )", e.isZERO());
233
234        assertTrue("not isZERO( b )", !b.isZERO());
235
236        L.add(b);
237        e = sredpar.leftNormalform(L, a);
238        assertTrue("isZERO( e ) some times", e.isZERO());
239    }
240
241
242    /**
243     * Test Weyl Rational reduction.
244     */
245    public void testWeylRatReduction() {
246        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
247
248        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
249        wl.generate(fac);
250
251        a = fac.random(kl, ll, el, q);
252        b = fac.random(kl, ll, el, q);
253
254        assertTrue("not isZERO( a )", !a.isZERO());
255
256        L.add(a);
257
258        e = sred.leftNormalform(L, a);
259        assertTrue("isZERO( e )", e.isZERO());
260
261        assertTrue("not isZERO( b )", !b.isZERO());
262
263        L.add(b);
264        e = sred.leftNormalform(L, a);
265        assertTrue("isZERO( e ) some times", e.isZERO());
266    }
267
268
269    /**
270     * Test Weyl Rational reduction parallel.
271     */
272    public void testWeylRatReductionPar() {
273        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
274
275        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
276        wl.generate(fac);
277
278        a = fac.random(kl, ll, el, q);
279        b = fac.random(kl, ll, el, q);
280
281        assertTrue("not isZERO( a )", !a.isZERO());
282
283        L.add(a);
284
285        e = sredpar.leftNormalform(L, a);
286        assertTrue("isZERO( e )", e.isZERO());
287
288        assertTrue("not isZERO( b )", !b.isZERO());
289
290        L.add(b);
291        e = sredpar.leftNormalform(L, a);
292        assertTrue("isZERO( e ) some times", e.isZERO());
293    }
294
295
296    /**
297     * Test Rat reduction recording.
298     */
299    public void testRatReductionRecording() {
300        List<GenSolvablePolynomial<BigRational>> row = null;
301
302        a = fac.random(kl, ll, el, q);
303        b = fac.random(kl, ll, el, q);
304        c = fac.random(kl, ll, el, q);
305        d = fac.random(kl, ll, el, q);
306
307        assertTrue("not isZERO( a )", !a.isZERO());
308
309        L = new ArrayList<GenSolvablePolynomial<BigRational>>();
310
311        L.add(a);
312        row = new ArrayList<GenSolvablePolynomial<BigRational>>(L.size());
313        for (int m = 0; m < L.size(); m++) {
314            row.add(null);
315        }
316        e = sred.leftNormalform(row, L, a);
317        assertTrue("isZERO( e )", e.isZERO());
318        assertTrue("not isZERO( b )", !b.isZERO());
319        assertTrue("is leftReduction ", sred.isLeftReductionNF(row, L, a, e));
320
321        L.add(b);
322        row = new ArrayList<GenSolvablePolynomial<BigRational>>(L.size());
323        for (int m = 0; m < L.size(); m++) {
324            row.add(null);
325        }
326        e = sred.leftNormalform(row, L, b);
327        assertTrue("is leftReduction ", sred.isLeftReductionNF(row, L, b, e));
328
329        L.add(c);
330        row = new ArrayList<GenSolvablePolynomial<BigRational>>(L.size());
331        for (int m = 0; m < L.size(); m++) {
332            row.add(null);
333        }
334        e = sred.leftNormalform(row, L, c);
335        assertTrue("is leftReduction ", sred.isLeftReductionNF(row, L, c, e));
336
337        L.add(d);
338        row = new ArrayList<GenSolvablePolynomial<BigRational>>(L.size());
339        for (int m = 0; m < L.size(); m++) {
340            row.add(null);
341        }
342        e = sred.leftNormalform(row, L, d);
343        assertTrue("is leftReduction ", sred.isLeftReductionNF(row, L, d, e));
344    }
345
346}