001/*
002 * $Id: GBProxyTest.java 5104 2015-02-07 13:12:43Z 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 proxy of sequential and parallel 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, b, c, d, e;
084
085
086    int rl = 3; //4; //3; 
087
088
089    int kl = 7;
090
091
092    int ll = 7;
093
094
095    int el = 3;
096
097
098    float q = 0.2f; //0.4f
099
100
101    @Override
102    protected void setUp() {
103        BigRational coeff = new BigRational(9);
104        fac = new GenPolynomialRing<BigRational>(coeff, rl);
105        a = b = c = d = e = null;
106        GroebnerBaseAbstract<BigRational> bbs = new GroebnerBaseSeq<BigRational>();
107        //GroebnerBaseAbstract<BigRational> bbs = new GroebnerBaseSeqPairSeq<BigRational>();
108        int nt = ComputerThreads.N_CPUS - 1;
109        //System.out.println("nt = " + nt);
110        GroebnerBaseAbstract<BigRational> bbp = new GroebnerBaseParallel<BigRational>(nt);
111        //GroebnerBaseAbstract<BigRational> bbp = new GroebnerBaseSeqPairParallel<BigRational>(nt);
112        bb = new GBProxy<BigRational>(bbs, bbp);
113    }
114
115
116    @Override
117    protected void tearDown() {
118        int s = bb.cancel();
119        logger.info("canceled tasks: " + s);
120        //assertTrue("s >= 0 " + s, s >= 0);
121        bb.terminate();
122        a = b = c = d = e = null;
123        fac = null;
124        bb = null;
125    }
126
127
128    /**
129     * Test GBase.
130     */
131    public void testGBase() {
132        L = new ArrayList<GenPolynomial<BigRational>>();
133
134        a = fac.random(kl, ll, el, q);
135        b = fac.random(kl, ll, el, q);
136        c = fac.random(kl, ll, el, q);
137        d = fac.random(kl, ll, el, q);
138        e = d; //fac.random(kl, ll, el, q );
139
140
141        L.add(a);
142        L = bb.GB(L);
143        assertTrue("isGB( { a } )", bb.isGB(L));
144
145        L.add(b);
146        //System.out.println("L = " + L.size() );
147        L = bb.GB(L);
148        assertTrue("isGB( { a, b } )", bb.isGB(L));
149
150        L.add(c);
151        L = bb.GB(L);
152        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
153
154        L.add(d);
155        L = bb.GB(L);
156        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
157
158        L.add(e);
159        L = bb.GB(L);
160        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
161    }
162
163
164    /**
165     * Test Trinks7 GBase.
166     */
167    @SuppressWarnings({ "cast", "unchecked" })
168    public void testTrinks7GBase() {
169        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
170                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
171                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
172                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
173        Reader source = new StringReader(exam);
174        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
175        try {
176            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
177        } catch (ClassCastException e) {
178            fail("" + e);
179        } catch (IOException e) {
180            fail("" + e);
181        }
182        //System.out.println("F = " + F);
183
184        G = bb.GB(F.list);
185        assertEquals("#GB(Trinks7) == 6", 6, G.size());
186        assertTrue("isGB( GB(Trinks7) ) " + G, bb.isGB(G));
187        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
188        //System.out.println("G = " + trinks);
189    }
190
191}