001 /*
002 * $Id: RGroebnerBaseSeqTest.java 3423 2010-12-24 10:56:50Z kredel $
003 */
004
005 package edu.jas.gbufd;
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.BigInteger;
021 import edu.jas.arith.BigRational;
022 import edu.jas.arith.ModInteger;
023 import edu.jas.arith.ModIntegerRing;
024 import edu.jas.arith.Product;
025 import edu.jas.arith.ProductRing;
026 import edu.jas.gb.GroebnerBase;
027 import edu.jas.poly.GenPolynomial;
028 import edu.jas.poly.GenPolynomialRing;
029 import edu.jas.poly.GenPolynomialTokenizer;
030 import edu.jas.poly.PolynomialList;
031 import edu.jas.poly.TermOrder;
032 import edu.jas.poly.PolyUtil;
033 import edu.jas.structure.RingFactory;
034
035
036 /**
037 * R-Groebner base sequential tests with JUnit.
038 * @author Heinz Kredel.
039 */
040
041 public class RGroebnerBaseSeqTest extends TestCase {
042
043
044 //private static final Logger logger = Logger.getLogger(RGroebnerBaseSeqTest.class);
045
046 /**
047 * main
048 */
049 public static void main(String[] args) {
050 BasicConfigurator.configure();
051 junit.textui.TestRunner.run(suite());
052 }
053
054
055 /**
056 * Constructs a <CODE>RGroebnerBaseSeqTest</CODE> object.
057 * @param name String.
058 */
059 public RGroebnerBaseSeqTest(String name) {
060 super(name);
061 }
062
063
064 /**
065 * suite.
066 */
067 public static Test suite() {
068 TestSuite suite = new TestSuite(RGroebnerBaseSeqTest.class);
069 return suite;
070 }
071
072
073 ProductRing<BigRational> pfac;
074
075
076 GenPolynomialRing<Product<BigRational>> fac;
077
078
079 List<GenPolynomial<Product<BigRational>>> L;
080
081
082 PolynomialList<Product<BigRational>> F;
083
084
085 List<GenPolynomial<Product<BigRational>>> G;
086
087
088 GroebnerBase<Product<BigRational>> bb;
089
090
091 GenPolynomial<Product<BigRational>> a;
092
093
094 GenPolynomial<Product<BigRational>> b;
095
096
097 GenPolynomial<Product<BigRational>> c;
098
099
100 GenPolynomial<Product<BigRational>> d;
101
102
103 GenPolynomial<Product<BigRational>> e;
104
105
106 int rl = 3; //4; //3;
107
108
109 int kl = 10;
110
111
112 int ll = 7;
113
114
115 int el = 3;
116
117
118 float q = 0.2f; //0.4f
119
120
121 @Override
122 protected void setUp() {
123 BigRational coeff = new BigRational(9);
124 pfac = new ProductRing<BigRational>(coeff, 4);
125 fac = new GenPolynomialRing<Product<BigRational>>(pfac, rl);
126 a = b = c = d = e = null;
127 bb = new RGroebnerBaseSeq<Product<BigRational>>();
128 }
129
130
131 @Override
132 protected void tearDown() {
133 a = b = c = d = e = null;
134 fac = null;
135 bb = null;
136 }
137
138
139 /**
140 * Test sequential GBase.
141 *
142 */
143 public void testSequentialGBase() {
144
145 L = new ArrayList<GenPolynomial<Product<BigRational>>>();
146
147 a = fac.random(kl, ll, el, q);
148 b = fac.random(kl, ll, el, q);
149 c = fac.random(kl, ll, el, q);
150 d = fac.random(kl, ll, el, q);
151 e = d; //fac.random(kl, ll, el, q );
152
153 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
154 return;
155 }
156
157 assertTrue("not isZERO( a )", !a.isZERO());
158 L.add(a);
159
160 L = bb.GB(L);
161 assertTrue("isGB( { a } )", bb.isGB(L));
162 //System.out.println("L = " + 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 //System.out.println("L = " + L );
171
172 assertTrue("not isZERO( c )", !c.isZERO());
173 L.add(c);
174
175 L = bb.GB(L);
176 assertTrue("isGB( { a, b, c } )", bb.isGB(L));
177 //System.out.println("L = " + L );
178
179 assertTrue("not isZERO( d )", !d.isZERO());
180 L.add(d);
181
182 L = bb.GB(L);
183 assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
184 //System.out.println("L = " + L );
185
186 assertTrue("not isZERO( e )", !e.isZERO());
187 L.add(e);
188
189 L = bb.GB(L);
190 assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
191 //System.out.println("L = " + L );
192 }
193
194
195 /**
196 * Test Trinks7 GBase.
197 *
198 */
199 @SuppressWarnings("unchecked")
200 public void testTrinks7() {
201 String exam = "Z(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
202 + "( 35 P + 40 Z + 25 T - 27 S ), "
203 + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
204 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
205 + "( 99 W - 11 B S + 3 B**2 ), " + "( 10000 B**2 + 6600 B + 2673 ) "
206 + ") ";
207 Reader source = new StringReader(exam);
208 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
209
210 PolynomialList<BigInteger> F = null;
211
212 try {
213 F = (PolynomialList<BigInteger>) parser.nextPolynomialSet();
214 } catch (ClassCastException e) {
215 fail("" + e);
216 } catch (IOException e) {
217 fail("" + e);
218 }
219 //System.out.println("F = " + F);
220 int rl = F.ring.nvar;
221 TermOrder to = F.ring.tord;
222 String[] vars = F.ring.getVars();
223 PolynomialList<Product<ModInteger>> trinks;
224
225 List<RingFactory<ModInteger>> colist;
226 colist = new ArrayList<RingFactory<ModInteger>>();
227
228 //colist.add( new ModIntegerRing(2) );
229 //colist.add( new ModIntegerRing(3) );
230 //colist.add( new ModIntegerRing(5) );
231 //colist.add( new ModIntegerRing(30) ); // now ok, was not possible
232 colist.add(new ModIntegerRing(19));
233 colist.add(new ModIntegerRing(23));
234 //colist.add( new ModIntegerRing((2<<30)-19) );
235 //System.out.println("colist = " + colist);
236
237 ProductRing<ModInteger> pfac;
238 pfac = new ProductRing<ModInteger>(colist);
239 //System.out.println("pfac = " + pfac);
240
241 GenPolynomialRing<Product<ModInteger>> fac;
242 fac = new GenPolynomialRing<Product<ModInteger>>(pfac, rl, to, vars);
243 //System.out.println("fac = " + fac);
244
245 List<GenPolynomial<Product<ModInteger>>> Fp;
246 Fp = PolyUtil.toProduct(fac, F.list);
247
248 List<GenPolynomial<Product<ModInteger>>> Fpp;
249 Fpp = new ArrayList<GenPolynomial<Product<ModInteger>>>();
250
251 for (GenPolynomial<Product<ModInteger>> a : Fp) {
252 Fpp.add(a.multiply(pfac.getAtomic(0)));
253 Fpp.add(a.multiply(pfac.getAtomic(1)));
254 //Fpp.add( a );
255 }
256 Fp = Fpp;
257
258 trinks = new PolynomialList<Product<ModInteger>>(fac, Fp);
259 //System.out.println("Fp = " + trinks);
260
261 GroebnerBase<Product<ModInteger>> bbr = new RGroebnerBaseSeq<Product<ModInteger>>();
262
263 List<GenPolynomial<Product<ModInteger>>> G;
264 G = bbr.GB(Fp);
265 //System.out.println("gb = " + G );
266
267 //assertEquals("#GB(Trinks7) == 6", 6, G.size() );
268 //System.out.println("Fp = " + trinks);
269 trinks = new PolynomialList<Product<ModInteger>>(fac, G);
270 //System.out.println("G = " + trinks);
271
272 assertTrue("isGB( GB(Trinks7) )", bbr.isGB(G));
273 }
274
275
276 }