001/* 002 * $Id: GroebnerBaseWalkTest.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.List; 012 013 014import edu.jas.arith.BigRational; 015import edu.jas.gb.GroebnerBaseAbstract; 016import edu.jas.poly.GenPolynomial; 017import edu.jas.poly.GenPolynomialTokenizer; 018import edu.jas.poly.PolynomialList; 019import edu.jas.poly.TermOrderByName; 020 021import junit.framework.Test; 022import junit.framework.TestCase; 023import junit.framework.TestSuite; 024 025 026/** 027 * Groebner base Groebner Walk tests with JUnit. 028 * @author Heinz Kredel 029 */ 030 031public class GroebnerBaseWalkTest extends TestCase { 032 033 034 035 036 /** 037 * main 038 */ 039 public static void main(String[] args) { 040 junit.textui.TestRunner.run(suite()); 041 } 042 043 044 /** 045 * Constructs a <CODE>GroebnerBaseWalkTest</CODE> object. 046 * @param name String. 047 */ 048 public GroebnerBaseWalkTest(String name) { 049 super(name); 050 } 051 052 053 /** 054 * suite. 055 */ 056 public static Test suite() { 057 TestSuite suite = new TestSuite(GroebnerBaseWalkTest.class); 058 return suite; 059 } 060 061 062 List<GenPolynomial<BigRational>> L, Lp; 063 064 065 PolynomialList<BigRational> F; 066 067 068 List<GenPolynomial<BigRational>> G, Gp; 069 070 071 GroebnerBaseAbstract<BigRational> bb; 072 073 074 GroebnerBaseAbstract<BigRational> bbw; 075 076 077 @Override 078 protected void setUp() { 079 //BigRational cf = new BigRational(); 080 //bb = new GroebnerBaseSeq<BigRational>(cf); 081 bb = GBFactory.<BigRational> getImplementation(/*cf*/); 082 bbw = new GroebnerBaseWalk<BigRational>(bb); 083 } 084 085 086 @Override 087 protected void tearDown() { 088 bb.terminate(); 089 bbw.terminate(); 090 bb = null; 091 bbw = null; 092 } 093 094 095 /** 096 * Test FJLT GBase. Example from the FJLT paper. 097 */ 098 @SuppressWarnings({ "unchecked", "cast" }) 099 public void testFJLTGBase() { // (y,x) 100 String exam = "(y,x) L " // REVILEX REVITDG 101 + "( (x**2 - y**3), (x**3 - y**2 - x) )"; 102 103 Reader source = new StringReader(exam); 104 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 105 try { 106 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 107 } catch (ClassCastException e) { 108 fail("" + e); 109 } catch (IOException e) { 110 fail("" + e); 111 } 112 //System.out.println("F = " + F); 113 114 G = bb.GB(F.list); 115 PolynomialList<BigRational> seq = new PolynomialList<BigRational>(F.ring, G); 116 //System.out.println("seq G = " + seq); 117 assertTrue("isGB( GB(FJLT) )", bb.isGB(G)); 118 assertTrue("isMinimalGB( GB(FJLT) )", bb.isMinimalGB(G)); 119 assertEquals("#GB(FJLT) == 2", 2, G.size()); 120 //assertEquals("#GB(FJLT) == 3", 3, G.size()); 121 Gp = bbw.GB(F.list); 122 PolynomialList<BigRational> fjlt = new PolynomialList<BigRational>(F.ring, Gp); 123 //System.out.println("walk G = " + fjlt); 124 assertTrue("isGB( GB(FJLT) )", bb.isGB(Gp)); 125 assertTrue("isMinimalGB( GB(FJLT) )", bb.isMinimalGB(Gp)); 126 assertEquals("#GB(FJLT) == 2", 2, Gp.size()); 127 //assertEquals("#GB(FJLT) == 3", 3, Gp.size()); 128 //Collections.reverse(G); // now in minimal 129 assertEquals("G == Gp: ", seq, fjlt); 130 } 131 132 133 /** 134 * Test example GBase. 135 */ 136 @SuppressWarnings({ "unchecked", "cast" }) 137 public void testFGLMGBase() { //(z,y,x) 138 String exam = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )" 139 + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )"; 140 141 Reader source = new StringReader(exam); 142 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 143 try { 144 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 145 } catch (ClassCastException e) { 146 fail("" + e); 147 } catch (IOException e) { 148 fail("" + e); 149 } 150 //System.out.println("F = " + F); 151 152 G = bb.GB(F.list); 153 PolynomialList<BigRational> P = new PolynomialList<BigRational>(F.ring, G); 154 //System.out.println("G = " + P); 155 assertTrue("isGB( GB(P) )", bb.isGB(G)); 156 assertEquals("#GB(P) == 3", 3, G.size()); 157 158 Gp = bbw.GB(F.list); 159 PolynomialList<BigRational> P2 = new PolynomialList<BigRational>(F.ring, Gp); 160 //System.out.println("G = " + P2); 161 assertTrue("isGB( GB(P2) )", bb.isGB(Gp)); 162 assertEquals("#GB(P2) == 3", 3, Gp.size()); 163 assertEquals("GB == FGLM", P, P2); 164 } 165 166 167 /** 168 * Test Trinks GBase. 169 */ 170 @SuppressWarnings({ "unchecked", "cast" }) 171 public void testTrinksGBase() { 172 String exam = "(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 177 Reader source = new StringReader(exam); 178 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 179 try { 180 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 181 } catch (ClassCastException e) { 182 fail("" + e); 183 } catch (IOException e) { 184 fail("" + e); 185 } 186 //System.out.println("F = " + F); 187 188 long t = System.currentTimeMillis(); 189 G = bb.GB(F.list); 190 t = System.currentTimeMillis() - t; 191 PolynomialList<BigRational> seq = new PolynomialList<BigRational>(F.ring, G); 192 //System.out.println("seq G = " + seq); 193 assertTrue("isGB( GB(Trinks) )", bb.isGB(G)); 194 assertTrue("isMinimalGB( GB(Trinks) )", bb.isMinimalGB(G)); 195 assertEquals("#GB(Trinks) == 6", 6, G.size()); 196 long w = System.currentTimeMillis(); 197 Gp = bbw.GB(F.list); 198 w = System.currentTimeMillis() - w; 199 PolynomialList<BigRational> tri = new PolynomialList<BigRational>(F.ring, Gp); 200 //System.out.println("walk G = " + tri); 201 //System.out.println("lex = " + t + ", walk = " + w + " in milliseconds"); 202 assertTrue("findbugs ", t + w >= 0L); 203 assertTrue("isGB( GB(Trinks) )", bb.isGB(Gp)); 204 assertTrue("isMinimalGB( GB(Trinks) )", bb.isMinimalGB(Gp)); 205 //assertEquals("#GB(Trinks) == 6", 6, Gp.size()); 206 //Collections.reverse(G); // now in minimal 207 assertEquals("G == Gp: ", seq, tri); // ideal.equals 208 } 209 210 211 /** 212 * Test Trinks GBase with different t1 and t2. 213 */ 214 @SuppressWarnings({ "unchecked", "cast" }) 215 public void testTrinksGBaseT1T2() { // t2 = G|4| = IGRLEX.blockOrder(4) 216 String exam = "(B,S,T,Z,P,W) G|4| " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 217 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 218 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 219 + "( 99 W - 11 B S + 3 B**2 ) " /*+ ", ( 10000 B**2 + 6600 B + 2673 )"*/ + ") "; 220 221 Reader source = new StringReader(exam); 222 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 223 try { 224 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 225 } catch (ClassCastException e) { 226 fail("" + e); 227 } catch (IOException e) { 228 fail("" + e); 229 } 230 //System.out.println("F = " + F); 231 // set t1, t2 = TermOrderNyName.IGRLEX.blockOrder(4) 232 bbw = new GroebnerBaseWalk<BigRational>(bb, TermOrderByName.IGRLEX.blockOrder(2)); 233 // Test wrong way from INVLEX to IGRLEX. 234 //bbw = new GroebnerBaseWalk<BigRational>(bb, TermOrderByName.INVLEX); 235 //System.out.println("bbw = " + bbw); 236 237 Gp = bbw.GB(F.list); 238 //PolynomialList<BigRational> tri = new PolynomialList<BigRational>(F.ring, Gp); 239 //System.out.println("walk G = " + tri); 240 assertTrue("isGB( GB(Trinks) )", bb.isGB(Gp)); 241 assertTrue("isMinimalGB( GB(Trinks) )", bb.isMinimalGB(Gp)); 242 //assertEquals("#GB(Trinks) == 6", 6, Gp.size()); 243 //Collections.reverse(G); // now in minimal 244 } 245 246 247 /** 248 * Test ISSAC GBase. Example from the FGLM paper. 249 */ 250 @SuppressWarnings({ "unchecked", "cast" }) 251 public void testFGLMissacGBase() { // 252 String exam = "Mod 32003 (w,z,y,x) L " // Mod 9223372036854775783 536870909 32003 (z,y,x,w) 253 + "( " 254 + " (8*w^2 + 5*w*x - 4*w*y + 2*w*z + 3*w + 5*x^2 + 2*x*y - 7*x*z - 7*x + 7*y^2 -8*y*z - 7*y + 7*z^2 - 8*z + 8)," 255 + "(3*w^2 - 5*w*x - 3*w*y - 6*w*z + 9*w + 4*x^2 + 2*x*y - 2*x*z + 7*x + 9*y^2 + 6*y*z + 5*y + 7*z^2 + 7*z + 5)," 256 + "(-2*w^2 + 9*w*x + 9*w*y - 7*w*z - 4*w + 8*x^2 + 9*x*y - 3*x*z + 8*x + 6*y^2 - 7*y*z + 4*y - 6*z^2 + 8*z + 2)," 257 + "(7*w^2 + 5*w*x + 3*w*y - 5*w*z - 5*w + 2*x^2 + 9*x*y - 7*x*z + 4*x -4*y^2 - 5*y*z + 6*y - 4*z^2 - 9*z + 2)" 258 + " )"; 259 260 Reader source = new StringReader(exam); 261 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 262 try { 263 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 264 } catch (ClassCastException e) { 265 fail("" + e); 266 } catch (IOException e) { 267 fail("" + e); 268 } 269 //System.out.println("F = " + F); 270 271 //G = bb.GB(F.list); 272 long t = System.currentTimeMillis(); 273 G = bb.GB(F.list); 274 t = System.currentTimeMillis() - t; 275 //PolynomialList<BigRational> seq = new PolynomialList<BigRational>(F.ring, G); 276 //System.out.println("seq G = " + seq); 277 assertTrue("isGB( GB() )", bb.isGB(G)); 278 assertTrue("isMinimalGB( GB() )", bb.isMinimalGB(G)); 279 //assertEquals("#GB(FJLT) == 2", 2, G.size()); 280 281 long w = System.currentTimeMillis(); 282 Gp = bbw.GB(F.list); 283 w = System.currentTimeMillis() - w; 284 //PolynomialList<BigRational> fjlt = new PolynomialList<BigRational>(F.ring, Gp); 285 //System.out.println("walk G = " + fjlt); 286 //System.out.println("lex = " + t + ", walk = " + w + " in milliseconds"); 287 assertTrue("findbugs ", t + w >= 0L); 288 assertTrue("isGB( GB() )", bb.isGB(Gp)); 289 assertTrue("isMinimalGB( GB() )", bb.isMinimalGB(Gp)); 290 //assertEquals("#GB(FJLT) == 2", 2, Gp.size()); 291 //Collections.reverse(G); // now in minimal 292 assertEquals("G == Gp: ", G, Gp); 293 } 294}