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