Module jas
[hide private]
[frames] | no frames]

Source Code for Module jas

   1  '''jython interface to JAS. 
   2  ''' 
   3   
   4  # $Id: jas.py 3590 2011-03-30 21:19:39Z kredel $ 
   5   
   6  from java.lang           import System 
   7  from java.io             import StringReader 
   8  from java.util           import ArrayList 
   9   
  10  from org.apache.log4j    import BasicConfigurator; 
  11   
  12  from edu.jas.structure   import RingElem, RingFactory, Power 
  13  from edu.jas.arith       import BigInteger, BigRational, BigComplex, BigDecimal,\ 
  14                                  ModInteger, ModIntegerRing, BigQuaternion, BigOctonion,\ 
  15                                  Product, ProductRing 
  16  from edu.jas.poly        import GenPolynomial, GenPolynomialRing,\ 
  17                                  GenSolvablePolynomial, GenSolvablePolynomialRing,\ 
  18                                  GenPolynomialTokenizer, OrderedPolynomialList, PolyUtil,\ 
  19                                  TermOrderOptimization, TermOrder, PolynomialList,\ 
  20                                  AlgebraicNumber, AlgebraicNumberRing,\ 
  21                                  OrderedModuleList, ModuleList,\ 
  22                                  Complex, ComplexRing 
  23  from edu.jas.ps          import UnivPowerSeries, UnivPowerSeriesRing,\ 
  24                                  UnivPowerSeriesMap, Coefficients, \ 
  25                                  MultiVarPowerSeries, MultiVarPowerSeriesRing,\ 
  26                                  MultiVarPowerSeriesMap, MultiVarCoefficients,\ 
  27                                  StandardBaseSeq 
  28  from edu.jas.gb          import EReductionSeq, DGroebnerBaseSeq, EGroebnerBaseSeq,\ 
  29                                  GroebnerBaseDistributed, GBDist, GroebnerBaseParallel,\ 
  30                                  GroebnerBaseSeq, GroebnerBaseSeqPairSeq,\ 
  31                                  ReductionSeq, GroebnerBaseSeqPairParallel,\ 
  32                                  SolvableGroebnerBaseParallel, SolvableGroebnerBaseSeq 
  33  from edu.jas.gbufd       import GroebnerBasePseudoRecSeq, GroebnerBasePseudoSeq,\ 
  34                                  RGroebnerBasePseudoSeq, RGroebnerBaseSeq, RReductionSeq 
  35  from edu.jas.gbmod       import ModGroebnerBaseAbstract, ModSolvableGroebnerBaseAbstract,\ 
  36                                  SolvableSyzygyAbstract, SyzygyAbstract 
  37  from edu.jas.vector      import GenVector, GenVectorModul,\ 
  38                                  GenMatrix, GenMatrixRing 
  39  from edu.jas.application import PolyUtilApp, Residue, ResidueRing, Ideal,\ 
  40                                  Local, LocalRing, IdealWithRealAlgebraicRoots,\ 
  41                                  ComprehensiveGroebnerBaseSeq, ExtensionFieldBuilder 
  42  from edu.jas.kern        import ComputerThreads, StringUtil, Scripting 
  43  from edu.jas.ufd         import GreatestCommonDivisor, PolyUfdUtil, GCDFactory,\ 
  44                                  FactorFactory, SquarefreeFactory, Quotient, QuotientRing 
  45  from edu.jas.root        import RealRootsSturm, Interval, RealAlgebraicNumber, RealAlgebraicRing,\ 
  46                                  ComplexRootsSturm, Rectangle, RootFactory 
  47  from edu.jas.integrate   import ElementaryIntegration 
  48  from edu.jas.util        import ExecutableServer 
  49  from edu.jas             import structure, arith, poly, ps, gb, gbmod, vector,\ 
  50                                  application, util, ufd 
  51  from edu                 import jas 
  52  #PrettyPrint.setInternal(); 
  53   
  54  from org.python.core     import PyInstance, PyJavaInstance, PyList, PyTuple,\ 
  55                                  PyInteger, PyLong, PyFloat, PyString 
  56   
  57  # set output to Python scripting 
  58  Scripting.setLang(Scripting.Lang.Python); 
  59   
60 -def startLog():
61 '''Configure the log4j system and start logging. 62 ''' 63 BasicConfigurator.configure();
64
65 -def terminate():
66 '''Terminate the running thread pools. 67 ''' 68 ComputerThreads.terminate();
69
70 -def noThreads():
71 '''Turn off automatic parallel threads usage. 72 ''' 73 print "nt = ", ComputerThreads.NO_THREADS; 74 ComputerThreads.setNoThreads(); #NO_THREADS = #0; #1; #True; 75 print "nt = ", ComputerThreads.NO_THREADS;
76 77
78 -class Ring:
79 '''Represents a JAS polynomial ring: GenPolynomialRing. 80 81 Methods to create ideals and ideals with parametric coefficients. 82 ''' 83
84 - def __init__(self,ringstr="",ring=None):
85 '''Ring constructor. 86 ''' 87 if ring == None: 88 sr = StringReader( ringstr ); 89 tok = GenPolynomialTokenizer(sr); 90 self.pset = tok.nextPolynomialSet(); 91 self.ring = self.pset.ring; 92 else: 93 self.ring = ring; 94 self.engine = GCDFactory.getProxy(self.ring.coFac); 95 try: 96 self.sqf = SquarefreeFactory.getImplementation(self.ring.coFac); 97 # print "sqf: ", self.sqf; 98 # except Exception, e: 99 # print "error " + str(e) 100 except: 101 pass 102 try: 103 self.factor = FactorFactory.getImplementation(self.ring.coFac); 104 #print "factor: ", self.factor; 105 except: 106 pass
107 # except Exception, e: 108 # print "error " + str(e) 109
110 - def __str__(self):
111 '''Create a string representation. 112 ''' 113 return str(self.ring.toScript());
114
115 - def ideal(self,ringstr="",list=None):
116 '''Create an ideal. 117 ''' 118 return Ideal(self,ringstr,list=list);
119
120 - def paramideal(self,ringstr="",list=None,gbsys=None):
121 '''Create an ideal in a polynomial ring with parameter coefficients. 122 ''' 123 return ParamIdeal(self,ringstr,list,gbsys);
124
125 - def gens(self):
126 '''Get list of generators of the polynomial ring. 127 ''' 128 L = self.ring.generators(); 129 N = [ RingElem(e) for e in L ]; 130 return N;
131
132 - def one(self):
133 '''Get the one of the polynomial ring. 134 ''' 135 return RingElem( self.ring.getONE() );
136
137 - def zero(self):
138 '''Get the zero of the polynomial ring. 139 ''' 140 return RingElem( self.ring.getZERO() );
141
142 - def random(self,k=5,l=7,d=3,q=0.3):
143 '''Get a random polynomial. 144 ''' 145 r = self.ring.random(k,l,d,q); 146 if self.ring.coFac.isField(): 147 r = r.monic(); 148 return RingElem( r );
149
150 - def element(self,polystr):
151 '''Create an element from a string. 152 ''' 153 I = Ideal(self, "( " + polystr + " )"); 154 list = I.pset.list; 155 if len(list) > 0: 156 return RingElem( list[0] );
157
158 - def gcd(self,a,b):
159 '''Compute the greatest common divisor of a and b. 160 ''' 161 if isinstance(a,RingElem): 162 a = a.elem; 163 else: 164 a = self.element( str(a) ); 165 a = a.elem; 166 if isinstance(b,RingElem): 167 b = b.elem; 168 else: 169 b = self.element( str(b) ); 170 b = b.elem; 171 return RingElem( self.engine.gcd(a,b) );
172
173 - def squarefreeFactors(self,a):
174 '''Compute squarefree factors of polynomial. 175 ''' 176 if isinstance(a,RingElem): 177 a = a.elem; 178 else: 179 a = self.element( str(a) ); 180 a = a.elem; 181 cf = self.ring.coFac; 182 if cf.getClass().getSimpleName() == "GenPolynomialRing": 183 e = self.sqf.recursiveSquarefreeFactors( a ); 184 else: 185 e = self.sqf.squarefreeFactors( a ); 186 L = {}; 187 for a in e.keySet(): 188 i = e.get(a); 189 L[ RingElem( a ) ] = i; 190 return L;
191
192 - def factors(self,a):
193 '''Compute irreducible factorization for modular, integer, 194 rational number and algebriac number coefficients. 195 ''' 196 if isinstance(a,RingElem): 197 a = a.elem; 198 else: 199 a = self.element( str(a) ); 200 a = a.elem; 201 try: 202 cf = self.ring.coFac; 203 if cf.getClass().getSimpleName() == "GenPolynomialRing": 204 e = self.factor.recursiveFactors( a ); 205 else: 206 e = self.factor.factors( a ); 207 L = {}; 208 for a in e.keySet(): 209 i = e.get(a); 210 L[ RingElem( a ) ] = i; 211 return L; 212 except Exception, e: 213 print "error " + str(e) 214 return None
215
216 - def factorsAbsolute(self,a):
217 '''Compute absolute irreducible factorization for (modular,) 218 rational number coefficients. 219 ''' 220 if isinstance(a,RingElem): 221 a = a.elem; 222 else: 223 a = self.element( str(a) ); 224 a = a.elem; 225 try: 226 L = self.factor.factorsAbsolute( a ); 227 ## L = {}; 228 ## for a in e.keySet(): 229 ## i = e.get(a); 230 ## L[ RingElem( a ) ] = i; 231 return L; 232 except Exception, e: 233 print "error in factorsAbsolute " + str(e) 234 return None
235
236 - def realRoots(self,a,eps=None):
237 '''Compute real roots of univariate polynomial. 238 ''' 239 if isinstance(a,RingElem): 240 a = a.elem; 241 else: 242 a = self.element( str(a) ); 243 a = a.elem; 244 if isinstance(eps,RingElem): 245 eps = eps.elem; 246 try: 247 if eps == None: 248 #R = RealRootsSturm().realRoots( a ); 249 R = RootFactory.realAlgebraicNumbers( a ); 250 else: 251 R = RootFactory.realAlgebraicNumbers( a, eps ); 252 R = [ RingElem(r) for r in R ]; 253 #R = [ RingElem(BigDecimal(r.getRational())) for r in R ]; 254 return R; 255 except Exception, e: 256 print "error " + str(e) 257 return None
258
259 - def complexRoots(self,a,eps=None):
260 '''Compute complex roots of univariate polynomial. 261 ''' 262 if isinstance(a,RingElem): 263 a = a.elem; 264 else: 265 a = self.element( str(a) ); 266 a = a.elem; 267 if isinstance(eps,RingElem): 268 eps = eps.elem; 269 cmplx = False; 270 try: 271 x = a.ring.coFac.getONE().getRe(); 272 cmplx = True; 273 except Exception, e: 274 pass; 275 try: 276 if eps == None: 277 if cmplx: 278 R = RootFactory.complexAlgebraicNumbersComplex(a); 279 else: 280 R = RootFactory.complexAlgebraicNumbers(a); 281 # R = ComplexRootsSturm(a.ring.coFac).complexRoots( a ); 282 # R = [ r.centerApprox() for r in R ]; 283 # R = [ r.ring.getRoot() for r in R ]; 284 R = [ RingElem(r) for r in R ]; 285 else: 286 if cmplx: 287 R = RootFactory.complexAlgebraicNumbersComplex(a,eps); 288 else: 289 R = RootFactory.complexAlgebraicNumbers(a,eps); 290 R = [ RingElem(r) for r in R ]; 291 # R = [ r.decimalMagnitude() for r in R ]; 292 # R = ComplexRootsSturm(a.ring.coFac).complexRoots( a, eps ); 293 # R = ComplexRootsSturm(a.ring.coFac).approximateRoots( a, eps ); 294 return R; 295 except Exception, e: 296 print "error " + str(e) 297 return None
298
299 - def integrate(self,a):
300 '''Integrate (univariate) rational function. 301 ''' 302 if isinstance(a,RingElem): 303 a = a.elem; 304 else: 305 a = self.element( str(a) ); 306 a = a.elem; 307 cf = self.ring; 308 try: 309 cf = cf.ring; 310 except: 311 pass; 312 integrator = ElementaryIntegration(cf.coFac); 313 ei = integrator.integrate(a); 314 return ei;
315
316 - def powerseriesRing(self):
317 '''Get a power series ring from this ring. 318 ''' 319 pr = MultiVarPowerSeriesRing(self.ring); 320 return MultiSeriesRing(ring=pr);
321 322
323 -class Ideal:
324 '''Represents a JAS polynomial ideal: PolynomialList and Ideal. 325 326 Methods for Groebner bases, ideal sum, intersection and others. 327 ''' 328
329 - def __init__(self,ring,polystr="",list=None):
330 '''Ideal constructor. 331 ''' 332 self.ring = ring; 333 if list == None: 334 sr = StringReader( polystr ); 335 tok = GenPolynomialTokenizer(ring.ring,sr); 336 self.list = tok.nextPolynomialList(); 337 else: 338 self.list = pylist2arraylist(list,rec=1); 339 self.pset = OrderedPolynomialList(ring.ring,self.list); 340 self.roots = None; 341 self.prime = None; 342 self.primary = None;
343
344 - def __str__(self):
345 '''Create a string representation. 346 ''' 347 return str(self.pset.toScript());
348
349 - def paramideal(self):
350 '''Create an ideal in a polynomial ring with parameter coefficients. 351 ''' 352 return ParamIdeal(self.ring,"",self.list);
353
354 - def GB(self):
355 '''Compute a Groebner base. 356 ''' 357 s = self.pset; 358 cofac = s.ring.coFac; 359 F = s.list; 360 t = System.currentTimeMillis(); 361 if cofac.isField(): 362 G = GroebnerBaseSeq().GB(F); 363 else: 364 v = None; 365 try: 366 v = cofac.vars; 367 except: 368 pass 369 if v == None: 370 G = GroebnerBasePseudoSeq(cofac).GB(F); 371 else: 372 G = GroebnerBasePseudoRecSeq(cofac).GB(F); 373 t = System.currentTimeMillis() - t; 374 print "sequential GB executed in %s ms" % t; 375 return Ideal(self.ring,"",G);
376
377 - def isGB(self):
378 '''Test if this is a Groebner base. 379 ''' 380 s = self.pset; 381 cofac = s.ring.coFac; 382 F = s.list; 383 t = System.currentTimeMillis(); 384 if cofac.isField(): 385 b = GroebnerBaseSeq().isGB(F); 386 else: 387 v = None; 388 try: 389 v = cofac.vars; 390 except: 391 pass 392 if v == None: 393 b = GroebnerBasePseudoSeq(cofac).isGB(F); 394 else: 395 b = GroebnerBasePseudoRecSeq(cofac).isGB(F); 396 t = System.currentTimeMillis() - t; 397 print "isGB executed in %s ms" % t; 398 return b;
399 400
401 - def eGB(self):
402 '''Compute an e-Groebner base. 403 ''' 404 s = self.pset; 405 cofac = s.ring.coFac; 406 F = s.list; 407 t = System.currentTimeMillis(); 408 if cofac.isField(): 409 G = GroebnerBaseSeq().GB(F); 410 else: 411 G = EGroebnerBaseSeq().GB(F) 412 t = System.currentTimeMillis() - t; 413 print "sequential e-GB executed in %s ms" % t; 414 return Ideal(self.ring,"",G);
415 416
417 - def iseGB(self):
418 '''Test if this is an e-Groebner base. 419 ''' 420 s = self.pset; 421 cofac = s.ring.coFac; 422 F = s.list; 423 t = System.currentTimeMillis(); 424 if cofac.isField(): 425 b = GroebnerBaseSeq().isGB(F); 426 else: 427 b = EGroebnerBaseSeq().isGB(F) 428 t = System.currentTimeMillis() - t; 429 print "is e-GB test executed in %s ms" % t; 430 return b;
431 432
433 - def dGB(self):
434 '''Compute an d-Groebner base. 435 ''' 436 s = self.pset; 437 cofac = s.ring.coFac; 438 F = s.list; 439 t = System.currentTimeMillis(); 440 if cofac.isField(): 441 G = GroebnerBaseSeq().GB(F); 442 else: 443 G = DGroebnerBaseSeq().GB(F) 444 t = System.currentTimeMillis() - t; 445 print "sequential d-GB executed in %s ms" % t; 446 return Ideal(self.ring,"",G);
447 448
449 - def isdGB(self):
450 '''Test if this is a d-Groebner base. 451 ''' 452 s = self.pset; 453 cofac = s.ring.coFac; 454 F = s.list; 455 t = System.currentTimeMillis(); 456 if cofac.isField(): 457 b = GroebnerBaseSeq().isGB(F); 458 else: 459 b = DGroebnerBaseSeq().isGB(F) 460 t = System.currentTimeMillis() - t; 461 print "is d-GB test executed in %s ms" % t; 462 return b;
463 464
465 - def parGB(self,th):
466 '''Compute in parallel a Groebner base. 467 ''' 468 s = self.pset; 469 F = s.list; 470 bbpar = GroebnerBaseSeqPairParallel(th); 471 t = System.currentTimeMillis(); 472 G = bbpar.GB(F); 473 t = System.currentTimeMillis() - t; 474 bbpar.terminate(); 475 print "parallel-new %s executed in %s ms" % (th, t); 476 return Ideal(self.ring,"",G);
477
478 - def parOldGB(self,th):
479 '''Compute in parallel a Groebner base. 480 ''' 481 s = self.pset; 482 F = s.list; 483 bbpar = GroebnerBaseParallel(th); 484 t = System.currentTimeMillis(); 485 G = bbpar.GB(F); 486 t = System.currentTimeMillis() - t; 487 bbpar.terminate(); 488 print "parallel-old %s executed in %s ms" % (th, t); 489 return Ideal(self.ring,"",G);
490
491 - def distGB(self,th=2,machine="examples/machines.localhost",port=7114):
492 '''Compute on a distributed system a Groebner base. 493 ''' 494 s = self.pset; 495 F = s.list; 496 t = System.currentTimeMillis(); 497 # G = GroebnerBaseDistributed.Server(F,th); 498 #G = GBDist(th,machine,port).execute(F); 499 gbd = GBDist(th,machine,port); 500 t1 = System.currentTimeMillis(); 501 G = gbd.execute(F); 502 t1 = System.currentTimeMillis() - t1; 503 gbd.terminate(0); 504 t = System.currentTimeMillis() - t; 505 print "distributed %s executed in %s ms (%s ms start-up)" % (th,t1,t-t1); 506 return Ideal(self.ring,"",G);
507
508 - def distClient(self,port=8114):
509 '''Client for a distributed computation. 510 ''' 511 s = self.pset; 512 es = ExecutableServer( port ); 513 es.init(); 514 return None;
515
516 - def eReduction(self,p):
517 '''Compute a e-normal form of p with respect to this ideal. 518 ''' 519 s = self.pset; 520 G = s.list; 521 if isinstance(p,RingElem): 522 p = p.elem; 523 t = System.currentTimeMillis(); 524 n = EReductionSeq().normalform(G,p); 525 t = System.currentTimeMillis() - t; 526 print "sequential eReduction executed in %s ms" % t; 527 return RingElem(n);
528
529 - def reduction(self,p):
530 '''Compute a normal form of p with respect to this ideal. 531 ''' 532 s = self.pset; 533 G = s.list; 534 if isinstance(p,RingElem): 535 p = p.elem; 536 t = System.currentTimeMillis(); 537 n = ReductionSeq().normalform(G,p); 538 t = System.currentTimeMillis() - t; 539 print "sequential reduction executed in %s ms" % t; 540 return RingElem(n);
541
542 - def NF(self,reducer):
543 '''Compute a normal form of this ideal with respect to reducer. 544 ''' 545 s = self.pset; 546 F = s.list; 547 G = reducer.list; 548 t = System.currentTimeMillis(); 549 N = ReductionSeq().normalform(G,F); 550 t = System.currentTimeMillis() - t; 551 print "sequential NF executed in %s ms" % t; 552 return Ideal(self.ring,"",N);
553
554 - def intersectRing(self,ring):
555 '''Compute the intersection of this and the given polynomial ring. 556 ''' 557 s = jas.application.Ideal(self.pset); 558 N = s.intersect(ring.ring); 559 return Ideal(ring,"",N.getList());
560
561 - def intersect(self,id2):
562 '''Compute the intersection of this and the given ideal. 563 ''' 564 s1 = jas.application.Ideal(self.pset); 565 s2 = jas.application.Ideal(id2.pset); 566 N = s1.intersect(s2); 567 return Ideal(self.ring,"",N.getList());
568
569 - def eliminateRing(self,ring):
570 '''Compute the elimination ideal of this and the given polynomial ring. 571 ''' 572 s = jas.application.Ideal(self.pset); 573 N = s.eliminate(ring.ring); 574 r = Ring( ring=N.getRing() ); 575 return Ideal(r,"",N.getList());
576
577 - def sum(self,other):
578 '''Compute the sum of this and the ideal. 579 ''' 580 s = jas.application.Ideal(self.pset); 581 t = jas.application.Ideal(other.pset); 582 N = s.sum( t ); 583 return Ideal(self.ring,"",N.getList());
584
585 - def optimize(self):
586 '''Optimize the term order on the variables. 587 ''' 588 p = self.pset; 589 o = TermOrderOptimization.optimizeTermOrder(p); 590 r = Ring("",o.ring); 591 return Ideal(r,"",o.list);
592
593 - def realRoots(self):
594 '''Compute real roots of 0-dim ideal. 595 ''' 596 I = jas.application.Ideal(self.pset); 597 self.roots = jas.application.PolyUtilApp.realAlgebraicRoots(I); 598 for R in self.roots: 599 R.doDecimalApproximation(); 600 return self.roots;
601
602 - def realRootsPrint(self):
603 '''Print decimal approximation of real roots of 0-dim ideal. 604 ''' 605 if self.roots == None: 606 I = jas.application.Ideal(self.pset); 607 self.roots = jas.application.PolyUtilApp.realAlgebraicRoots(I); 608 for R in self.roots: 609 R.doDecimalApproximation(); 610 D = []; 611 for Ir in self.roots: 612 for Dr in Ir.decimalApproximation(): 613 print str(Dr); 614 print;
615
616 - def radicalDecomp(self):
617 '''Compute radical decomposition of this ideal. 618 ''' 619 I = jas.application.Ideal(self.pset); 620 self.radical = I.radicalDecomposition(); 621 return self.radical;
622
623 - def complexRoots(self):
624 '''Compute complex roots of 0-dim ideal. 625 ''' 626 I = jas.application.Ideal(self.pset); 627 self.croots = jas.application.PolyUtilApp.complexAlgebraicRoots(I); 628 #for R in self.croots: 629 # R.doDecimalApproximation(); 630 return self.croots;
631
632 - def primeDecomp(self):
633 '''Compute prime decomposition of this ideal. 634 ''' 635 I = jas.application.Ideal(self.pset); 636 self.prime = I.primeDecomposition(); 637 return self.prime;
638
639 - def primaryDecomp(self):
640 '''Compute primary decomposition of this ideal. 641 ''' 642 I = jas.application.Ideal(self.pset); 643 ## if self.prime == None: 644 ## self.prime = I.primeDecomposition(); 645 self.primary = I.primaryDecomposition(); 646 return self.primary;
647
648 - def toInteger(self):
649 '''Convert rational coefficients to integer coefficients. 650 ''' 651 p = self.pset; 652 l = p.list; 653 r = p.ring; 654 ri = GenPolynomialRing( BigInteger(), r.nvar, r.tord, r.vars ); 655 pi = PolyUtil.integerFromRationalCoefficients(ri,l); 656 r = Ring("",ri); 657 return Ideal(r,"",pi);
658
659 - def toModular(self,mf):
660 '''Convert integer coefficients to modular coefficients. 661 ''' 662 p = self.pset; 663 l = p.list; 664 r = p.ring; 665 rm = GenPolynomialRing( mf, r.nvar, r.tord, r.vars ); 666 pm = PolyUtil.fromIntegerCoefficients(rm,l); 667 r = Ring("",rm); 668 return Ideal(r,"",pm);
669 670 ## def syzygy(self): 671 ## '''Syzygy of generating polynomials. 672 ## ''' 673 ## p = self.pset; 674 ## l = p.list; 675 ## s = SyzygyAbstract().zeroRelations( l ); 676 ## m = Module("",p.ring); 677 ## return SubModule(m,"",s); 678 679
680 -class ParamIdeal:
681 '''Represents a JAS polynomial ideal with polynomial coefficients. 682 683 Methods to compute comprehensive Groebner bases. 684 ''' 685
686 - def __init__(self,ring,polystr="",list=None,gbsys=None):
687 '''Parametric ideal constructor. 688 ''' 689 self.ring = ring; 690 if list == None and polystr != None: 691 sr = StringReader( polystr ); 692 tok = GenPolynomialTokenizer(ring.ring,sr); 693 self.list = tok.nextPolynomialList(); 694 else: 695 self.list = pylist2arraylist(list,rec=1); 696 self.gbsys = gbsys; 697 self.pset = OrderedPolynomialList(ring.ring,self.list);
698
699 - def __str__(self):
700 '''Create a string representation. 701 ''' 702 if self.gbsys == None: 703 return self.pset.toScript(); 704 else: 705 return self.gbsys.toString(); # toScript() not available
706 # return self.pset.toScript() + "\n" + self.gbsys.toScript(); 707
708 - def optimizeCoeff(self):
709 '''Optimize the term order on the variables of the coefficients. 710 ''' 711 p = self.pset; 712 o = TermOrderOptimization.optimizeTermOrderOnCoefficients(p); 713 r = Ring("",o.ring); 714 return ParamIdeal(r,"",o.list);
715
716 - def optimizeCoeffQuot(self):
717 '''Optimize the term order on the variables of the quotient coefficients. 718 ''' 719 p = self.pset; 720 l = p.list; 721 r = p.ring; 722 q = r.coFac; 723 c = q.ring; 724 rc = GenPolynomialRing( c, r.nvar, r.tord, r.vars ); 725 #print "rc = ", rc; 726 lp = PolyUfdUtil.integralFromQuotientCoefficients(rc,l); 727 #print "lp = ", lp; 728 pp = PolynomialList(rc,lp); 729 #print "pp = ", pp; 730 oq = TermOrderOptimization.optimizeTermOrderOnCoefficients(pp); 731 oor = oq.ring; 732 qo = oor.coFac; 733 cq = QuotientRing( qo ); 734 rq = GenPolynomialRing( cq, r.nvar, r.tord, r.vars ); 735 #print "rq = ", rq; 736 o = PolyUfdUtil.quotientFromIntegralCoefficients(rq,oq.list); 737 r = Ring("",rq); 738 return ParamIdeal(r,"",o);
739
740 - def toIntegralCoeff(self):
741 '''Convert rational function coefficients to integral function coefficients. 742 ''' 743 p = self.pset; 744 l = p.list; 745 r = p.ring; 746 q = r.coFac; 747 c = q.ring; 748 rc = GenPolynomialRing( c, r.nvar, r.tord, r.vars ); 749 #print "rc = ", rc; 750 lp = PolyUfdUtil.integralFromQuotientCoefficients(rc,l); 751 #print "lp = ", lp; 752 r = Ring("",rc); 753 return ParamIdeal(r,"",lp);
754
755 - def toModularCoeff(self,mf):
756 '''Convert integral function coefficients to modular function coefficients. 757 ''' 758 p = self.pset; 759 l = p.list; 760 r = p.ring; 761 c = r.coFac; 762 #print "c = ", c; 763 cm = GenPolynomialRing( mf, c.nvar, c.tord, c.vars ); 764 #print "cm = ", cm; 765 rm = GenPolynomialRing( cm, r.nvar, r.tord, r.vars ); 766 #print "rm = ", rm; 767 pm = PolyUfdUtil.fromIntegerCoefficients(rm,l); 768 r = Ring("",rm); 769 return ParamIdeal(r,"",pm);
770
771 - def toQuotientCoeff(self):
772 '''Convert integral function coefficients to rational function coefficients. 773 ''' 774 p = self.pset; 775 l = p.list; 776 r = p.ring; 777 c = r.coFac; 778 #print "c = ", c; 779 q = QuotientRing(c); 780 #print "q = ", q; 781 qm = GenPolynomialRing( q, r.nvar, r.tord, r.vars ); 782 #print "qm = ", qm; 783 pm = PolyUfdUtil.quotientFromIntegralCoefficients(qm,l); 784 r = Ring("",qm); 785 return ParamIdeal(r,"",pm);
786
787 - def GB(self):
788 '''Compute a Groebner base. 789 ''' 790 I = Ideal(self.ring,"",self.pset.list); 791 g = I.GB(); 792 return ParamIdeal(g.ring,"",g.pset.list);
793
794 - def isGB(self):
795 '''Test if this is a Groebner base. 796 ''' 797 I = Ideal(self.ring,"",self.pset.list); 798 return I.isGB();
799
800 - def CGB(self):
801 '''Compute a comprehensive Groebner base. 802 ''' 803 s = self.pset; 804 F = s.list; 805 t = System.currentTimeMillis(); 806 if self.gbsys == None: 807 self.gbsys = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).GBsys(F); 808 G = self.gbsys.getCGB(); 809 t = System.currentTimeMillis() - t; 810 print "sequential comprehensive executed in %s ms" % t; 811 return ParamIdeal(self.ring,"",G,self.gbsys);
812
813 - def CGBsystem(self):
814 '''Compute a comprehensive Groebner system. 815 ''' 816 s = self.pset; 817 F = s.list; 818 t = System.currentTimeMillis(); 819 S = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).GBsys(F); 820 t = System.currentTimeMillis() - t; 821 print "sequential comprehensive system executed in %s ms" % t; 822 return ParamIdeal(self.ring,None,F,S);
823
824 - def isCGB(self):
825 '''Test if this is a comprehensive Groebner base. 826 ''' 827 s = self.pset; 828 F = s.list; 829 t = System.currentTimeMillis(); 830 b = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).isGB(F); 831 t = System.currentTimeMillis() - t; 832 print "isCGB executed in %s ms" % t; 833 return b;
834
835 - def isCGBsystem(self):
836 '''Test if this is a comprehensive Groebner system. 837 ''' 838 s = self.pset; 839 S = self.gbsys; 840 t = System.currentTimeMillis(); 841 b = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).isGBsys(S); 842 t = System.currentTimeMillis() - t; 843 print "isCGBsystem executed in %s ms" % t; 844 return b;
845
846 - def regularRepresentation(self):
847 '''Convert Groebner system to a representation with regular ring coefficents. 848 ''' 849 if self.gbsys == None: 850 return None; 851 G = PolyUtilApp.toProductRes(self.gbsys.list); 852 ring = Ring(None,G[0].ring); 853 return ParamIdeal(ring,None,G);
854
855 - def regularRepresentationBC(self):
856 '''Convert Groebner system to a boolean closed representation with regular ring coefficents. 857 ''' 858 if self.gbsys == None: 859 return None; 860 G = PolyUtilApp.toProductRes(self.gbsys.list); 861 ring = Ring(None,G[0].ring); 862 res = RReductionSeq(); 863 G = res.booleanClosure(G); 864 return ParamIdeal(ring,None,G);
865
866 - def regularGB(self):
867 '''Compute a Groebner base over a regular ring. 868 ''' 869 s = self.pset; 870 F = s.list; 871 t = System.currentTimeMillis(); 872 G = RGroebnerBasePseudoSeq(self.ring.ring.coFac).GB(F); 873 t = System.currentTimeMillis() - t; 874 print "sequential regular GB executed in %s ms" % t; 875 return ParamIdeal(self.ring,None,G);
876
877 - def isRegularGB(self):
878 '''Test if this is Groebner base over a regular ring. 879 ''' 880 s = self.pset; 881 F = s.list; 882 t = System.currentTimeMillis(); 883 b = RGroebnerBasePseudoSeq(self.ring.ring.coFac).isGB(F); 884 t = System.currentTimeMillis() - t; 885 print "isRegularGB executed in %s ms" % t; 886 return b;
887
888 - def stringSlice(self):
889 '''Get each component (slice) of regular ring coefficients separate. 890 ''' 891 s = self.pset; 892 b = PolyUtilApp.productToString(s); 893 return b;
894 895
896 -class SolvableRing(Ring):
897 '''Represents a JAS solvable polynomial ring: GenSolvablePolynomialRing. 898 899 Has a method to create solvable ideals. 900 ''' 901
902 - def __init__(self,ringstr="",ring=None):
903 '''Solvable polynomial ring constructor. 904 ''' 905 if ring == None: 906 sr = StringReader( ringstr ); 907 tok = GenPolynomialTokenizer(sr); 908 self.pset = tok.nextSolvablePolynomialSet(); 909 self.ring = self.pset.ring; 910 else: 911 self.ring = ring; 912 if not self.ring.isAssociative(): 913 print "warning: ring is not associative";
914
915 - def __str__(self):
916 '''Create a string representation. 917 ''' 918 return str(self.ring.toScript());
919
920 - def ideal(self,ringstr="",list=None):
921 '''Create a solvable ideal. 922 ''' 923 return SolvableIdeal(self,ringstr,list);
924
925 - def one(self):
926 '''Get the one of the solvable polynomial ring. 927 ''' 928 return RingElem( self.ring.getONE() );
929
930 - def zero(self):
931 '''Get the zero of the solvable polynomial ring. 932 ''' 933 return RingElem( self.ring.getZERO() );
934
935 - def element(self,polystr):
936 '''Create an element from a string. 937 ''' 938 I = SolvableIdeal(self, "( " + polystr + " )"); 939 list = I.pset.list; 940 if len(list) > 0: 941 return RingElem( list[0] );
942 943
944 -class SolvableIdeal:
945 '''Represents a JAS solvable polynomial ideal. 946 947 Methods for left, right two-sided Groebner basees and others. 948 ''' 949
950 - def __init__(self,ring,ringstr="",list=None):
951 '''Constructor for an ideal in a solvable polynomial ring. 952 ''' 953 self.ring = ring; 954 if list == None: 955 sr = StringReader( ringstr ); 956 tok = GenPolynomialTokenizer(ring.ring,sr); 957 self.list = tok.nextSolvablePolynomialList(); 958 else: 959 self.list = pylist2arraylist(list,rec=1); 960 self.pset = OrderedPolynomialList(ring.ring,self.list);
961
962 - def __str__(self):
963 '''Create a string representation. 964 ''' 965 return str(self.pset.toScript());
966
967 - def leftGB(self):
968 '''Compute a left Groebner base. 969 ''' 970 s = self.pset; 971 F = s.list; 972 t = System.currentTimeMillis(); 973 G = SolvableGroebnerBaseSeq().leftGB(F); 974 t = System.currentTimeMillis() - t; 975 print "executed leftGB in %s ms" % t; 976 return SolvableIdeal(self.ring,"",G);
977
978 - def isLeftGB(self):
979 '''Test if this is a left Groebner base. 980 ''' 981 s = self.pset; 982 F = s.list; 983 t = System.currentTimeMillis(); 984 b = SolvableGroebnerBaseSeq().isLeftGB(F); 985 t = System.currentTimeMillis() - t; 986 print "isLeftGB executed in %s ms" % t; 987 return b;
988
989 - def twosidedGB(self):
990 '''Compute a two-sided Groebner base. 991 ''' 992 s = self.pset; 993 F = s.list; 994 t = System.currentTimeMillis(); 995 G = SolvableGroebnerBaseSeq().twosidedGB(F); 996 t = System.currentTimeMillis() - t; 997 print "executed twosidedGB in %s ms" % t; 998 return SolvableIdeal(self.ring,"",G);
999
1000 - def isTwosidedGB(self):
1001 '''Test if this is a two-sided Groebner base. 1002 ''' 1003 s = self.pset; 1004 F = s.list; 1005 t = System.currentTimeMillis(); 1006 b = SolvableGroebnerBaseSeq().isTwosidedGB(F); 1007 t = System.currentTimeMillis() - t; 1008 print "isTwosidedGB executed in %s ms" % t; 1009 return b;
1010
1011 - def rightGB(self):
1012 '''Compute a right Groebner base. 1013 ''' 1014 s = self.pset; 1015 F = s.list; 1016 t = System.currentTimeMillis(); 1017 G = SolvableGroebnerBaseSeq().rightGB(F); 1018 t = System.currentTimeMillis() - t; 1019 print "executed rightGB in %s ms" % t; 1020 return SolvableIdeal(self.ring,"",G);
1021
1022 - def isRightGB(self):
1023 '''Test if this is a right Groebner base. 1024 ''' 1025 s = self.pset; 1026 F = s.list; 1027 t = System.currentTimeMillis(); 1028 b = SolvableGroebnerBaseSeq().isRightGB(F); 1029 t = System.currentTimeMillis() - t; 1030 print "isRightGB executed in %s ms" % t; 1031 return b;
1032
1033 - def intersect(self,ring):
1034 '''Compute the intersection of this and the polynomial ring. 1035 ''' 1036 s = jas.application.SolvableIdeal(self.pset); 1037 N = s.intersect(ring.ring); 1038 return SolvableIdeal(self.ring,"",N.getList());
1039
1040 - def sum(self,other):
1041 '''Compute the sum of this and the other ideal. 1042 ''' 1043 s = jas.application.SolvableIdeal(self.pset); 1044 t = jas.application.SolvableIdeal(other.pset); 1045 N = s.sum( t ); 1046 return SolvableIdeal(self.ring,"",N.getList());
1047
1048 - def parLeftGB(self,th):
1049 '''Compute a left Groebner base in parallel. 1050 ''' 1051 s = self.pset; 1052 F = s.list; 1053 bbpar = SolvableGroebnerBaseParallel(th); 1054 t = System.currentTimeMillis(); 1055 G = bbpar.leftGB(F); 1056 t = System.currentTimeMillis() - t; 1057 bbpar.terminate(); 1058 print "parallel %s leftGB executed in %s ms" % (th, t); 1059 return SolvableIdeal(self.ring,"",G);
1060
1061 - def parTwosidedGB(self,th):
1062 '''Compute a two-sided Groebner base in parallel. 1063 ''' 1064 s = self.pset; 1065 F = s.list; 1066 bbpar = SolvableGroebnerBaseParallel(th); 1067 t = System.currentTimeMillis(); 1068 G = bbpar.twosidedGB(F); 1069 t = System.currentTimeMillis() - t; 1070 bbpar.terminate(); 1071 print "parallel %s twosidedGB executed in %s ms" % (th, t); 1072 return SolvableIdeal(self.ring,"",G);
1073 1074
1075 -class Module:
1076 '''Represents a JAS module over a polynomial ring. 1077 1078 Method to create sub-modules. 1079 ''' 1080
1081 - def __init__(self,modstr="",ring=None,cols=0):
1082 '''Module constructor. 1083 ''' 1084 if ring == None: 1085 sr = StringReader( modstr ); 1086 tok = GenPolynomialTokenizer(sr); 1087 self.mset = tok.nextSubModuleSet(); 1088 if self.mset.cols >= 0: 1089 self.cols = self.mset.cols; 1090 else: 1091 self.cols = cols; 1092 else: 1093 self.mset = ModuleList(ring.ring,None); 1094 self.cols = cols; 1095 self.ring = self.mset.ring;
1096
1097 - def __str__(self):
1098 '''Create a string representation. 1099 ''' 1100 return str(self.mset.toScript());
1101
1102 - def submodul(self,modstr="",list=None):
1103 '''Create a sub-module. 1104 ''' 1105 return SubModule(self,modstr,list);
1106
1107 - def element(self,modstr):
1108 '''Create an element from a string. 1109 ''' 1110 I = SubModule(self, "( " + modstr + " )"); 1111 list = I.mset.list; 1112 if len(list) > 0: 1113 return RingElem( list[0] );
1114
1115 - def gens(self):
1116 '''Get the generators of this module. 1117 ''' 1118 gm = GenVectorModul(self.ring,self.cols); 1119 L = gm.generators(); 1120 #for g in L: 1121 # print "g = ", str(g); 1122 N = [ RingElem(e) for e in L ]; # want use val here, but can not 1123 return N;
1124 1125
1126 -class SubModule:
1127 '''Represents a JAS sub-module over a polynomial ring. 1128 1129 Methods to compute Groebner bases. 1130 ''' 1131
1132 - def __init__(self,module,modstr="",list=None):
1133 '''Constructor for a sub-module. 1134 ''' 1135 self.module = module; 1136 if list == None: 1137 sr = StringReader( modstr ); 1138 tok = GenPolynomialTokenizer(module.ring,sr); 1139 self.list = tok.nextSubModuleList(); 1140 else: 1141 if isinstance(list,PyList) or isinstance(list,PyTuple): 1142 if len(list) != 0: 1143 if isinstance(list[0],RingElem): 1144 list = [ re.elem for re in list ]; 1145 self.list = pylist2arraylist(list,self.module.ring,rec=2); 1146 else: 1147 self.list = list; 1148 #print "list = ", str(list); 1149 #e = self.list[0]; 1150 #print "e = ", e; 1151 self.mset = OrderedModuleList(module.ring,self.list); 1152 self.cols = self.mset.cols; 1153 self.rows = self.mset.rows; 1154 #print "list = %s" % self.list; 1155 #print "cols = %s" % self.cols; 1156 #print "mset = %s" % self.mset.toString(); 1157 #print "mset = %s" % self.mset.toScript(); 1158 self.pset = self.mset.getPolynomialList();
1159
1160 - def __str__(self):
1161 '''Create a string representation. 1162 ''' 1163 return str(self.mset.toScript()); # + "\n\n" + str(self.pset);
1164
1165 - def GB(self):
1166 '''Compute a Groebner base. 1167 ''' 1168 t = System.currentTimeMillis(); 1169 G = ModGroebnerBaseAbstract().GB(self.mset); 1170 t = System.currentTimeMillis() - t; 1171 print "executed module GB in %s ms" % t; 1172 return SubModule(self.module,"",G.list);
1173
1174 - def isGB(self):
1175 '''Test if this is a Groebner base. 1176 ''' 1177 t = System.currentTimeMillis(); 1178 b = ModGroebnerBaseAbstract().isGB(self.mset); 1179 t = System.currentTimeMillis() - t; 1180 print "module isGB executed in %s ms" % t; 1181 return b;
1182 1183 ## def isSyzygy(self,g): 1184 ## '''Test if this is a syzygy of the polynomials in g. 1185 ## ''' 1186 ## l = self.list; 1187 ## print "l = %s" % l; 1188 ## print "g = %s" % g; 1189 ## t = System.currentTimeMillis(); 1190 ## z = SyzygyAbstract().isZeroRelation( l, g.list ); 1191 ## t = System.currentTimeMillis() - t; 1192 ## print "executed isSyzygy in %s ms" % t; 1193 ## return z; 1194 1195
1196 -class SolvableModule(Module):
1197 '''Represents a JAS module over a solvable polynomial ring. 1198 1199 Method to create solvable sub-modules. 1200 ''' 1201
1202 - def __init__(self,modstr="",ring=None,cols=0):
1203 '''Solvable module constructor. 1204 ''' 1205 if ring == None: 1206 sr = StringReader( modstr ); 1207 tok = GenPolynomialTokenizer(sr); 1208 self.mset = tok.nextSolvableSubModuleSet(); 1209 if self.mset.cols >= 0: 1210 self.cols = self.mset.cols; 1211 else: 1212 self.mset = ModuleList(ring.ring,None); 1213 self.cols = cols; 1214 self.ring = self.mset.ring;
1215
1216 - def __str__(self):
1217 '''Create a string representation. 1218 ''' 1219 return str(self.mset.toScript());
1220
1221 - def submodul(self,modstr="",list=None):
1222 '''Create a solvable sub-module. 1223 ''' 1224 return SolvableSubModule(self,modstr,list);
1225
1226 - def element(self,modstr):
1227 '''Create an element from a string. 1228 ''' 1229 I = SolvableSubModule(self, "( " + modstr + " )"); 1230 list = I.mset.list; 1231 if len(list) > 0: 1232 return RingElem( list[0] );
1233 1234
1235 -class SolvableSubModule:
1236 '''Represents a JAS sub-module over a solvable polynomial ring. 1237 1238 Methods to compute left, right and two-sided Groebner bases. 1239 ''' 1240
1241 - def __init__(self,module,modstr="",list=None):
1242 '''Constructor for sub-module over a solvable polynomial ring. 1243 ''' 1244 self.module = module; 1245 if list == None: 1246 sr = StringReader( modstr ); 1247 tok = GenPolynomialTokenizer(module.ring,sr); 1248 self.list = tok.nextSolvableSubModuleList(); 1249 else: 1250 if isinstance(list,PyList) or isinstance(list,PyTuple): 1251 self.list = pylist2arraylist(list,self.module.ring,rec=2); 1252 else: 1253 self.list = list; 1254 self.mset = OrderedModuleList(module.ring,self.list); 1255 self.cols = self.mset.cols; 1256 self.rows = self.mset.rows;
1257
1258 - def __str__(self):
1259 '''Create a string representation. 1260 ''' 1261 return str(self.mset.toScript()); # + "\n\n" + str(self.pset);
1262
1263 - def leftGB(self):
1264 '''Compute a left Groebner base. 1265 ''' 1266 t = System.currentTimeMillis(); 1267 G = ModSolvableGroebnerBaseAbstract().leftGB(self.mset); 1268 t = System.currentTimeMillis() - t; 1269 print "executed left module GB in %s ms" % t; 1270 return SolvableSubModule(self.module,"",G.list);
1271
1272 - def isLeftGB(self):
1273 '''Test if this is a left Groebner base. 1274 ''' 1275 t = System.currentTimeMillis(); 1276 b = ModSolvableGroebnerBaseAbstract().isLeftGB(self.mset); 1277 t = System.currentTimeMillis() - t; 1278 print "module isLeftGB executed in %s ms" % t; 1279 return b;
1280
1281 - def twosidedGB(self):
1282 '''Compute a two-sided Groebner base. 1283 ''' 1284 t = System.currentTimeMillis(); 1285 G = ModSolvableGroebnerBaseAbstract().twosidedGB(self.mset); 1286 t = System.currentTimeMillis() - t; 1287 print "executed in %s ms" % t; 1288 return SolvableSubModule(self.module,"",G.list);
1289
1290 - def isTwosidedGB(self):
1291 '''Test if this is a two-sided Groebner base. 1292 ''' 1293 t = System.currentTimeMillis(); 1294 b = ModSolvableGroebnerBaseAbstract().isTwosidedGB(self.mset); 1295 t = System.currentTimeMillis() - t; 1296 print "module isTwosidedGB executed in %s ms" % t; 1297 return b;
1298
1299 - def rightGB(self):
1300 '''Compute a right Groebner base. 1301 ''' 1302 t = System.currentTimeMillis(); 1303 G = ModSolvableGroebnerBaseAbstract().rightGB(self.mset); 1304 t = System.currentTimeMillis() - t; 1305 print "executed module rightGB in %s ms" % t; 1306 return SolvableSubModule(self.module,"",G.list);
1307
1308 - def isRightGB(self):
1309 '''Test if this is a right Groebner base. 1310 ''' 1311 t = System.currentTimeMillis(); 1312 b = ModSolvableGroebnerBaseAbstract().isRightGB(self.mset); 1313 t = System.currentTimeMillis() - t; 1314 print "module isRightGB executed in %s ms" % t; 1315 return b;
1316 1317
1318 -class SeriesRing:
1319 '''Represents a JAS power series ring: UnivPowerSeriesRing. 1320 1321 Methods for univariate power series arithmetic. 1322 ''' 1323
1324 - def __init__(self,ringstr="",truncate=None,ring=None,cofac=None,name="z"):
1325 '''Ring constructor. 1326 ''' 1327 if ring == None: 1328 if len(ringstr) > 0: 1329 sr = StringReader( ringstr ); 1330 tok = GenPolynomialTokenizer(sr); 1331 pset = tok.nextPolynomialSet(); 1332 ring = pset.ring; 1333 vname = ring.vars; 1334 name = vname[0]; 1335 cofac = ring.coFac; 1336 if isinstance(cofac,RingElem): 1337 cofac = cofac.elem; 1338 if truncate == None: 1339 self.ring = UnivPowerSeriesRing(cofac,name); 1340 else: 1341 self.ring = UnivPowerSeriesRing(cofac,truncate,name); 1342 else: 1343 self.ring = ring;
1344
1345 - def __str__(self):
1346 '''Create a string representation. 1347 ''' 1348 return str(self.ring.toScript());
1349
1350 - def gens(self):
1351 '''Get the generators of the power series ring. 1352 ''' 1353 L = self.ring.generators(); 1354 N = [ RingElem(e) for e in L ]; 1355 return N;
1356
1357 - def one(self):
1358 '''Get the one of the power series ring. 1359 ''' 1360 return RingElem( self.ring.getONE() );
1361
1362 - def zero(self):
1363 '''Get the zero of the power series ring. 1364 ''' 1365 return RingElem( self.ring.getZERO() );
1366
1367 - def random(self,n):
1368 '''Get a random power series. 1369 ''' 1370 return RingElem( self.ring.random(n) );
1371
1372 - def exp(self):
1373 '''Get the exponential power series. 1374 ''' 1375 return RingElem( self.ring.getEXP() );
1376
1377 - def sin(self):
1378 '''Get the sinus power series. 1379 ''' 1380 return RingElem( self.ring.getSIN() );
1381
1382 - def cos(self):
1383 '''Get the cosinus power series. 1384 ''' 1385 return RingElem( self.ring.getCOS() );
1386
1387 - def tan(self):
1388 '''Get the tangens power series. 1389 ''' 1390 return RingElem( self.ring.getTAN() );
1391
1392 - def create(self,ifunc=None,jfunc=None,clazz=None):
1393 '''Create a power series with given generating function. 1394 1395 ifunc(int i) must return a value which is used in RingFactory.fromInteger(). 1396 jfunc(int i) must return a value of type ring.coFac. 1397 clazz must implement the Coefficients abstract class. 1398 ''' 1399 class coeff( Coefficients ): 1400 def __init__(self,cofac): 1401 self.coFac = cofac;
1402 def generate(self,i): 1403 if jfunc == None: 1404 return self.coFac.fromInteger( ifunc(i) ); 1405 else: 1406 return jfunc(i);
1407 if clazz == None: 1408 ps = UnivPowerSeries( self.ring, coeff(self.ring.coFac) ); 1409 else: 1410 ps = UnivPowerSeries( self.ring, clazz ); 1411 return RingElem( ps ); 1412
1413 - def fixPoint(self,psmap):
1414 '''Create a power series as fixed point of the given mapping. 1415 1416 psmap must implement the UnivPowerSeriesMap interface. 1417 ''' 1418 ps = self.ring.fixPoint( psmap ); 1419 return RingElem( ps );
1420
1421 - def gcd(self,a,b):
1422 '''Compute the greatest common divisor of a and b. 1423 ''' 1424 if isinstance(a,RingElem): 1425 a = a.elem; 1426 if isinstance(b,RingElem): 1427 b = b.elem; 1428 return RingElem( a.gcd(b) );
1429
1430 - def fromPoly(self,a):
1431 '''Convert a GenPolynomial to a power series. 1432 ''' 1433 if isinstance(a,RingElem): 1434 a = a.elem; 1435 return RingElem( self.ring.fromPolynomial(a) );
1436 1437
1438 -class MultiSeriesRing:
1439 '''Represents a JAS power series ring: MultiVarPowerSeriesRing. 1440 1441 Methods for multivariate power series arithmetic. 1442 ''' 1443
1444 - def __init__(self,ringstr="",truncate=None,ring=None,cofac=None,names=None):
1445 '''Ring constructor. 1446 ''' 1447 if ring == None: 1448 if len(ringstr) > 0: 1449 sr = StringReader( ringstr ); 1450 tok = GenPolynomialTokenizer(sr); 1451 pset = tok.nextPolynomialSet(); 1452 ring = pset.ring; 1453 names = ring.vars; 1454 cofac = ring.coFac; 1455 if isinstance(cofac,RingElem): 1456 cofac = cofac.elem; 1457 if truncate == None: 1458 self.ring = MultiVarPowerSeriesRing(cofac,names); 1459 else: 1460 self.ring = MultiVarPowerSeriesRing(cofac,len(names),truncate,names); 1461 else: 1462 self.ring = ring;
1463
1464 - def __str__(self):
1465 '''Create a string representation. 1466 ''' 1467 return str(self.ring.toScript());
1468
1469 - def gens(self):
1470 '''Get the generators of the power series ring. 1471 ''' 1472 L = self.ring.generators(); 1473 N = [ RingElem(e) for e in L ]; 1474 return N;
1475
1476 - def one(self):
1477 '''Get the one of the power series ring. 1478 ''' 1479 return RingElem( self.ring.getONE() );
1480
1481 - def zero(self):
1482 '''Get the zero of the power series ring. 1483 ''' 1484 return RingElem( self.ring.getZERO() );
1485
1486 - def random(self,n):
1487 '''Get a random power series. 1488 ''' 1489 return RingElem( self.ring.random(n) );
1490
1491 - def exp(self,r):
1492 '''Get the exponential power series, var r. 1493 ''' 1494 return RingElem( self.ring.getEXP(r) );
1495
1496 - def sin(self,r):
1497 '''Get the sinus power series, var r. 1498 ''' 1499 return RingElem( self.ring.getSIN(r) );
1500
1501 - def cos(self,r):
1502 '''Get the cosinus power series, var r. 1503 ''' 1504 return RingElem( self.ring.getCOS(r) );
1505
1506 - def tan(self,r):
1507 '''Get the tangens power series, var r. 1508 ''' 1509 return RingElem( self.ring.getTAN(r) );
1510
1511 - def create(self,ifunc=None,jfunc=None,clazz=None):
1512 '''Create a power series with given generating function. 1513 1514 ifunc(int i) must return a value which is used in RingFactory.fromInteger(). 1515 jfunc(int i) must return a value of type ring.coFac. 1516 clazz must implement the Coefficients abstract class. 1517 ''' 1518 class coeff( MultiVarCoefficients ): 1519 def __init__(self,r): 1520 MultiVarCoefficients.__init__(self,r); 1521 self.coFac = r.coFac;
1522 def generate(self,i): 1523 if jfunc == None: 1524 return self.coFac.fromInteger( ifunc(i) ); 1525 else: 1526 return jfunc(i);
1527 #print "ifunc" 1528 if clazz == None: 1529 ps = MultiVarPowerSeries( self.ring, coeff(self.ring) ); 1530 else: 1531 ps = MultiVarPowerSeries( self.ring, clazz ); 1532 #print "ps ", ps.toScript(); 1533 return RingElem( ps ); 1534
1535 - def fixPoint(self,psmap):
1536 '''Create a power series as fixed point of the given mapping. 1537 1538 psmap must implement the UnivPowerSeriesMap interface. 1539 ''' 1540 ps = self.ring.fixPoint( psmap ); 1541 return RingElem( ps );
1542
1543 - def gcd(self,a,b):
1544 '''Compute the greatest common divisor of a and b. 1545 ''' 1546 if isinstance(a,RingElem): 1547 a = a.elem; 1548 if isinstance(b,RingElem): 1549 b = b.elem; 1550 return RingElem( a.gcd(b) );
1551
1552 - def fromPoly(self,a):
1553 '''Convert a GenPolynomial to a power series. 1554 ''' 1555 if isinstance(a,RingElem): 1556 a = a.elem; 1557 return RingElem( self.ring.fromPolynomial(a) );
1558 1559
1560 -class PSIdeal:
1561 '''Represents a JAS power series ideal. 1562 1563 Method for Standard bases. 1564 ''' 1565
1566 - def __init__(self,ring,polylist,ideal=None,list=None):
1567 '''PSIdeal constructor. 1568 ''' 1569 if isinstance(ring,Ring) or isinstance(ring,PolyRing): 1570 ring = MultiVarPowerSeriesRing(ring.ring); 1571 if isinstance(ring,MultiSeriesRing): 1572 ring = ring.ring; 1573 self.ring = ring; 1574 #print "ring = ", ring.toScript(); 1575 if ideal != None: 1576 polylist = ideal.pset.list; 1577 if list == None: 1578 self.polylist = pylist2arraylist( [ a.elem for a in polylist ] ); 1579 #print "polylist = ", self.polylist; 1580 self.list = self.ring.fromPolynomial(self.polylist); 1581 else: 1582 self.polylist = None; 1583 self.list = pylist2arraylist( [ a.elem for a in list ] );
1584
1585 - def __str__(self):
1586 '''Create a string representation. 1587 ''' 1588 return str([ a.toScript() for a in self.list ]);
1589
1590 - def STD(self,trunc=None):
1591 '''Compute a standard base. 1592 ''' 1593 pr = self.ring; 1594 if trunc != None: 1595 pr.setTruncate(trunc); 1596 #print "pr = ", pr.toScript(); 1597 F = self.list; 1598 #print "F = ", F; 1599 tm = StandardBaseSeq(); 1600 t = System.currentTimeMillis(); 1601 S = tm.STD(F); 1602 t = System.currentTimeMillis() - t; 1603 print "sequential standard base executed in %s ms" % t; 1604 #Sp = [ RingElem(a.asPolynomial()) for a in S ]; 1605 Sp = [ RingElem(a) for a in S ]; 1606 #return Sp; 1607 return PSIdeal(self.ring,None,list=Sp);
1608 1609
1610 -def pylist2arraylist(list,fac=None,rec=1):
1611 '''Convert a Python list to a Java ArrayList. 1612 1613 If list is a Python list, it is converted, else list is left unchanged. 1614 ''' 1615 #print "list type(%s) = %s" % (list,type(list)); 1616 if isinstance(list,PyList) or isinstance(list,PyTuple): 1617 L = ArrayList(); 1618 for e in list: 1619 t = True; 1620 if isinstance(e,RingElem): 1621 t = False; 1622 e = e.elem; 1623 if isinstance(e,PyList) or isinstance(e,PyTuple): 1624 if rec <= 1: 1625 e = makeJasArith(e); 1626 else: 1627 t = False; 1628 e = pylist2arraylist(e,fac,rec-1); 1629 try: 1630 n = e.getClass().getSimpleName(); 1631 if n == "ArrayList": 1632 t = False; 1633 except: 1634 pass; 1635 if t and fac != None: 1636 #print "e.p(%s) = %s" % (e,e.getClass().getName()); 1637 e = fac.parse( str(e) ); #or makeJasArith(e) ? 1638 L.add(e); 1639 list = L; 1640 #print "list type(%s) = %s" % (list,type(list)); 1641 return list
1642 1643
1644 -def makeJasArith(item):
1645 '''Construct a jas.arith object. 1646 If item is a python tuple or list then a BigRational, BigComplex is constructed. 1647 If item is a python float then a BigDecimal is constructed. 1648 ''' 1649 #print "item type(%s) = %s" % (item,type(item)); 1650 if isinstance(item,PyInteger) or isinstance(item,PyLong): 1651 return BigInteger( item ); 1652 if isinstance(item,PyFloat): # ?? what to do ?? 1653 return BigDecimal( str(item) ); 1654 if isinstance(item,PyTuple) or isinstance(item,PyList): 1655 if len(item) > 2: 1656 print "len(item) > 2, remaining items ignored"; 1657 #print "item[0] type(%s) = %s" % (item[0],type(item[0])); 1658 isc = isinstance(item[0],PyTuple) or isinstance(item[0],PyList) 1659 if len(item) > 1: 1660 isc = isc or isinstance(item[1],PyTuple) or isinstance(item[1],PyList); 1661 if isc: 1662 if len(item) > 1: 1663 re = makeJasArith( item[0] ); 1664 if not re.isField(): 1665 re = BigRational( re.val ); 1666 im = makeJasArith( item[1] ); 1667 if not im.isField(): 1668 im = BigRational( im.val ); 1669 jasArith = BigComplex( re, im ); 1670 else: 1671 re = makeJasArith( item[0] ); 1672 jasArith = BigComplex( re ); 1673 else: 1674 if len(item) > 1: 1675 jasArith = BigRational( item[0] ).divide( BigRational( item[1] ) ); 1676 else: 1677 jasArith = BigRational( item[0] ); 1678 return jasArith; 1679 print "unknown item type(%s) = %s" % (item,type(item)); 1680 return item;
1681 1682
1683 -def ZZ(z=0):
1684 '''Create JAS BigInteger as ring element. 1685 ''' 1686 if isinstance(z,RingElem): 1687 z = z.elem; 1688 r = BigInteger(z); 1689 return RingElem(r);
1690 1691
1692 -def ZM(m,z=0,field=False):
1693 '''Create JAS ModInteger as ring element. 1694 ''' 1695 if isinstance(m,RingElem): 1696 m = m.elem; 1697 if isinstance(z,RingElem): 1698 z = z.elem; 1699 # if z != 0 and ( z == False ): # never true 1700 # field = z; 1701 # z = 0; 1702 if field: 1703 mf = ModIntegerRing(m,field); 1704 else: 1705 mf = ModIntegerRing(m); 1706 r = ModInteger(mf,z); 1707 return RingElem(r);
1708 1709
1710 -def QQ(d=0,n=1):
1711 '''Create JAS BigRational as ring element. 1712 ''' 1713 if isinstance(d,PyTuple) or isinstance(d,PyList): 1714 if n != 1: 1715 print "%s ignored" % n; 1716 if len(d) > 1: 1717 n = d[1]; 1718 d = d[0]; 1719 if isinstance(d,RingElem): 1720 d = d.elem; 1721 if isinstance(n,RingElem): 1722 n = n.elem; 1723 if n == 1: 1724 if d == 0: 1725 r = BigRational(); 1726 else: 1727 r = BigRational(d); 1728 else: 1729 d = BigRational(d); 1730 n = BigRational(n); 1731 r = d.divide(n); # BigRational(d,n); only for short integers 1732 return RingElem(r);
1733 1734
1735 -def CC(re=BigRational(),im=BigRational()):
1736 '''Create JAS BigComplex as ring element. 1737 ''' 1738 if re == 0: 1739 re = BigRational(); 1740 if im == 0: 1741 im = BigRational(); 1742 if isinstance(re,PyTuple) or isinstance(re,PyList): 1743 if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): 1744 if len(re) > 1: 1745 im = QQ( re[1] ); 1746 re = QQ( re[0] ); 1747 else: 1748 re = QQ(re); 1749 # re = makeJasArith( re ); 1750 if isinstance(im,PyTuple) or isinstance(im,PyList): 1751 im = QQ( im ); 1752 # im = makeJasArith( im ); 1753 if isinstance(re,RingElem): 1754 re = re.elem; 1755 if isinstance(im,RingElem): 1756 im = im.elem; 1757 if im.isZERO(): 1758 if re.isZERO(): 1759 c = BigComplex(); 1760 else: 1761 c = BigComplex(re); 1762 else: 1763 c = BigComplex(re,im); 1764 return RingElem(c);
1765
1766 -def CR(re=BigRational(),im=BigRational(),ring=None):
1767 '''Create JAS generic Complex as ring element. 1768 ''' 1769 if re == 0: 1770 re = BigRational(); 1771 if im == 0: 1772 im = BigRational(); 1773 if isinstance(re,PyTuple) or isinstance(re,PyList): 1774 if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): 1775 if len(re) > 1: 1776 im = QQ( re[1] ); 1777 re = QQ( re[0] ); 1778 else: 1779 re = QQ(re); 1780 # re = makeJasArith( re ); 1781 if isinstance(im,PyTuple) or isinstance(im,PyList): 1782 im = QQ( im ); 1783 # im = makeJasArith( im ); 1784 if isinstance(re,RingElem): 1785 re = re.elem; 1786 if isinstance(im,RingElem): 1787 im = im.elem; 1788 if ring == None: 1789 ring = re.factory(); 1790 r = ComplexRing(ring); 1791 if im.isZERO(): 1792 if re.isZERO(): 1793 c = Complex(r); 1794 else: 1795 c = Complex(r,re); 1796 else: 1797 c = BigComplex(r,re,im); 1798 return RingElem(c);
1799
1800 -def DD(d=0):
1801 '''Create JAS BigDecimal as ring element. 1802 ''' 1803 if isinstance(d,RingElem): 1804 d = d.elem; 1805 if isinstance(d,PyFloat): 1806 d = str(d); 1807 #print "d type(%s) = %s" % (d,type(d)); 1808 if d == 0: 1809 r = BigDecimal(); 1810 else: 1811 r = BigDecimal(d); 1812 return RingElem(r);
1813 1814
1815 -def Quat(re=BigRational(),im=BigRational(),jm=BigRational(),km=BigRational()):
1816 '''Create JAS BigQuaternion as ring element. 1817 ''' 1818 if re == 0: 1819 re = BigRational(); 1820 if im == 0: 1821 im = BigRational(); 1822 if jm == 0: 1823 jm = BigRational(); 1824 if km == 0: 1825 km = BigRational(); 1826 if isinstance(re,PyTuple) or isinstance(re,PyList): 1827 if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): 1828 if len(re) > 1: 1829 im = QQ( re[1] ); 1830 re = QQ( re[0] ); 1831 else: 1832 re = QQ(re); 1833 # re = makeJasArith( re ); 1834 if isinstance(im,PyTuple) or isinstance(im,PyList): 1835 im = QQ( im ); 1836 if isinstance(jm,PyTuple) or isinstance(jm,PyList): 1837 jm = QQ( jm ); 1838 if isinstance(km,PyTuple) or isinstance(km,PyList): 1839 kim = QQ( km ); 1840 # im = makeJasArith( im ); 1841 if isinstance(re,RingElem): 1842 re = re.elem; 1843 if isinstance(im,RingElem): 1844 im = im.elem; 1845 if isinstance(jm,RingElem): 1846 jm = jm.elem; 1847 if isinstance(km,RingElem): 1848 km = km.elem; 1849 c = BigQuaternion(re,im,jm,km); 1850 return RingElem(c);
1851 1852
1853 -def Oct(ro=BigQuaternion(),io=BigQuaternion()):
1854 '''Create JAS BigOctonion as ring element. 1855 ''' 1856 if ro == 0: 1857 ro = BigQuaternion(); 1858 if io == 0: 1859 io = BigQuaternion(); 1860 if isinstance(ro,PyTuple) or isinstance(ro,PyList): 1861 if isinstance(ro[0],PyTuple) or isinstance(ro[0],PyList): 1862 if len(ro) > 1: 1863 io = QQ( ro[1] ); 1864 ro = QQ( ro[0] ); 1865 else: 1866 ro = QQ(ro); 1867 # re = makeJasArith( re ); 1868 if isinstance(io,PyTuple) or isinstance(io,PyList): 1869 io = QQ( io ); 1870 # im = makeJasArith( im ); 1871 if isinstance(ro,RingElem): 1872 ro = ro.elem; 1873 if isinstance(io,RingElem): 1874 io = io.elem; 1875 c = BigOctonion(ro,io); 1876 return RingElem(c);
1877 1878
1879 -def AN(m,z=0,field=False,pr=None):
1880 '''Create JAS AlgebraicNumber as ring element. 1881 ''' 1882 if isinstance(m,RingElem): 1883 m = m.elem; 1884 if isinstance(z,RingElem): 1885 z = z.elem; 1886 # if z != 0 and ( z == True or z == False ): # not working 1887 # field = z; 1888 # z = 0; 1889 #print "m.getClass() = " + str(m.getClass().getName()); 1890 #print "field = " + str(field); 1891 if m.getClass().getSimpleName() == "AlgebraicNumber": 1892 mf = AlgebraicNumberRing(m.factory().modul,m.factory().isField()); 1893 else: 1894 if field: 1895 mf = AlgebraicNumberRing(m,field); 1896 else: 1897 mf = AlgebraicNumberRing(m); 1898 #print "mf = " + mf.toString(); 1899 if z == 0: 1900 r = AlgebraicNumber(mf); 1901 else: 1902 r = AlgebraicNumber(mf,z); 1903 return RingElem(r);
1904 1905
1906 -def RealN(m,i,r=0):
1907 '''Create JAS RealAlgebraicNumber as ring element. 1908 ''' 1909 if isinstance(m,RingElem): 1910 m = m.elem; 1911 if isinstance(r,RingElem): 1912 r = r.elem; 1913 if isinstance(i,PyTuple) or isinstance(i,PyList): 1914 il = BigRational(i[0]); 1915 ir = BigRational(i[1]); 1916 i = Interval(il,ir); 1917 #print "m.getClass() = " + str(m.getClass().getName()); 1918 if m.getClass().getSimpleName() == "RealAlgebraicNumber": 1919 mf = RealAlgebraicRing(m.factory().algebraic.modul,i); 1920 else: 1921 mf = RealAlgebraicRing(m,i); 1922 if r == 0: 1923 rr = RealAlgebraicNumber(mf); 1924 else: 1925 rr = RealAlgebraicNumber(mf,r); 1926 return RingElem(rr);
1927 1928
1929 -def RF(pr,d=0,n=1):
1930 '''Create JAS rational function Quotient as ring element. 1931 ''' 1932 if isinstance(d,PyTuple) or isinstance(d,PyList): 1933 if n != 1: 1934 print "%s ignored" % n; 1935 if len(d) > 1: 1936 n = d[1]; 1937 d = d[0]; 1938 if isinstance(d,RingElem): 1939 d = d.elem; 1940 if isinstance(n,RingElem): 1941 n = n.elem; 1942 if isinstance(pr,RingElem): 1943 pr = pr.elem; 1944 if isinstance(pr,Ring): 1945 pr = pr.ring; 1946 qr = QuotientRing(pr); 1947 if d == 0: 1948 r = Quotient(qr); 1949 else: 1950 if n == 1: 1951 r = Quotient(qr,d); 1952 else: 1953 r = Quotient(qr,d,n); 1954 return RingElem(r);
1955 1956
1957 -def RC(ideal,r=0):
1958 '''Create JAS polynomial Residue as ring element. 1959 ''' 1960 if ideal == None: 1961 raise ValueError, "No ideal given." 1962 if isinstance(ideal,Ideal): 1963 ideal = jas.application.Ideal(ideal.pset); 1964 #ideal.doGB(); 1965 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 1966 if ideal.getList().get(0).ring.getClass().getSimpleName() == "ResidueRing": 1967 rc = ResidueRing( ideal.getList().get(0).ring.ideal ); 1968 else: 1969 rc = ResidueRing(ideal); 1970 if isinstance(r,RingElem): 1971 r = r.elem; 1972 if r == 0: 1973 r = Residue(rc); 1974 else: 1975 r = Residue(rc,r); 1976 return RingElem(r);
1977 1978
1979 -def LC(ideal,d=0,n=1):
1980 '''Create JAS polynomial Local as ring element. 1981 ''' 1982 if ideal == None: 1983 raise ValueError, "No ideal given." 1984 if isinstance(ideal,Ideal): 1985 ideal = jas.application.Ideal(ideal.pset); 1986 #ideal.doGB(); 1987 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 1988 if ideal.getList().get(0).ring.getClass().getSimpleName() == "LocalRing": 1989 lc = LocalRing( ideal.getList().get(0).ring.ideal ); 1990 else: 1991 lc = LocalRing(ideal); 1992 if isinstance(d,PyTuple) or isinstance(d,PyList): 1993 if n != 1: 1994 print "%s ignored" % n; 1995 if len(d) > 1: 1996 n = d[1]; 1997 d = d[0]; 1998 if isinstance(d,RingElem): 1999 d = d.elem; 2000 if isinstance(n,RingElem): 2001 n = n.elem; 2002 if d == 0: 2003 r = Local(lc); 2004 else: 2005 if n == 1: 2006 r = Local(lc,d); 2007 else: 2008 r = Local(lc,d,n); 2009 return RingElem(r);
2010 2011
2012 -def RR(flist,n=1,r=0):
2013 '''Create JAS regular ring Product as ring element. 2014 ''' 2015 if not isinstance(n,PyInteger): 2016 r = n; 2017 n = 1; 2018 if flist == None: 2019 raise ValueError, "No list given." 2020 if isinstance(flist,PyList) or isinstance(flist,PyTuple): 2021 flist = pylist2arraylist( [ x.factory() for x in flist ], rec=1); 2022 ncop = 0; 2023 else: 2024 ncop = n; 2025 if isinstance(flist,RingElem): 2026 flist = flist.elem; 2027 flist = flist.factory(); 2028 ncop = n; 2029 #print "flist = " + str(flist); 2030 #print "ncop = " + str(ncop); 2031 if ncop == 0: 2032 pr = ProductRing(flist); 2033 else: 2034 pr = ProductRing(flist,ncop); 2035 #print "r type(%s) = %s" % (r,type(r)); 2036 if isinstance(r,RingElem): 2037 r = r.elem; 2038 #if isinstance(r,PyJavaInstance): 2039 try: 2040 #print "r.class() = %s" % r.getClass().getSimpleName(); 2041 if r.getClass().getSimpleName() == "Product": 2042 #print "r.val = %s" % r.val; 2043 r = r.val; 2044 except: 2045 pass; 2046 #print "r = " + str(r); 2047 if r == 0: 2048 r = Product(pr); 2049 else: 2050 r = Product(pr,r); 2051 return RingElem(r);
2052 2053
2054 -def PS(cofac,name,f=None,truncate=None):
2055 '''Create JAS UnivPowerSeries as ring element. 2056 ''' 2057 cf = cofac; 2058 if isinstance(cofac,RingElem): 2059 cf = cofac.elem.factory(); 2060 if isinstance(cofac,Ring): 2061 cf = cofac.ring; 2062 if isinstance(truncate,RingElem): 2063 truncate = truncate.elem; 2064 if truncate == None: 2065 ps = UnivPowerSeriesRing(cf,name); 2066 else: 2067 ps = UnivPowerSeriesRing(cf,truncate,name); 2068 if f == None: 2069 r = ps.getZERO(); 2070 else: 2071 class Coeff( Coefficients ): 2072 def __init__(self,cofac): 2073 self.coFac = cofac;
2074 def generate(self,i): 2075 a = f(i); 2076 if isinstance(a,RingElem): 2077 a = a.elem; 2078 #print "a = " + str(a); 2079 return a; 2080 r = UnivPowerSeries(ps,Coeff(cf)); 2081 return RingElem(r); 2082 2083
2084 -def MPS(cofac,names,f=None,truncate=None):
2085 '''Create JAS MultiVarPowerSeries as ring element. 2086 ''' 2087 cf = cofac; 2088 if isinstance(cofac,RingElem): 2089 cf = cofac.elem.factory(); 2090 if isinstance(cofac,Ring): 2091 cf = cofac.ring; 2092 vars = names; 2093 if isinstance(vars,PyString): 2094 vars = GenPolynomialTokenizer.variableList(vars); 2095 nv = len(vars); 2096 if isinstance(truncate,RingElem): 2097 truncate = truncate.elem; 2098 if truncate == None: 2099 ps = MultiVarPowerSeriesRing(cf,nv,vars); 2100 else: 2101 ps = MultiVarPowerSeriesRing(cf,nv,vars,truncate); 2102 if f == None: 2103 r = ps.getZERO(); 2104 else: 2105 class MCoeff( MultiVarCoefficients ): 2106 def __init__(self,r): 2107 MultiVarCoefficients.__init__(self,r); 2108 self.coFac = r.coFac;
2109 def generate(self,i): 2110 a = f(i); 2111 if isinstance(a,RingElem): 2112 a = a.elem; 2113 return a; 2114 r = MultiVarPowerSeries(ps,MCoeff(ps)); 2115 #print "r = " + str(r); 2116 return RingElem(r); 2117 2118
2119 -def Vec(cofac,n,v=None):
2120 '''Create JAS GenVector ring element. 2121 ''' 2122 cf = cofac; 2123 if isinstance(cofac,RingElem): 2124 cf = cofac.elem.factory(); 2125 if isinstance(cofac,Ring): 2126 cf = cofac.ring; 2127 if isinstance(n,RingElem): 2128 n = n.elem; 2129 if isinstance(v,RingElem): 2130 v = v.elem; 2131 vr = GenVectorModul(cf,n); 2132 if v == None: 2133 r = GenVector(vr); 2134 else: 2135 r = GenVector(vr,v); 2136 return RingElem(r);
2137 2138
2139 -def Mat(cofac,n,m,v=None):
2140 '''Create JAS GenMatrix ring element. 2141 ''' 2142 cf = cofac; 2143 if isinstance(cofac,RingElem): 2144 cf = cofac.elem.factory(); 2145 if isinstance(cofac,Ring): 2146 cf = cofac.ring; 2147 if isinstance(n,RingElem): 2148 n = n.elem; 2149 if isinstance(m,RingElem): 2150 m = m.elem; 2151 if isinstance(v,RingElem): 2152 v = v.elem; 2153 #print "cf type(%s) = %s" % (cf,type(cf)); 2154 mr = GenMatrixRing(cf,n,m); 2155 if v == None: 2156 r = GenMatrix(mr); 2157 else: 2158 r = GenMatrix(mr,v); 2159 return RingElem(r);
2160 2161
2162 -def coercePair(a,b):
2163 '''Coerce type a to type b or type b to type a. 2164 ''' 2165 #print "a type(%s) = %s" % (a,type(a)); 2166 #print "b type(%s) = %s" % (b,type(b)); 2167 try: 2168 if not a.isPolynomial() and b.isPolynomial(): 2169 s = b.coerce(a); 2170 o = b; 2171 else: 2172 s = a; 2173 o = a.coerce(b); 2174 except: 2175 s = a; 2176 o = a.coerce(b); 2177 return (s,o);
2178 2179
2180 -class RingElem:
2181 '''Proxy for JAS ring elements. 2182 2183 Methods to be used as + - * ** / %. 2184 ''' 2185
2186 - def __init__(self,elem):
2187 '''Constructor for ring element. 2188 ''' 2189 if isinstance(elem,RingElem): 2190 self.elem = elem.elem; 2191 else: 2192 self.elem = elem; 2193 try: 2194 self.ring = self.elem.factory(); 2195 except: 2196 self.ring = self.elem;
2197
2198 - def __str__(self):
2199 '''Create a string representation. 2200 ''' 2201 return str(self.elem.toScript());
2202
2203 - def zero(self):
2204 '''Zero element of this ring. 2205 ''' 2206 return RingElem( self.elem.factory().getZERO() );
2207
2208 - def isZERO(self):
2209 '''Test if this is the zero element of the ring. 2210 ''' 2211 return self.elem.isZERO();
2212
2213 - def one(self):
2214 '''One element of this ring. 2215 ''' 2216 return RingElem( self.elem.factory().getONE() );
2217
2218 - def isONE(self):
2219 '''Test if this is the one element of the ring. 2220 ''' 2221 return self.elem.isONE();
2222
2223 - def signum(self):
2224 '''Get the sign of this element. 2225 ''' 2226 return self.elem.signum();
2227
2228 - def __abs__(self):
2229 '''Absolute value. 2230 ''' 2231 return RingElem( self.elem.abs() );
2232
2233 - def __neg__(self):
2234 '''Negative value. 2235 ''' 2236 return RingElem( self.elem.negate() );
2237
2238 - def __pos__(self):
2239 '''Positive value. 2240 ''' 2241 return self;
2242
2243 - def coerce(self,other):
2244 '''Coerce other to self. 2245 ''' 2246 #print "self type(%s) = %s" % (self,type(self)); 2247 #print "other type(%s) = %s" % (other,type(other)); 2248 #print "self.elem class(%s) = %s" % (self.elem,self.elem.getClass()); 2249 if self.elem.getClass().getSimpleName() == "GenVector": 2250 #print "self, other = %s, %s " % (self,other); 2251 if isinstance(other,PyTuple) or isinstance(other,PyList): 2252 o = pylist2arraylist(other,self.elem.factory().coFac,rec=1); 2253 o = GenVector(self.elem.factory(),o); 2254 return RingElem( o ); 2255 if self.elem.getClass().getSimpleName() == "GenMatrix": 2256 #print "self, other = %s, %s " % (type(self),type(other)); 2257 #print "o type(%s) = %s, str = %s" % (o,type(o),str(o)); 2258 if isinstance(other,PyTuple) or isinstance(other,PyList): 2259 o = pylist2arraylist(other,self.elem.factory().coFac,rec=2); 2260 o = GenMatrix(self.elem.factory(),o); 2261 return RingElem( o ); 2262 if isinstance(other,RingElem): 2263 if self.isPolynomial() and not other.isPolynomial(): 2264 #print "self.ring = %s" % (self.ring); 2265 o = self.ring.parse( other.elem.toString() ); # not toScript() 2266 #print "o type(%s) = %s, str = %s" % (o,type(o),str(o)); 2267 return RingElem( o ); 2268 return other; 2269 #print "--1"; 2270 if isinstance(other,PyTuple) or isinstance(other,PyList): 2271 # assume BigRational or BigComplex 2272 # assume self will be compatible with them. todo: check this 2273 o = makeJasArith(other); 2274 #print "other class(%s) = %s" % (o,o.getClass()); 2275 if self.isPolynomial(): 2276 #print "other type(%s) = %s" % (o,type(o)); 2277 o = self.ring.parse( o.toString() ); # not toScript(); 2278 #o = o.elem; 2279 if self.elem.getClass().getSimpleName() == "BigComplex": 2280 #print "other type(%s) = %s" % (o,type(o)); 2281 o = CC( o ); 2282 o = o.elem; 2283 if self.elem.getClass().getSimpleName() == "BigQuaternion": 2284 #print "other type(%s) = %s" % (o,type(o)); 2285 o = Quat( o ); 2286 o = o.elem; 2287 if self.elem.getClass().getSimpleName() == "BigOctonion": 2288 #print "other type(%s) = %s" % (o,type(o)); 2289 o = Oct( Quat(o) ); 2290 o = o.elem; 2291 if self.elem.getClass().getSimpleName() == "Product": 2292 #print "other type(%s) = %s" % (o,type(o)); 2293 o = RR(self.ring, self.elem.multiply(o) ); # valueOf 2294 #print "o = %s" % o; 2295 o = o.elem; 2296 return RingElem(o); 2297 #print "--2"; 2298 # test if self.elem is a factory itself 2299 if self.isFactory(): 2300 if isinstance(other,PyInteger) or isinstance(other,PyLong): 2301 o = self.elem.fromInteger(other); 2302 else: 2303 if isinstance(other,PyFloat): # ?? what to do ?? 2304 o = self.elem.fromInteger( int(other) ); 2305 else: 2306 print "unknown other type(%s) = %s" % (other,type(other)); 2307 o = self.elem.parse( str(other) ); 2308 return RingElem(o); 2309 #print "--3"; 2310 #print "other type(%s) = %s" % (other,type(other)); 2311 # self.elem has a ring factory 2312 if isinstance(other,PyInteger) or isinstance(other,PyLong): 2313 o = self.elem.factory().fromInteger(other); 2314 else: 2315 if isinstance(other,PyFloat): # ?? what to do ?? 2316 #print "other type(%s) = %s" % (other,type(other)); 2317 #print "self type(%s) = %s" % (self.elem,self.elem.getClass().getName()); 2318 o = BigDecimal(other); 2319 if self.elem.getClass().getSimpleName() == "Product": 2320 o = RR(self.ring, self.elem.idempotent().multiply(o) ); # valueOf 2321 o = o.elem; 2322 else: 2323 o = self.elem.factory().getZERO().sum( o ); 2324 else: 2325 print "unknown other type(%s) = %s" % (other,type(other)); 2326 o = self.elem.factory().parse( str(other) ); 2327 #print "--4"; 2328 return RingElem(o);
2329
2330 - def isFactory(self):
2331 '''Test if this is itself a ring factory. 2332 ''' 2333 f = self.elem.factory(); 2334 if self.elem == f: 2335 return True; 2336 else: 2337 return False;
2338
2339 - def isPolynomial(self):
2340 '''Test if this is a polynomial. 2341 ''' 2342 try: 2343 nv = self.elem.ring.nvar; 2344 except: 2345 return False; 2346 return True;
2347
2348 - def __cmp__(self,other):
2349 '''Compare two ring elements. 2350 ''' 2351 [s,o] = coercePair(self,other); 2352 return s.elem.compareTo( o.elem );
2353
2354 - def __hash__(self):
2355 '''Hash value. 2356 ''' 2357 return self.elem.hashCode();
2358
2359 - def __mul__(self,other):
2360 '''Multiply two ring elements. 2361 ''' 2362 [s,o] = coercePair(self,other); 2363 #print "self type(%s) = %s" % (s,type(s)); 2364 #print "other type(%s) = %s" % (o,type(o)); 2365 return RingElem( s.elem.multiply( o.elem ) );
2366
2367 - def __rmul__(self,other):
2368 '''Reverse multiply two ring elements. 2369 ''' 2370 [s,o] = coercePair(self,other); 2371 return o.__mul__(s);
2372
2373 - def __add__(self,other):
2374 '''Add two ring elements. 2375 ''' 2376 [s,o] = coercePair(self,other); 2377 return RingElem( s.elem.sum( o.elem ) );
2378
2379 - def __radd__(self,other):
2380 '''Reverse add two ring elements. 2381 ''' 2382 [s,o] = coercePair(self,other); 2383 return o.__add__(s);
2384
2385 - def __sub__(self,other):
2386 '''Subtract two ring elements. 2387 ''' 2388 [s,o] = coercePair(self,other); 2389 return RingElem( s.elem.subtract( o.elem ) );
2390
2391 - def __rsub__(self,other):
2392 '''Reverse subtract two ring elements. 2393 ''' 2394 [s,o] = coercePair(self,other); 2395 return o.__sub__(self);
2396
2397 - def __div__(self,other):
2398 '''Divide two ring elements. 2399 ''' 2400 [s,o] = coercePair(self,other); 2401 return RingElem( s.elem.divide( o.elem ) );
2402
2403 - def __rdiv__(self,other):
2404 '''Reverse divide two ring elements. 2405 ''' 2406 [s,o] = coercePair(self,other); 2407 return o.__div__(s);
2408
2409 - def __mod__(self,other):
2410 '''Modular remainder of two ring elements. 2411 ''' 2412 [s,o] = coercePair(self,other); 2413 return RingElem( s.elem.remainder( o.elem ) );
2414
2415 - def __xor__(self,other):
2416 '''Can not be used as power. 2417 ''' 2418 return None;
2419
2420 - def __pow__(self,other):
2421 '''Power of this to other. 2422 ''' 2423 #print "self type(%s) = %s" % (self,type(self)); 2424 #print "pow other type(%s) = %s" % (other,type(other)); 2425 if isinstance(other,PyInteger) or isinstance(other,PyLong): 2426 n = other; 2427 else: 2428 if isinstance(other,RingElem): 2429 n = other.elem; 2430 #if isinstance(n,BigRational): # does not work 2431 if n.getClass().getSimpleName() == "BigRational": 2432 n = n.numerator().intValue() / n.denominator().intValue(); 2433 #if isinstance(n,BigInteger): # does not work 2434 if n.getClass().getSimpleName() == "BigInteger": 2435 n = n.intValue(); 2436 if self.isFactory(): 2437 p = Power(self.elem).power( self.elem, n ); 2438 else: 2439 p = Power(self.elem.ring).power( self.elem, n ); 2440 return RingElem( p );
2441
2442 - def __eq__(self,other):
2443 '''Test if two ring elements are equal. 2444 ''' 2445 o = other; 2446 if isinstance(other,RingElem): 2447 o = other.elem; 2448 return self.elem.equals(o)
2449
2450 - def __ne__(self,other):
2451 '''Test if two ring elements are not equal. 2452 ''' 2453 o = other; 2454 if isinstance(other,RingElem): 2455 o = other.elem; 2456 return not self.elem.equals(o)
2457
2458 - def __float__(self):
2459 '''Convert to Python float. 2460 ''' 2461 #print "self type(%s) = %s" % (self,type(self)); 2462 e = self.elem; 2463 if e.getClass().getSimpleName() == "BigInteger": 2464 e = BigRational(e); 2465 if e.getClass().getSimpleName() == "BigRational": 2466 e = BigDecimal(e); 2467 if e.getClass().getSimpleName() == "BigDecimal": 2468 e = e.toString(); 2469 e = float(e); 2470 return e;
2471
2472 - def factory(self):
2473 '''Get the factory of this element. 2474 ''' 2475 fac = self.elem.factory(); 2476 try: 2477 nv = fac.nvar; 2478 except: 2479 return RingElem(fac); 2480 #return PolyRing(fac.coFac,fac.getVars(),fac.tord); 2481 return RingElem(fac);
2482
2483 - def gens(self):
2484 '''Get the generators for the factory of this element. 2485 ''' 2486 L = self.elem.factory().generators(); 2487 #print "L = %s" % L; 2488 N = [ RingElem(e) for e in L ]; 2489 #print "N = %s" % N; 2490 return N;
2491
2492 - def monic(self):
2493 '''Monic polynomial. 2494 ''' 2495 return RingElem( self.elem.monic() );
2496
2497 - def evaluate(self,a):
2498 '''Evaluate at a for power series. 2499 ''' 2500 #print "self type(%s) = %s" % (self,type(self)); 2501 #print "a type(%s) = %s" % (a,type(a)); 2502 x = None; 2503 if isinstance(a,RingElem): 2504 x = a.elem; 2505 if isinstance(a,PyTuple) or isinstance(a,PyList): 2506 # assume BigRational or BigComplex 2507 # assume self will be compatible with them. todo: check this 2508 x = makeJasArith(a); 2509 try: 2510 e = self.elem.evaluate(x); 2511 except: 2512 e = 0; 2513 return RingElem( e );
2514
2515 - def integrate(self,a=0,r=None):
2516 '''Integrate a power series with constant a or as rational function. 2517 2518 a is the integration constant, r is for partial integration in variable r. 2519 ''' 2520 #print "self type(%s) = %s" % (self,type(self)); 2521 #print "a type(%s) = %s" % (a,type(a)); 2522 x = None; 2523 if isinstance(a,RingElem): 2524 x = a.elem; 2525 if isinstance(a,PyTuple) or isinstance(a,PyList): 2526 # assume BigRational or BigComplex 2527 # assume self will be compatible with them. todo: check this 2528 x = makeJasArith(a); 2529 try: 2530 if r != None: 2531 e = self.elem.integrate(x,r); 2532 else: 2533 e = self.elem.integrate(x); 2534 return RingElem( e ); 2535 except: 2536 pass; 2537 cf = self.elem.ring; 2538 try: 2539 cf = cf.ring; 2540 except: 2541 pass; 2542 integrator = ElementaryIntegration(cf.coFac); 2543 ei = integrator.integrate(self.elem); 2544 return ei;
2545
2546 - def differentiate(self,r=None):
2547 '''Differentiate a power series. 2548 2549 r is for partial differentiation in variable r. 2550 ''' 2551 try: 2552 if r != None: 2553 e = self.elem.differentiate(r); 2554 else: 2555 e = self.elem.differentiate(); 2556 except: 2557 e = self.elem.factory().getZERO(); 2558 return RingElem( e );
2559
2560 - def coefficients(self):
2561 '''Get the coefficients of a polynomial. 2562 ''' 2563 a = self.elem; 2564 #L = [ c.toScriptFactory() for c in a.coefficientIterator() ]; 2565 L = [ RingElem(c) for c in a.coefficientIterator() ]; 2566 return L
2567 2568
2569 -class PolyRing(Ring):
2570 '''Represents a JAS polynomial ring: GenPolynomialRing. 2571 2572 Provides more convenient constructor. 2573 Then returns a Ring. 2574 ''' 2575
2576 - def __init__(self,coeff,vars,order=TermOrder(TermOrder.IGRLEX)):
2577 '''Ring constructor. 2578 2579 coeff = factory for coefficients, 2580 vars = string with variable names, 2581 order = term order. 2582 ''' 2583 if coeff == None: 2584 raise ValueError, "No coefficient given." 2585 cf = coeff; 2586 if isinstance(coeff,RingElem): 2587 cf = coeff.elem.factory(); 2588 if isinstance(coeff,Ring): 2589 cf = coeff.ring; 2590 if vars == None: 2591 raise ValueError, "No variable names given." 2592 names = vars; 2593 if isinstance(vars,PyString): 2594 names = GenPolynomialTokenizer.variableList(vars); 2595 nv = len(names); 2596 to = PolyRing.lex; 2597 if isinstance(order,TermOrder): 2598 to = order; 2599 tring = GenPolynomialRing(cf,nv,to,names); 2600 #want: super(Ring,self).__init__(ring=tring) 2601 self.ring = tring; 2602 self.engine = GCDFactory.getProxy(self.ring.coFac); 2603 try: 2604 self.sqf = SquarefreeFactory.getImplementation(self.ring.coFac); 2605 except: 2606 pass 2607 # except Exception, e: 2608 # print "error " + str(e) 2609 try: 2610 self.factor = FactorFactory.getImplementation(self.ring.coFac); 2611 # except Exception, e: 2612 # print "error " + str(e) 2613 except: 2614 pass
2615 2616
2617 - def __str__(self):
2618 '''Create a string representation. 2619 ''' 2620 return self.ring.toScript();
2621 2622 lex = TermOrder(TermOrder.INVLEX) 2623 2624 grad = TermOrder(TermOrder.IGRLEX)
2625 2626
2627 -class SolvPolyRing(SolvableRing):
2628 '''Represents a JAS solvable polynomial ring: GenSolvablePolynomialRing. 2629 2630 Provides more convenient constructor. 2631 Then returns a Ring. 2632 ''' 2633
2634 - def __init__(self,coeff,vars,order,rel=None):
2635 '''Ring constructor. 2636 2637 coeff = factory for coefficients, 2638 vars = string with variable names, 2639 order = term order, 2640 rel = triple list of relations. (e,f,p,...) with e * f = p as relation. 2641 ''' 2642 if coeff == None: 2643 raise ValueError, "No coefficient given." 2644 cf = coeff; 2645 if isinstance(coeff,RingElem): 2646 cf = coeff.elem.factory(); 2647 if isinstance(coeff,Ring): 2648 cf = coeff.ring; 2649 if vars == None: 2650 raise ValueError, "No variable names given." 2651 names = vars; 2652 if isinstance(vars,PyString): 2653 names = GenPolynomialTokenizer.variableList(vars); 2654 nv = len(names); 2655 to = PolyRing.lex; 2656 if isinstance(order,TermOrder): 2657 to = order; 2658 ring = GenSolvablePolynomialRing(cf,nv,to,names); 2659 if rel != None: 2660 #print "rel = " + str(rel); 2661 table = ring.table; 2662 L = []; 2663 for x in rel: 2664 if isinstance(x,RingElem): 2665 x = x.elem; 2666 L.append(x); 2667 #print "rel = " + str(L); 2668 for i in range(0,len(L),3): 2669 table.update( L[i], L[i+1], L[i+2] ); 2670 self.ring = ring;
2671
2672 - def __str__(self):
2673 '''Create a string representation. 2674 ''' 2675 return self.ring.toScript();
2676 2677
2678 -class EF:
2679 '''Extension field builder. 2680 2681 Construction of extension field towers according to the builder pattern. 2682 ''' 2683
2684 - def __init__(self,base):
2685 '''Constructor to set base field. 2686 ''' 2687 if isinstance(base,RingElem): 2688 factory = base.elem; 2689 else: 2690 factory = base; 2691 try: 2692 factory = self.factory.factory(); 2693 except: 2694 pass 2695 #print "factory: " + factory.toScript() + " :: " + factory.toString(); 2696 if isinstance(factory,ExtensionFieldBuilder): 2697 self.builder = factory; 2698 else: 2699 self.builder = ExtensionFieldBuilder(factory);
2700
2701 - def __str__(self):
2702 '''Create a string representation. 2703 ''' 2704 return str(self.builder.toScript());
2705
2706 - def extend(self,vars,algebraic=None):
2707 '''Create an extension field. 2708 2709 If algebraic is given as string expression, then an algebraic 2710 extension field is constructed, else a transcendental 2711 extension field is constructed. 2712 ''' 2713 if algebraic == None: 2714 ef = self.builder.transcendentExtension(vars); 2715 else: 2716 ef = self.builder.algebraicExtension(vars,algebraic); 2717 return EF(ef.build());
2718
2719 - def realExtend(self,vars,algebraic,interval):
2720 '''Create a real extension field. 2721 2722 Construct a real algebraic extension field with an isolating interval for a real root. 2723 ''' 2724 ef = self.builder.realAlgebraicExtension(vars,algebraic,interval); 2725 return EF(ef.build());
2726
2727 - def complexExtend(self,vars,algebraic,rectangle):
2728 '''Create a complex extension field. 2729 2730 Construct a complex algebraic extension field with an isolating rectangle for a complex root. 2731 ''' 2732 ef = self.builder.complexAlgebraicExtension(vars,algebraic,rectangle); 2733 return EF(ef.build());
2734
2735 - def polynomial(self,vars):
2736 '''Create an polynomial ring extension. 2737 ''' 2738 ef = self.builder.polynomialExtension(vars); 2739 return EF(ef.build());
2740
2741 - def build(self):
2742 '''Get extension field tower. 2743 2744 ''' 2745 rf = self.builder.build(); 2746 if isinstance(rf,GenPolynomialRing): 2747 return PolyRing(rf.coFac,rf.getVars()); 2748 else: 2749 return RingElem(rf.getZERO());
2750