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