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