001/* 002 * $Id: GroebnerBasePartTest.java 5048 2014-12-30 17:45:01Z 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.Arrays; 013import java.util.List; 014 015import junit.framework.Test; 016import junit.framework.TestCase; 017import junit.framework.TestSuite; 018 019import org.apache.log4j.BasicConfigurator; 020 021import edu.jas.arith.BigRational; 022import edu.jas.gb.GroebnerBase; 023import edu.jas.gb.GroebnerBaseSeq; 024import edu.jas.kern.ComputerThreads; 025import edu.jas.poly.GenPolynomial; 026import edu.jas.poly.GenPolynomialRing; 027import edu.jas.poly.GenPolynomialTokenizer; 028import edu.jas.poly.PolynomialList; 029import edu.jas.poly.TermOrderOptimization; 030import edu.jas.util.KsubSet; 031 032 033/** 034 * Partial Groebner base sequential tests with JUnit. 035 * @author Heinz Kredel. 036 */ 037 038public class GroebnerBasePartTest extends TestCase { 039 040 041 //private static final Logger logger = Logger.getLogger(GroebnerBasePartTest.class); 042 043 /** 044 * main 045 */ 046 public static void main(String[] args) { 047 BasicConfigurator.configure(); 048 junit.textui.TestRunner.run(suite()); 049 } 050 051 052 /** 053 * Constructs a <CODE>GroebnerBasePartTest</CODE> object. 054 * @param name String. 055 */ 056 public GroebnerBasePartTest(String name) { 057 super(name); 058 } 059 060 061 /** 062 * suite. 063 */ 064 public static Test suite() { 065 TestSuite suite = new TestSuite(GroebnerBasePartTest.class); 066 return suite; 067 } 068 069 070 GenPolynomialRing<BigRational> fac; 071 072 073 List<GenPolynomial<BigRational>> L; 074 075 076 PolynomialList<BigRational> F; 077 078 079 List<GenPolynomial<BigRational>> G; 080 081 082 GroebnerBase<BigRational> bb; 083 084 085 GroebnerBasePartial<BigRational> bbp; 086 087 088 GenPolynomial<BigRational> a; 089 090 091 GenPolynomial<BigRational> b; 092 093 094 GenPolynomial<BigRational> c; 095 096 097 GenPolynomial<BigRational> d; 098 099 100 GenPolynomial<BigRational> e; 101 102 103 int rl = 3; //4; //3; 104 105 106 int kl = 10; 107 108 109 int ll = 7; 110 111 112 int el = 3; 113 114 115 float q = 0.2f; //0.4f 116 117 118 @Override 119 protected void setUp() { 120 BigRational coeff = new BigRational(9); 121 fac = new GenPolynomialRing<BigRational>(coeff, rl); 122 a = b = c = d = e = null; 123 bb = new GroebnerBaseSeq<BigRational>(); 124 bbp = new GroebnerBasePartial<BigRational>(); 125 } 126 127 128 @Override 129 protected void tearDown() { 130 a = b = c = d = e = null; 131 fac = null; 132 bb = null; 133 ComputerThreads.terminate(); 134 } 135 136 137 /** 138 * Test partial recursive Trinks7 GBase. 139 * 140 */ 141 @SuppressWarnings("cast") 142 public void testTrinks7GBasePartRec() { 143 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 144 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 145 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 146 + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") "; 147 148 Reader source = new StringReader(exam); 149 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 150 try { 151 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 152 } catch (ClassCastException e) { 153 fail("" + e); 154 } catch (IOException e) { 155 fail("" + e); 156 } 157 //System.out.println("F = " + F); 158 159 //PolynomialList<BigRational> Fo = TermOrderOptimization.optimizeTermOrder(F); 160 //System.out.println("\nFo = " + Fo); 161 162 PolynomialList<GenPolynomial<BigRational>> rtrinks = bbp.partialGBrec(F.list, new String[] { "P", 163 "Z", "T", "W" }); 164 assertTrue("isGB( GB(Trinks7) )", bbp.isGBrec(rtrinks.list)); 165 //System.out.println("\nTrinksR = " + rtrinks); 166 167 // not meaning-full 168 PolynomialList<BigRational> trinks = bbp.partialGB(F.list, new String[] { "P", "Z", "T", "W" }); 169 //System.out.println("\ntrinks = " + trinks); 170 assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list)); 171 } 172 173 174 /** 175 * Test partial Trinks7 GBase. 176 * 177 */ 178 @SuppressWarnings("cast") 179 public void testTrinks7GBasePart() { 180 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 181 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 182 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 183 + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") "; 184 185 Reader source = new StringReader(exam); 186 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 187 try { 188 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 189 } catch (ClassCastException e) { 190 fail("" + e); 191 } catch (IOException e) { 192 fail("" + e); 193 } 194 //System.out.println("F = " + F); 195 196 //PolynomialList<BigRational> Fo = TermOrderOptimization.optimizeTermOrder(F); 197 //System.out.println("\nFo = " + Fo); 198 199 PolynomialList<BigRational> trinks = bbp.partialGB(F.list, new String[] { "B", "S", "P", "Z", "T", 200 "W" }); 201 //PolynomialList<BigRational> trinks = bbp.partialGB(F.list, new String[] { "T", "Z", "P", "W", "B", "S" }); 202 assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list)); 203 //System.out.println("\nG = " + trinks); 204 205 try { 206 @SuppressWarnings("unused") 207 PolynomialList<GenPolynomial<BigRational>> tr = bbp.partialGBrec(F.list, new String[] { "B", "S", 208 "P", "Z", "T", "W" }); 209 fail("must throw exception"); 210 } catch (IllegalArgumentException e) { 211 //pass 212 } 213 } 214 215 216 /** 217 * Test partial permutation. 218 * 219 */ 220 public void testPartialPermutation() { 221 String[] vars = new String[] { "B", "S", "T", "Z", "P", "W" }; 222 String[] pvars = new String[] { "P", "Z", "T", "W" }; 223 String[] rvars = new String[] { "S", "B" }; 224 List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, pvars); 225 //System.out.println("perm1 = " + perm1); 226 227 List<Integer> perm2 = GroebnerBasePartial.partialPermutation(vars, pvars, null); 228 //System.out.println("perm2 = " + perm2); 229 230 assertEquals("perm1 == perm2 ", perm1, perm2); 231 232 List<Integer> perm3 = GroebnerBasePartial.partialPermutation(vars, pvars, rvars); 233 //System.out.println("perm3 = " + perm3); 234 assertFalse("perm1 != perm3 ", perm1.equals(perm3)); 235 } 236 237 238 /** 239 * Test elimination partial permutation. 240 * 241 */ 242 public void xtestElimPartialPermutation() { 243 String[] vars = new String[] { "B", "S", "T", "Z", "P", "W" }; 244 String[] evars = new String[] { "P", "Z" }; 245 String[] pvars = new String[] { "T", "W" }; 246 String[] rvars = new String[] { "B", "S" }; 247 List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, rvars); 248 System.out.println("perm1 = " + perm1); 249 250 List<Integer> perm2 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, null); 251 System.out.println("perm2 = " + perm2); 252 253 assertEquals("perm1 == perm2 ", perm1, perm2); 254 255 rvars = new String[] { "S", "B" }; 256 257 List<Integer> perm3 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, rvars); 258 System.out.println("perm3 = " + perm3); 259 assertFalse("perm1 != perm3 ", perm1.equals(perm3)); 260 } 261 262 263 /** 264 * Test elim partial Trinks7 GBase. 265 * 266 */ 267 @SuppressWarnings("cast") 268 public void testTrinks7GBaseElimPart() { 269 String exam = "(B,S,T,Z,P,W) G " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 270 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 271 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 272 + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") "; 273 274 Reader source = new StringReader(exam); 275 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 276 try { 277 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 278 } catch (ClassCastException e) { 279 fail("" + e); 280 } catch (IOException e) { 281 fail("" + e); 282 } 283 //System.out.println("F = " + F); 284 285 String[] evars = new String[] { "P", "Z" }; 286 String[] pvars = new String[] { "B", "S", "T", "W" }; 287 //System.out.println("evars = " + Arrays.toString(evars)); 288 //System.out.println("pvars = " + Arrays.toString(pvars)); 289 290 PolynomialList<BigRational> trinks = bbp.elimPartialGB(F.list, evars, pvars); 291 assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list)); 292 //System.out.println("\nG = " + trinks); 293 } 294 295 296 /** 297 * Test partial GBase. 298 * 299 */ 300 @SuppressWarnings("cast") 301 public void testGBasePart() { 302 String exam = "(a,b,c,d,e,f) G " + "( " + "( a ), " + "( b^2 ), " + "( c^3 ), " + "( d^4 ), " 303 + "( e^5 ), " + "( f^6 ) " + ") "; 304 305 Reader source = new StringReader(exam); 306 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 307 try { 308 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 309 } catch (ClassCastException e) { 310 fail("" + e); 311 } catch (IOException e) { 312 fail("" + e); 313 } 314 //System.out.println("F = " + F); 315 316 // String[] evars = new String[] { "c", "d", "e", "f", "a", "b"}; 317 String[] evars = new String[] { "a", "b" }; 318 //System.out.println("evars = " + Arrays.toString(evars)); 319 320 PolynomialList<BigRational> G = bbp.partialGB(F.list, evars); 321 assertTrue("isGB( GB(G) )", bbp.isGB(G.list)); 322 //System.out.println("evars = " + Arrays.toString(evars)); 323 //System.out.println("G = " + G); 324 } 325 326 327 /** 328 * Test permutation generation. 329 * 330 */ 331 @SuppressWarnings("cast") 332 public void testPermGen() { 333 String[] vars = new String[] { "a", "b", "c", "d", "e", "f" }; 334 //System.out.println("vars = " + Arrays.toString(vars)); 335 336 List<String> sv = new ArrayList<String>(vars.length); 337 for (int i = 0; i < vars.length; i++) { 338 sv.add(vars[i]); 339 } 340 //System.out.println("sv = " + sv); 341 342 String exam = "(a,b,c,d,e,f) G " + "( " + "( a ), " + "( b^2 ), " + "( c^3 ), " + "( d^4 ), " 343 + "( e^5 ), " + "( f^6 ) " + ") "; 344 345 Reader source = new StringReader(exam); 346 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 347 PolynomialList<BigRational> F = null; 348 try { 349 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 350 } catch (ClassCastException e) { 351 fail("" + e); 352 } catch (IOException e) { 353 fail("" + e); 354 } 355 //System.out.println("F = " + F); 356 357 for (int i = 0; i <= vars.length; i++) { 358 KsubSet<String> ps = new KsubSet<String>(sv, i); 359 //System.out.println("========================== ps : " + i); 360 for (List<String> ev : ps) { 361 //System.out.println("ev = " + ev); 362 363 String[] evars = new String[ev.size()]; 364 for (int j = 0; j < ev.size(); j++) { 365 evars[j] = ev.get(j); 366 } 367 //System.out.println("evars = " + Arrays.toString(evars)); 368 String[] rvars = GroebnerBasePartial.remainingVars(vars, evars); 369 //System.out.println("rvars = " + Arrays.toString(rvars)); 370 371 List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, evars); 372 //System.out.println("perm1 = " + perm1); 373 List<Integer> perm2 = GroebnerBasePartial.getPermutation(vars, rvars); 374 //System.out.println("perm2 = " + perm2); 375 assertEquals("perm1 == perm2 " + Arrays.toString(evars), perm1, perm2); 376 377 GenPolynomialRing<BigRational> r = new GenPolynomialRing<BigRational>(fac.coFac, vars); 378 GenPolynomialRing<BigRational> pr1 = TermOrderOptimization 379 .<BigRational> permutation(perm1, r); 380 //System.out.println("pr1 = " + pr1); 381 GenPolynomialRing<BigRational> pr2 = TermOrderOptimization 382 .<BigRational> permutation(perm2, r); 383 //System.out.println("pr2 = " + pr2); 384 assertEquals("pr1 == pr2 ", pr1, pr2); 385 386 List<GenPolynomial<BigRational>> pF1 = TermOrderOptimization.<BigRational> permutation(perm1, 387 pr1, F.list); 388 //System.out.println("pF1 = " + pF1); 389 List<GenPolynomial<BigRational>> pF2 = TermOrderOptimization.<BigRational> permutation(perm2, 390 pr2, F.list); 391 //System.out.println("pF2 = " + pF2); 392 assertEquals("pF1 == pF2 ", pF1, pF2); 393 } 394 } 395 } 396 397}