001/* 002 * $Id: GroebnerBaseSeqRationalTest.java 4290 2012-11-04 14:47:45Z kredel $ 003 */ 004 005package edu.jas.gbufd; 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.gb.GroebnerBaseAbstract; 023import edu.jas.gb.GroebnerBaseSeq; 024import edu.jas.poly.GenPolynomial; 025import edu.jas.poly.GenPolynomialRing; 026import edu.jas.poly.GenPolynomialTokenizer; 027import edu.jas.poly.PolynomialList; 028 029 030/** 031 * Groebner base sequential rational fraction free tests with JUnit. 032 * @author Heinz Kredel. 033 */ 034 035public class GroebnerBaseSeqRationalTest extends TestCase { 036 037 038 private static final Logger logger = Logger.getLogger(GroebnerBaseSeqRationalTest.class); 039 040 041 /** 042 * main 043 */ 044 public static void main(String[] args) { 045 BasicConfigurator.configure(); 046 junit.textui.TestRunner.run(suite()); 047 } 048 049 050 /** 051 * Constructs a <CODE>GroebnerBaseSeqRationalTest</CODE> object. 052 * @param name String. 053 */ 054 public GroebnerBaseSeqRationalTest(String name) { 055 super(name); 056 } 057 058 059 /** 060 * suite. 061 */ 062 public static Test suite() { 063 TestSuite suite = new TestSuite(GroebnerBaseSeqRationalTest.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 = 4; //4; //3; 099 100 101 int kl = 7; // 10 102 103 104 int ll = 7; 105 106 107 int el = 3; // 4 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 bb = new GroebnerBaseRational<BigRational>(); 119 } 120 121 122 @Override 123 protected void tearDown() { 124 a = b = c = d = e = null; 125 fac = null; 126 bb = null; 127 } 128 129 130 /** 131 * Test sequential GBase. 132 */ 133 public void testSequentialGBase() { 134 L = new ArrayList<GenPolynomial<BigRational>>(); 135 136 a = fac.random(kl, ll, el, q); 137 b = fac.random(kl, ll, el, q); 138 c = fac.random(kl, ll, el, q); 139 d = fac.random(kl, ll, el, q); 140 e = d; //fac.random(kl, ll, el, q ); 141 142 if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) { 143 return; 144 } 145 146 assertTrue("not isZERO( a )", !a.isZERO()); 147 L.add(a); 148 149 L = bb.GB(L); 150 assertTrue("isGB( { a } )", bb.isGB(L)); 151 assertTrue("isMinimalGB( { a } )", bb.isMinimalGB(L)); 152 153 assertTrue("not isZERO( b )", !b.isZERO()); 154 L.add(b); 155 //System.out.println("L = " + L.size() ); 156 157 L = bb.GB(L); 158 assertTrue("isGB( { a, b } )", bb.isGB(L)); 159 assertTrue("isMinimalGB( { a, b } )", bb.isMinimalGB(L)); 160 161 assertTrue("not isZERO( c )", !c.isZERO()); 162 L.add(c); 163 164 L = bb.GB(L); 165 assertTrue("isGB( { a, b, c } )", bb.isGB(L)); 166 assertTrue("isMinimalGB( { a, b, c } )", bb.isMinimalGB(L)); 167 168 assertTrue("not isZERO( d )", !d.isZERO()); 169 L.add(d); 170 171 L = bb.GB(L); 172 assertTrue("isGB( { a, b, c, d } )", bb.isGB(L)); 173 assertTrue("isMinimalGB( { a, b, c, d } )", bb.isMinimalGB(L)); 174 175 assertTrue("not isZERO( e )", !e.isZERO()); 176 L.add(e); 177 178 L = bb.GB(L); 179 assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L)); 180 assertTrue("isMinimalGB( { a, b, c, d, e } )", bb.isMinimalGB(L)); 181 } 182 183 184 /** 185 * Test Trinks7 GBase. 186 */ 187 @SuppressWarnings("unchecked") 188 public void testTrinks7GBase() { 189 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 190 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 191 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 192 + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") "; 193 String exam2 = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )" 194 + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )"; 195 196 Reader source = new StringReader(exam); 197 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 198 try { 199 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 200 } catch (ClassCastException e) { 201 fail("" + e); 202 } catch (IOException e) { 203 fail("" + e); 204 } 205 //System.out.println("F = " + F); 206 207 G = bb.GB(F.list); 208 assertTrue("isGB( GB(Trinks7) )", bb.isGB(G)); 209 assertTrue("isMinimalGB( GB(Trinks7) )", bb.isMinimalGB(G)); 210 assertEquals("#GB(Trinks7) == 6", 6, G.size()); 211 //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G); 212 //System.out.println("G = " + trinks); 213 } 214 215 216 /** 217 * Test Trinks7 compare GBase. 218 */ 219 @SuppressWarnings("unchecked") 220 public void testTrinks7CompareGBase() { 221 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 222 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 223 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 224 + "( 99 W - 11 B S + 3 B**2 ), " 225 //+ "( B**2 + 33/50 B + 2673/10000 ) " 226 + ") "; 227 228 Reader source = new StringReader(exam); 229 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 230 try { 231 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 232 } catch (ClassCastException e) { 233 fail("" + e); 234 } catch (IOException e) { 235 fail("" + e); 236 } 237 //System.out.println("F = " + F); 238 239 long i = System.currentTimeMillis(); 240 G = bb.GB(F.list); 241 i = System.currentTimeMillis() - i; 242 assertTrue("isGB( GB(Trinks7) )", bb.isGB(G)); 243 assertTrue("isMinimalGB( GB(Trinks7) )", bb.isMinimalGB(G)); 244 assertEquals("#GB(Trinks7) == 6", 6, G.size()); 245 246 GroebnerBaseAbstract<BigRational> bbr = new GroebnerBaseSeq<BigRational>(); 247 List<GenPolynomial<BigRational>> Gr; 248 long r = System.currentTimeMillis(); 249 Gr = bbr.GB(F.list); 250 r = System.currentTimeMillis() - r; 251 assertTrue("isGB( GB(Trinks7) )", bbr.isGB(Gr)); 252 assertTrue("isMinimalGB( GB(Trinks7) )", bbr.isMinimalGB(Gr)); 253 assertEquals("#GB(Trinks7) == 6", 6, Gr.size()); 254 255 if (logger.isInfoEnabled()) { 256 logger.info("i = " + i + ", r = " + r); 257 } 258 assertEquals("GB_r == GB_i", G, Gr); 259 260 //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G); 261 //System.out.println("G = " + trinks); 262 } 263 264}