001 /* 002 * $Id: GroebnerBasePartTest.java 3423 2010-12-24 10:56:50Z kredel $ 003 */ 004 005 package edu.jas.gbufd; 006 007 008 import java.io.IOException; 009 import java.io.Reader; 010 import java.io.StringReader; 011 import java.util.ArrayList; 012 import java.util.Arrays; 013 import java.util.List; 014 015 import junit.framework.Test; 016 import junit.framework.TestCase; 017 import junit.framework.TestSuite; 018 019 import org.apache.log4j.BasicConfigurator; 020 021 import edu.jas.arith.BigRational; 022 import edu.jas.gb.GroebnerBase; 023 import edu.jas.gb.GroebnerBaseSeq; 024 import edu.jas.kern.ComputerThreads; 025 import edu.jas.poly.GenPolynomial; 026 import edu.jas.poly.GenPolynomialRing; 027 import edu.jas.poly.GenPolynomialTokenizer; 028 import edu.jas.poly.PolynomialList; 029 import edu.jas.poly.TermOrderOptimization; 030 import edu.jas.util.KsubSet; 031 032 033 /** 034 * Groebner base sequential tests with JUnit. 035 * @author Heinz Kredel. 036 */ 037 038 public 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("unchecked") 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("unchecked") 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 PolynomialList<GenPolynomial<BigRational>> tr = bbp.partialGBrec(F.list, new String[] { "B", "S", 207 "P", "Z", "T", "W" }); 208 fail("must throw exception"); 209 } catch (IllegalArgumentException e) { 210 //pass 211 } 212 } 213 214 215 /** 216 * Test partial permutation. 217 * 218 */ 219 public void testPartialPermutation() { 220 String[] vars = new String[] { "B", "S", "T", "Z", "P", "W" }; 221 String[] pvars = new String[] { "P", "Z", "T", "W" }; 222 String[] rvars = new String[] { "S", "B" }; 223 List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, pvars); 224 //System.out.println("perm1 = " + perm1); 225 226 List<Integer> perm2 = GroebnerBasePartial.partialPermutation(vars, pvars, null); 227 //System.out.println("perm2 = " + perm2); 228 229 assertEquals("perm1 == perm2 ", perm1, perm2); 230 231 List<Integer> perm3 = GroebnerBasePartial.partialPermutation(vars, pvars, rvars); 232 //System.out.println("perm3 = " + perm3); 233 assertFalse("perm1 != perm3 ", perm1.equals(perm3)); 234 } 235 236 237 /** 238 * Test elimination partial permutation. 239 * 240 */ 241 public void xtestElimPartialPermutation() { 242 String[] vars = new String[] { "B", "S", "T", "Z", "P", "W" }; 243 String[] evars = new String[] { "P", "Z" }; 244 String[] pvars = new String[] { "T", "W" }; 245 String[] rvars = new String[] { "B", "S" }; 246 List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, rvars); 247 System.out.println("perm1 = " + perm1); 248 249 List<Integer> perm2 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, null); 250 System.out.println("perm2 = " + perm2); 251 252 assertEquals("perm1 == perm2 ", perm1, perm2); 253 254 rvars = new String[] { "S", "B" }; 255 256 List<Integer> perm3 = GroebnerBasePartial.partialPermutation(vars, evars, pvars, rvars); 257 System.out.println("perm3 = " + perm3); 258 assertFalse("perm1 != perm3 ", perm1.equals(perm3)); 259 } 260 261 262 /** 263 * Test elim partial Trinks7 GBase. 264 * 265 */ 266 @SuppressWarnings("unchecked") 267 public void testTrinks7GBaseElimPart() { 268 String exam = "(B,S,T,Z,P,W) G " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 269 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 270 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 271 + "( 99 W - 11 B S + 3 B**2 ) " + "( B**2 + 33/50 B + 2673/10000 ) " + ") "; 272 273 Reader source = new StringReader(exam); 274 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 275 try { 276 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 277 } catch (ClassCastException e) { 278 fail("" + e); 279 } catch (IOException e) { 280 fail("" + e); 281 } 282 //System.out.println("F = " + F); 283 284 String[] evars = new String[] { "P", "Z" }; 285 String[] pvars = new String[] { "B", "S", "T", "W" }; 286 //System.out.println("evars = " + Arrays.toString(evars)); 287 //System.out.println("pvars = " + Arrays.toString(pvars)); 288 289 PolynomialList<BigRational> trinks = bbp.elimPartialGB(F.list, evars, pvars); 290 assertTrue("isGB( GB(Trinks7) )", bbp.isGB(trinks.list)); 291 //System.out.println("\nG = " + trinks); 292 } 293 294 295 /** 296 * Test partial GBase. 297 * 298 */ 299 @SuppressWarnings("unchecked") 300 public void testGBasePart() { 301 String exam = "(a,b,c,d,e,f) G " + "( " + "( a ), " + "( b^2 ), " + "( c^3 ), " + "( d^4 ), " 302 + "( e^5 ), " + "( f^6 ) " + ") "; 303 304 Reader source = new StringReader(exam); 305 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 306 try { 307 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 308 } catch (ClassCastException e) { 309 fail("" + e); 310 } catch (IOException e) { 311 fail("" + e); 312 } 313 //System.out.println("F = " + F); 314 315 // String[] evars = new String[] { "c", "d", "e", "f", "a", "b"}; 316 String[] evars = new String[] { "a", "b" }; 317 //System.out.println("evars = " + Arrays.toString(evars)); 318 319 PolynomialList<BigRational> G = bbp.partialGB(F.list, evars); 320 assertTrue("isGB( GB(G) )", bbp.isGB(G.list)); 321 //System.out.println("evars = " + Arrays.toString(evars)); 322 //System.out.println("G = " + G); 323 } 324 325 326 /** 327 * Test permutation generation. 328 * 329 */ 330 @SuppressWarnings("unchecked") 331 public void testPermGen() { 332 String[] vars = new String[] { "a", "b", "c", "d", "e", "f" }; 333 //System.out.println("vars = " + Arrays.toString(vars)); 334 335 List<String> sv = new ArrayList<String>(vars.length); 336 for (int i = 0; i < vars.length; i++) { 337 sv.add(vars[i]); 338 } 339 //System.out.println("sv = " + sv); 340 341 String exam = "(a,b,c,d,e,f) G " + "( " + "( a ), " + "( b^2 ), " + "( c^3 ), " + "( d^4 ), " 342 + "( e^5 ), " + "( f^6 ) " + ") "; 343 344 Reader source = new StringReader(exam); 345 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 346 PolynomialList<BigRational> F = null; 347 try { 348 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 349 } catch (ClassCastException e) { 350 fail("" + e); 351 } catch (IOException e) { 352 fail("" + e); 353 } 354 //System.out.println("F = " + F); 355 356 for (int i = 0; i <= vars.length; i++) { 357 KsubSet<String> ps = new KsubSet<String>(sv, i); 358 //System.out.println("========================== ps : " + i); 359 for (List<String> ev : ps) { 360 //System.out.println("ev = " + ev); 361 362 String[] evars = new String[ev.size()]; 363 for (int j = 0; j < ev.size(); j++) { 364 evars[j] = ev.get(j); 365 } 366 //System.out.println("evars = " + Arrays.toString(evars)); 367 String[] rvars = GroebnerBasePartial.remainingVars(vars, evars); 368 //System.out.println("rvars = " + Arrays.toString(rvars)); 369 370 List<Integer> perm1 = GroebnerBasePartial.partialPermutation(vars, evars); 371 //System.out.println("perm1 = " + perm1); 372 List<Integer> perm2 = GroebnerBasePartial.getPermutation(vars, rvars); 373 //System.out.println("perm2 = " + perm2); 374 assertEquals("perm1 == perm2 " + Arrays.toString(evars), perm1, perm2); 375 376 GenPolynomialRing<BigRational> r = new GenPolynomialRing<BigRational>(fac.coFac, vars); 377 GenPolynomialRing<BigRational> pr1 = TermOrderOptimization 378 .<BigRational> permutation(perm1, r); 379 //System.out.println("pr1 = " + pr1); 380 GenPolynomialRing<BigRational> pr2 = TermOrderOptimization 381 .<BigRational> permutation(perm2, r); 382 //System.out.println("pr2 = " + pr2); 383 assertEquals("pr1 == pr2 ", pr1, pr2); 384 385 List<GenPolynomial<BigRational>> pF1 = TermOrderOptimization.<BigRational> permutation(perm1, 386 pr1, F.list); 387 //System.out.println("pF1 = " + pF1); 388 List<GenPolynomial<BigRational>> pF2 = TermOrderOptimization.<BigRational> permutation(perm2, 389 pr2, F.list); 390 //System.out.println("pF2 = " + pF2); 391 assertEquals("pF1 == pF2 ", pF1, pF2); 392 } 393 } 394 } 395 396 }