001/*
002 * $Id: SyzygyTest.java 5272 2015-07-27 20:54:14Z kredel $
003 */
004
005package edu.jas.gbufd;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.ArrayList;
012import java.util.List;
013
014import junit.framework.Test;
015import junit.framework.TestCase;
016import junit.framework.TestSuite;
017
018import org.apache.log4j.BasicConfigurator;
019
020import edu.jas.arith.BigRational;
021import edu.jas.gb.GroebnerBase;
022import edu.jas.gb.GroebnerBaseSeq;
023import edu.jas.gbufd.GBFactory;
024import edu.jas.gbufd.Syzygy;
025import edu.jas.gbufd.SyzygySeq;
026import edu.jas.poly.GenPolynomial;
027import edu.jas.poly.GenPolynomialRing;
028import edu.jas.poly.GenPolynomialTokenizer;
029import edu.jas.poly.ModuleList;
030import edu.jas.poly.PolynomialList;
031import edu.jas.poly.TermOrder;
032
033
034/**
035 * Syzygy tests with JUnit.
036 * @author Heinz Kredel.
037 */
038
039public class SyzygyTest extends TestCase {
040
041
042    //private static final Logger logger = Logger.getLogger(SyzygyTest.class);
043
044    /**
045     * main.
046     */
047    public static void main(String[] args) {
048        BasicConfigurator.configure();
049        junit.textui.TestRunner.run(suite());
050    }
051
052
053    /**
054     * Constructs a <CODE>SyzygyTest</CODE> object.
055     * @param name String.
056     */
057    public SyzygyTest(String name) {
058        super(name);
059    }
060
061
062    /**
063 */
064    public static Test suite() {
065        TestSuite suite = new TestSuite(SyzygyTest.class);
066        return suite;
067    }
068
069
070    GenPolynomialRing<BigRational> fac;
071
072
073    PolynomialList<BigRational> F;
074
075
076    List<GenPolynomial<BigRational>> G;
077
078
079    GroebnerBase<BigRational> bb;
080
081
082    GroebnerBase<BigRational> mbb;
083
084
085    Syzygy<BigRational> sz;
086
087
088    GenPolynomial<BigRational> a, b, c, d, e;
089
090
091    GenPolynomial<BigRational> zero;
092
093
094    GenPolynomial<BigRational> one;
095
096
097    TermOrder tord;
098
099
100    List<GenPolynomial<BigRational>> L;
101
102
103    List<List<GenPolynomial<BigRational>>> K;
104
105
106    List<GenPolynomial<BigRational>> V;
107
108
109    List<List<GenPolynomial<BigRational>>> W;
110
111
112    ModuleList<BigRational> M;
113
114
115    ModuleList<BigRational> N;
116
117
118    ModuleList<BigRational> Z;
119
120
121    int rl = 3; //4; //3; 
122
123
124    int kl = 3; //7;
125
126
127    int ll = 7; //9;
128
129
130    int el = 2;
131
132
133    float q = 0.3f; //0.4f
134
135
136    @Override
137    protected void setUp() {
138        BigRational coeff = new BigRational(9);
139        tord = new TermOrder();
140        fac = new GenPolynomialRing<BigRational>(coeff, rl, tord);
141
142        bb = GBFactory.getImplementation(coeff);
143        mbb = new GroebnerBaseSeq<BigRational>(); //coeff);
144        sz = new SyzygySeq<BigRational>(coeff);
145
146        a = b = c = d = e = null;
147        L = null;
148        K = null;
149        V = null;
150
151        do {
152            a = fac.random(kl, ll, el, q);
153            b = fac.random(kl, ll, el, q);
154            c = fac.random(kl, ll, el, q);
155            d = fac.random(kl, ll, el, q);
156        } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO());
157        e = d; //fac.random(kl, ll, el, q );
158
159        one = fac.getONE();
160        zero = fac.getZERO();
161    }
162
163
164    @Override
165    protected void tearDown() {
166        a = b = c = d = e = null;
167        L = null;
168        K = null;
169        V = null;
170        fac = null;
171        tord = null;
172        bb = null;
173        mbb = null;
174        sz = null;
175    }
176
177
178    /**
179     * Test sequential Syzygy.
180     */
181    public void testSequentialSyzygy() {
182        L = new ArrayList<GenPolynomial<BigRational>>();
183
184        assertTrue("not isZERO( a )", !a.isZERO());
185        L.add(a);
186        assertTrue("isGB( { a } )", bb.isGB(L));
187        K = sz.zeroRelations(L);
188        assertTrue("is ZR( { a } )", sz.isZeroRelation(K, L));
189
190        assertTrue("not isZERO( b )", !b.isZERO());
191        L.add(b);
192        L = bb.GB(L);
193        assertTrue("isGB( { a, b } )", bb.isGB(L));
194        //System.out.println("\nL = " + L );
195        K = sz.zeroRelations(L);
196        //System.out.println("\nN = " + N );
197        assertTrue("is ZR( { a, b } )", sz.isZeroRelation(K, L));
198
199        assertTrue("not isZERO( c )", !c.isZERO());
200        L.add(c);
201        L = bb.GB(L);
202        //System.out.println("\nL = " + L );
203        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
204        K = sz.zeroRelations(L);
205        //System.out.println("\nN = " + N );
206        assertTrue("is ZR( { a, b, c } )", sz.isZeroRelation(K, L));
207
208        assertTrue("not isZERO( d )", !d.isZERO());
209        L.add(d);
210        L = bb.GB(L);
211        //System.out.println("\nL = " + L );
212        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
213        K = sz.zeroRelations(L);
214        //System.out.println("\nN = " + N );
215        assertTrue("is ZR( { a, b, c, d } )", sz.isZeroRelation(K, L));
216
217        //System.out.println("N = " + N );
218        /*
219        */
220    }
221
222
223    /**
224     * Test sequential module Syzygy.
225     */
226    public void testSequentialModSyzygy() {
227        W = new ArrayList<List<GenPolynomial<BigRational>>>();
228
229        assertTrue("not isZERO( a )", !a.isZERO());
230        V = new ArrayList<GenPolynomial<BigRational>>();
231        V.add(a);
232        V.add(zero);
233        V.add(one);
234        W.add(V);
235        M = new ModuleList<BigRational>(fac, W);
236        assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M));
237
238        N = mbb.GB(M);
239        assertTrue("isGB( { (a,0,1) } )", mbb.isGB(N));
240
241        Z = sz.zeroRelations(N);
242        //System.out.println("Z = " + Z);
243        assertTrue("is ZR( { a) } )", sz.isZeroRelation(Z, N));
244
245        assertTrue("not isZERO( b )", !b.isZERO());
246        V = new ArrayList<GenPolynomial<BigRational>>();
247        V.add(b);
248        V.add(one);
249        V.add(zero);
250        W.add(V);
251        M = new ModuleList<BigRational>(fac, W);
252        //System.out.println("W = " + W.size() );
253
254        N = mbb.GB(M);
255        assertTrue("isGB( { a, b } )", mbb.isGB(N));
256
257        Z = sz.zeroRelations(N);
258        //System.out.println("Z = " + Z);
259        assertTrue("is ZR( { a, b } )", sz.isZeroRelation(Z, N));
260
261        assertTrue("not isZERO( c )", !c.isZERO());
262        V = new ArrayList<GenPolynomial<BigRational>>();
263        V.add(c);
264        V.add(one);
265        V.add(zero);
266        W.add(V);
267        M = new ModuleList<BigRational>(fac, W);
268        //System.out.println("W = " + W.size() );
269
270        N = mbb.GB(M);
271        //System.out.println("GB(M) = " + N);
272        assertTrue("isGB( { a,b,c) } )", mbb.isGB(N));
273
274        Z = sz.zeroRelations(N);
275        //System.out.println("Z = " + Z);
276        //boolean b = Syzygy.isZeroRelation(Z,N);
277        //System.out.println("boolean = " + b);
278        assertTrue("is ZR( { a,b,c } )", sz.isZeroRelation(Z, N));
279
280    }
281
282
283    /**
284     * Test sequential arbitrary base Syzygy.
285     */
286    public void testSequentialArbitrarySyzygy() {
287        L = new ArrayList<GenPolynomial<BigRational>>();
288
289        assertTrue("not isZERO( a )", !a.isZERO());
290        L.add(a);
291        assertTrue("isGB( { a } )", bb.isGB(L));
292        K = sz.zeroRelationsArbitrary(L);
293        assertTrue("is ZR( { a } )", sz.isZeroRelation(K, L));
294
295        assertTrue("not isZERO( b )", !b.isZERO());
296        L.add(b);
297        // L = bb.GB(L);
298        // assertTrue("isGB( { a, b } )", bb.isGB(L) );
299        //System.out.println("\nL = " + L );
300        K = sz.zeroRelationsArbitrary(L);
301        //System.out.println("\nN = " + N );
302        assertTrue("is ZR( { a, b } )", sz.isZeroRelation(K, L));
303
304        assertTrue("not isZERO( c )", !c.isZERO());
305        L.add(c);
306        //L = bb.GB(L);
307        //System.out.println("\nL = " + L );
308        //assertTrue("isGB( { a, b, c } )", bb.isGB(L) );
309        K = sz.zeroRelationsArbitrary(L);
310        //System.out.println("\nN = " + N );
311        assertTrue("is ZR( { a, b, c } )", sz.isZeroRelation(K, L));
312
313        assertTrue("not isZERO( d )", !d.isZERO());
314        L.add(d);
315        //L = bb.GB(L);
316        //System.out.println("\nL = " + L );
317        //assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) );
318        K = sz.zeroRelationsArbitrary(L);
319        //System.out.println("\nN = " + N );
320        assertTrue("is ZR( { a, b, c, d } )", sz.isZeroRelation(K, L));
321
322        //System.out.println("N = " + N );
323
324    }
325
326
327    /**
328     * Test sequential arbitrary base Syzygy, ex CLO 2, p 214 ff.
329     */
330    @SuppressWarnings("cast")
331    public void testSequentialArbitrarySyzygyCLO() {
332
333        PolynomialList<BigRational> F = null;
334
335        String exam = "(x,y) G " + "( " + "( x y + x ), " + "( y^2 + 1 ) " + ") ";
336        Reader source = new StringReader(exam);
337        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
338        try {
339            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
340        } catch (ClassCastException e) {
341            fail("" + e);
342        } catch (IOException e) {
343            fail("" + e);
344        }
345        //System.out.println("F = " + F);
346
347        L = F.list;
348        K = sz.zeroRelationsArbitrary(L);
349        assertTrue("is ZR( { a, b } )", sz.isZeroRelation(K, L));
350    }
351
352
353    /**
354     * Test sequential arbitrary module Syzygy.
355     */
356    public void testSequentialArbitraryModSyzygy() {
357        W = new ArrayList<List<GenPolynomial<BigRational>>>();
358
359        assertTrue("not isZERO( a )", !a.isZERO());
360        V = new ArrayList<GenPolynomial<BigRational>>();
361        V.add(a);
362        V.add(zero);
363        V.add(one);
364        W.add(V);
365        M = new ModuleList<BigRational>(fac, W);
366        assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M));
367
368        Z = sz.zeroRelationsArbitrary(M);
369        //System.out.println("Z = " + Z);
370        assertTrue("is ZR( { a) } )", sz.isZeroRelation(Z, M));
371
372        assertTrue("not isZERO( b )", !b.isZERO());
373        V = new ArrayList<GenPolynomial<BigRational>>();
374        V.add(b);
375        V.add(one);
376        V.add(zero);
377        W.add(V);
378        M = new ModuleList<BigRational>(fac, W);
379        //System.out.println("W = " + W.size() );
380
381        Z = sz.zeroRelationsArbitrary(M);
382        //System.out.println("Z = " + Z);
383        assertTrue("is ZR( { a, b } )", sz.isZeroRelation(Z, M));
384
385        assertTrue("not isZERO( c )", !c.isZERO());
386        V = new ArrayList<GenPolynomial<BigRational>>();
387        V.add(c);
388        V.add(one);
389        V.add(zero);
390        W.add(V);
391        M = new ModuleList<BigRational>(fac, W);
392        //System.out.println("W = " + W.size() );
393
394        Z = sz.zeroRelationsArbitrary(M);
395        //System.out.println("Z = " + Z);
396        //boolean b = Syzygy.isZeroRelation(Z,N);
397        //System.out.println("boolean = " + b);
398        assertTrue("is ZR( { a,b,c } )", sz.isZeroRelation(Z, M));
399
400    }
401
402}