001/*
002 * $Id: WordIdealTest.java 5048 2014-12-30 17:45:01Z 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 org.apache.log4j.BasicConfigurator;
016
017import edu.jas.arith.BigRational;
018import edu.jas.gb.WordGroebnerBase;
019import edu.jas.gb.WordGroebnerBaseSeq;
020// import edu.jas.kern.ComputerThreads;
021import edu.jas.poly.GenWordPolynomial;
022import edu.jas.poly.GenWordPolynomialRing;
023import edu.jas.poly.PolynomialList;
024import edu.jas.poly.TermOrder;
025
026
027/**
028 * WordIdeal tests with JUnit.
029 * @author Heinz Kredel.
030 */
031public class WordIdealTest extends TestCase {
032
033
034    //private static final Logger logger = Logger.getLogger(WordIdealTest.class);
035
036
037    /**
038     * main
039     */
040    public static void main(String[] args) {
041        BasicConfigurator.configure();
042        junit.textui.TestRunner.run(suite());
043    }
044
045
046    /**
047     * Constructs a <CODE>WordIdealTest</CODE> object.
048     * @param name String.
049     */
050    public WordIdealTest(String name) {
051        super(name);
052    }
053
054
055    /**
056     * suite.
057     */
058    public static Test suite() {
059        TestSuite suite = new TestSuite(WordIdealTest.class);
060        return suite;
061    }
062
063
064    TermOrder to;
065
066
067    GenWordPolynomialRing<BigRational> fac;
068
069
070    List<GenWordPolynomial<BigRational>> L, M;
071
072
073    PolynomialList<BigRational> F;
074
075
076    List<GenWordPolynomial<BigRational>> G;
077
078
079    WordGroebnerBase<BigRational> bb;
080
081
082    GenWordPolynomial<BigRational> a, b, c, d, e;
083
084
085    int kl = 3; //10
086
087
088    int ll = 5; //7
089
090
091    int el = 2;
092
093
094    @Override
095    protected void setUp() {
096        BigRational coeff = new BigRational(17, 1);
097        to = new TermOrder( /*TermOrder.INVLEX*/);
098        String[] vars = new String[] { "x", "y", "z" };
099        //WordFactory wf = new WordFactory(vars);
100        fac = new GenWordPolynomialRing<BigRational>(coeff, vars);
101        bb = new WordGroebnerBaseSeq<BigRational>();
102        //bb = GBFactory.getImplementation(coeff);
103        a = b = c = d = e = null;
104    }
105
106
107    @Override
108    protected void tearDown() {
109        a = b = c = d = e = null;
110        fac = null;
111        bb = null;
112        //ComputerThreads.terminate();
113    }
114
115
116    /**
117     * Test Ideal sum.
118     */
119    public void testIdealSum() {
120        WordIdeal<BigRational> I, J, K;
121        L = new ArrayList<GenWordPolynomial<BigRational>>();
122
123        a = fac.random(kl, ll, el);
124        b = fac.random(kl, ll, el);
125        c = fac.random(kl, ll, el);
126        d = fac.random(kl, ll, el);
127        e = d; //fac.random(kl, ll, el);
128
129        //System.out.println("a = " + a);
130        //System.out.println("b = " + b);
131        //System.out.println("c = " + c);
132        //System.out.println("d = " + d);
133
134        L.add(a);
135        //System.out.println("L = " + L.size() );
136
137        I = new WordIdeal<BigRational>(fac, L, true);
138        assertTrue("isGB( I )", I.isGB());
139
140        I = new WordIdeal<BigRational>(fac, L, false);
141        assertTrue("isGB( I )", I.isGB());
142
143        L = bb.GB(L);
144        assertTrue("isGB( { a } )", bb.isGB(L));
145
146        I = new WordIdeal<BigRational>(fac, L, true);
147        assertTrue("isGB( I )", I.isGB());
148
149        I = new WordIdeal<BigRational>(fac, L, false);
150        assertTrue("isGB( I )", I.isGB());
151
152        //if (!true) { 
153        //    return;
154        //}
155
156        //assertTrue("not isZERO( b )", !b.isZERO());
157        L.add(b);
158        //System.out.println("L = " + L.size() );
159
160        I = new WordIdeal<BigRational>(fac, L, false);
161        assertTrue("not isZERO( I )", !I.isZERO());
162        //assertTrue("not isONE( I )", !I.isONE() );
163        //assertTrue("not isGB( I )", !I.isGB() );
164
165        L = bb.GB(L);
166        assertTrue("isGB( { a, b } )", bb.isGB(L));
167
168        I = new WordIdeal<BigRational>(fac, L, true);
169        assertTrue("not isZERO( I )", !I.isZERO());
170        // assertTrue("not isONE( I )", !I.isONE() );
171        assertTrue("isGB( I )", I.isGB());
172
173        J = I;
174        K = J.sum(I);
175        //assertTrue("not isZERO( K )", !K.isZERO());
176        assertTrue("isGB( K )", K.isGB());
177        assertTrue("equals( K, I )", K.equals(I));
178
179        L = new ArrayList<GenWordPolynomial<BigRational>>();
180
181        L.add(c);
182        assertTrue("isGB( { c } )", bb.isGB(L));
183
184        J = new WordIdeal<BigRational>(fac, L, true);
185        K = J.sum(I);
186        assertTrue("isGB( K )", K.isGB());
187        assertTrue("K contains(I)", K.contains(I));
188        assertTrue("K contains(J)", K.contains(J));
189
190        L = new ArrayList<GenWordPolynomial<BigRational>>();
191        L.add(d);
192
193        assertTrue("isGB( { d } )", bb.isGB(L));
194        J = new WordIdeal<BigRational>(fac, L, true);
195        I = K;
196        K = J.sum(I);
197        assertTrue("isGB( K )", K.isGB());
198        assertTrue("K contains(I)", K.contains(I));
199        assertTrue("K contains(J)", K.contains(J));
200
201        L = new ArrayList<GenWordPolynomial<BigRational>>();
202        L.add(e);
203
204        assertTrue("isGB( { e } )", bb.isGB(L));
205        J = new WordIdeal<BigRational>(fac, L, true);
206        I = K;
207        K = J.sum(I);
208        assertTrue("isGB( K )", K.isGB());
209        assertTrue("equals( K, I )", K.equals(I));
210        assertTrue("K contains(J)", K.contains(I));
211        assertTrue("I contains(K)", I.contains(K));
212    }
213
214
215    /**
216     * Test WordIdeal product. Sometimes non-terminating.
217     */
218    public void testWordIdealProduct() {
219        WordIdeal<BigRational> I, J, K, H, G;
220        a = fac.random(kl, ll, el);
221        b = fac.random(kl, ll, el);
222        c = fac.random(kl, ll, el);
223        d = c; //fac.random(kl, ll, el);
224        e = d; //fac.random(kl, ll, el);
225
226        //System.out.println("a = " + a);
227        //System.out.println("b = " + b);
228        //System.out.println("c = " + c);
229        //System.out.println("d = " + d);
230
231        L = new ArrayList<GenWordPolynomial<BigRational>>();
232        L.add(a);
233
234        I = new WordIdeal<BigRational>(fac, L, false);
235        assertTrue("not isONE( I )", !I.isONE() || a.isConstant());
236        assertTrue("isGB( I )", I.isGB());
237
238        L = new ArrayList<GenWordPolynomial<BigRational>>();
239        L.add(b);
240
241        J = new WordIdeal<BigRational>(fac, L, false);
242        assertTrue("not isONE( J )", !J.isONE() || a.isConstant() || b.isConstant());
243        assertTrue("isGB( J )", J.isGB());
244
245        K = I.product(J);
246        //System.out.println("I = " + I);
247        //System.out.println("J = " + J);
248        //System.out.println("K = " + K);
249        H = J.product(I);
250        //System.out.println("H = " + H);
251        G = K.sum(H);
252        //System.out.println("G = " + G);
253        //assertTrue("not isZERO( K )", !K.isZERO());
254        assertTrue("isGB( K )", K.isGB());
255        assertTrue("isGB( H )", H.isGB());
256        assertTrue("isGB( G )", G.isGB());
257        //non-com assertTrue("I contains(K)", I.contains(K));
258        assertTrue("J contains(K)", J.contains(K));
259
260        //if (true) { // TODO
261        //    return;
262        //}
263
264        /*
265        H = I.intersect(J);
266        assertTrue("not isZERO( H )", !H.isZERO());
267        assertTrue("isGB( H )", H.isGB());
268        assertTrue("I contains(H)", I.contains(H));
269        assertTrue("J contains(H)", J.contains(H));
270        //non-com assertTrue("H contains(K)", H.contains(K));
271        */
272
273        /*
274        L = new ArrayList<GenWordPolynomial<BigRational>>();
275        L.add(a);
276        L.add(c);
277        L = bb.GB(L);
278
279        I = new WordIdeal<BigRational>(fac, L, true);
280        //assertTrue("not isZERO( I )", !I.isZERO());
281        //assertTrue("not isONE( I )", !I.isONE() );
282        assertTrue("isGB( I )", I.isGB());
283
284        K = I.product(J);
285        //System.out.println("I = " + I);
286        //System.out.println("J = " + J);
287        //System.out.println("K = " + K);
288        //assertTrue("not isZERO( K )", !K.isZERO());
289        assertTrue("isGB( K )", K.isGB());
290        //non-com assertTrue("I contains(K)", I.contains(K));
291        assertTrue("J contains(K)", J.contains(K));
292        */
293    }
294
295
296    /**
297     * Test WordIdeal common zeros.
298     */
299    @SuppressWarnings("cast")
300    public void testWordIdealCommonZeros() {
301        WordIdeal<BigRational> I, J;
302        L = new ArrayList<GenWordPolynomial<BigRational>>();
303
304        I = new WordIdeal<BigRational>(fac, L, true);
305        assertEquals("commonZeroTest( I )", I.commonZeroTest(), 1);
306
307        a = fac.getZERO();
308        L.add(a);
309        I = new WordIdeal<BigRational>(fac, L, true);
310        assertEquals("commonZeroTest( I )", I.commonZeroTest(), 1);
311
312        b = fac.getONE();
313        L.add(b);
314        I = new WordIdeal<BigRational>(fac, L, true);
315        assertEquals("commonZeroTest( I )", I.commonZeroTest(), -1);
316
317        L = new ArrayList<GenWordPolynomial<BigRational>>();
318        a = fac.random(kl, ll, el);
319        if (!a.isZERO() && !a.isConstant()) {
320            L.add(a);
321            I = new WordIdeal<BigRational>(fac, L, true);
322            assertEquals("commonZeroTest( I )", I.commonZeroTest(), 1);
323        }
324
325        L = (List<GenWordPolynomial<BigRational>>) fac.univariateList();
326        //System.out.println("L = " + L);
327        I = new WordIdeal<BigRational>(fac, L, true);
328        assertEquals("commonZeroTest( I )", I.commonZeroTest(), 0);
329
330        J = I.product(I);
331        //System.out.println("J = " + J);
332        assertEquals("commonZeroTest( J )", J.commonZeroTest(), 0);
333
334        L.remove(0);
335        I = new WordIdeal<BigRational>(fac, L, true);
336        assertEquals("commonZeroTest( I )", I.commonZeroTest(), 1);
337    }
338
339}