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