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