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