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