001/*
002 * $Id: GBSigBasedTest.java 5591 2016-08-17 21:41:22Z kredel $
003 */
004
005package edu.jas.gb;
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;
019import org.apache.log4j.Logger;
020
021import edu.jas.arith.BigRational;
022// import edu.jas.kern.ComputerThreads;
023import edu.jas.poly.GenPolynomial;
024import edu.jas.poly.GenPolynomialRing;
025import edu.jas.poly.GenPolynomialTokenizer;
026import edu.jas.poly.PolynomialList;
027
028
029/**
030 * Groebner base sig based via jython tests with JUnit.
031 * @author Heinz Kredel.
032 */
033
034public class GBSigBasedTest extends TestCase {
035
036
037    private static final Logger logger = Logger.getLogger(GBSigBasedTest.class);
038
039
040    /**
041     * main
042     @param args not used
043     */
044    public static void main(String[] args) {
045        BasicConfigurator.configure();
046        junit.textui.TestRunner.run(suite());
047    }
048
049
050    /**
051     * Constructs a <CODE>GBSigBasedTest</CODE> object.
052     * @param name String.
053     */
054    public GBSigBasedTest(String name) {
055        super(name);
056    }
057
058
059    /**
060     * suite.
061     * @return test suite.
062     */
063    public static Test suite() {
064        TestSuite suite = new TestSuite(GBSigBasedTest.class);
065        return suite;
066    }
067
068
069    GenPolynomialRing<BigRational> fac;
070
071
072    List<GenPolynomial<BigRational>> L;
073
074
075    PolynomialList<BigRational> F;
076
077
078    List<GenPolynomial<BigRational>> G, Gn;
079
080
081    GroebnerBaseAbstract<BigRational> bb;
082
083
084    GroebnerBaseAbstract<BigRational> bbn;
085
086
087    GenPolynomial<BigRational> a, b, c, d, e;
088
089
090    int rl = 3; //4; //3; 
091
092
093    int kl = 5; //10;
094
095
096    int ll = 5; //7;
097
098
099    int el = 3;
100
101
102    float q = 0.2f; //0.4f
103
104
105    @Override
106    protected void setUp() {
107        BigRational coeff = new BigRational(9);
108        fac = new GenPolynomialRing<BigRational>(coeff, rl);
109        a = b = c = d = e = null;
110        //bb = new GBSigBased<BigRational>();
111        bb = new GBSigBased<BigRational>(GBSigBased.GBAlgo.ggv1); //sbgb);
112        //bbn = new GroebnerBaseSeq<BigRational>(new ReductionSeq<BigRational>(),new OrderedSyzPairlist<BigRational>());
113        bbn = new GroebnerBaseSeq<BigRational>();
114        //logger.info("using " + bb);
115    }
116
117
118    @Override
119    protected void tearDown() {
120        int s = bb.cancel();
121        logger.debug("canceled tasks: " + s);
122        //assertTrue("s >= 0 " + s, s >= 0);
123        a = b = c = d = e = null;
124        fac = null;
125        bb = null;
126        bbn = null;
127    }
128
129
130    /**
131     * Test GBase.
132     */
133    public void testGBase() {
134        L = new ArrayList<GenPolynomial<BigRational>>();
135
136        a = fac.random(kl, ll, el, q);
137        b = fac.random(kl, ll, el, q);
138        c = fac.random(kl, ll, el, q);
139        d = fac.random(kl, ll, el, q);
140        e = d; //fac.random(kl, ll, el, q );
141
142        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
143            return;
144        }
145
146        L.add(a);
147        L = bb.GB(L);
148        assertTrue("isGB( { a } )", bb.isGB(L));
149
150        L.add(b);
151        //System.out.println("L = " + L.size() );
152        L = bb.GB(L);
153        assertTrue("isGB( { a, b } )", bb.isGB(L));
154
155        L.add(c);
156        L = bb.GB(L);
157        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
158
159        L.add(d);
160        L = bb.GB(L);
161        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
162
163        L.add(e);
164        L = bb.GB(L);
165        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
166    }
167
168
169    /**
170     * Test Trinks7 GBase.
171     */
172    @SuppressWarnings({ "unchecked", "cast" })
173    public void testTrinks7GBase() {
174        //String exam = "Mod 32003 (B,S,T,Z,P,W) L " 
175        String exam = "Rat (B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
176                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
177                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
178                        + "( 99 W - 11 B S + 3 B**2 ) " + "( 10000 B**2 + 6600 B + 2673 ) " + ") ";
179        Reader source = new StringReader(exam);
180        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
181        try {
182            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
183        } catch (ClassCastException e) {
184            fail("" + e);
185        } catch (IOException e) {
186            fail("" + e);
187        }
188        //System.out.println("F = " + F);
189
190        long t;
191        t = System.currentTimeMillis();
192        Gn = bbn.GB(F.list);
193        t = System.currentTimeMillis() - t;
194        System.out.println("bbn took = " + t);
195        G = bb.GB(F.list);
196        G = bb.GB(F.list);
197        assertEquals("#GB(Trinks7) == 6", 6, G.size());
198        assertTrue("isGB( GB(Trinks7) ) " + G, bb.isGB(G));
199        assertEquals("#G == #Gn", G.size(), Gn.size());
200        assertTrue("t >= 0 ", t >= 0);
201        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
202        //System.out.println("G = " + trinks);
203        //         t = System.currentTimeMillis();
204        //         Gn = bbn.GB(F.list);
205        //         t = System.currentTimeMillis() - t;
206        //         System.out.println("bbn took = " + t);
207        //         G = bb.GB(F.list);
208        //         assertEquals("#G == #Gn", G.size(), Gn.size());
209        //         t = System.currentTimeMillis();
210        //         Gn = bbn.GB(F.list);
211        //         t = System.currentTimeMillis() - t;
212        //         System.out.println("bbn took = " + t);
213        //         G = bb.GB(F.list);
214        //         assertEquals("#G == #Gn", G.size(), Gn.size());
215        //         assertEquals("#GB(Trinks7) == 6", 6, G.size());
216    }
217
218}