001/* 002 * $Id: SyzygyTest.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.BigRational; 020import edu.jas.gb.GroebnerBase; 021import edu.jas.gb.GroebnerBaseSeq; 022import edu.jas.gbufd.GBFactory; 023import edu.jas.gbufd.Syzygy; 024import edu.jas.gbufd.SyzygySeq; 025import edu.jas.poly.GenPolynomial; 026import edu.jas.poly.GenPolynomialRing; 027import edu.jas.poly.GenPolynomialTokenizer; 028import edu.jas.poly.ModuleList; 029import edu.jas.poly.PolynomialList; 030import edu.jas.poly.TermOrder; 031 032 033/** 034 * Syzygy tests with JUnit. 035 * @author Heinz Kredel 036 */ 037 038public class SyzygyTest extends TestCase { 039 040 041 042 /** 043 * main. 044 */ 045 public static void main(String[] args) { 046 junit.textui.TestRunner.run(suite()); 047 } 048 049 050 /** 051 * Constructs a <CODE>SyzygyTest</CODE> object. 052 * @param name String. 053 */ 054 public SyzygyTest(String name) { 055 super(name); 056 } 057 058 059 /** 060 */ 061 public static Test suite() { 062 TestSuite suite = new TestSuite(SyzygyTest.class); 063 return suite; 064 } 065 066 067 GenPolynomialRing<BigRational> fac; 068 069 070 PolynomialList<BigRational> F; 071 072 073 List<GenPolynomial<BigRational>> G; 074 075 076 GroebnerBase<BigRational> bb; 077 078 079 GroebnerBase<BigRational> mbb; 080 081 082 Syzygy<BigRational> sz; 083 084 085 GenPolynomial<BigRational> a, b, c, d, e; 086 087 088 GenPolynomial<BigRational> zero; 089 090 091 GenPolynomial<BigRational> one; 092 093 094 TermOrder tord; 095 096 097 List<GenPolynomial<BigRational>> L; 098 099 100 List<List<GenPolynomial<BigRational>>> K; 101 102 103 List<GenPolynomial<BigRational>> V; 104 105 106 List<List<GenPolynomial<BigRational>>> W; 107 108 109 ModuleList<BigRational> M; 110 111 112 ModuleList<BigRational> N; 113 114 115 ModuleList<BigRational> Z; 116 117 118 int rl = 3; //4; //3; 119 120 121 int kl = 3; //7; 122 123 124 int ll = 7; //9; 125 126 127 int el = 2; 128 129 130 float q = 0.3f; //0.4f 131 132 133 @Override 134 protected void setUp() { 135 BigRational coeff = new BigRational(9); 136 tord = new TermOrder(); 137 fac = new GenPolynomialRing<BigRational>(coeff, rl, tord); 138 139 bb = GBFactory.getImplementation(coeff); 140 mbb = new GroebnerBaseSeq<BigRational>(); //coeff); 141 sz = new SyzygySeq<BigRational>(coeff); 142 143 a = b = c = d = e = null; 144 L = null; 145 K = null; 146 V = null; 147 148 do { 149 a = fac.random(kl, ll, el, q); 150 b = fac.random(kl, ll, el, q); 151 c = fac.random(kl, ll, el, q); 152 d = fac.random(kl, ll, el, q); 153 } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()); 154 e = d; //fac.random(kl, ll, el, q ); 155 156 one = fac.getONE(); 157 zero = fac.getZERO(); 158 } 159 160 161 @Override 162 protected void tearDown() { 163 a = b = c = d = e = null; 164 L = null; 165 K = null; 166 V = null; 167 fac = null; 168 tord = null; 169 bb = null; 170 mbb = null; 171 sz = null; 172 } 173 174 175 /** 176 * Test sequential Syzygy. 177 */ 178 public void testSequentialSyzygy() { 179 L = new ArrayList<GenPolynomial<BigRational>>(); 180 181 assertTrue("not isZERO( a )", !a.isZERO()); 182 L.add(a); 183 assertTrue("isGB( { a } )", bb.isGB(L)); 184 K = sz.zeroRelations(L); 185 assertTrue("is ZR( { a } )", sz.isZeroRelation(K, L)); 186 187 assertTrue("not isZERO( b )", !b.isZERO()); 188 L.add(b); 189 L = bb.GB(L); 190 assertTrue("isGB( { a, b } )", bb.isGB(L)); 191 //System.out.println("\nL = " + L ); 192 K = sz.zeroRelations(L); 193 //System.out.println("\nN = " + N ); 194 assertTrue("is ZR( { a, b } )", sz.isZeroRelation(K, L)); 195 196 assertTrue("not isZERO( c )", !c.isZERO()); 197 L.add(c); 198 L = bb.GB(L); 199 //System.out.println("\nL = " + L ); 200 assertTrue("isGB( { a, b, c } )", bb.isGB(L)); 201 K = sz.zeroRelations(L); 202 //System.out.println("\nN = " + N ); 203 assertTrue("is ZR( { a, b, c } )", sz.isZeroRelation(K, L)); 204 205 assertTrue("not isZERO( d )", !d.isZERO()); 206 L.add(d); 207 L = bb.GB(L); 208 //System.out.println("\nL = " + L ); 209 assertTrue("isGB( { a, b, c, d } )", bb.isGB(L)); 210 K = sz.zeroRelations(L); 211 //System.out.println("\nN = " + N ); 212 assertTrue("is ZR( { a, b, c, d } )", sz.isZeroRelation(K, L)); 213 214 //System.out.println("N = " + N ); 215 /* 216 */ 217 } 218 219 220 /** 221 * Test sequential module Syzygy. 222 */ 223 public void testSequentialModSyzygy() { 224 W = new ArrayList<List<GenPolynomial<BigRational>>>(); 225 226 assertTrue("not isZERO( a )", !a.isZERO()); 227 V = new ArrayList<GenPolynomial<BigRational>>(); 228 V.add(a); 229 V.add(zero); 230 V.add(one); 231 W.add(V); 232 M = new ModuleList<BigRational>(fac, W); 233 assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M)); 234 235 N = mbb.GB(M); 236 assertTrue("isGB( { (a,0,1) } )", mbb.isGB(N)); 237 238 Z = sz.zeroRelations(N); 239 //System.out.println("Z = " + Z); 240 assertTrue("is ZR( { a) } )", sz.isZeroRelation(Z, N)); 241 242 assertTrue("not isZERO( b )", !b.isZERO()); 243 V = new ArrayList<GenPolynomial<BigRational>>(); 244 V.add(b); 245 V.add(one); 246 V.add(zero); 247 W.add(V); 248 M = new ModuleList<BigRational>(fac, W); 249 //System.out.println("W = " + W.size() ); 250 251 N = mbb.GB(M); 252 assertTrue("isGB( { a, b } )", mbb.isGB(N)); 253 254 Z = sz.zeroRelations(N); 255 //System.out.println("Z = " + Z); 256 assertTrue("is ZR( { a, b } )", sz.isZeroRelation(Z, N)); 257 258 assertTrue("not isZERO( c )", !c.isZERO()); 259 V = new ArrayList<GenPolynomial<BigRational>>(); 260 V.add(c); 261 V.add(one); 262 V.add(zero); 263 W.add(V); 264 M = new ModuleList<BigRational>(fac, W); 265 //System.out.println("W = " + W.size() ); 266 267 N = mbb.GB(M); 268 //System.out.println("GB(M) = " + N); 269 assertTrue("isGB( { a,b,c) } )", mbb.isGB(N)); 270 271 Z = sz.zeroRelations(N); 272 //System.out.println("Z = " + Z); 273 //boolean b = Syzygy.isZeroRelation(Z,N); 274 //System.out.println("boolean = " + b); 275 assertTrue("is ZR( { a,b,c } )", sz.isZeroRelation(Z, N)); 276 277 } 278 279 280 /** 281 * Test sequential arbitrary base Syzygy. 282 */ 283 public void testSequentialArbitrarySyzygy() { 284 L = new ArrayList<GenPolynomial<BigRational>>(); 285 286 assertTrue("not isZERO( a )", !a.isZERO()); 287 L.add(a); 288 assertTrue("isGB( { a } )", bb.isGB(L)); 289 K = sz.zeroRelationsArbitrary(L); 290 assertTrue("is ZR( { a } )", sz.isZeroRelation(K, L)); 291 292 assertTrue("not isZERO( b )", !b.isZERO()); 293 L.add(b); 294 // L = bb.GB(L); 295 // assertTrue("isGB( { a, b } )", bb.isGB(L) ); 296 //System.out.println("\nL = " + L ); 297 K = sz.zeroRelationsArbitrary(L); 298 //System.out.println("\nN = " + N ); 299 assertTrue("is ZR( { a, b } )", sz.isZeroRelation(K, L)); 300 301 assertTrue("not isZERO( c )", !c.isZERO()); 302 L.add(c); 303 //L = bb.GB(L); 304 //System.out.println("\nL = " + L ); 305 //assertTrue("isGB( { a, b, c } )", bb.isGB(L) ); 306 K = sz.zeroRelationsArbitrary(L); 307 //System.out.println("\nN = " + N ); 308 assertTrue("is ZR( { a, b, c } )", sz.isZeroRelation(K, L)); 309 310 assertTrue("not isZERO( d )", !d.isZERO()); 311 L.add(d); 312 //L = bb.GB(L); 313 //System.out.println("\nL = " + L ); 314 //assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) ); 315 K = sz.zeroRelationsArbitrary(L); 316 //System.out.println("\nN = " + N ); 317 assertTrue("is ZR( { a, b, c, d } )", sz.isZeroRelation(K, L)); 318 319 //System.out.println("N = " + N ); 320 321 } 322 323 324 /** 325 * Test sequential arbitrary base Syzygy, ex CLO 2, p 214 ff. 326 */ 327 @SuppressWarnings("unchecked") 328 public void testSequentialArbitrarySyzygyCLO() { 329 330 PolynomialList<BigRational> F = null; 331 332 String exam = "(x,y) G " + "( " + "( x y + x ), " + "( y^2 + 1 ) " + ") "; 333 Reader source = new StringReader(exam); 334 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 335 try { 336 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 337 } catch (ClassCastException e) { 338 fail("" + e); 339 } catch (IOException e) { 340 fail("" + e); 341 } 342 //System.out.println("F = " + F); 343 344 L = F.list; 345 K = sz.zeroRelationsArbitrary(L); 346 assertTrue("is ZR( { a, b } )", sz.isZeroRelation(K, L)); 347 } 348 349 350 /** 351 * Test sequential arbitrary module Syzygy. 352 */ 353 public void testSequentialArbitraryModSyzygy() { 354 W = new ArrayList<List<GenPolynomial<BigRational>>>(); 355 356 assertTrue("not isZERO( a )", !a.isZERO()); 357 V = new ArrayList<GenPolynomial<BigRational>>(); 358 V.add(a); 359 V.add(zero); 360 V.add(one); 361 W.add(V); 362 M = new ModuleList<BigRational>(fac, W); 363 assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M)); 364 365 Z = sz.zeroRelationsArbitrary(M); 366 //System.out.println("Z = " + Z); 367 assertTrue("is ZR( { a) } )", sz.isZeroRelation(Z, M)); 368 369 assertTrue("not isZERO( b )", !b.isZERO()); 370 V = new ArrayList<GenPolynomial<BigRational>>(); 371 V.add(b); 372 V.add(one); 373 V.add(zero); 374 W.add(V); 375 M = new ModuleList<BigRational>(fac, W); 376 //System.out.println("W = " + W.size() ); 377 378 Z = sz.zeroRelationsArbitrary(M); 379 //System.out.println("Z = " + Z); 380 assertTrue("is ZR( { a, b } )", sz.isZeroRelation(Z, M)); 381 382 assertTrue("not isZERO( c )", !c.isZERO()); 383 V = new ArrayList<GenPolynomial<BigRational>>(); 384 V.add(c); 385 V.add(one); 386 V.add(zero); 387 W.add(V); 388 M = new ModuleList<BigRational>(fac, W); 389 //System.out.println("W = " + W.size() ); 390 391 Z = sz.zeroRelationsArbitrary(M); 392 //System.out.println("Z = " + Z); 393 //boolean b = Syzygy.isZeroRelation(Z,N); 394 //System.out.println("boolean = " + b); 395 assertTrue("is ZR( { a,b,c } )", sz.isZeroRelation(Z, M)); 396 397 } 398 399}