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

Source Code for Module jas

   1  '''jython interface to JAS. 
   2  ''' 
   3   
   4  # $Id$ 
   5   
   6  from java.lang           import System 
   7  from java.io             import StringReader 
   8  from java.util           import ArrayList, List, Collections 
   9   
  10  from edu.jas.structure   import RingElem, RingFactory, Power 
  11  from edu.jas.arith       import BigInteger, BigRational, BigComplex, BigDecimal,\ 
  12                                  ModInteger, ModIntegerRing, ModLong, ModLongRing, ModInt, ModIntRing,\ 
  13                                  BigQuaternion, BigQuaternionRing, BigOctonion,\ 
  14                                  Product, ProductRing, ArithUtil, PrimeList, PrimeInteger 
  15  from edu.jas.poly        import GenPolynomial, GenPolynomialRing, Monomial,\ 
  16                                  IndexFactory, GenExteriorPolynomial, GenExteriorPolynomialRing,\ 
  17                                  GenSolvablePolynomial, GenSolvablePolynomialRing,\ 
  18                                  RecSolvablePolynomial, RecSolvablePolynomialRing,\ 
  19                                  RecSolvableWordPolynomial, RecSolvableWordPolynomialRing,\ 
  20                                  QLRSolvablePolynomial, QLRSolvablePolynomialRing,\ 
  21                                  GenWordPolynomial, GenWordPolynomialRing,\ 
  22                                  ExpVector,\ 
  23                                  Word, WordFactory,\ 
  24                                  GenPolynomialTokenizer, OrderedPolynomialList, PolyUtil,\ 
  25                                  TermOrderOptimization, TermOrder, TermOrderByName,\ 
  26                                  PolynomialList, AlgebraicNumber, AlgebraicNumberRing,\ 
  27                                  OrderedModuleList, ModuleList,\ 
  28                                  Complex, ComplexRing 
  29  from edu.jas.ps          import UnivPowerSeries, UnivPowerSeriesRing,\ 
  30                                  UnivPowerSeriesMap, Coefficients, \ 
  31                                  MultiVarPowerSeries, MultiVarPowerSeriesRing,\ 
  32                                  MultiVarPowerSeriesMap, MultiVarCoefficients,\ 
  33                                  StandardBaseSeq 
  34  from edu.jas.gb          import EReductionSeq, DGroebnerBaseSeq, EGroebnerBaseSeq,\ 
  35                                  GroebnerBaseDistributedEC, GroebnerBaseDistributedHybridEC,\ 
  36                                  GroebnerBaseSeq, GroebnerBaseSeqIter, GroebnerBaseSeqPairSeq,\ 
  37                                  OrderedPairlist, OrderedSyzPairlist, ReductionSeq,\ 
  38                                  GroebnerBaseParallel, GroebnerBaseSeqPairParallel,\ 
  39                                  SolvableGroebnerBaseParallel, SolvableGroebnerBaseSeq,\ 
  40                                  SolvableReductionSeq, WordGroebnerBaseSeq 
  41  from edu.jas.gbufd       import GroebnerBasePseudoRecSeq, GroebnerBasePseudoSeq,\ 
  42                                  SolvableGroebnerBasePseudoSeq, SolvablePseudoReductionSeq,\ 
  43                                  PseudoReductionSeq, GroebnerBasePseudoParallel,\ 
  44                                  WordGroebnerBasePseudoSeq, WordPseudoReductionSeq,\ 
  45                                  SolvableGroebnerBasePseudoRecSeq, WordGroebnerBasePseudoRecSeq,\ 
  46                                  RGroebnerBasePseudoSeq, RGroebnerBaseSeq, RReductionSeq,\ 
  47                                  GroebnerBaseFGLM, GroebnerBaseWalk,\ 
  48                                  CharacteristicSetWu, PolyGBUtil,\ 
  49                                  SolvableSyzygySeq, SyzygySeq 
  50  #from edu.jas.gbmod       import ModGroebnerBaseSeq, ModSolvableGroebnerBaseSeq 
  51  from edu.jas.vector      import GenVector, GenVectorModul, BasicLinAlg,\ 
  52                                  GenMatrix, GenMatrixRing, LinAlg 
  53  from edu.jas.application import FactorFactory, PolyUtilApp, RingFactoryTokenizer,\ 
  54                                  Residue, ResidueRing, Ideal,\ 
  55                                  Local, LocalRing, IdealWithRealAlgebraicRoots,\ 
  56                                  SolvableIdeal, SolvableResidue, SolvableResidueRing,\ 
  57                                  SolvableLocal, SolvableLocalRing,\ 
  58                                  SolvableLocalResidue, SolvableLocalResidueRing,\ 
  59                                  ResidueSolvablePolynomial, ResidueSolvablePolynomialRing,\ 
  60                                  LocalSolvablePolynomial, LocalSolvablePolynomialRing,\ 
  61                                  WordIdeal, WordResidue, WordResidueRing,\ 
  62                                  AlgebraicRootsPrimElem, RootFactoryApp,\ 
  63                                  ComprehensiveGroebnerBaseSeq, ExtensionFieldBuilder 
  64  from edu.jas.kern        import ComputerThreads, StringUtil, Scripting 
  65  from edu.jas.ufd         import GreatestCommonDivisor, PolyUfdUtil, GCDFactory,\ 
  66                                  SquarefreeFactory, Quotient, QuotientRing 
  67  from edu.jas.fd          import SolvableQuotient, SolvableQuotientRing,\ 
  68                                  QuotSolvablePolynomial, QuotSolvablePolynomialRing 
  69  from edu.jas.root        import RealRootsSturm, Interval, RealAlgebraicNumber, RealAlgebraicRing,\ 
  70                                  ComplexRootsSturm, Rectangle, RealArithUtil, RootFactory 
  71  from edu.jas.integrate   import ElementaryIntegration 
  72  from edu.jas.util        import ExecutableServer 
  73  #from edu.jas             import structure, arith, poly, ps, gb, gbmod, vector,\ 
  74  #                                application, util, ufd 
  75  from edu                 import jas 
  76  #PrettyPrint.setInternal(); 
  77   
  78  from org.python.core     import PyInstance, PyList, PyTuple,\ 
  79                                  PyInteger, PyLong, PyFloat, PyString 
  80  # not suitable PySequence 
  81   
  82  # set output to Python scripting 
  83  Scripting.setLang(Scripting.Lang.Python); 
  84   
85 -def startLog():
86 '''Configure the log4j system and start logging. 87 88 BasicConfigurator from log4j version 1 is no more supported, please use log4j2 configuration. 89 '''
90 #print "BasicConfigurator from log4j version 1 is no more supported, please use log4j2 configuration"; 91 92 print "Java Algebra System (JAS) version 2.7" 93
94 -def terminate():
95 '''Terminate the running thread pools. 96 ''' 97 ComputerThreads.terminate();
98
99 -def noThreads():
100 '''Turn off automatic parallel threads usage. 101 ''' 102 print "nt = ", ComputerThreads.NO_THREADS; 103 ComputerThreads.setNoThreads(); #NO_THREADS = #0; #1; #True; 104 print "nt = ", ComputerThreads.NO_THREADS;
105 106 auto_inject = True 107 '''Define variables automatically in the global namespace. 108 ''' 109
110 -def inject_variable(name, value):
111 '''Inject a variable into the main global namespace 112 113 INPUT: 114 - "name" - a string 115 - "value" - anything 116 117 Found in Sage. 118 AUTHORS: 119 - William Stein 120 ''' 121 assert type(name) is str 122 import sys 123 depth = 0 124 redef = None 125 while True: 126 G = sys._getframe(depth).f_globals 127 #print "G = `%s` " % G; 128 depth += 1 129 if depth > 100: 130 print "depth limit %s reached " % depth; 131 break 132 if G is None: 133 continue 134 # orig: if G["__name__"] == "__main__" and G["__package__"] is None: 135 try: 136 if G["__name__"] is None: 137 break 138 except: 139 #print "G[__name__] undefined"; 140 break 141 if G["__name__"] == "__main__": 142 try: 143 if G["__package__"] is None: 144 break 145 except: 146 break 147 if G is None: 148 print "error at G no global environment found for `%s` = `%s` " % (name, value); 149 return 150 if name in G: 151 redef = name; 152 #print "redefining global variable `%s` from `%s` " % (name, G[name]); 153 G[name] = value 154 return redef
155
156 -def inject_generators(gens):
157 '''Inject generators as variables into the main global namespace 158 159 INPUT: 160 - "gens" - generators 161 ''' 162 for v in gens: 163 #print "vars = " + str(v); 164 redef = []; 165 s = str(v); 166 if s.find("/") < 0 and s.find("(") < 0 and s.find(",") < 0 and s.find("{") < 0 and s.find("[") < 0 and s.find("|") < 0: 167 if s[0:1] == "1": 168 s = "one" + s[1:] 169 #print "var = " + s; 170 rd = inject_variable(s,v) 171 if rd != None: 172 redef.append(rd); 173 if redef != []: 174 print "WARN: redefined variables " + ", ".join(redef);
175
176 -def nameFromValue(v):
177 '''Get a meaningful name from a value. 178 179 INPUT: 180 - "v" - the given value. 181 ''' 182 import re; 183 ri = re.compile(r'\A[0-9].*'); 184 vs = str(v); 185 vs = vs.replace(" ",""); 186 vs = vs.replace("\n",""); 187 if vs.find("(") >= 0: 188 vs = vs.replace("(",""); 189 vs = vs.replace(")",""); 190 if vs.find("{") >= 0: 191 vs = vs.replace("{",""); 192 vs = vs.replace("}",""); 193 if vs.find("[") >= 0: 194 vs = vs.replace("[",""); 195 vs = vs.replace("]",""); 196 if vs.find(",") >= 0: 197 vs = vs.replace(",",""); 198 #if vs.find("|") >= 0: 199 # vs = vs.replace("|","div"); # case "1"? 200 if vs.find("/") >= 0: 201 vs = vs.replace("/","div"); 202 if vs[0:1] == "1" and not ri.match(vs): 203 vs = 'one' + vs[1:]; 204 if vs == "1": 205 vs = "one"; 206 if vs == "0i1" or vs == "0I1": 207 vs = "I"; 208 if vs.find("|") >= 0 or ri.match(vs): 209 #print "0vs = " + str(vs); 210 return None; 211 return vs;
212 213
214 -class Ring:
215 '''Represents a JAS polynomial ring: GenPolynomialRing. 216 217 Methods to create ideals and ideals with parametric coefficients. 218 ''' 219
220 - def __init__(self,ringstr="",ring=None,fast=False):
221 '''Ring constructor. 222 223 ringstr string representation to be parsed. 224 ring JAS ring object. 225 ''' 226 if ring == None: 227 sr = StringReader( ringstr ); 228 tok = RingFactoryTokenizer(sr); 229 pfac = tok.nextPolynomialRing(); 230 #tok = GenPolynomialTokenizer(pfac,sr); 231 #plist = tok.nextPolynomialList(); 232 #self.pset = PolynomialList(pfac,plist); 233 self.ring = pfac; 234 else: 235 if isinstance(ring,Ring): 236 self.ring = ring.ring; 237 else: 238 self.ring = ring; 239 if fast: 240 return; 241 self.engine = Ring.getEngineGcd(self.ring); 242 self.sqf = Ring.getEngineSqf(self.ring); 243 self.factor = Ring.getEngineFactor(self.ring); 244 self.variable_generators();
245
246 - def getEngineGcd(r):
247 '''Get the polynomial gcd engine implementation. 248 249 r is the given polynomial ring. 250 ''' 251 if isinstance(r,RingElem): 252 r = r.elem; 253 if not isinstance(r,GenPolynomialRing): 254 return None; 255 try: 256 i = GCDFactory.getProxy(r.coFac); 257 except: 258 i = None 259 return i;
260 261 getEngineGcd = staticmethod(getEngineGcd); 262
263 - def getEngineSqf(r):
264 '''Get the polynomial squarefree engine implementation. 265 266 r is the given polynomial ring. 267 ''' 268 if isinstance(r,RingElem): 269 r = r.elem; 270 if not isinstance(r,GenPolynomialRing): 271 return None; 272 try: 273 i = SquarefreeFactory.getImplementation(r.coFac); 274 except: 275 i = None 276 return i;
277 278 getEngineSqf = staticmethod(getEngineSqf); 279
280 - def getEngineFactor(r):
281 '''Get the polynomial factorization engine implementation. 282 283 r is the given polynomial ring. 284 ''' 285 if isinstance(r,RingElem): 286 r = r.elem; 287 if not isinstance(r,GenPolynomialRing): 288 return None; 289 try: 290 i = FactorFactory.getImplementation(r.coFac); 291 except: 292 i = None 293 return i;
294 295 getEngineFactor = staticmethod(getEngineFactor); 296
297 - def variable_generators(self):
298 '''Define instance variables for generators. 299 ''' 300 vns = [] 301 redef = [] 302 #print "dict: " + str(self.__dict__) 303 for v in self.gens(): #ring.generators(): 304 #vr = RingElem(v); 305 #print "vars = " + str(v); 306 vs = nameFromValue(v); 307 if vs is None: 308 #print "0vs = " + str(vs); 309 continue; 310 try: 311 if self.__dict__[vs] is None: 312 self.__dict__[vs] = v; 313 else: 314 print vs + " not redefined to " + str(v); 315 except: 316 self.__dict__[vs] = v; 317 if auto_inject: 318 rd = inject_variable(vs,v) 319 vns.append(vs) 320 if rd != None: 321 redef.append(rd); 322 if auto_inject: 323 print "globally defined variables: " + ", ".join(vns) 324 if redef != []: 325 print "WARN: redefined global variables: " + ", ".join(redef);
326 #print "dict: " + str(self.__dict__) 327
328 - def __str__(self):
329 '''Create a string representation. 330 ''' 331 return str(self.ring.toScript());
332
333 - def __eq__(self,other):
334 '''Test if two rings are equal. 335 ''' 336 if not isinstance(other, Ring): 337 return False; 338 s = self.ring; 339 t = other.ring; 340 return s.equals(t)
341
342 - def ideal(self,ringstr="",list=None):
343 '''Create an ideal. 344 ''' 345 return Ideal(self,ringstr,list=list);
346
347 - def paramideal(self,ringstr="",list=None,gbsys=None):
348 '''Create an ideal in a polynomial ring with parameter coefficients. 349 ''' 350 return ParamIdeal(self,ringstr,list,gbsys);
351
352 - def powerseriesRing(self):
353 '''Get a power series ring from this ring. 354 ''' 355 pr = MultiVarPowerSeriesRing(self.ring); 356 return MultiSeriesRing(ring=pr);
357
358 - def gens(self):
359 '''Get list of generators of the polynomial ring. 360 ''' 361 L = self.ring.generators(); 362 N = [ RingElem(e) for e in L ]; 363 return N;
364
365 - def inject_variables(self):
366 '''Inject generators as variables into the main global namespace 367 ''' 368 inject_generators(self.gens());
369
370 - def one(self):
371 '''Get the one of the polynomial ring. 372 ''' 373 return RingElem( self.ring.getONE() );
374
375 - def zero(self):
376 '''Get the zero of the polynomial ring. 377 ''' 378 return RingElem( self.ring.getZERO() );
379
380 - def random(self,k=5,l=7,d=3,q=0.3):
381 '''Get a random polynomial. 382 ''' 383 r = self.ring.random(k,l,d,q); 384 if self.ring.coFac.isField(): 385 r = r.monic(); 386 return RingElem( r );
387
388 - def element(self,poly):
389 '''Create an element from a string or object. 390 ''' 391 if not isinstance(poly,str): 392 try: 393 if self.ring == poly.ring: 394 return RingElem(poly); 395 except Exception, e: 396 pass 397 poly = str(poly); 398 I = Ideal(self, "( " + poly + " )"); 399 list = I.pset.list; 400 if len(list) > 0: 401 return RingElem( list[0] );
402
403 - def gcd(self,a,b):
404 '''Compute the greatest common divisor of a and b. 405 ''' 406 if isinstance(a,RingElem): 407 a = a.elem; 408 else: 409 a = self.element( a ); 410 a = a.elem; 411 if isinstance(b,RingElem): 412 b = b.elem; 413 else: 414 b = self.element( b ); 415 b = b.elem; 416 return RingElem( self.engine.gcd(a,b) );
417
418 - def squarefreeFactors(self,a):
419 '''Compute squarefree factors of polynomial. 420 ''' 421 if isinstance(a,RingElem): 422 a = a.elem; 423 else: 424 a = self.element( a ); 425 a = a.elem; 426 cf = self.ring.coFac; 427 if isinstance(cf,GenPolynomialRing): 428 e = self.sqf.recursiveSquarefreeFactors( a ); 429 else: 430 e = self.sqf.squarefreeFactors( a ); 431 L = {}; 432 for a in e.keySet(): 433 i = e.get(a); 434 L[ RingElem( a ) ] = i; 435 return L;
436
437 - def factors(self,a):
438 '''Compute irreducible factorization for modular, integer, 439 rational number and algebriac number coefficients. 440 ''' 441 if isinstance(a,RingElem): 442 a = a.elem; 443 else: 444 a = self.element( a ); 445 a = a.elem; 446 try: 447 cf = self.ring.coFac; 448 if isinstance(cf,GenPolynomialRing) and cf.isCommutative(): 449 e = self.factor.recursiveFactors( a ); 450 else: 451 e = self.factor.factors( a ); 452 L = {}; 453 for a in e.keySet(): 454 i = e.get(a); 455 L[ RingElem( a ) ] = i; 456 return L; 457 except Exception, e: 458 print "error " + str(e) 459 return None
460
461 - def factorsAbsolute(self,a):
462 '''Compute absolute irreducible factorization for (modular,) 463 rational number coefficients. 464 ''' 465 if isinstance(a,RingElem): 466 a = a.elem; 467 else: 468 a = self.element( a ); 469 a = a.elem; 470 try: 471 L = self.factor.factorsAbsolute( a ); 472 ## L = {}; 473 ## for a in e.keySet(): 474 ## i = e.get(a); 475 ## L[ RingElem( a ) ] = i; 476 return L; 477 except Exception, e: 478 print "error in factorsAbsolute " + str(e) 479 return None
480
481 - def realRoots(self,a,eps=None):
482 '''Compute real roots of univariate polynomial. 483 ''' 484 if not isinstance(a,RingElem): 485 a = RingElem(a); 486 return a.realRoots(eps);
487
488 - def complexRoots(self,a,eps=None):
489 '''Compute complex roots of univariate polynomial. 490 ''' 491 if not isinstance(a,RingElem): 492 a = RingElem(a); 493 return a.complexRoots(eps);
494
495 - def algebraicRoots(self,a,eps=None):
496 '''Algebraic real and Compute complex roots of univariate polynomial. 497 ''' 498 if not isinstance(a,RingElem): 499 a = RingElem(a); 500 return a.algebraicRoots(eps);
501
502 - def rootRefine(self,a,eps=None):
503 '''Compute algebraic roots refinement. 504 ''' 505 if not isinstance(a,RingElem): 506 a = RingElem(a); 507 return a.rootRefine(eps);
508
509 - def decimalRoots(self,a,eps=None):
510 '''Compute decimal approximation of real and complex roots of univariate polynomial. 511 ''' 512 if not isinstance(a,RingElem): 513 a = RingElem(a); 514 return a.decimalRoots(eps);
515
516 - def rootsOfUnity(self,a):
517 '''Roots of unity of real and complex algebraic numbers. 518 ''' 519 if not isinstance(a,RingElem): 520 a = RingElem(a); 521 return a.rootsOfUnity();
522
523 - def rootReduce(self,a, b):
524 '''Root reduce of real and complex algebraic numbers. 525 Compute an extension field with a primitive element. 526 ''' 527 if not isinstance(a,RingElem): 528 a = RingElem(a); 529 return a.rootReduce(b);
530
531 - def integrate(self,a):
532 '''Integrate rational function or power series. 533 ''' 534 if not isinstance(a,RingElem): 535 a = RingElem(a); 536 return a.integrate();
537
538 - def subring(self,polystr="",list=None):
539 '''Sub ring generators as Groebner base. 540 ''' 541 if list == None: 542 sr = StringReader( polystr ); 543 tok = GenPolynomialTokenizer(self.ring,sr); 544 self.list = tok.nextPolynomialList(); 545 else: 546 self.list = pylist2arraylist(list,rec=1); 547 sr = PolyGBUtil.subRing(self.list); 548 srr = [ RingElem(a) for a in sr ]; 549 return srr;
550
551 - def subringmember(self, list, a):
552 '''Sub ring member test. list is a Groebner base. 553 Test if a \in K[list]. 554 ''' 555 sr = [ p.elem for p in list]; 556 if isinstance(a,RingElem): 557 a = a.elem; 558 b = PolyGBUtil.subRingMember(sr, a); 559 return b;
560
561 - def CRT(self,polystr="", list=None, rem=None):
562 '''Chinese remainder theorem. 563 ''' 564 if list == None: 565 sr = StringReader( polystr ); 566 tok = GenPolynomialTokenizer(self.ring,sr); 567 self.list = tok.nextPolynomialList(); 568 else: 569 self.list = pylist2arraylist(list,rec=2); 570 if rem == None: 571 raise ValueError, "No remainders given." 572 else: 573 self.remlist = pylist2arraylist(rem,rec=1); 574 #print "list = " + str(self.list); 575 #print "remlist = " + str(self.remlist); 576 #print 577 h = PolyGBUtil.chineseRemainderTheorem(self.list, self.remlist); 578 if h != None: 579 h = RingElem(h); 580 return h;
581
582 - def CRTinterpol(self,polystr="", list=None, rem=None):
583 '''Chinese remainder theorem, interpolation. 584 ''' 585 if list == None: 586 sr = StringReader( polystr ); 587 tok = GenPolynomialTokenizer(self.ring,sr); 588 self.list = tok.nextPolynomialList(); 589 else: 590 self.list = pylist2arraylist(list,rec=2); 591 if rem == None: 592 raise ValueError, "No remainders given." 593 else: 594 self.remlist = pylist2arraylist(rem,rec=1); 595 #print "ring = " + str(self.ring); 596 #print "list = " + str(self.list); 597 #print "remlist = " + str(self.remlist); 598 #print 599 h = PolyGBUtil.CRTInterpolation(self.ring, self.list, self.remlist); 600 if h != None: 601 h = RingElem(h); 602 return h;
603 604
605 -class Ideal:
606 '''Represents a JAS polynomial ideal: PolynomialList and Ideal. 607 608 Methods for Groebner bases, ideal sum, intersection and others. 609 ''' 610
611 - def __init__(self,ring,polystr="",list=None):
612 '''Ideal constructor. 613 ''' 614 self.ring = ring; 615 if list == None: 616 sr = StringReader( polystr ); 617 tok = GenPolynomialTokenizer(ring.ring,sr); 618 self.list = tok.nextPolynomialList(); 619 else: 620 self.list = pylist2arraylist(list,rec=1); 621 self.pset = OrderedPolynomialList(ring.ring,self.list); 622 self.roots = None; 623 self.croots = None; 624 self.prime = None; 625 self.primary = None;
626
627 - def __str__(self):
628 '''Create a string representation. 629 ''' 630 return str(self.pset.toScript());
631
632 - def __eq__(self,other):
633 '''Test if two ideals are equal. 634 ''' 635 o = other; 636 if isinstance(other,Ideal): 637 o = other.pset; 638 return self.pset.equals(o)
639
640 - def paramideal(self):
641 '''Create an ideal in a polynomial ring with parameter coefficients. 642 ''' 643 return ParamIdeal(self.ring,"",self.list);
644
645 - def GB(self):
646 '''Compute a Groebner base. 647 ''' 648 s = self.pset; 649 cofac = s.ring.coFac; 650 F = s.list; 651 kind = ""; 652 t = System.currentTimeMillis(); 653 if cofac.isField(): 654 G = GroebnerBaseSeq(ReductionSeq(),OrderedSyzPairlist()).GB(F); 655 #G = GroebnerBaseSeqIter(ReductionSeq(),OrderedSyzPairlist()).GB(F); 656 #G = GroebnerBaseSeq().GB(F); 657 kind = "field" 658 else: 659 if isinstance(cofac,GenPolynomialRing) and cofac.isCommutative(): 660 G = GroebnerBasePseudoRecSeq(cofac).GB(F); 661 kind = "pseudorec" 662 else: 663 G = GroebnerBasePseudoSeq(cofac).GB(F); 664 kind = "pseudo" 665 t = System.currentTimeMillis() - t; 666 print "sequential(%s) GB executed in %s ms" % (kind, t); 667 return Ideal(self.ring,"",G);
668
669 - def isGB(self):
670 '''Test if this is a Groebner base. 671 ''' 672 s = self.pset; 673 cofac = s.ring.coFac; 674 F = s.list; 675 t = System.currentTimeMillis(); 676 if cofac.isField(): 677 b = GroebnerBaseSeq().isGB(F); 678 else: 679 if isinstance(cofac,GenPolynomialRing) and cofac.isCommutative(): 680 b = GroebnerBasePseudoRecSeq(cofac).isGB(F); 681 else: 682 b = GroebnerBasePseudoSeq(cofac).isGB(F); 683 t = System.currentTimeMillis() - t; 684 #print "isGB executed in %s ms" % t; 685 return b;
686
687 - def eGB(self):
688 '''Compute an e-Groebner base. 689 ''' 690 s = self.pset; 691 cofac = s.ring.coFac; 692 F = s.list; 693 t = System.currentTimeMillis(); 694 G = EGroebnerBaseSeq().GB(F) 695 t = System.currentTimeMillis() - t; 696 print "sequential e-GB executed in %s ms" % t; 697 return Ideal(self.ring,"",G);
698
699 - def iseGB(self):
700 '''Test if this is an e-Groebner base. 701 ''' 702 s = self.pset; 703 cofac = s.ring.coFac; 704 F = s.list; 705 t = System.currentTimeMillis(); 706 b = EGroebnerBaseSeq().isGB(F) 707 t = System.currentTimeMillis() - t; 708 print "is e-GB test executed in %s ms" % t; 709 return b;
710
711 - def eExtGB(self):
712 '''Compute an extended e-Groebner base. 713 ''' 714 s = self.pset; 715 cofac = s.ring.coFac; 716 F = s.list; 717 t = System.currentTimeMillis(); 718 G = EGroebnerBaseSeq().extGB(F) 719 t = System.currentTimeMillis() - t; 720 print "sequential extended e-GB executed in %s ms" % t; 721 return G; #Ideal(self.ring,"",G);
722
723 - def iseExtGB(self, eg):
724 '''Test if eg is an extended e-Groebner base. 725 ''' 726 s = self.pset; 727 cofac = s.ring.coFac; 728 F = s.list; 729 t = System.currentTimeMillis(); 730 b = EGroebnerBaseSeq().isMinReductionMatrix(eg) 731 t = System.currentTimeMillis() - t; 732 print "sequential test extended e-GB executed in %s ms" % t; 733 return b;
734
735 - def dGB(self):
736 '''Compute an d-Groebner base. 737 ''' 738 s = self.pset; 739 cofac = s.ring.coFac; 740 F = s.list; 741 t = System.currentTimeMillis(); 742 G = DGroebnerBaseSeq().GB(F) 743 t = System.currentTimeMillis() - t; 744 print "sequential d-GB executed in %s ms" % t; 745 return Ideal(self.ring,"",G);
746
747 - def isdGB(self):
748 '''Test if this is a d-Groebner base. 749 ''' 750 s = self.pset; 751 cofac = s.ring.coFac; 752 F = s.list; 753 t = System.currentTimeMillis(); 754 b = DGroebnerBaseSeq().isGB(F) 755 t = System.currentTimeMillis() - t; 756 print "is d-GB test executed in %s ms" % t; 757 return b;
758
759 - def parNewGB(self,th):
760 '''Compute in parallel a Groebner base. 761 ''' 762 s = self.pset; 763 F = s.list; 764 bbpar = GroebnerBaseSeqPairParallel(th); 765 t = System.currentTimeMillis(); 766 G = bbpar.GB(F); 767 t = System.currentTimeMillis() - t; 768 bbpar.terminate(); 769 print "parallel-new %s executed in %s ms" % (th, t); 770 return Ideal(self.ring,"",G);
771
772 - def parGB(self,th):
773 '''Compute in parallel a Groebner base. 774 ''' 775 s = self.pset; 776 F = s.list; 777 cofac = s.ring.coFac; 778 if cofac.isField(): 779 bbpar = GroebnerBaseParallel(th); 780 else: 781 bbpar = GroebnerBasePseudoParallel(th,cofac); 782 t = System.currentTimeMillis(); 783 G = bbpar.GB(F); 784 t = System.currentTimeMillis() - t; 785 bbpar.terminate(); 786 print "parallel %s executed in %s ms" % (th, t); 787 return Ideal(self.ring,"",G);
788
789 - def distGB(self,th=2,machine="examples/machines.localhost",port=55711):
790 '''Compute on a distributed system a Groebner base. 791 ''' 792 s = self.pset; 793 F = s.list; 794 t = System.currentTimeMillis(); 795 #old: gbd = GBDist(th,machine,port); 796 gbd = GroebnerBaseDistributedEC(machine,th,port); 797 #gbd = GroebnerBaseDistributedHybridEC(machine,th,port); 798 t1 = System.currentTimeMillis(); 799 G = gbd.GB(F); 800 t1 = System.currentTimeMillis() - t1; 801 gbd.terminate(); 802 t = System.currentTimeMillis() - t; 803 print "distributed %s executed in %s ms (%s ms start-up)" % (th,t1,t-t1); 804 return Ideal(self.ring,"",G);
805
806 - def distClient(self,port=4711): #8114
807 '''Client for a distributed computation. 808 ''' 809 e1 = ExecutableServer( port ); 810 e1.init(); 811 e2 = ExecutableServer( port+1 ); 812 e2.init(); 813 self.exers = [e1,e2]; 814 return None;
815
816 - def distClientStop(self):
817 '''Stop client for a distributed computation. 818 ''' 819 for es in self.exers: 820 es.terminate(); 821 return None;
822
823 - def eReduction(self,p):
824 '''Compute a e-normal form of p with respect to this ideal. 825 ''' 826 s = self.pset; 827 G = s.list; 828 if isinstance(p,RingElem): 829 p = p.elem; 830 t = System.currentTimeMillis(); 831 n = EReductionSeq().normalform(G,p); 832 t = System.currentTimeMillis() - t; 833 print "sequential eReduction executed in %s ms" % t; 834 return RingElem(n);
835
836 - def eReductionRec(self,row,p):
837 '''Compute a e-normal form with recording of p with respect to this ideal. 838 ''' 839 s = self.pset; 840 G = s.list; 841 row = pylist2arraylist(row,None,rec=1); 842 if isinstance(p,RingElem): 843 p = p.elem; 844 t = System.currentTimeMillis(); 845 n = EReductionSeq().normalform(row,G,p); 846 t = System.currentTimeMillis() - t; 847 print "sequential eReduction recording executed in %s ms" % t; 848 return row, RingElem(n);
849
850 - def iseReductionRec(self, row, p, n):
851 '''Test if n is a e-normal form with recording of p with respect to this ideal. 852 ''' 853 s = self.pset; 854 G = s.list; 855 row = pylist2arraylist(row,None,rec=1); 856 if isinstance(p,RingElem): 857 p = p.elem; 858 if isinstance(n,RingElem): 859 n = n.elem; 860 t = System.currentTimeMillis(); 861 b = EReductionSeq().isReductionNF(row,G,p,n); 862 t = System.currentTimeMillis() - t; 863 return b;
864
865 - def reduction(self,p):
866 '''Compute a normal form of p with respect to this ideal. 867 ''' 868 s = self.pset; 869 G = s.list; 870 if isinstance(p,RingElem): 871 p = p.elem; 872 if self.ring.ring.coFac.isField(): 873 n = ReductionSeq().normalform(G,p); 874 else: 875 n = PseudoReductionSeq().normalform(G,p); 876 #F = PseudoReductionSeq().normalformFactor(G,p); 877 #print "F.multiplicator = " + str(F.multiplicator) 878 #n = F.pol 879 return RingElem(n);
880
881 - def NF(self,reducer):
882 '''Compute a normal form of this ideal with respect to reducer. 883 ''' 884 s = self.pset; 885 F = s.list; 886 G = reducer.list; 887 t = System.currentTimeMillis(); 888 N = ReductionSeq().normalform(G,F); 889 t = System.currentTimeMillis() - t; 890 print "sequential NF executed in %s ms" % t; 891 return Ideal(self.ring,"",N);
892
893 - def lift(self,p):
894 '''Represent p as element of this ideal. 895 ''' 896 G = self.pset.list; 897 z = self.ring.ring.getZERO(); 898 R = [ z for x in G]; 899 if isinstance(p,RingElem): 900 p = p.elem; 901 if self.ring.ring.coFac.isField(): 902 n = ReductionSeq().normalform(R,G,p); 903 else: 904 n = PseudoReductionSeq().normalform(R,G,p); 905 if not n.isZERO(): 906 raise ValueError, "p ist not a member of the ideal" 907 return [ RingElem(f) for f in R ];
908
909 - def interreduced_basis(self):
910 '''Compute a interreduced ideal basis of this. 911 912 Compatibility method for Sage/Singular. 913 ''' 914 F = self.pset.list; 915 if self.ring.ring.coFac.isField(): 916 N = ReductionSeq().irreducibleSet(F); 917 else: 918 N = PseudoReductionSeq().irreducibleSet(F); 919 #N = GroebnerBaseSeq().minimalGB(F); 920 return [ RingElem(n) for n in N ];
921
922 - def intersectRing(self,ring):
923 '''Compute the intersection of this and the given polynomial ring. 924 ''' 925 s = jas.application.Ideal(self.pset); 926 N = s.intersect(ring.ring); 927 return Ideal(ring,"",N.getList());
928
929 - def intersect(self,id2):
930 '''Compute the intersection of this and the given ideal id2. 931 ''' 932 s1 = jas.application.Ideal(self.pset); 933 s2 = jas.application.Ideal(id2.pset); 934 N = s1.intersect(s2); 935 return Ideal(self.ring,"",N.getList());
936
937 - def eliminateRing(self,ring):
938 '''Compute the elimination ideal of this and the given polynomial ring. 939 ''' 940 s = jas.application.Ideal(self.pset); 941 N = s.eliminate(ring.ring); 942 r = Ring( ring=N.getRing() ); 943 return Ideal(r,"",N.getList());
944
945 - def sat(self,id2):
946 '''Compute the saturation of this with respect to given ideal id2. 947 ''' 948 s1 = jas.application.Ideal(self.pset); 949 s2 = jas.application.Ideal(id2.pset); 950 #Q = s1.infiniteQuotient(s2); 951 Q = s1.infiniteQuotientRab(s2); 952 return Ideal(self.ring,"",Q.getList());
953
954 - def sum(self,other):
955 '''Compute the sum of this and the ideal. 956 ''' 957 s = jas.application.Ideal(self.pset); 958 t = jas.application.Ideal(other.pset); 959 N = s.sum( t ); 960 return Ideal(self.ring,"",N.getList());
961
962 - def univariates(self):
963 '''Compute the univariate polynomials in each variable of this ideal. 964 ''' 965 s = jas.application.Ideal(self.pset); 966 L = s.constructUnivariate(); 967 N = [ RingElem(e) for e in L ]; 968 return N;
969
970 - def inverse(self,p):
971 '''Compute the inverse polynomial modulo this ideal, if it exists. 972 ''' 973 s = jas.application.Ideal(self.pset); 974 if isinstance(p,RingElem): 975 p = p.elem; 976 i = s.inverse(p); 977 return RingElem(i);
978
979 - def eInverse(self,p):
980 '''Compute the e-inverse polynomial modulo this e-ideal, if it exists. 981 ''' 982 if isinstance(p,RingElem): 983 p = p.elem; 984 i = EGroebnerBaseSeq().inverse(p, self.list); 985 return RingElem(i);
986
987 - def iseInverse(self,i,p):
988 '''Test if i is a e-inverse of p modulo this e-ideal. 989 ''' 990 if isinstance(i,RingElem): 991 i = i.elem; 992 if isinstance(p,RingElem): 993 p = p.elem; 994 r = EReductionSeq().normalform(self.list, i.multiply(p)); 995 return r.abs().isONE();
996 997
998 - def optimize(self):
999 '''Optimize the term order on the variables. 1000 ''' 1001 p = self.pset; 1002 o = TermOrderOptimization.optimizeTermOrder(p); 1003 r = Ring("",o.ring); 1004 return Ideal(r,"",o.list);
1005
1006 - def dimension(self):
1007 '''Compute the dimension of the ideal. 1008 ''' 1009 I = jas.application.Ideal(self.pset); 1010 d = I.dimension(); 1011 return d;
1012
1013 - def realRoots(self):
1014 '''Compute real roots of 0-dim ideal. 1015 ''' 1016 I = jas.application.Ideal(self.pset); 1017 self.roots = jas.application.PolyUtilApp.realAlgebraicRoots(I); 1018 for R in self.roots: 1019 R.doDecimalApproximation(); 1020 return self.roots;
1021
1022 - def realRootsPrint(self):
1023 '''Print decimal approximation of real roots of 0-dim ideal. 1024 ''' 1025 if self.roots == None: 1026 I = jas.application.Ideal(self.pset); 1027 self.roots = jas.application.PolyUtilApp.realAlgebraicRoots(I); 1028 for R in self.roots: 1029 R.doDecimalApproximation(); 1030 for Ir in self.roots: 1031 for Dr in Ir.decimalApproximation(): 1032 print str(Dr); 1033 print;
1034
1035 - def radicalDecomp(self):
1036 '''Compute radical decomposition of this ideal. 1037 ''' 1038 I = jas.application.Ideal(self.pset); 1039 self.radical = I.radicalDecomposition(); 1040 return self.radical;
1041
1042 - def decomposition(self):
1043 '''Compute irreducible decomposition of this ideal. 1044 ''' 1045 I = jas.application.Ideal(self.pset); 1046 self.irrdec = I.decomposition(); 1047 return self.irrdec;
1048
1049 - def complexRoots(self):
1050 '''Compute complex roots of 0-dim ideal. 1051 ''' 1052 I = jas.application.Ideal(self.pset); 1053 self.croots = jas.application.PolyUtilApp.complexAlgebraicRoots(I); 1054 for R in self.croots: 1055 R.doDecimalApproximation(); 1056 return self.croots;
1057
1058 - def complexRootsPrint(self):
1059 '''Print decimal approximation of complex roots of 0-dim ideal. 1060 ''' 1061 if self.croots == None: 1062 I = jas.application.Ideal(self.pset); 1063 self.croots = jas.application.PolyUtilApp.realAlgebraicRoots(I); 1064 for R in self.croots: 1065 R.doDecimalApproximation(); 1066 for Ic in self.croots: 1067 for Dc in Ic.decimalApproximation(): 1068 print str(Dc); 1069 print;
1070
1071 - def primeDecomp(self):
1072 '''Compute prime decomposition of this ideal. 1073 ''' 1074 I = jas.application.Ideal(self.pset); 1075 self.prime = I.primeDecomposition(); 1076 return self.prime;
1077
1078 - def primaryDecomp(self):
1079 '''Compute primary decomposition of this ideal. 1080 ''' 1081 I = jas.application.Ideal(self.pset); 1082 ## if self.prime == None: 1083 ## self.prime = I.primeDecomposition(); 1084 self.primary = I.primaryDecomposition(); 1085 return self.primary;
1086
1087 - def toInteger(self):
1088 '''Convert rational coefficients to integer coefficients. 1089 ''' 1090 p = self.pset; 1091 l = p.list; 1092 r = p.ring; 1093 ri = GenPolynomialRing( BigInteger(), r.nvar, r.tord, r.vars ); 1094 pi = PolyUtil.integerFromRationalCoefficients(ri,l); 1095 r = Ring("",ri); 1096 return Ideal(r,"",pi);
1097
1098 - def toModular(self,mf):
1099 '''Convert integer coefficients to modular coefficients. 1100 ''' 1101 p = self.pset; 1102 l = p.list; 1103 r = p.ring; 1104 if isinstance(mf,RingElem): 1105 mf = mf.ring; 1106 rm = GenPolynomialRing( mf, r.nvar, r.tord, r.vars ); 1107 pm = PolyUtil.fromIntegerCoefficients(rm,l); 1108 r = Ring("",rm); 1109 return Ideal(r,"",pm);
1110
1111 - def CS(self):
1112 '''Compute a Characteristic Set. 1113 ''' 1114 s = self.pset; 1115 cofac = s.ring.coFac; 1116 F = s.list; 1117 t = System.currentTimeMillis(); 1118 if cofac.isField(): 1119 G = CharacteristicSetWu().characteristicSet(F); 1120 else: 1121 print "CS not implemented for coefficients %s" % cofac.toScriptFactory(); 1122 G = None; 1123 t = System.currentTimeMillis() - t; 1124 print "sequential CS executed in %s ms" % t; 1125 return Ideal(self.ring,"",G);
1126
1127 - def isCS(self):
1128 '''Test for Characteristic Set. 1129 ''' 1130 s = self.pset; 1131 cofac = s.ring.coFac; 1132 F = s.list.clone(); 1133 Collections.reverse(F); # todo 1134 t = System.currentTimeMillis(); 1135 if cofac.isField(): 1136 b = CharacteristicSetWu().isCharacteristicSet(F); 1137 else: 1138 print "isCS not implemented for coefficients %s" % cofac.toScriptFactory(); 1139 b = False; 1140 t = System.currentTimeMillis() - t; 1141 return b;
1142
1143 - def csReduction(self,p):
1144 '''Compute a normal form of p with respect to this characteristic set. 1145 ''' 1146 s = self.pset; 1147 F = s.list.clone(); 1148 Collections.reverse(F); # todo 1149 if isinstance(p,RingElem): 1150 p = p.elem; 1151 t = System.currentTimeMillis(); 1152 n = CharacteristicSetWu().characteristicSetReduction(F,p); 1153 t = System.currentTimeMillis() - t; 1154 #print "sequential char set reduction executed in %s ms" % t; 1155 return RingElem(n);
1156
1157 - def syzygy(self):
1158 '''Syzygy of generating polynomials. 1159 ''' 1160 p = self.pset; 1161 l = p.list; 1162 t = System.currentTimeMillis(); 1163 s = SyzygySeq(p.ring.coFac).zeroRelations( l ); 1164 t = System.currentTimeMillis() - t; 1165 print "executed syzygy in %s ms" % t; 1166 m = Module("",p.ring); #cols=s[0].size() 1167 return SubModule(m,"",s);
1168
1169 - def isSyzygy(self,m):
1170 '''Test if this is a syzygy of the module in m. 1171 ''' 1172 p = self.pset; 1173 g = p.list; 1174 l = m.list; 1175 t = System.currentTimeMillis(); 1176 z = SyzygySeq(p.ring.coFac).isZeroRelation( l, g ); 1177 t = System.currentTimeMillis() - t; 1178 print "executed isSyzygy in %s ms" % t; 1179 return z;
1180 1181
1182 -class ParamIdeal:
1183 '''Represents a JAS polynomial ideal with polynomial coefficients. 1184 1185 Methods to compute comprehensive Groebner bases. 1186 ''' 1187
1188 - def __init__(self,ring,polystr="",list=None,gbsys=None):
1189 '''Parametric ideal constructor. 1190 ''' 1191 self.ring = ring; 1192 if list == None and polystr != None: 1193 sr = StringReader( polystr ); 1194 tok = GenPolynomialTokenizer(ring.ring,sr); 1195 self.list = tok.nextPolynomialList(); 1196 else: 1197 self.list = pylist2arraylist(list,rec=1); 1198 self.gbsys = gbsys; 1199 self.pset = OrderedPolynomialList(ring.ring,self.list);
1200
1201 - def __str__(self):
1202 '''Create a string representation. 1203 ''' 1204 if self.gbsys == None: 1205 return self.pset.toScript(); 1206 else: 1207 # return self.gbsys.toString(); # toScript() not available 1208 return self.pset.toScript() + "\n" + self.gbsys.toScript();
1209
1210 - def optimizeCoeff(self):
1211 '''Optimize the term order on the variables of the coefficients. 1212 ''' 1213 p = self.pset; 1214 o = TermOrderOptimization.optimizeTermOrderOnCoefficients(p); 1215 r = Ring("",o.ring); 1216 return ParamIdeal(r,"",o.list);
1217
1218 - def optimizeCoeffQuot(self):
1219 '''Optimize the term order on the variables of the quotient coefficients. 1220 ''' 1221 p = self.pset; 1222 l = p.list; 1223 r = p.ring; 1224 q = r.coFac; 1225 c = q.ring; 1226 rc = GenPolynomialRing( c, r.nvar, r.tord, r.vars ); 1227 #print "rc = ", rc; 1228 lp = PolyUfdUtil.integralFromQuotientCoefficients(rc,l); 1229 #print "lp = ", lp; 1230 pp = PolynomialList(rc,lp); 1231 #print "pp = ", pp; 1232 oq = TermOrderOptimization.optimizeTermOrderOnCoefficients(pp); 1233 oor = oq.ring; 1234 qo = oor.coFac; 1235 cq = QuotientRing( qo ); 1236 rq = GenPolynomialRing( cq, r.nvar, r.tord, r.vars ); 1237 #print "rq = ", rq; 1238 o = PolyUfdUtil.quotientFromIntegralCoefficients(rq,oq.list); 1239 r = Ring("",rq); 1240 return ParamIdeal(r,"",o);
1241
1242 - def toIntegralCoeff(self):
1243 '''Convert rational function coefficients to integral function coefficients. 1244 ''' 1245 p = self.pset; 1246 l = p.list; 1247 r = p.ring; 1248 q = r.coFac; 1249 c = q.ring; 1250 rc = GenPolynomialRing( c, r.nvar, r.tord, r.vars ); 1251 #print "rc = ", rc; 1252 lp = PolyUfdUtil.integralFromQuotientCoefficients(rc,l); 1253 #print "lp = ", lp; 1254 r = Ring("",rc); 1255 return ParamIdeal(r,"",lp);
1256
1257 - def toModularCoeff(self,mf):
1258 '''Convert integral function coefficients to modular function coefficients. 1259 ''' 1260 p = self.pset; 1261 l = p.list; 1262 r = p.ring; 1263 c = r.coFac; 1264 #print "c = ", c; 1265 if isinstance(mf,RingElem): 1266 mf = mf.ring; 1267 cm = GenPolynomialRing( mf, c.nvar, c.tord, c.vars ); 1268 #print "cm = ", cm; 1269 rm = GenPolynomialRing( cm, r.nvar, r.tord, r.vars ); 1270 #print "rm = ", rm; 1271 pm = PolyUfdUtil.fromIntegerCoefficients(rm,l); 1272 r = Ring("",rm); 1273 return ParamIdeal(r,"",pm);
1274
1275 - def toQuotientCoeff(self):
1276 '''Convert integral function coefficients to rational function coefficients. 1277 ''' 1278 p = self.pset; 1279 l = p.list; 1280 r = p.ring; 1281 c = r.coFac; 1282 #print "c = ", c; 1283 q = QuotientRing(c); 1284 #print "q = ", q; 1285 qm = GenPolynomialRing( q, r.nvar, r.tord, r.vars ); 1286 #print "qm = ", qm; 1287 pm = PolyUfdUtil.quotientFromIntegralCoefficients(qm,l); 1288 r = Ring("",qm); 1289 return ParamIdeal(r,"",pm);
1290
1291 - def GB(self):
1292 '''Compute a Groebner base. 1293 ''' 1294 I = Ideal(self.ring,"",self.pset.list); 1295 g = I.GB(); 1296 return ParamIdeal(g.ring,"",g.pset.list);
1297
1298 - def isGB(self):
1299 '''Test if this is a Groebner base. 1300 ''' 1301 I = Ideal(self.ring,"",self.pset.list); 1302 return I.isGB();
1303
1304 - def CGB(self):
1305 '''Compute a comprehensive Groebner base. 1306 ''' 1307 s = self.pset; 1308 F = s.list; 1309 t = System.currentTimeMillis(); 1310 if self.gbsys == None: 1311 self.gbsys = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).GBsys(F); 1312 G = self.gbsys.getCGB(); 1313 t = System.currentTimeMillis() - t; 1314 print "sequential comprehensive executed in %s ms" % t; 1315 return ParamIdeal(self.ring,"",G,self.gbsys);
1316
1317 - def CGBsystem(self):
1318 '''Compute a comprehensive Groebner system. 1319 ''' 1320 s = self.pset; 1321 F = s.list; 1322 t = System.currentTimeMillis(); 1323 S = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).GBsys(F); 1324 t = System.currentTimeMillis() - t; 1325 print "sequential comprehensive system executed in %s ms" % t; 1326 return ParamIdeal(self.ring,None,F,S);
1327
1328 - def isCGB(self):
1329 '''Test if this is a comprehensive Groebner base. 1330 ''' 1331 s = self.pset; 1332 F = s.list; 1333 t = System.currentTimeMillis(); 1334 b = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).isGB(F); 1335 t = System.currentTimeMillis() - t; 1336 print "isCGB executed in %s ms" % t; 1337 return b;
1338
1339 - def isCGBsystem(self):
1340 '''Test if this is a comprehensive Groebner system. 1341 ''' 1342 s = self.pset; 1343 S = self.gbsys; 1344 t = System.currentTimeMillis(); 1345 b = ComprehensiveGroebnerBaseSeq(self.ring.ring.coFac).isGBsys(S); 1346 t = System.currentTimeMillis() - t; 1347 print "isCGBsystem executed in %s ms" % t; 1348 return b;
1349
1350 - def regularRepresentation(self):
1351 '''Convert Groebner system to a representation with regular ring coefficents. 1352 ''' 1353 if self.gbsys == None: 1354 return None; 1355 G = PolyUtilApp.toProductRes(self.gbsys.list); 1356 ring = Ring(None,G[0].ring); 1357 return ParamIdeal(ring,None,G);
1358
1359 - def regularRepresentationBC(self):
1360 '''Convert Groebner system to a boolean closed representation with regular ring coefficents. 1361 ''' 1362 if self.gbsys == None: 1363 return None; 1364 G = PolyUtilApp.toProductRes(self.gbsys.list); 1365 ring = Ring(None,G[0].ring); 1366 res = RReductionSeq(); 1367 G = res.booleanClosure(G); 1368 return ParamIdeal(ring,None,G);
1369
1370 - def regularGB(self):
1371 '''Compute a Groebner base over a regular ring. 1372 ''' 1373 s = self.pset; 1374 F = s.list; 1375 t = System.currentTimeMillis(); 1376 G = RGroebnerBasePseudoSeq(self.ring.ring.coFac).GB(F); 1377 t = System.currentTimeMillis() - t; 1378 print "sequential regular GB executed in %s ms" % t; 1379 return ParamIdeal(self.ring,None,G);
1380
1381 - def isRegularGB(self):
1382 '''Test if this is Groebner base over a regular ring. 1383 ''' 1384 s = self.pset; 1385 F = s.list; 1386 t = System.currentTimeMillis(); 1387 b = RGroebnerBasePseudoSeq(self.ring.ring.coFac).isGB(F); 1388 t = System.currentTimeMillis() - t; 1389 print "isRegularGB executed in %s ms" % t; 1390 return b;
1391
1392 - def stringSlice(self):
1393 '''Get each component (slice) of regular ring coefficients separate. 1394 ''' 1395 s = self.pset; 1396 b = PolyUtilApp.productToString(s); 1397 return b;
1398 1399
1400 -class SolvableRing(Ring):
1401 '''Represents a JAS solvable polynomial ring: GenSolvablePolynomialRing. 1402 1403 Has a method to create solvable ideals. 1404 ''' 1405
1406 - def __init__(self,ringstr="",ring=None):
1407 '''Solvable polynomial ring constructor. 1408 ''' 1409 if ring == None: 1410 sr = StringReader( ringstr ); 1411 tok = RingFactoryTokenizer(sr); 1412 pfac = tok.nextSolvablePolynomialRing(); 1413 #tok = GenPolynomialTokenizer(pfac,sr); 1414 #plist = tok.nextSolvablePolynomialList(); 1415 #self.pset = PolynomialList(pfac,plist); 1416 self.ring = pfac; 1417 else: 1418 if isinstance(ring,Ring): 1419 self.ring = ring.ring; 1420 else: 1421 self.ring = ring; 1422 if not self.ring.isAssociative(): 1423 print "warning: ring is not associative"; 1424 else: 1425 print "ring is associative"; 1426 Ring.__init__(self,ring=self.ring)
1427
1428 - def __str__(self):
1429 '''Create a string representation. 1430 ''' 1431 return str(self.ring.toScript());
1432
1433 - def ideal(self,ringstr="",list=None):
1434 '''Create a solvable ideal. 1435 ''' 1436 return SolvableIdeal(self,ringstr,list);
1437
1438 - def one(self):
1439 '''Get the one of the solvable polynomial ring. 1440 ''' 1441 return RingElem( self.ring.getONE() );
1442
1443 - def zero(self):
1444 '''Get the zero of the solvable polynomial ring. 1445 ''' 1446 return RingElem( self.ring.getZERO() );
1447
1448 - def element(self,poly):
1449 '''Create an element from a string or object. 1450 ''' 1451 if not isinstance(poly,str): 1452 try: 1453 if self.ring == poly.ring: 1454 return RingElem(poly); 1455 except Exception, e: 1456 pass 1457 poly = str(poly); 1458 I = SolvableIdeal(self, "( " + poly + " )"); 1459 list = I.pset.list; 1460 if len(list) > 0: 1461 return RingElem( list[0] );
1462 1463
1464 -class SolvableIdeal:
1465 '''Represents a JAS solvable polynomial ideal. 1466 1467 Methods for left, right two-sided Groebner basees and others. 1468 ''' 1469
1470 - def __init__(self,ring,ringstr="",list=None):
1471 '''Constructor for an ideal in a solvable polynomial ring. 1472 ''' 1473 self.ring = ring; 1474 if list == None: 1475 sr = StringReader( ringstr ); 1476 tok = GenPolynomialTokenizer(ring.ring,sr); 1477 self.list = tok.nextSolvablePolynomialList(); 1478 else: 1479 self.list = pylist2arraylist(list,rec=1); 1480 self.pset = OrderedPolynomialList(ring.ring,self.list);
1481 #self.pset = PolynomialList(ring.ring,self.list); 1482
1483 - def __str__(self):
1484 '''Create a string representation. 1485 ''' 1486 return str(self.pset.toScript());
1487
1488 - def __cmp__(self,other):
1489 '''Compare two ideals. 1490 ''' 1491 t = False; 1492 if not isinstance(other,SolvableIdeal): 1493 return t; 1494 t = self.pset.compareTo(other.pset); 1495 return t;
1496
1497 - def __eq__(self,other):
1498 '''Test if two ideals are equal. 1499 ''' 1500 if not isinstance(other, SolvableIdeal): 1501 return False; 1502 s = self.pset; 1503 t = other.pset; 1504 return s.equals(t)
1505
1506 - def leftGB(self):
1507 '''Compute a left Groebner base. 1508 ''' 1509 cofac = self.ring.ring.coFac; 1510 F = self.pset.list; 1511 kind = ""; 1512 t = System.currentTimeMillis(); 1513 if isinstance(cofac,GenPolynomialRing): #and cofac.isCommutative(): 1514 G = SolvableGroebnerBasePseudoRecSeq(cofac).leftGB(F); 1515 kind = "pseudorec" 1516 else: 1517 if cofac.isField() or not cofac.isCommutative(): 1518 G = SolvableGroebnerBaseSeq().leftGB(F); 1519 kind = "field|nocom" 1520 else: 1521 G = SolvableGroebnerBasePseudoSeq(cofac).leftGB(F); 1522 kind = "pseudo" 1523 t = System.currentTimeMillis() - t; 1524 print "sequential(%s) left GB executed in %s ms" % (kind, t); 1525 return SolvableIdeal(self.ring,"",G);
1526
1527 - def isLeftGB(self):
1528 '''Test if this is a left Groebner base. 1529 ''' 1530 cofac = self.ring.ring.coFac; 1531 F = self.pset.list; 1532 kind = ""; 1533 t = System.currentTimeMillis(); 1534 if isinstance(cofac,GenPolynomialRing): #and cofac.isCommutative(): 1535 b = SolvableGroebnerBasePseudoRecSeq(cofac).isLeftGB(F); 1536 kind = "pseudorec" 1537 else: 1538 if cofac.isField() or not cofac.isCommutative(): 1539 b = SolvableGroebnerBaseSeq().isLeftGB(F); 1540 kind = "field|nocom" 1541 else: 1542 b = SolvableGroebnerBasePseudoSeq(cofac).isLeftGB(F); 1543 kind = "pseudo" 1544 t = System.currentTimeMillis() - t; 1545 print "sequential(%s) isLeftGB executed in %s ms" % (kind, t); 1546 return b;
1547
1548 - def twosidedGB(self):
1549 '''Compute a two-sided Groebner base. 1550 ''' 1551 cofac = self.ring.ring.coFac; 1552 F = self.pset.list; 1553 kind = ""; 1554 t = System.currentTimeMillis(); 1555 if isinstance(cofac,GenPolynomialRing): # and cofac.isCommutative(): 1556 G = SolvableGroebnerBasePseudoRecSeq(cofac).twosidedGB(F); 1557 kind = "pseudorec" 1558 else: 1559 if cofac.isField() or not cofac.isCommutative(): 1560 G = SolvableGroebnerBaseSeq().twosidedGB(F); 1561 kind = "field|nocom" 1562 else: 1563 G = SolvableGroebnerBasePseudoSeq(cofac).twosidedGB(F); 1564 kind = "pseudo" 1565 t = System.currentTimeMillis() - t; 1566 print "sequential(%s) twosided GB executed in %s ms" % (kind, t); 1567 return SolvableIdeal(self.ring,"",G);
1568
1569 - def isTwosidedGB(self):
1570 '''Test if this is a two-sided Groebner base. 1571 ''' 1572 cofac = self.ring.ring.coFac; 1573 F = self.pset.list; 1574 kind = ""; 1575 t = System.currentTimeMillis(); 1576 if isinstance(cofac,GenPolynomialRing): # and cofac.isCommutative(): 1577 b = SolvableGroebnerBasePseudoRecSeq(cofac).isTwosidedGB(F); 1578 kind = "pseudorec" 1579 else: 1580 if cofac.isField() or not cofac.isCommutative(): 1581 b = SolvableGroebnerBaseSeq().isTwosidedGB(F); 1582 kind = "field|nocom" 1583 else: 1584 b = SolvableGroebnerBasePseudoSeq(cofac).isTwosidedGB(F); 1585 kind = "pseudo" 1586 t = System.currentTimeMillis() - t; 1587 print "sequential(%s) isTwosidedGB executed in %s ms" % (kind, t); 1588 return b;
1589
1590 - def rightGB(self):
1591 '''Compute a right Groebner base. 1592 ''' 1593 cofac = self.ring.ring.coFac; 1594 F = self.pset.list; 1595 kind = ""; 1596 t = System.currentTimeMillis(); 1597 if isinstance(cofac,GenPolynomialRing): # and cofac.isCommutative(): 1598 G = SolvableGroebnerBasePseudoRecSeq(cofac).rightGB(F); 1599 kind = "pseudorec" 1600 else: 1601 if cofac.isField() or not cofac.isCommutative(): 1602 G = SolvableGroebnerBaseSeq().rightGB(F); 1603 kind = "field|nocom" 1604 else: 1605 G = SolvableGroebnerBasePseudoSeq(cofac).rightGB(F); 1606 kind = "pseudo" 1607 t = System.currentTimeMillis() - t; 1608 print "sequential(%s) right GB executed in %s ms" % (kind, t); 1609 return SolvableIdeal(self.ring,"",G);
1610
1611 - def isRightGB(self):
1612 '''Test if this is a right Groebner base. 1613 ''' 1614 cofac = self.ring.ring.coFac; 1615 F = self.pset.list; 1616 kind = ""; 1617 t = System.currentTimeMillis(); 1618 if isinstance(cofac,GenPolynomialRing): # and cofac.isCommutative(): 1619 b = SolvableGroebnerBasePseudoRecSeq(cofac).isRightGB(F); 1620 kind = "pseudorec" 1621 else: 1622 if cofac.isField() or not cofac.isCommutative(): 1623 b = SolvableGroebnerBaseSeq().isRightGB(F); 1624 kind = "field|nocom" 1625 else: 1626 b = SolvableGroebnerBasePseudoSeq(cofac).isRightGB(F); 1627 kind = "pseudo" 1628 t = System.currentTimeMillis() - t; 1629 print "sequential(%s) isRightGB executed in %s ms" % (kind, t); 1630 return b;
1631
1632 - def intersectRing(self,ring):
1633 '''Compute the intersection of this and the polynomial ring. 1634 ''' 1635 s = jas.application.SolvableIdeal(self.pset); 1636 N = s.intersect(ring.ring); 1637 return SolvableIdeal(self.ring,"",N.getList());
1638
1639 - def intersect(self,other):
1640 '''Compute the intersection of this and the other ideal. 1641 ''' 1642 s = jas.application.SolvableIdeal(self.pset); 1643 t = jas.application.SolvableIdeal(other.pset); 1644 N = s.intersect( t ); 1645 return SolvableIdeal(self.ring,"",N.getList());
1646
1647 - def sum(self,other):
1648 '''Compute the sum of this and the other ideal. 1649 ''' 1650 s = jas.application.SolvableIdeal(self.pset); 1651 t = jas.application.SolvableIdeal(other.pset); 1652 N = s.sum( t ); 1653 return SolvableIdeal(self.ring,"",N.getList());
1654
1655 - def univariates(self):
1656 '''Compute the univariate polynomials in each variable of this twosided ideal. 1657 ''' 1658 s = jas.application.SolvableIdeal(self.pset); 1659 L = s.constructUnivariate(); 1660 N = [ RingElem(e) for e in L ]; 1661 return N;
1662
1663 - def toQuotientCoefficients(self):
1664 '''Convert to polynomials with SolvableQuotient coefficients. 1665 ''' 1666 if isinstance(self.pset.ring.coFac,SolvableResidueRing): 1667 cf = self.pset.ring.coFac.ring; 1668 else: 1669 if isinstance(self.pset.ring.coFac,GenSolvablePolynomialRing): 1670 cf = self.pset.ring.coFac; 1671 #else: if @pset.ring.coFac.getClass().getSimpleName() == "GenPolynomialRing" 1672 # cf = @pset.ring.coFac; 1673 # puts "cf = " + cf.toScript(); 1674 else: 1675 return self; 1676 rrel = self.pset.ring.table.relationList(); 1677 rrel.addAll(self.pset.ring.polCoeff.coeffTable.relationList()); 1678 #print "rrel = " + str(rrel); 1679 qf = SolvableQuotientRing(cf); 1680 qr = QuotSolvablePolynomialRing(qf,self.pset.ring); 1681 #print "qr = " + str(qr); 1682 qrel = [ RingElem(qr.fromPolyCoefficients(r)) for r in rrel ]; 1683 qring = SolvPolyRing(qf,self.ring.ring.getVars(),self.ring.ring.tord,qrel); 1684 #print "qring = " + str(qring); 1685 qlist = [ qr.fromPolyCoefficients(self.ring.ring.toPolyCoefficients(r)) for r in self.list ]; 1686 qlist = [ RingElem(r) for r in qlist ]; 1687 return SolvableIdeal(qring,"",qlist);
1688
1689 - def inverse(self,p):
1690 '''Compute the inverse polynomial modulo this ideal, if it exists. 1691 ''' 1692 s = jas.application.SolvableIdeal(self.pset); 1693 if isinstance(p,RingElem): 1694 p = p.elem; 1695 i = s.inverse(p); 1696 return RingElem(i);
1697
1698 - def leftReduction(self,p):
1699 '''Compute a left normal form of p with respect to this ideal. 1700 ''' 1701 s = self.pset; 1702 G = s.list; 1703 if isinstance(p,RingElem): 1704 p = p.elem; 1705 n = SolvableReductionSeq().leftNormalform(G,p); 1706 return RingElem(n);
1707
1708 - def rightReduction(self,p):
1709 '''Compute a right normal form of p with respect to this ideal. 1710 ''' 1711 s = self.pset; 1712 G = s.list; 1713 if isinstance(p,RingElem): 1714 p = p.elem; 1715 n = SolvableReductionSeq().rightNormalform(G,p); 1716 return RingElem(n);
1717
1718 - def parLeftGB(self,th):
1719 '''Compute a left Groebner base in parallel. 1720 ''' 1721 s = self.pset; 1722 F = s.list; 1723 bbpar = SolvableGroebnerBaseParallel(th); 1724 t = System.currentTimeMillis(); 1725 G = bbpar.leftGB(F); 1726 t = System.currentTimeMillis() - t; 1727 bbpar.terminate(); 1728 print "parallel %s leftGB executed in %s ms" % (th, t); 1729 return SolvableIdeal(self.ring,"",G);
1730
1731 - def parTwosidedGB(self,th):
1732 '''Compute a two-sided Groebner base in parallel. 1733 ''' 1734 s = self.pset; 1735 F = s.list; 1736 bbpar = SolvableGroebnerBaseParallel(th); 1737 t = System.currentTimeMillis(); 1738 G = bbpar.twosidedGB(F); 1739 t = System.currentTimeMillis() - t; 1740 bbpar.terminate(); 1741 print "parallel %s twosidedGB executed in %s ms" % (th, t); 1742 return SolvableIdeal(self.ring,"",G);
1743
1744 - def leftSyzygy(self):
1745 '''left Syzygy of generating polynomials. 1746 ''' 1747 p = self.pset; 1748 l = p.list; 1749 t = System.currentTimeMillis(); 1750 s = SolvableSyzygySeq(p.ring.coFac).leftZeroRelationsArbitrary( l ); 1751 t = System.currentTimeMillis() - t; 1752 print "executed left syzygy in %s ms" % t; 1753 m = SolvableModule("",p.ring); 1754 return SolvableSubModule(m,"",s);
1755
1756 - def isLeftSyzygy(self,m):
1757 '''Test if this is a left syzygy of the module in m. 1758 ''' 1759 p = self.pset; 1760 g = p.list; 1761 l = m.list; 1762 t = System.currentTimeMillis(); 1763 z = SolvableSyzygySeq(p.ring.coFac).isLeftZeroRelation( l, g ); 1764 t = System.currentTimeMillis() - t; 1765 print "executed isLeftSyzygy in %s ms" % t; 1766 return z;
1767
1768 - def rightSyzygy(self):
1769 '''right Syzygy of generating polynomials. 1770 ''' 1771 p = self.pset; 1772 l = p.list; 1773 t = System.currentTimeMillis(); 1774 s = SolvableSyzygySeq(p.ring.coFac).rightZeroRelationsArbitrary( l ); 1775 t = System.currentTimeMillis() - t; 1776 print "executed right syzygy in %s ms" % t; 1777 m = SolvableModule("",p.ring); 1778 return SolvableSubModule(m,"",s);
1779
1780 - def isRightSyzygy(self,m):
1781 '''Test if this is a right syzygy of the module in m. 1782 ''' 1783 p = self.pset; 1784 g = p.list; 1785 l = m.list; 1786 t = System.currentTimeMillis(); 1787 z = SolvableSyzygySeq(p.ring.coFac).isRightZeroRelation( l, g ); 1788 t = System.currentTimeMillis() - t; 1789 print "executed isRightSyzygy in %s ms" % t; 1790 return z;
1791
1792 - def dimension(self):
1793 '''Compute the dimension of the ideal. 1794 ''' 1795 I = jas.application.SolvableIdeal(self.pset); 1796 d = I.dimension(); 1797 return d;
1798 1799 1800
1801 -class Module:
1802 '''Represents a JAS module over a polynomial ring. 1803 1804 Method to create sub-modules. 1805 ''' 1806
1807 - def __init__(self,modstr="",ring=None,cols=0):
1808 '''Module constructor. 1809 ''' 1810 if ring == None: 1811 sr = StringReader( modstr ); 1812 tok = RingFactoryTokenizer(sr); 1813 pfac = tok.nextPolynomialRing(); 1814 #tok = GenPolynomialTokenizer(pfac,sr); 1815 #mlist = tok.nextSubModuleList(); 1816 #self.mset = ModuleList(pfac,mlist); 1817 #if self.mset.cols >= 0: 1818 # self.cols = self.mset.cols; 1819 #else: 1820 self.ring = pfac 1821 else: 1822 if isinstance(ring,Ring): 1823 self.ring = ring.ring; 1824 else: 1825 self.ring = ring; 1826 self.mset = ModuleList(self.ring,None); # because of toScript 1827 if cols < 0: 1828 cols = 0; 1829 self.cols = cols;
1830
1831 - def __str__(self):
1832 '''Create a string representation. 1833 ''' 1834 return str(self.mset.toScript());
1835
1836 - def submodul(self,modstr="",list=None):
1837 '''Create a sub-module. 1838 ''' 1839 return SubModule(self,modstr,list);
1840
1841 - def element(self,poly):
1842 '''Create an element from a string or object. 1843 ''' 1844 if not isinstance(poly,str): 1845 try: 1846 if self.ring == poly.ring: 1847 return RingElem(poly); 1848 except Exception, e: 1849 pass 1850 poly = str(poly); 1851 I = SubModule(self, "( " + poly + " )"); 1852 list = I.mset.list; 1853 if len(list) > 0: 1854 return RingElem( list[0] );
1855
1856 - def gens(self):
1857 '''Get the generators of this module. 1858 ''' 1859 gm = GenVectorModul(self.ring,self.cols); 1860 L = gm.generators(); 1861 #for g in L: 1862 # print "g = ", str(g); 1863 N = [ RingElem(e) for e in L ]; # want use val here, but can not 1864 return N;
1865
1866 - def inject_variables(self):
1867 '''Inject generators as variables into the main global namespace 1868 ''' 1869 inject_generators(self.gens());
1870 1871
1872 -class SubModule:
1873 '''Represents a JAS sub-module over a polynomial ring. 1874 1875 Methods to compute Groebner bases. 1876 ''' 1877
1878 - def __init__(self,module,modstr="",list=None):
1879 '''Constructor for a sub-module. 1880 ''' 1881 self.module = module; 1882 if list == None: 1883 sr = StringReader( modstr ); 1884 tok = GenPolynomialTokenizer(module.ring,sr); 1885 self.list = tok.nextSubModuleList(); 1886 else: 1887 if isinstance(list,PyList) or isinstance(list,PyTuple): 1888 if len(list) != 0: 1889 if isinstance(list[0],RingElem): 1890 list = [ re.elem for re in list ]; 1891 self.list = pylist2arraylist(list,self.module.ring,rec=2); 1892 else: 1893 self.list = list; 1894 #print "list = ", str(list); 1895 #e = self.list[0]; 1896 #print "e = ", e; 1897 self.mset = OrderedModuleList(module.ring,self.list); 1898 self.cols = self.mset.cols; 1899 self.rows = self.mset.rows; 1900 #print "list = %s" % self.list; 1901 #print "cols = %s" % self.cols; 1902 #print "mset = %s" % self.mset.toString(); 1903 #print "mset = %s" % self.mset.toScript(); 1904 self.pset = self.mset.getPolynomialList();
1905
1906 - def __str__(self):
1907 '''Create a string representation. 1908 ''' 1909 return str(self.mset.toScript()); # + "\n\n" + str(self.pset);
1910
1911 - def GB(self):
1912 '''Compute a Groebner base. 1913 ''' 1914 t = System.currentTimeMillis(); 1915 #G = ModGroebnerBaseSeq(self.module.ring.coFac).GB(self.mset); 1916 G = GroebnerBaseSeq().GB(self.mset); 1917 t = System.currentTimeMillis() - t; 1918 print "executed module GB in %s ms" % t; 1919 return SubModule(self.module,"",G.list);
1920
1921 - def isGB(self):
1922 '''Test if this is a Groebner base. 1923 ''' 1924 t = System.currentTimeMillis(); 1925 #b = ModGroebnerBaseSeq(self.module.ring.coFac).isGB(self.mset); 1926 b = GroebnerBaseSeq().isGB(self.mset); 1927 t = System.currentTimeMillis() - t; 1928 print "module isGB executed in %s ms" % t; 1929 return b;
1930
1931 - def isSyzygy(self,g):
1932 '''Test if this is a syzygy of the vectors in g. 1933 ''' 1934 l = self.list; 1935 if isinstance(g,Ideal): 1936 s = g.pset.list; 1937 else: 1938 if isinstance(g,SubModule): 1939 s = g.mset; 1940 l = self.mset; 1941 else: 1942 raise ValueError, "unknown type %s" % g.getClass().getName(); 1943 #print "l = %s" % l; 1944 #print "s = %s" % s; 1945 t = System.currentTimeMillis(); 1946 z = SyzygySeq(self.module.ring.coFac).isZeroRelation( l, s ); 1947 t = System.currentTimeMillis() - t; 1948 print "executed isSyzygy in %s ms" % t; 1949 return z;
1950
1951 - def syzygy(self):
1952 '''Compute syzygys of this module. 1953 ''' 1954 l = self.mset; 1955 t = System.currentTimeMillis(); 1956 p = SyzygySeq(self.module.ring.coFac).zeroRelations( l ); 1957 t = System.currentTimeMillis() - t; 1958 print "executed module syzygy in %s ms" % t; 1959 #print "p = " + str(p.ring.toScript()); 1960 m = Module("",p.ring,p.cols); 1961 return SubModule(m,"",p.list);
1962 1963
1964 -class SolvableModule(Module):
1965 '''Represents a JAS module over a solvable polynomial ring. 1966 1967 Method to create solvable sub-modules. 1968 ''' 1969
1970 - def __init__(self,modstr="",ring=None,cols=0):
1971 '''Solvable module constructor. 1972 ''' 1973 if ring == None: 1974 sr = StringReader( modstr ); 1975 tok = RingFactoryTokenizer(sr); 1976 pfac = tok.nextSolvablePolynomialRing(); 1977 #tok = GenPolynomialTokenizer(pfac,sr); 1978 #mlist = tok.nextSolvableSubModuleList(); 1979 #self.mset = PolynomialList(pfac,mlist); 1980 #if self.mset.cols >= 0: 1981 # self.cols = self.mset.cols; 1982 self.ring = pfac; 1983 else: 1984 if isinstance(ring,Ring): 1985 self.ring = ring.ring; 1986 else: 1987 self.ring = ring; 1988 self.mset = ModuleList(self.ring,None); 1989 if cols < 0: 1990 cols = 0; 1991 self.cols = cols;
1992
1993 - def __str__(self):
1994 '''Create a string representation. 1995 ''' 1996 return str(self.mset.toScript());
1997
1998 - def submodul(self,modstr="",list=None):
1999 '''Create a solvable sub-module. 2000 ''' 2001 return SolvableSubModule(self,modstr,list);
2002
2003 - def element(self,poly):
2004 '''Create an element from a string or object. 2005 ''' 2006 if not isinstance(poly,str): 2007 try: 2008 if self.ring == poly.ring: 2009 return RingElem(poly); 2010 except Exception, e: 2011 pass 2012 poly = str(poly); 2013 I = SolvableSubModule(self, "( " + poly + " )"); 2014 list = I.mset.list; 2015 if len(list) > 0: 2016 return RingElem( list[0] );
2017 2018
2019 -class SolvableSubModule:
2020 '''Represents a JAS sub-module over a solvable polynomial ring. 2021 2022 Methods to compute left, right and two-sided Groebner bases. 2023 ''' 2024
2025 - def __init__(self,module,modstr="",list=None):
2026 '''Constructor for sub-module over a solvable polynomial ring. 2027 ''' 2028 self.module = module; 2029 if list == None: 2030 sr = StringReader( modstr ); 2031 tok = GenPolynomialTokenizer(module.ring,sr); 2032 self.list = tok.nextSolvableSubModuleList(); 2033 else: 2034 if isinstance(list,PyList) or isinstance(list,PyTuple): 2035 self.list = pylist2arraylist(list,self.module.ring,rec=2); 2036 else: 2037 self.list = list; 2038 self.mset = OrderedModuleList(module.ring,self.list); 2039 self.cols = self.mset.cols; 2040 self.rows = self.mset.rows;
2041
2042 - def __str__(self):
2043 '''Create a string representation. 2044 ''' 2045 return str(self.mset.toScript()); # + "\n\n" + str(self.pset);
2046
2047 - def leftGB(self):
2048 '''Compute a left Groebner base. 2049 ''' 2050 t = System.currentTimeMillis(); 2051 #G = SolvableGroebnerBaseSeq(self.module.ring.coFac).leftGB(self.mset); 2052 G = SolvableGroebnerBaseSeq().leftGB(self.mset); 2053 t = System.currentTimeMillis() - t; 2054 print "executed left module GB in %s ms" % t; 2055 return SolvableSubModule(self.module,"",G.list);
2056
2057 - def isLeftGB(self):
2058 '''Test if this is a left Groebner base. 2059 ''' 2060 t = System.currentTimeMillis(); 2061 #b = SolvableGroebnerBaseSeq(self.module.ring.coFac).isLeftGB(self.mset); 2062 b = SolvableGroebnerBaseSeq().isLeftGB(self.mset); 2063 t = System.currentTimeMillis() - t; 2064 print "module isLeftGB executed in %s ms" % t; 2065 return b;
2066
2067 - def twosidedGB(self):
2068 '''Compute a two-sided Groebner base. 2069 ''' 2070 t = System.currentTimeMillis(); 2071 #G = SolvableGroebnerBaseSeq(self.module.ring.coFac).twosidedGB(self.mset); 2072 G = SolvableGroebnerBaseSeq().twosidedGB(self.mset); 2073 t = System.currentTimeMillis() - t; 2074 print "executed twosided module GB in %s ms" % t; 2075 return SolvableSubModule(self.module,"",G.list);
2076
2077 - def isTwosidedGB(self):
2078 '''Test if this is a two-sided Groebner base. 2079 ''' 2080 t = System.currentTimeMillis(); 2081 #b = SolvableGroebnerBaseSeq(self.module.ring.coFac).isTwosidedGB(self.mset); 2082 b = SolvableGroebnerBaseSeq().isTwosidedGB(self.mset); 2083 t = System.currentTimeMillis() - t; 2084 print "module isTwosidedGB executed in %s ms" % t; 2085 return b;
2086
2087 - def rightGB(self):
2088 '''Compute a right Groebner base. 2089 ''' 2090 t = System.currentTimeMillis(); 2091 #G = SolvableGroebnerBaseSeq(self.module.ring.coFac).rightGB(self.mset); 2092 G = SolvableGroebnerBaseSeq().rightGB(self.mset); 2093 t = System.currentTimeMillis() - t; 2094 print "executed right module GB in %s ms" % t; 2095 return SolvableSubModule(self.module,"",G.list);
2096
2097 - def isRightGB(self):
2098 '''Test if this is a right Groebner base. 2099 ''' 2100 t = System.currentTimeMillis(); 2101 #b = SolvableGroebnerBaseSeq(self.module.ring.coFac).isRightGB(self.mset); 2102 b = SolvableGroebnerBaseSeq().isRightGB(self.mset); 2103 t = System.currentTimeMillis() - t; 2104 print "module isRightGB executed in %s ms" % t; 2105 return b;
2106
2107 - def isLeftSyzygy(self,g):
2108 '''Test if this is a syzygy of the vectors in g. 2109 ''' 2110 l = self.list; 2111 if isinstance(g,SolvableIdeal): 2112 s = g.pset.list; 2113 else: 2114 if isinstance(g,SolvableSubModule): 2115 s = g.mset; 2116 l = self.mset; 2117 else: 2118 raise ValueError, "unknown type %s" % g.getClass().getName(); 2119 #print "l = %s" % l; 2120 #print "g = %s" % g; 2121 t = System.currentTimeMillis(); 2122 z = SolvableSyzygySeq(self.module.ring.coFac).isLeftZeroRelation( l, s ); 2123 t = System.currentTimeMillis() - t; 2124 print "executed isLeftSyzygy in %s ms" % t; 2125 return z;
2126
2127 - def leftSyzygy(self):
2128 '''Compute left syzygys of this module. 2129 ''' 2130 l = self.mset; 2131 t = System.currentTimeMillis(); 2132 p = SolvableSyzygySeq(self.module.ring.coFac).leftZeroRelationsArbitrary( l ); 2133 t = System.currentTimeMillis() - t; 2134 print "executed left module syzygy in %s ms" % t; 2135 #print "p = " + str(p.ring.toScript()); 2136 m = SolvableModule("",p.ring,p.cols); 2137 return SolvableSubModule(m,"",p.list);
2138
2139 - def isRightSyzygy(self,g):
2140 '''Test if this is a syzygy of the vectors in g. 2141 ''' 2142 l = self.list; 2143 if isinstance(g,SolvableIdeal): 2144 s = g.pset.list; 2145 else: 2146 if isinstance(g,SolvableSubModule): 2147 s = g.mset; 2148 l = self.mset; 2149 else: 2150 raise ValueError, "unknown type %s" % g.getClass().getName(); 2151 #print "l = %s" % l; 2152 #print "g = %s" % g; 2153 t = System.currentTimeMillis(); 2154 z = SolvableSyzygySeq(self.module.ring.coFac).isRightZeroRelation( l, s ); 2155 t = System.currentTimeMillis() - t; 2156 print "executed isRightSyzygy in %s ms" % t; 2157 return z;
2158
2159 - def rightSyzygy(self):
2160 '''Compute right syzygys of this module. 2161 ''' 2162 l = self.mset; 2163 t = System.currentTimeMillis(); 2164 # no: p = SolvableSyzygySeq(self.module.ring.coFac).rightZeroRelations( l ); 2165 p = SolvableSyzygySeq(self.module.ring.coFac).rightZeroRelationsArbitrary( l ); 2166 t = System.currentTimeMillis() - t; 2167 print "executed right module syzygy in %s ms" % t; 2168 #print "p = " + str(p.ring.toScript()); 2169 m = SolvableModule("",p.ring,p.cols); 2170 return SolvableSubModule(m,"",p.list);
2171 2172
2173 -class SeriesRing:
2174 '''Represents a JAS power series ring: UnivPowerSeriesRing. 2175 2176 Methods for univariate power series arithmetic. 2177 ''' 2178
2179 - def __init__(self,ringstr="",truncate=None,ring=None,cofac=None,name="z"):
2180 '''Ring constructor. 2181 ''' 2182 if ring == None: 2183 if len(ringstr) > 0: 2184 sr = StringReader( ringstr ); 2185 tok = RingFactoryTokenizer(sr); 2186 pfac = tok.nextPolynomialRing(); 2187 #tok = GenPolynomialTokenizer(pfac,sr); 2188 #plist = tok.nextPolynomialList(); 2189 #pset = PolynomialList(pfac,plist); 2190 ring = pfac; 2191 vname = ring.vars; 2192 name = vname[0]; 2193 cofac = ring.coFac; 2194 if isinstance(cofac,RingElem): 2195 cofac = cofac.elem; 2196 if truncate == None: 2197 self.ring = UnivPowerSeriesRing(cofac,name); 2198 else: 2199 self.ring = UnivPowerSeriesRing(cofac,truncate,name); 2200 else: 2201 self.ring = ring;
2202
2203 - def __str__(self):
2204 '''Create a string representation. 2205 ''' 2206 return str(self.ring.toScript());
2207
2208 - def gens(self):
2209 '''Get the generators of the power series ring. 2210 ''' 2211 L = self.ring.generators(); 2212 N = [ RingElem(e) for e in L ]; 2213 return N;
2214
2215 - def inject_variables(self):
2216 '''Inject generators as variables into the main global namespace 2217 ''' 2218 inject_generators(self.gens());
2219
2220 - def one(self):
2221 '''Get the one of the power series ring. 2222 ''' 2223 return RingElem( self.ring.getONE() );
2224
2225 - def zero(self):
2226 '''Get the zero of the power series ring. 2227 ''' 2228 return RingElem( self.ring.getZERO() );
2229
2230 - def random(self,n):
2231 '''Get a random power series. 2232 ''' 2233 return RingElem( self.ring.random(n) );
2234
2235 - def exp(self):
2236 '''Get the exponential power series. 2237 ''' 2238 return RingElem( self.ring.getEXP() );
2239
2240 - def sin(self):
2241 '''Get the sinus power series. 2242 ''' 2243 return RingElem( self.ring.getSIN() );
2244
2245 - def cos(self):
2246 '''Get the cosinus power series. 2247 ''' 2248 return RingElem( self.ring.getCOS() );
2249
2250 - def tan(self):
2251 '''Get the tangens power series. 2252 ''' 2253 return RingElem( self.ring.getTAN() );
2254
2255 - def create(self,ifunc=None,jfunc=None,clazz=None):
2256 '''Create a power series with given generating function. 2257 2258 ifunc(int i) must return a value which is used in RingFactory.fromInteger(). 2259 jfunc(int i) must return a value of type ring.coFac. 2260 clazz must implement the Coefficients abstract class. 2261 ''' 2262 class coeff( Coefficients ): 2263 def __init__(self,cofac): 2264 self.coFac = cofac;
2265 def generate(self,i): 2266 if jfunc == None: 2267 return self.coFac.fromInteger( ifunc(i) ); 2268 else: 2269 return jfunc(i);
2270 if clazz == None: 2271 ps = UnivPowerSeries( self.ring, coeff(self.ring.coFac) ); 2272 else: 2273 ps = UnivPowerSeries( self.ring, clazz ); 2274 return RingElem( ps ); 2275
2276 - def fixPoint(self,psmap):
2277 '''Create a power series as fixed point of the given mapping. 2278 2279 psmap must implement the UnivPowerSeriesMap interface. 2280 ''' 2281 ps = self.ring.fixPoint( psmap ); 2282 return RingElem( ps );
2283
2284 - def gcd(self,a,b):
2285 '''Compute the greatest common divisor of a and b. 2286 ''' 2287 if isinstance(a,RingElem): 2288 a = a.elem; 2289 if isinstance(b,RingElem): 2290 b = b.elem; 2291 return RingElem( a.gcd(b) );
2292
2293 - def fromPoly(self,a):
2294 '''Convert a GenPolynomial to a power series. 2295 ''' 2296 if isinstance(a,RingElem): 2297 a = a.elem; 2298 return RingElem( self.ring.fromPolynomial(a) );
2299 2300
2301 -class MultiSeriesRing:
2302 '''Represents a JAS power series ring: MultiVarPowerSeriesRing. 2303 2304 Methods for multivariate power series arithmetic. 2305 ''' 2306
2307 - def __init__(self,ringstr="",truncate=None,ring=None,cofac=None,names=None):
2308 '''Ring constructor. 2309 ''' 2310 if ring == None: 2311 if len(ringstr) > 0: 2312 sr = StringReader( ringstr ); 2313 tok = RingFactoryTokenizer(sr); 2314 pfac = tok.nextPolynomialRing(); 2315 #tok = GenPolynomialTokenizer(pfac,sr); 2316 #plist = tok.nextPolynomialList(); 2317 #pset = PolynomialList(pfac,plist); 2318 ring = pfac; 2319 names = ring.vars; 2320 cofac = ring.coFac; 2321 if isinstance(cofac,RingElem): 2322 cofac = cofac.elem; 2323 if truncate == None: 2324 self.ring = MultiVarPowerSeriesRing(cofac,names); 2325 else: 2326 self.ring = MultiVarPowerSeriesRing(cofac,len(names),truncate,names); 2327 else: 2328 self.ring = ring;
2329
2330 - def __str__(self):
2331 '''Create a string representation. 2332 ''' 2333 return str(self.ring.toScript());
2334
2335 - def gens(self):
2336 '''Get the generators of the power series ring. 2337 ''' 2338 L = self.ring.generators(); 2339 N = [ RingElem(e) for e in L ]; 2340 return N;
2341
2342 - def inject_variables(self):
2343 '''Inject generators as variables into the main global namespace 2344 ''' 2345 inject_generators(self.gens());
2346
2347 - def one(self):
2348 '''Get the one of the power series ring. 2349 ''' 2350 return RingElem( self.ring.getONE() );
2351
2352 - def zero(self):
2353 '''Get the zero of the power series ring. 2354 ''' 2355 return RingElem( self.ring.getZERO() );
2356
2357 - def random(self,n):
2358 '''Get a random power series. 2359 ''' 2360 return RingElem( self.ring.random(n) );
2361
2362 - def exp(self,r):
2363 '''Get the exponential power series, var r. 2364 ''' 2365 return RingElem( self.ring.getEXP(r) );
2366
2367 - def sin(self,r):
2368 '''Get the sinus power series, var r. 2369 ''' 2370 return RingElem( self.ring.getSIN(r) );
2371
2372 - def cos(self,r):
2373 '''Get the cosinus power series, var r. 2374 ''' 2375 return RingElem( self.ring.getCOS(r) );
2376
2377 - def tan(self,r):
2378 '''Get the tangens power series, var r. 2379 ''' 2380 return RingElem( self.ring.getTAN(r) );
2381
2382 - def create(self,ifunc=None,jfunc=None,clazz=None):
2383 '''Create a power series with given generating function. 2384 2385 ifunc(int i) must return a value which is used in RingFactory.fromInteger(). 2386 jfunc(int i) must return a value of type ring.coFac. 2387 clazz must implement the Coefficients abstract class. 2388 ''' 2389 class coeff( MultiVarCoefficients ): 2390 def __init__(self,r): 2391 MultiVarCoefficients.__init__(self,r); 2392 self.coFac = r.coFac;
2393 def generate(self,i): 2394 if jfunc == None: 2395 return self.coFac.fromInteger( ifunc(i) ); 2396 else: 2397 return jfunc(i);
2398 #print "ifunc" 2399 if clazz == None: 2400 ps = MultiVarPowerSeries( self.ring, coeff(self.ring) ); 2401 else: 2402 ps = MultiVarPowerSeries( self.ring, clazz ); 2403 #print "ps ", ps.toScript(); 2404 return RingElem( ps ); 2405
2406 - def fixPoint(self,psmap):
2407 '''Create a power series as fixed point of the given mapping. 2408 2409 psmap must implement the UnivPowerSeriesMap interface. 2410 ''' 2411 ps = self.ring.fixPoint( psmap ); 2412 return RingElem( ps );
2413
2414 - def gcd(self,a,b):
2415 '''Compute the greatest common divisor of a and b. 2416 ''' 2417 if isinstance(a,RingElem): 2418 a = a.elem; 2419 if isinstance(b,RingElem): 2420 b = b.elem; 2421 return RingElem( a.gcd(b) );
2422
2423 - def fromPoly(self,a):
2424 '''Convert a GenPolynomial to a power series. 2425 ''' 2426 if isinstance(a,RingElem): 2427 a = a.elem; 2428 return RingElem( self.ring.fromPolynomial(a) );
2429 2430
2431 -class PSIdeal:
2432 '''Represents a JAS power series ideal. 2433 2434 Method for Standard bases. 2435 ''' 2436
2437 - def __init__(self,ring,polylist,ideal=None,list=None):
2438 '''PSIdeal constructor. 2439 ''' 2440 if isinstance(ring,Ring) or isinstance(ring,PolyRing): 2441 ring = MultiVarPowerSeriesRing(ring.ring); 2442 if isinstance(ring,MultiSeriesRing): 2443 ring = ring.ring; 2444 self.ring = ring; 2445 #print "ring = ", ring.toScript(); 2446 if ideal != None: 2447 polylist = ideal.pset.list; 2448 if list == None: 2449 self.polylist = pylist2arraylist( [ a.elem for a in polylist ] ); 2450 #print "polylist = ", self.polylist; 2451 self.list = self.ring.fromPolynomial(self.polylist); 2452 else: 2453 self.polylist = None; 2454 self.list = pylist2arraylist( [ a.elem for a in list ] );
2455
2456 - def __str__(self):
2457 '''Create a string representation. 2458 ''' 2459 ll = [ e.toScript() for e in self.list ] 2460 return "( " + ", ".join(ll) + " )";
2461
2462 - def STD(self,trunc=None):
2463 '''Compute a standard base. 2464 ''' 2465 pr = self.ring; 2466 if trunc != None: 2467 pr.setTruncate(trunc); 2468 #print "pr = ", pr.toScript(); 2469 F = self.list; 2470 #print "F = ", F; 2471 tm = StandardBaseSeq(); 2472 t = System.currentTimeMillis(); 2473 S = tm.STD(F); 2474 t = System.currentTimeMillis() - t; 2475 print "sequential standard base executed in %s ms" % t; 2476 #Sp = [ RingElem(a.asPolynomial()) for a in S ]; 2477 Sp = [ RingElem(a) for a in S ]; 2478 #return Sp; 2479 return PSIdeal(self.ring,None,list=Sp);
2480 2481
2482 -def pylist2arraylist(list,fac=None,rec=1):
2483 '''Convert a Python list to a Java ArrayList. 2484 2485 If list is a Python list, it is converted, else list is left unchanged. 2486 ''' 2487 #print "list type(%s) = %s" % (list,type(list)); 2488 if isinstance(list,PyList) or isinstance(list,PyTuple): 2489 L = ArrayList(); 2490 for e in list: 2491 t = True; 2492 if isinstance(e,RingElem): 2493 t = False; 2494 e = e.elem; 2495 if isinstance(e,PyList) or isinstance(e,PyTuple): 2496 if rec <= 1: 2497 e = makeJasArith(e); 2498 else: 2499 t = False; 2500 e = pylist2arraylist(e,fac,rec-1); 2501 try: 2502 #n = e.getClass().getSimpleName(); 2503 if isinstance(e,ArrayList) or isinstance(e,LinkedList): 2504 t = False; 2505 except: 2506 pass; 2507 if t and fac != None: 2508 #print "e.p(%s) = %s :: %s" % (e,e,fac); 2509 e = fac.parse( str(e) ); #or makeJasArith(e) ? 2510 L.add(e); 2511 list = L; 2512 #print "list type(%s) = %s" % (list,type(list)); 2513 return list
2514 2515
2516 -def arraylist2pylist(list,rec=1):
2517 '''Convert a Java ArrayList to a Python list. 2518 2519 If list is a Java ArrayList list, it is converted, else list is left unchanged. 2520 ''' 2521 #print "list type(%s) = %s" % (list,type(list)); 2522 if isinstance(list,List): 2523 L = []; 2524 for e in list: 2525 if not isinstance(e,RingElem): 2526 e = RingElem(e); 2527 L.append(e); 2528 list = L; 2529 #print "list type(%s) = %s" % (list,type(list)); 2530 return list
2531 2532
2533 -def makeJasArith(item):
2534 '''Construct a jas.arith object. 2535 If item is a python tuple or list then a BigRational, BigComplex is constructed. 2536 If item is a python float then a BigDecimal is constructed. 2537 Otherwise, item is returned unchanged. 2538 ''' 2539 #print "item type(%s) = %s" % (item,type(item)); 2540 if isinstance(item,PyInteger) or isinstance(item,PyLong): 2541 return BigInteger( item ); 2542 if isinstance(item,PyFloat): # ?? what to do ?? 2543 return BigDecimal( str(item) ); 2544 if isinstance(item,PyTuple) or isinstance(item,PyList): 2545 if len(item) > 2: 2546 print "len(item) > 2, remaining items ignored"; 2547 #print "item[0] type(%s) = %s" % (item[0],type(item[0])); 2548 isc = isinstance(item[0],PyTuple) or isinstance(item[0],PyList) 2549 if len(item) > 1: 2550 isc = isc or isinstance(item[1],PyTuple) or isinstance(item[1],PyList); 2551 if isc: 2552 if len(item) > 1: 2553 re = makeJasArith( item[0] ); 2554 if not re.isField(): 2555 re = BigRational( re.val ); 2556 im = makeJasArith( item[1] ); 2557 if not im.isField(): 2558 im = BigRational( im.val ); 2559 jasArith = BigComplex( re, im ); 2560 else: 2561 re = makeJasArith( item[0] ); 2562 jasArith = BigComplex( re ); 2563 else: 2564 if len(item) > 1: 2565 jasArith = BigRational( item[0] ).divide( BigRational( item[1] ) ); 2566 else: 2567 jasArith = BigRational( item[0] ); 2568 #print "makeJasArith: type(%s) = %s" % (jasArith,type(jasArith)); 2569 return jasArith; 2570 #print "makeJasArith: unknown item type(%s) = %s" % (item,type(item)); 2571 return item;
2572 2573
2574 -def ZZ(z=0):
2575 '''Create JAS BigInteger as ring element. 2576 ''' 2577 if isinstance(z,RingElem): 2578 z = z.elem; 2579 r = BigInteger(z); 2580 return RingElem(r);
2581
2582 -def ZM(m,z=0,field=False):
2583 '''Create JAS ModInteger as ring element. 2584 ''' 2585 if isinstance(m,RingElem): 2586 m = m.elem; 2587 if isinstance(z,RingElem): 2588 z = z.elem; 2589 if z != 0 and ( z == False or z == True ): # never true 2590 field = z; 2591 z = 0; 2592 if m < ModLongRing.MAX_LONG: 2593 if m < ModIntRing.MAX_INT: 2594 if field: 2595 mf = ModIntRing(m,field); 2596 else: 2597 mf = ModIntRing(m); 2598 r = ModInt(mf,z); 2599 else: 2600 if field: 2601 mf = ModLongRing(m,field); 2602 else: 2603 mf = ModLongRing(m); 2604 r = ModLong(mf,z); 2605 else: 2606 if field: 2607 mf = ModIntegerRing(m,field); 2608 else: 2609 mf = ModIntegerRing(m); 2610 r = ModInteger(mf,z); 2611 return RingElem(r);
2612 2613
2614 -def ZML(m,z=0,field=False):
2615 '''Create JAS ModLong as ring element. 2616 ''' 2617 return ZM(m,z,field);
2618 2619
2620 -def ZMI(m,z=0,field=False):
2621 '''Create JAS ModInt as ring element. 2622 ''' 2623 return ZM(m,z,field);
2624 2625
2626 -def GF(m,z=0):
2627 '''Create JAS ModInteger as field element. 2628 ''' 2629 #print "m = %s" % m 2630 return ZM(m,z,True);
2631 2632
2633 -def GFL(m,z=0):
2634 '''Create JAS ModLong as field element. 2635 ''' 2636 #print "m = %s" % m 2637 return ZM(m,z,True);
2638 2639
2640 -def GFI(m,z=0):
2641 '''Create JAS ModInt as field element. 2642 ''' 2643 return ZM(m,z,True);
2644 2645
2646 -def QQ(d=0,n=1):
2647 '''Create JAS BigRational as ring element. 2648 ''' 2649 if isinstance(d,PyTuple) or isinstance(d,PyList): 2650 if n != 1: 2651 print "%s ignored" % n; 2652 if len(d) > 1: 2653 n = d[1]; 2654 d = d[0]; 2655 if isinstance(d,RingElem): 2656 d = d.elem; 2657 if isinstance(n,RingElem): 2658 n = n.elem; 2659 if n == 1: 2660 if d == 0: 2661 r = BigRational(); 2662 else: 2663 r = BigRational(d); 2664 else: 2665 d = BigRational(d); 2666 n = BigRational(n); 2667 r = d.divide(n); # BigRational(d,n); only for short integers 2668 return RingElem(r);
2669 2670
2671 -def CC(re=BigRational(),im=BigRational()):
2672 '''Create JAS BigComplex as ring element. 2673 ''' 2674 if re == 0: 2675 re = BigRational(); 2676 if im == 0: 2677 im = BigRational(); 2678 if isinstance(re,PyTuple) or isinstance(re,PyList): 2679 if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): 2680 if len(re) > 1: 2681 im = QQ( re[1] ); 2682 re = QQ( re[0] ); 2683 else: 2684 re = QQ(re); 2685 # re = makeJasArith( re ); 2686 if isinstance(im,PyTuple) or isinstance(im,PyList): 2687 im = QQ( im ); 2688 # im = makeJasArith( im ); 2689 if isinstance(re,RingElem): 2690 re = re.elem; 2691 if isinstance(im,RingElem): 2692 im = im.elem; 2693 if im.isZERO(): 2694 if re.isZERO(): 2695 c = BigComplex(); 2696 else: 2697 c = BigComplex(re); 2698 else: 2699 c = BigComplex(re,im); 2700 return RingElem(c);
2701 2702
2703 -def CR(re=BigRational(),im=BigRational(),ring=None):
2704 '''Create JAS generic Complex as ring element. 2705 ''' 2706 if re == 0: 2707 re = BigRational(); 2708 if im == 0: 2709 im = BigRational(); 2710 if isinstance(re,PyTuple) or isinstance(re,PyList): 2711 if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): 2712 if len(re) > 1: 2713 im = QQ( re[1] ); 2714 re = QQ( re[0] ); 2715 else: 2716 re = QQ(re); 2717 # re = makeJasArith( re ); 2718 if isinstance(im,PyTuple) or isinstance(im,PyList): 2719 im = QQ( im ); 2720 # im = makeJasArith( im ); 2721 if isinstance(re,RingElem): 2722 re = re.elem; 2723 if isinstance(im,RingElem): 2724 im = im.elem; 2725 if ring == None: 2726 ring = re.factory(); 2727 r = ComplexRing(ring); 2728 #print "d type(%s) = %s" % (r,type(r)); 2729 if im.isZERO(): 2730 if re.isZERO(): 2731 c = Complex(r); 2732 else: 2733 c = Complex(r,re); 2734 else: 2735 c = Complex(r,re,im); 2736 #print "d type(%s) = %s" % (c,type(c)); 2737 return RingElem(c);
2738 2739
2740 -def DD(d=0):
2741 '''Create JAS BigDecimal as ring element. 2742 ''' 2743 if isinstance(d,RingElem): 2744 d = d.elem; 2745 if isinstance(d,PyFloat): 2746 d = str(d); 2747 #print "d type(%s) = %s" % (d,type(d)); 2748 if d == 0: 2749 r = BigDecimal(); 2750 else: 2751 r = BigDecimal(d); 2752 return RingElem(r);
2753 2754
2755 -def Quat(re=BigRational(),im=BigRational(),jm=BigRational(),km=BigRational()):
2756 '''Create JAS BigQuaternion as ring element. 2757 ''' 2758 if re == 0: 2759 re = BigRational(); 2760 if im == 0: 2761 im = BigRational(); 2762 if jm == 0: 2763 jm = BigRational(); 2764 if km == 0: 2765 km = BigRational(); 2766 if isinstance(re,PyTuple) or isinstance(re,PyList): 2767 if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): 2768 if len(re) > 1: 2769 im = QQ( re[1] ); 2770 re = QQ( re[0] ); 2771 else: 2772 re = QQ(re); 2773 # re = makeJasArith( re ); 2774 if isinstance(im,PyTuple) or isinstance(im,PyList): 2775 im = QQ( im ); 2776 if isinstance(jm,PyTuple) or isinstance(jm,PyList): 2777 jm = QQ( jm ); 2778 if isinstance(km,PyTuple) or isinstance(km,PyList): 2779 kim = QQ( km ); 2780 # im = makeJasArith( im ); 2781 if isinstance(re,RingElem): 2782 re = re.elem; 2783 if isinstance(im,RingElem): 2784 im = im.elem; 2785 if isinstance(jm,RingElem): 2786 jm = jm.elem; 2787 if isinstance(km,RingElem): 2788 km = km.elem; 2789 cf = BigQuaternionRing(); 2790 c = BigQuaternion(cf, re,im,jm,km); 2791 return RingElem(c);
2792 2793
2794 -def Oct(ro=0,io=0):
2795 '''Create JAS BigOctonion as ring element. 2796 ''' 2797 cf = BigQuaternionRing(); 2798 if ro == 0: 2799 ro = BigQuaternion(cf); 2800 if io == 0: 2801 io = BigQuaternion(cf); 2802 if isinstance(ro,PyTuple) or isinstance(ro,PyList): 2803 if isinstance(ro[0],PyTuple) or isinstance(ro[0],PyList): 2804 if len(ro) > 1: 2805 io = QQ( ro[1] ); 2806 ro = QQ( ro[0] ); 2807 else: 2808 ro = QQ(ro); 2809 # re = makeJasArith( re ); 2810 if isinstance(io,PyTuple) or isinstance(io,PyList): 2811 io = QQ( io ); 2812 # im = makeJasArith( im ); 2813 if isinstance(ro,RingElem): 2814 ro = ro.elem; 2815 if isinstance(io,RingElem): 2816 io = io.elem; 2817 c = BigOctonion(ro,io); 2818 return RingElem(c);
2819 2820
2821 -def AN(m,z=0,field=False,pr=None):
2822 '''Create JAS AlgebraicNumber as ring element. 2823 ''' 2824 if isinstance(m,RingElem): 2825 m = m.elem; 2826 if isinstance(z,RingElem): 2827 z = z.elem; 2828 # if (z == True or z == False): # not working 2829 # field = z; 2830 # z = 0; 2831 #print "m.getClass() = " + str(m.getClass().getName()); 2832 #print "field = " + str(field); 2833 if isinstance(m,AlgebraicNumber): 2834 mf = AlgebraicNumberRing(m.factory().modul,m.factory().isField()); 2835 else: 2836 if field: 2837 mf = AlgebraicNumberRing(m,field); 2838 else: 2839 mf = AlgebraicNumberRing(m); 2840 #print "mf = " + mf.toString(); 2841 if z == 0: 2842 r = AlgebraicNumber(mf); 2843 else: 2844 r = AlgebraicNumber(mf,z); 2845 return RingElem(r);
2846 2847 2848 _finiteFields = {}; '''List of already constructed FiniteFields.''' 2849 2850
2851 -def FF(p,n,z=0):
2852 '''Create JAS Field element as ring element. 2853 FF has p<sup>n</sup> elements. 2854 ''' 2855 if isinstance(p,RingElem): 2856 p = p.elem; 2857 if isinstance(n,RingElem): 2858 n = n.elem; 2859 if isinstance(z,RingElem): 2860 z = z.elem; 2861 if (p == 0): 2862 raise "p =="; 2863 if (n == 0): 2864 raise "n =="; 2865 field = True; 2866 if not PrimeInteger.isPrime(p): 2867 field = False; 2868 raise ValueError, str(p) + " not prime."; 2869 mf = _finiteFields.get( (p,n) ); 2870 if mf == None: 2871 cf = GF(p).ring; 2872 mf = PolyUfdUtil.algebraicNumberField(cf,n); 2873 _finiteFields[ (p,n) ] = mf; 2874 #print "mf = " + mf.toScript(); 2875 if z == 0: 2876 r = AlgebraicNumber(mf); 2877 else: 2878 r = AlgebraicNumber(mf,z); 2879 return RingElem(r);
2880 2881
2882 -def RealN(m,i,r=0):
2883 '''Create JAS RealAlgebraicNumber as ring element. 2884 ''' 2885 if isinstance(m,RingElem): 2886 m = m.elem; 2887 if isinstance(r,RingElem): 2888 r = r.elem; 2889 if isinstance(i,PyTuple) or isinstance(i,PyList): 2890 i = pylist2arraylist(i,BigRational(),1); 2891 i = Interval(i[0],i[1]); 2892 #print "m.getClass() = " + str(m.getClass().getName()); 2893 if isinstance(m,RealAlgebraicNumber): 2894 mf = RealAlgebraicRing(m.factory().algebraic.modul,i); 2895 else: 2896 mf = RealAlgebraicRing(m,i); 2897 if r == 0: 2898 rr = RealAlgebraicNumber(mf); 2899 else: 2900 rr = RealAlgebraicNumber(mf,r); 2901 return RingElem(rr);
2902 2903
2904 -def RF(pr,d=0,n=1):
2905 '''Create JAS rational function Quotient as ring element. 2906 ''' 2907 if isinstance(d,PyTuple) or isinstance(d,PyList): 2908 if n != 1: 2909 print "%s ignored" % n; 2910 if len(d) > 1: 2911 n = d[1]; 2912 d = d[0]; 2913 if isinstance(d,RingElem): 2914 d = d.elem; 2915 if isinstance(n,RingElem): 2916 n = n.elem; 2917 if isinstance(pr,RingElem): 2918 pr = pr.elem; 2919 if isinstance(pr,Ring): 2920 pr = pr.ring; 2921 qr = QuotientRing(pr); 2922 if d == 0: 2923 r = Quotient(qr); 2924 else: 2925 if n == 1: 2926 r = Quotient(qr,d); 2927 else: 2928 r = Quotient(qr,d,n); 2929 return RingElem(r);
2930 2931
2932 -def RC(ideal,r=0):
2933 '''Create JAS polynomial Residue as ring element. 2934 ''' 2935 if ideal == None: 2936 raise ValueError, "No ideal given." 2937 if isinstance(ideal,Ideal): 2938 ideal = jas.application.Ideal(ideal.pset); 2939 #ideal.doGB(); 2940 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 2941 if isinstance(ideal.getList().get(0).ring,ResidueRing): 2942 rc = ResidueRing( ideal.getList().get(0).ring.ideal ); 2943 else: 2944 rc = ResidueRing(ideal); 2945 if isinstance(r,RingElem): 2946 r = r.elem; 2947 if r == 0: 2948 r = Residue(rc); 2949 else: 2950 r = Residue(rc,r); 2951 return RingElem(r);
2952 2953
2954 -def LC(ideal,d=0,n=1):
2955 '''Create JAS polynomial Local as ring element. 2956 ''' 2957 if ideal == None: 2958 raise ValueError, "No ideal given." 2959 if isinstance(ideal,Ideal): 2960 ideal = jas.application.Ideal(ideal.pset); 2961 #ideal.doGB(); 2962 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 2963 if isinstance(ideal.getList().get(0).ring,LocalRing): 2964 lc = LocalRing( ideal.getList().get(0).ring.ideal ); 2965 else: 2966 lc = LocalRing(ideal); 2967 if isinstance(d,PyTuple) or isinstance(d,PyList): 2968 if n != 1: 2969 print "%s ignored" % n; 2970 if len(d) > 1: 2971 n = d[1]; 2972 d = d[0]; 2973 if isinstance(d,RingElem): 2974 d = d.elem; 2975 if isinstance(n,RingElem): 2976 n = n.elem; 2977 if d == 0: 2978 r = Local(lc); 2979 else: 2980 if n == 1: 2981 r = Local(lc,d); 2982 else: 2983 r = Local(lc,d,n); 2984 return RingElem(r);
2985 2986
2987 -def SRF(pr,d=0,n=1):
2988 '''Create JAS rational function SolvableQuotient as ring element. 2989 ''' 2990 if isinstance(d,PyTuple) or isinstance(d,PyList): 2991 if n != 1: 2992 print "%s ignored" % n; 2993 if len(d) > 1: 2994 n = d[1]; 2995 d = d[0]; 2996 if isinstance(d,RingElem): 2997 d = d.elem; 2998 if isinstance(n,RingElem): 2999 n = n.elem; 3000 if isinstance(pr,RingElem): 3001 pr = pr.elem; 3002 if isinstance(pr,Ring): 3003 pr = pr.ring; 3004 qr = SolvableQuotientRing(pr); 3005 if d == 0: 3006 r = SolvableQuotient(qr); 3007 else: 3008 if n == 1: 3009 r = SolvableQuotient(qr,d); 3010 else: 3011 r = SolvableQuotient(qr,d,n); 3012 return RingElem(r);
3013 3014
3015 -def SRC(ideal,r=0):
3016 '''Create JAS polynomial SolvableResidue as ring element. 3017 ''' 3018 #print "ideal = " + str(ideal); 3019 if ideal == None: # does not work 3020 print "ideal = " + str(ideal); 3021 if False: 3022 raise ValueError, "No ideal given." 3023 if isinstance(ideal,SolvableIdeal): 3024 ideal = jas.application.SolvableIdeal(ideal.pset); 3025 #ideal.doGB(); 3026 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 3027 if isinstance(ideal.getList().get(0).ring,SolvableResidueRing): 3028 rc = SolvableResidueRing( ideal.getList().get(0).ring.ideal ); 3029 else: 3030 rc = SolvableResidueRing(ideal); 3031 if isinstance(r,RingElem): 3032 r = r.elem; 3033 if r == 0: 3034 r = SolvableResidue(rc); 3035 else: 3036 r = SolvableResidue(rc,r); 3037 return RingElem(r);
3038 3039
3040 -def SLC(ideal,d=0,n=1):
3041 '''Create JAS polynomial SolvableLocal as ring element. 3042 ''' 3043 if ideal == None: 3044 #print "ideal = " + str(ideal); 3045 if False: 3046 raise ValueError, "No ideal given." 3047 if isinstance(ideal,SolvableIdeal): 3048 ideal = jas.application.SolvableIdeal(ideal.pset); 3049 #ideal.doGB(); 3050 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 3051 if isinstance(ideal.getList().get(0).ring,SolvableLocalRing): 3052 lc = SolvableLocalRing( ideal.getList().get(0).ring.ideal ); 3053 else: 3054 lc = SolvableLocalRing(ideal); 3055 if isinstance(d,PyTuple) or isinstance(d,PyList): 3056 if n != 1: 3057 print "%s ignored" % n; 3058 if len(d) > 1: 3059 n = d[1]; 3060 d = d[0]; 3061 if isinstance(d,RingElem): 3062 d = d.elem; 3063 if isinstance(n,RingElem): 3064 n = n.elem; 3065 if d == 0: 3066 r = SolvableLocal(lc); 3067 else: 3068 if n == 1: 3069 r = SolvableLocal(lc,d); 3070 else: 3071 r = SolvableLocal(lc,d,n); 3072 return RingElem(r);
3073 3074
3075 -def SLR(ideal,d=0,n=1):
3076 '''Create JAS polynomial SolvableLocalResidue as ring element. 3077 ''' 3078 if ideal == None: 3079 #print "ideal = " + str(ideal); 3080 if False: 3081 raise ValueError, "No ideal given." 3082 if isinstance(ideal,SolvableIdeal): 3083 ideal = jas.application.SolvableIdeal(ideal.pset); 3084 #ideal.doGB(); 3085 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 3086 cfr = ideal.getList().get(0).ring; 3087 if isinstance(cfr,SolvableLocalResidueRing): 3088 lc = SolvableLocalResidueRing( cfr.ideal ); 3089 else: 3090 lc = SolvableLocalResidueRing(ideal); 3091 if isinstance(d,PyTuple) or isinstance(d,PyList): 3092 if n != 1: 3093 print "%s ignored" % n; 3094 if len(d) > 1: 3095 n = d[1]; 3096 d = d[0]; 3097 if isinstance(d,RingElem): 3098 d = d.elem; 3099 if isinstance(n,RingElem): 3100 n = n.elem; 3101 if d == 0: 3102 r = SolvableLocalResidue(lc); 3103 else: 3104 if n == 1: 3105 r = SolvableLocalResidue(lc,d); 3106 else: 3107 r = SolvableLocalResidue(lc,d,n); 3108 return RingElem(r);
3109 3110 3111
3112 -def RR(flist,n=1,r=0):
3113 '''Create JAS regular ring Product as ring element. 3114 ''' 3115 if not isinstance(n,PyInteger): 3116 r = n; 3117 n = 1; 3118 if flist == None: 3119 raise ValueError, "No list given." 3120 if isinstance(flist,PyList) or isinstance(flist,PyTuple): 3121 flist = pylist2arraylist( [ x.factory() for x in flist ], rec=1); 3122 ncop = 0; 3123 else: 3124 ncop = n; 3125 if isinstance(flist,RingElem): 3126 flist = flist.elem; 3127 flist = flist.factory(); 3128 ncop = n; 3129 #print "flist = " + str(flist); 3130 #print "ncop = " + str(ncop); 3131 if ncop == 0: 3132 pr = ProductRing(flist); 3133 else: 3134 pr = ProductRing(flist,ncop); 3135 #print "r type(%s) = %s" % (r,type(r)); 3136 if isinstance(r,RingElem): 3137 r = r.elem; 3138 try: 3139 #print "r.class() = %s" % r.getClass().getSimpleName(); 3140 if isinstance(r,Product): 3141 #print "r.val = %s" % r.val; 3142 r = r.val; 3143 except: 3144 pass; 3145 #print "r = " + str(r); 3146 if r == 0: 3147 r = Product(pr); 3148 else: 3149 r = Product(pr,r); 3150 return RingElem(r);
3151 3152
3153 -def PS(cofac,name,f=None,truncate=None):
3154 '''Create JAS UnivPowerSeries as ring element. 3155 ''' 3156 cf = cofac; 3157 if isinstance(cofac,RingElem): 3158 cf = cofac.elem.factory(); 3159 if isinstance(cofac,Ring): 3160 cf = cofac.ring; 3161 if isinstance(truncate,RingElem): 3162 truncate = truncate.elem; 3163 if truncate == None: 3164 ps = UnivPowerSeriesRing(cf,name); 3165 else: 3166 ps = UnivPowerSeriesRing(cf,truncate,name); 3167 if f == None: 3168 r = ps.getZERO(); 3169 else: 3170 class Coeff( Coefficients ): 3171 def __init__(self,cofac): 3172 self.coFac = cofac;
3173 def generate(self,i): 3174 a = f(i); 3175 if isinstance(a,RingElem): 3176 a = a.elem; 3177 #print "a = " + str(a); 3178 return a; 3179 r = UnivPowerSeries(ps,Coeff(cf)); 3180 return RingElem(r); 3181 3182
3183 -def MPS(cofac,names,f=None,truncate=None):
3184 '''Create JAS MultiVarPowerSeries as ring element. 3185 ''' 3186 cf = cofac; 3187 if isinstance(cofac,RingElem): 3188 cf = cofac.elem.factory(); 3189 if isinstance(cofac,Ring): 3190 cf = cofac.ring; 3191 vars = names; 3192 if isinstance(vars,PyString): 3193 vars = GenPolynomialTokenizer.variableList(vars); 3194 nv = len(vars); 3195 if isinstance(truncate,RingElem): 3196 truncate = truncate.elem; 3197 if truncate == None: 3198 ps = MultiVarPowerSeriesRing(cf,nv,vars); 3199 else: 3200 ps = MultiVarPowerSeriesRing(cf,nv,vars,truncate); 3201 if f == None: 3202 r = ps.getZERO(); 3203 else: 3204 class MCoeff( MultiVarCoefficients ): 3205 def __init__(self,r): 3206 MultiVarCoefficients.__init__(self,r); 3207 self.coFac = r.coFac;
3208 def generate(self,i): 3209 a = f(i); 3210 if isinstance(a,RingElem): 3211 a = a.elem; 3212 return a; 3213 r = MultiVarPowerSeries(ps,MCoeff(ps)); 3214 #print "r = " + str(r); 3215 return RingElem(r); 3216 3217
3218 -def Vec(cofac,n,v=None):
3219 '''Create JAS GenVector ring element. 3220 ''' 3221 cf = cofac; 3222 if isinstance(cofac,RingElem): 3223 cf = cofac.elem.factory(); 3224 if isinstance(cofac,Ring): 3225 cf = cofac.ring; 3226 if isinstance(n,RingElem): 3227 n = n.elem; 3228 if isinstance(v,RingElem): 3229 v = v.elem; 3230 if isinstance(v,PyList) or isinstance(v,PyTuple): 3231 v = pylist2arraylist(v,cf,rec=1); 3232 vr = GenVectorModul(cf,n); 3233 if v == None: 3234 r = GenVector(vr); 3235 else: 3236 r = GenVector(vr,v); 3237 return RingElem(r);
3238 3239
3240 -def Mat(cofac,n,m,v=None):
3241 '''Create JAS GenMatrix ring element. 3242 ''' 3243 cf = cofac; 3244 if isinstance(cofac,RingElem): 3245 cf = cofac.elem.factory(); 3246 if isinstance(cofac,Ring): 3247 cf = cofac.ring; 3248 if isinstance(n,RingElem): 3249 n = n.elem; 3250 if isinstance(m,RingElem): 3251 m = m.elem; 3252 if isinstance(v,RingElem): 3253 v = v.elem; 3254 #print "cf type(%s) = %s" % (cf,type(cf)); 3255 if isinstance(v,PyList) or isinstance(v,PyTuple): 3256 v = pylist2arraylist(v,cf,rec=2); 3257 mr = GenMatrixRing(cf,n,m); 3258 if v == None: 3259 r = GenMatrix(mr); 3260 else: 3261 r = GenMatrix(mr,v); 3262 return RingElem(r);
3263 3264
3265 -def coercePair(a,b):
3266 '''Coerce type a to type b or type b to type a. 3267 ''' 3268 #print "a type(%s) = %s" % (a,type(a)); 3269 #print "b type(%s) = %s" % (b,type(b)); 3270 try: 3271 if not a.isPolynomial() and b.isPolynomial(): 3272 s = b.coerce(a); 3273 o = b; 3274 else: 3275 if not a.isAlgNum() and b.isAlgNum(): 3276 s = b.coerce(a); 3277 o = b; 3278 else: 3279 s = a; 3280 o = a.coerce(b); 3281 except: 3282 s = a; 3283 o = a.coerce(b); 3284 return (s,o);
3285 3286
3287 -def isJavaInstance(a):
3288 '''Test if a is a Java instance. 3289 ''' 3290 #print "a type(%s) = %s" % (a,type(a)); 3291 try: 3292 c = a.getClass(); 3293 except: 3294 return False; 3295 return True;
3296 3297
3298 -class RingElem:
3299 '''Proxy for JAS ring elements. 3300 3301 Methods to be used as + - * ** / %. 3302 ''' 3303
3304 - def __init__(self,elem):
3305 '''Constructor for ring element. 3306 ''' 3307 if isinstance(elem,RingElem): 3308 self.elem = elem.elem; 3309 else: 3310 self.elem = elem; 3311 try: 3312 self.ring = self.elem.factory(); 3313 except: 3314 self.ring = self.elem;
3315
3316 - def __str__(self):
3317 '''Create a string representation. 3318 ''' 3319 try: 3320 return str(self.elem.toScript()); 3321 except: 3322 return str(self.elem);
3323
3324 - def zero(self):
3325 '''Zero element of this ring. 3326 ''' 3327 return RingElem( self.ring.getZERO() );
3328
3329 - def isZERO(self):
3330 '''Test if this is the zero element of the ring. 3331 ''' 3332 return self.elem.isZERO();
3333
3334 - def one(self):
3335 '''One element of this ring. 3336 ''' 3337 return RingElem( self.ring.getONE() );
3338
3339 - def isONE(self):
3340 '''Test if this is the one element of the ring. 3341 ''' 3342 return self.elem.isONE();
3343
3344 - def signum(self):
3345 '''Get the sign of this element. 3346 ''' 3347 return self.elem.signum();
3348
3349 - def __abs__(self):
3350 '''Absolute value. 3351 ''' 3352 return RingElem( self.elem.abs() );
3353
3354 - def __neg__(self):
3355 '''Negative value. 3356 ''' 3357 return RingElem( self.elem.negate() );
3358
3359 - def __pos__(self):
3360 '''Positive value. 3361 ''' 3362 return self;
3363
3364 - def coerce(self,other):
3365 '''Coerce other to self. 3366 ''' 3367 #print "self type(%s) = %s" % (self,type(self)); 3368 #print "other type(%s) = %s" % (other,type(other)); 3369 #print "self.elem class(%s) = %s" % (self.elem,self.elem.getClass()); 3370 if isinstance(self.elem,GenVector): 3371 #print "self, other = %s, %s " % (self,other); 3372 if isinstance(other,PyTuple) or isinstance(other,PyList): 3373 o = pylist2arraylist(other,self.elem.factory().coFac,rec=1); 3374 o = GenVector(self.elem.factory(),o); 3375 return RingElem( o ); 3376 if isinstance(self.elem,GenMatrix): 3377 #print "self, other = %s, %s " % (type(self),type(other)); 3378 #print "o type(%s) = %s, str = %s" % (o,type(o),str(o)); 3379 if isinstance(other,PyTuple) or isinstance(other,PyList): 3380 o = pylist2arraylist(other,self.elem.factory().coFac,rec=2); 3381 o = GenMatrix(self.elem.factory(),o); 3382 return RingElem( o ); 3383 if isinstance(other,GenVector): 3384 o = other; 3385 return RingElem( o ); 3386 if isinstance(self.elem,GenPolynomial) or isinstance(self.elem,AlgebraicNumber): 3387 #print "self, other = %s, %s " % (type(self),type(other)); 3388 #print "o type(%s) = %s, str = %s" % (other,type(other),str(other)); 3389 if isinstance(other,PyInteger) or isinstance(other,PyLong): 3390 o = self.ring.fromInteger(other); 3391 return RingElem( o ); 3392 if isinstance(other,PyList) or isinstance(other,PyTuple): 3393 #print "self, other = %s, %s " % (type(self),type(other)); 3394 o = self.ring.parse( str(makeJasArith(other)) ); 3395 return RingElem( o ); 3396 if isinstance(other,RingElem): 3397 o = other.elem; 3398 else: 3399 o = other; 3400 if o == None: 3401 return RingElem( GenPolynomial(self.ring) ) 3402 if isJavaInstance(o): 3403 #print "self.elem, o = %s, %s " % (type(self.ring.coFac),type(o)); 3404 if isinstance(o,ExpVector): # want startsWith or substring(0,8) == "ExpVector": 3405 #o = GenPolynomial(self.ring,o); 3406 o = self.ring.valueOf(o); 3407 return RingElem( o ); 3408 #o = self.ring.parse( str(makeJasArith(o)) ); 3409 #o = self.ring.valueOf( makeJasArith(o) ); 3410 if isinstance(self.elem,GenPolynomial): 3411 if self.ring.coFac.getClass().getSimpleName() == o.getClass().getSimpleName(): 3412 #o = GenPolynomial(self.ring,o); 3413 o = self.ring.valueOf(o); 3414 return RingElem( o ); 3415 else: # AlgebraicNumber 3416 if self.ring.ring.coFac.getClass().getSimpleName() == o.getClass().getSimpleName(): 3417 o = self.ring.valueOf(o); 3418 return RingElem( o ); 3419 if isinstance(other,RingElem): 3420 if self.isPolynomial() and not other.isPolynomial(): 3421 #print "parse self.ring = %s" % (self.ring); 3422 o = self.ring.parse( other.elem.toString() ); # not toScript() 3423 #print "o type(%s) = %s, str = %s" % (o,type(o),str(o)); 3424 return RingElem( o ); 3425 return other; 3426 #print "--1"; 3427 if isinstance(other,PyTuple) or isinstance(other,PyList): 3428 # assume BigRational or BigComplex 3429 # assume self will be compatible with them. todo: check this 3430 o = makeJasArith(other); 3431 #print "other class(%s) = %s" % (o,o.getClass()); 3432 if self.isPolynomial(): 3433 #print "other type(%s) = %s" % (o,type(o)); 3434 o = self.ring.parse( o.toString() ); # not toScript(); 3435 #o = o.elem; 3436 if isinstance(self.elem,BigComplex): 3437 #print "other type(%s) = %s" % (o,type(o)); 3438 o = CC( o ); 3439 o = o.elem; 3440 if isinstance(self.elem,BigQuaternion): 3441 #print "other type(%s) = %s" % (o,type(o)); 3442 o = Quat( o ); 3443 o = o.elem; 3444 if isinstance(self.elem,BigOctonion): 3445 #print "other type(%s) = %s" % (o,type(o)); 3446 o = Oct( Quat(o) ); 3447 o = o.elem; 3448 if isinstance(self.elem,Product): 3449 #print "other type(%s) = %s" % (o,type(o)); 3450 o = RR(self.ring, self.elem.multiply(o) ); # valueOf 3451 #print "o = %s" % o; 3452 o = o.elem; 3453 return RingElem(o); 3454 #print "--2"; 3455 # test if self.elem is a factory itself 3456 if self.isFactory(): 3457 if isinstance(other,PyInteger) or isinstance(other,PyLong): 3458 o = self.elem.fromInteger(other); 3459 else: 3460 if isinstance(other,PyFloat): # ?? what to do ?? 3461 o = self.elem.fromInteger( int(other) ); 3462 else: 3463 print "coerce_1: unknown other type(%s) = %s" % (other,type(other)); 3464 o = self.elem.parse( str(other) ); 3465 return RingElem(o); 3466 #print "--3"; 3467 #print "other type(%s) = %s" % (other,type(other)); 3468 # self.elem has a ring factory 3469 if isinstance(other,PyInteger) or isinstance(other,PyLong): 3470 o = self.elem.factory().fromInteger(other); 3471 else: 3472 if isinstance(other,PyFloat): # ?? what to do ?? 3473 #print "other type(%s) = %s" % (other,type(other)); 3474 #print "self type(%s) = %s" % (self.elem,self.elem.getClass().getName()); 3475 o = BigDecimal(other); 3476 if isinstance(self.elem,Product): 3477 o = RR(self.ring, self.elem.idempotent().multiply(o) ); # valueOf 3478 o = o.elem; 3479 else: 3480 o = self.elem.factory().getZERO().sum( o ); 3481 else: 3482 print "coerce_2: unknown other type(%s) = %s" % (other,type(other)); 3483 print "coerce_2: self type(%s) = %s" % (self,type(self)); 3484 o = self.elem.factory().parse( str(other) ); 3485 #print "--4"; 3486 return RingElem(o);
3487
3488 - def isFactory(self):
3489 '''Test if this is itself a ring factory. 3490 ''' 3491 f = self.elem.factory(); 3492 if self.elem == f: 3493 return True; 3494 else: 3495 return False;
3496
3497 - def isPolynomial(self):
3498 '''Test if this is a polynomial. 3499 ''' 3500 try: 3501 nv = self.elem.ring.nvar; 3502 except: 3503 return False; 3504 return True;
3505
3506 - def isAlgNum(self):
3507 '''Test if this is an algebraic number. 3508 ''' 3509 try: 3510 nv = self.elem.ring.ring.nvar; 3511 except: 3512 #print "isAlgNum fail: type(%s) = %s" % (self.elem,type(self.elem)); 3513 return False; 3514 #print "isAlgNum true: type(%s) = %s" % (self.elem,type(self.elem)); 3515 return True;
3516
3517 - def __cmp__(self,other):
3518 '''Compare two ring elements. 3519 ''' 3520 [s,o] = coercePair(self,other); 3521 return s.elem.compareTo( o.elem );
3522
3523 - def __hash__(self):
3524 '''Hash value. 3525 ''' 3526 return self.elem.hashCode();
3527
3528 - def __len__(self):
3529 '''Length of the element. 3530 ''' 3531 return self.elem.length();
3532
3533 - def __mul__(self,other):
3534 '''Multiply two ring elements. 3535 ''' 3536 [s,o] = coercePair(self,other); 3537 #print "self type(%s) = %s" % (s,type(s)); 3538 #print "other type(%s) = %s" % (o,type(o)); 3539 if isinstance(s.elem,GenMatrix) and isinstance(o.elem,GenVector): 3540 return RingElem( BasicLinAlg().rightProduct(o.elem, s.elem) ); 3541 if isinstance(s.elem,GenVector) and isinstance(o.elem,GenMatrix): 3542 return RingElem( BasicLinAlg().leftProduct(s.elem, o.elem) ); 3543 return RingElem( s.elem.multiply( o.elem ) );
3544
3545 - def __rmul__(self,other):
3546 '''Reverse multiply two ring elements. 3547 ''' 3548 [s,o] = coercePair(self,other); 3549 return o.__mul__(s);
3550
3551 - def __add__(self,other):
3552 '''Add two ring elements. 3553 ''' 3554 [s,o] = coercePair(self,other); 3555 return RingElem( s.elem.sum( o.elem ) );
3556
3557 - def __radd__(self,other):
3558 '''Reverse add two ring elements. 3559 ''' 3560 [s,o] = coercePair(self,other); 3561 return o.__add__(s);
3562
3563 - def __sub__(self,other):
3564 '''Subtract two ring elements. 3565 ''' 3566 [s,o] = coercePair(self,other); 3567 return RingElem( s.elem.subtract( o.elem ) );
3568
3569 - def __rsub__(self,other):
3570 '''Reverse subtract two ring elements. 3571 ''' 3572 [s,o] = coercePair(self,other); 3573 return o.__sub__(self);
3574
3575 - def __div__(self,other):
3576 '''Divide two ring elements. 3577 ''' 3578 [s,o] = coercePair(self,other); 3579 return RingElem( s.elem.divide( o.elem ) );
3580
3581 - def __rdiv__(self,other):
3582 '''Reverse divide two ring elements. 3583 ''' 3584 [s,o] = coercePair(self,other); 3585 return o.__div__(s);
3586
3587 - def __mod__(self,other):
3588 '''Modular remainder of two ring elements. 3589 ''' 3590 [s,o] = coercePair(self,other); 3591 return RingElem( s.elem.remainder( o.elem ) );
3592
3593 - def __xor__(self,other):
3594 '''Can not be used as power. 3595 ''' 3596 return None;
3597
3598 - def __pow__(self,other,n=None):
3599 '''Power of this to other. 3600 ''' 3601 #print "self type(%s) = %s" % (self,type(self)); 3602 #print "pow other type(%s) = %s" % (other,type(other)); 3603 if isinstance(other,PyInteger) or isinstance(other,PyLong): 3604 n = other; 3605 else: 3606 if isinstance(other,RingElem): 3607 n = other.elem; 3608 #if isinstance(n,BigRational): # does not work 3609 if isinstance(n,BigRational): 3610 n = n.numerator().intValue() / n.denominator().intValue(); 3611 #if isinstance(n,BigInteger): # does not work 3612 if isinstance(n,BigInteger) or isinstance(n,Long) or isinstance(n,Integer): 3613 n = n.intValue(); 3614 if n == None: 3615 n = other; 3616 if self.isFactory(): 3617 p = Power(self.elem).power( self.elem, n ); 3618 else: 3619 p = Power(self.ring).power( self.elem, n ); 3620 return RingElem( p );
3621
3622 - def __eq__(self,other):
3623 '''Test if two ring elements are equal. 3624 ''' 3625 if other == None: 3626 return False; 3627 [s,o] = coercePair(self,other); 3628 return s.elem.equals(o.elem)
3629
3630 - def __ne__(self,other):
3631 '''Test if two ring elements are not equal. 3632 ''' 3633 if other == None: 3634 return False; 3635 [s,o] = coercePair(self,other); 3636 return not self.elem.equals(o.elem)
3637
3638 - def __float__(self):
3639 '''Convert to Python float. 3640 ''' 3641 #print "self type(%s) = %s" % (self,type(self)); 3642 e = self.elem; 3643 if isinstance(e,BigInteger): 3644 e = BigRational(e); 3645 if isinstance(e,BigRational): 3646 e = BigDecimal(e); 3647 if isinstance(e,BigDecimal): 3648 e = e.toString(); 3649 e = float(e); 3650 return e
3651
3652 - def __getitem__(self,i):
3653 '''Matrix or vector entry. 3654 ''' 3655 if isinstance(i,RingElem): 3656 i = i.elem; 3657 if isinstance(self.elem,GenMatrix): 3658 e = self.elem.getRow(i); 3659 return RingElem( e ); 3660 if isinstance(self.elem,GenVector): 3661 e = self.elem.get(i); 3662 return RingElem( e ); 3663 raise ValueError, "no matrix or vector " + str(self.ring)
3664
3665 - def factory(self):
3666 '''Get the factory of this element. 3667 ''' 3668 fac = self.elem.factory(); 3669 try: 3670 nv = fac.nvar; 3671 except: 3672 return RingElem(fac); 3673 #return PolyRing(fac.coFac,fac.getVars(),fac.tord); 3674 return RingElem(fac);
3675
3676 - def gens(self):
3677 '''Get the generators for the factory of this element. 3678 ''' 3679 L = self.elem.factory().generators(); 3680 #print "L = %s" % L; 3681 N = [ RingElem(e) for e in L ]; 3682 #print "N = %s" % N; 3683 return N;
3684
3685 - def inject_variables(self):
3686 '''Inject generators as variables into the main global namespace 3687 ''' 3688 inject_generators(self.gens());
3689
3690 - def monic(self):
3691 '''Monic polynomial. 3692 ''' 3693 return RingElem( self.elem.monic() );
3694
3695 - def homogenize(self,var='h'):
3696 '''homogenize polynomial. 3697 3698 INPUT: 3699 - "var" - variable name to use for homogenization 3700 ''' 3701 if not self.isPolynomial(): 3702 return self 3703 if var in self.ring.vars: 3704 r = self.ring.extend(1); 3705 print "WARN: case for variable %s not implemented, new name %s" % (var,r.vars[-1]) 3706 else: 3707 r = self.ring.extend([var]); 3708 h = self.elem; 3709 h = h.homogenize(r); 3710 return RingElem(h);
3711
3712 - def evaluate(self,a):
3713 '''Evaluate at a for power series or polynomial. 3714 ''' 3715 #print "self type(%s) = %s" % (self,type(self)); 3716 #print "a type(%s) = %s" % (a,type(a)); 3717 x = None; 3718 if isinstance(a,RingElem): 3719 x = a.elem; 3720 if isinstance(a,PyTuple) or isinstance(a,PyList): 3721 # assume BigRational or BigComplex 3722 # assume self will be compatible with them. todo: check this 3723 #x = makeJasArith(a); 3724 x = pylist2arraylist(a); 3725 else: 3726 x = pylist2arraylist([a]); 3727 try: 3728 if isinstance(self.elem,UnivPowerSeries): 3729 e = self.elem.evaluate(x[0]); 3730 else: 3731 if isinstance(self.elem,MultiVarPowerSeries): 3732 e = self.elem.evaluate(x); 3733 else: 3734 x = [ p.leadingBaseCoefficient() for p in x ]; 3735 #puts "x = " + x[0].getClass().getSimpleName().to_s; 3736 e = PolyUtil.evaluateAll(self.ring.coFac, self.elem, x); 3737 except: 3738 e = 0; 3739 return RingElem( e );
3740
3741 - def integrate(self,a=0,r=None):
3742 '''Integrate a power series or rational function with constant a. 3743 3744 a is the integration constant, r is for partial integration in variable r. 3745 ''' 3746 #print "self type(%s) = %s" % (self,type(self)); 3747 #print "a type(%s) = %s" % (a,type(a)); 3748 x = None; 3749 if isinstance(a,RingElem): 3750 x = a.elem; 3751 if isinstance(a,PyTuple) or isinstance(a,PyList): 3752 # assume BigRational or BigComplex 3753 # assume self will be compatible with them. todo: check this 3754 x = makeJasArith(a); 3755 # power series 3756 try: 3757 if r != None: 3758 e = self.elem.integrate(x,r); 3759 else: 3760 e = self.elem.integrate(x); 3761 return RingElem( e ); 3762 except: 3763 pass; 3764 cf = self.elem.ring; 3765 try: 3766 cf = cf.ring; 3767 except: 3768 pass; 3769 # rational function 3770 integrator = ElementaryIntegration(cf.coFac); 3771 ei = integrator.integrate(self.elem); 3772 return ei;
3773
3774 - def differentiate(self,r=None):
3775 '''Differentiate a power series. 3776 3777 r is for partial differentiation in variable r. 3778 ''' 3779 try: 3780 if r != None: 3781 e = self.elem.differentiate(r); 3782 else: 3783 e = self.elem.differentiate(); 3784 except: 3785 e = self.elem.factory().getZERO(); 3786 return RingElem( e );
3787
3788 - def random(self,n=3):
3789 '''Random element. 3790 3791 n size for random element will be less than 2**n. 3792 ''' 3793 if isinstance(n,RingElem): 3794 n = n.elem; 3795 return RingElem( self.elem.factory().random(n) );
3796
3797 - def gcd(self,b):
3798 '''Compute the greatest common divisor of this/self and b. 3799 3800 ''' 3801 a = self.elem; 3802 if isinstance(b,RingElem): 3803 b = b.elem; 3804 else: 3805 b = element( b ); 3806 b = b.elem; 3807 if self.isPolynomial(): 3808 engine = Ring.getEngineGcd(self.ring); 3809 return RingElem( engine.gcd(a,b) ); 3810 else: 3811 return RingElem( a.gcd(b) );
3812
3813 - def squarefreeFactors(self):
3814 '''Compute squarefree factors of polynomial. 3815 3816 ''' 3817 a = self.elem; 3818 if self.isPolynomial(): 3819 sqf = Ring.getEngineSqf(self.ring); 3820 if sqf == None: 3821 raise ValueError, "squarefreeFactors not implemented for " + self.ring.to_s; 3822 cf = self.ring.coFac; 3823 if isinstance(cf,GenPolynomialRing): 3824 e = sqf.recursiveSquarefreeFactors( a ); 3825 else: 3826 e = sqf.squarefreeFactors( a ); 3827 L = {}; 3828 for a in e.keySet(): 3829 i = e.get(a); 3830 L[ RingElem( a ) ] = i; 3831 return L; 3832 else: 3833 raise ValueError, "squarefreeFactors not implemented for " + a.to_s;
3834
3835 - def factors(self):
3836 '''Compute irreducible factorization for modular, integer, 3837 rational number and algebriac number coefficients. 3838 3839 ''' 3840 a = self.elem; 3841 if self.isPolynomial(): 3842 factor = Ring.getEngineFactor(self.ring); 3843 if factor == None: 3844 raise ValueError, "factors not implemented for " + self.ring.to_s; 3845 cf = self.ring.coFac; 3846 if isinstance(cf,GenPolynomialRing): 3847 e = factor.recursiveFactors( a ); 3848 else: 3849 e = factor.factors( a ); 3850 L = {}; 3851 for a in e.keySet(): 3852 i = e.get(a); 3853 L[ RingElem( a ) ] = i; 3854 return L; 3855 else: 3856 raise ValueError, "factors not implemented for " + a.to_s;
3857
3858 - def factorsAbsolute(self):
3859 '''Compute absolute irreducible factorization for (modular,) 3860 rational number coefficients. 3861 ''' 3862 a = self.elem; 3863 if self.isPolynomial(): 3864 factor = Ring.getEngineFactor(self.ring); 3865 if factor == None: 3866 raise ValueError, "factors not implemented for " + self.ring.to_s; 3867 try: 3868 L = factor.factorsAbsolute( a ); 3869 ## L = {}; 3870 ## for a in e.keySet(): 3871 ## i = e.get(a); 3872 ## L[ RingElem( a ) ] = i; 3873 return L; 3874 except Exception, e: 3875 raise ValueError, "error in factorsAbsolute " + str(e); 3876 else: 3877 raise ValueError, "factors not implemented for " + a.to_s;
3878
3879 - def realRoots(self,eps=None):
3880 '''Compute real roots of univariate polynomial. 3881 ''' 3882 a = self.elem; 3883 if isinstance(eps,RingElem): 3884 eps = eps.elem; 3885 try: 3886 if eps == None: 3887 #R = RealRootsSturm().realRoots( a ); 3888 R = RootFactory.realAlgebraicNumbers( a ); 3889 else: 3890 R = RootFactory.realAlgebraicNumbers( a, eps ); 3891 R = [ RingElem(r) for r in R ]; 3892 #R = [ RingElem(BigDecimal(r.getRational())) for r in R ]; 3893 return R; 3894 except Exception, e: 3895 print "error " + str(e) 3896 return None
3897
3898 - def complexRoots(self,eps=None):
3899 '''Compute complex roots of univariate polynomial. 3900 ''' 3901 a = self.elem; 3902 if isinstance(eps,RingElem): 3903 eps = eps.elem; 3904 cmplx = False; 3905 try: 3906 x = a.ring.coFac.getONE().getRe(); 3907 cmplx = True; 3908 except Exception, e: 3909 pass; 3910 try: 3911 if eps == None: 3912 if cmplx: 3913 R = RootFactory.complexAlgebraicNumbersComplex(a); 3914 else: 3915 R = RootFactory.complexAlgebraicNumbers(a); 3916 # R = ComplexRootsSturm(a.ring.coFac).complexRoots( a ); 3917 # R = [ r.centerApprox() for r in R ]; 3918 # R = [ r.ring.getRoot() for r in R ]; 3919 R = [ RingElem(r) for r in R ]; 3920 else: 3921 if cmplx: 3922 R = RootFactory.complexAlgebraicNumbersComplex(a,eps); 3923 else: 3924 R = RootFactory.complexAlgebraicNumbers(a,eps); 3925 R = [ RingElem(r) for r in R ]; 3926 # R = [ r.decimalMagnitude() for r in R ]; 3927 # R = ComplexRootsSturm(a.ring.coFac).complexRoots( a, eps ); 3928 # R = ComplexRootsSturm(a.ring.coFac).approximateRoots( a, eps ); 3929 return R; 3930 except Exception, e: 3931 print "error " + str(e) 3932 return None
3933
3934 - def algebraicRoots(self,eps=None):
3935 '''Compute algebraic roots, i.e. the real and complex roots of univariate polynomial. 3936 ''' 3937 a = self.elem; 3938 if isinstance(eps,RingElem): 3939 eps = eps.elem; 3940 try: 3941 if eps == None: 3942 R = RootFactory.algebraicRoots(a); 3943 else: 3944 R = RootFactory.algebraicRoots(a, eps); 3945 #R = [ RingElem(r) for r in R ]; 3946 return RingElem(R); 3947 except Exception, e: 3948 print "error " + str(e) 3949 return None
3950
3951 - def rootRefine(self,eps=None):
3952 '''Compute algebraic roots refinement. 3953 ''' 3954 a = self.elem; 3955 if isinstance(eps,RingElem): 3956 eps = eps.elem; 3957 try: 3958 RootFactory.rootRefine(a, eps); 3959 #R = [ RingElem(r) for r in R ]; 3960 return RingElem(a); 3961 except Exception, e: 3962 print "error " + str(e) 3963 return None
3964
3965 - def decimalRoots(self,eps=None):
3966 '''Compute decimal approximation of real and complex roots of univariate polynomial. 3967 ''' 3968 a = self.elem; 3969 if isinstance(eps,RingElem): 3970 eps = eps.elem; 3971 try: 3972 R = RootFactory.decimalRoots(a, eps); 3973 #R = [ RingElem(r) for r in R ]; 3974 return RingElem(R); 3975 except Exception, e: 3976 print "error " + str(e) 3977 return None
3978
3979 - def rootsOfUnity(self):
3980 '''Roots of unity of real and complex algebraic numbers. 3981 ''' 3982 a = self.elem; 3983 try: 3984 if isinstance(a,AlgebraicRootsPrimElem): 3985 R = RootFactoryApp.rootsOfUnity(a); 3986 else: 3987 R = RootFactory.rootsOfUnity(a); 3988 #R = [ RingElem(r) for r in R ]; 3989 return RingElem(R); 3990 except Exception, e: 3991 print "error " + str(e) 3992 return None
3993
3994 - def rootReduce(self, other):
3995 '''Root reduce of real and complex algebraic numbers. 3996 Compute an extension field with a primitive element. 3997 ''' 3998 a = self.elem; 3999 b = other; 4000 if isinstance(b,RingElem): 4001 b = b.elem; 4002 try: 4003 R = RootFactoryApp.rootReduce(a, b); 4004 #R = [ RingElem(r) for r in R ]; 4005 return RingElem(R); 4006 except Exception, e: 4007 print "error " + str(e) 4008 return None
4009
4010 - def contFrac(self, prec):
4011 '''Continued fraction computation of rational and real algebraic numbers. 4012 ''' 4013 a = self.elem; 4014 if isinstance(a,RealAlgebraicNumber): 4015 b = prec; 4016 if isinstance(b,RingElem): 4017 b = b.elem; 4018 if b < 1: 4019 b = 1; 4020 d = RealArithUtil.continuedFraction(a, b); 4021 return d; 4022 if isinstance(a,BigRational): 4023 d = ArithUtil.continuedFraction(a); 4024 return d; 4025 raise ValueError, "type " + str(type(a)) + " not supported";
4026
4027 - def contFracApprox(self, lst):
4028 '''Continued fraction expansion to approximate fraction. 4029 ''' 4030 if lst == None: 4031 d = BigRational.ZERO; 4032 return RingElem( d ); 4033 nb = ArithUtil.continuedFractionApprox(lst); 4034 return RingElem( nb );
4035
4036 - def solve(self, b):
4037 '''Solve system of linear equations. 4038 ''' 4039 if isinstance(b,RingElem): 4040 b = b.elem; 4041 x = LinAlg().solve(self.elem, b); 4042 return RingElem( x );
4043
4044 - def decompLU(self):
4045 '''Decompose to LU matrix. this is modified. 4046 ''' 4047 p = LinAlg().decompositionLU(self.elem); 4048 uu = self.elem.getUpper(); 4049 ll = self.elem.getLower(); 4050 return [ RingElem(ll), RingElem(uu), RingElem(p) ];
4051
4052 - def solveLU(self, p, b):
4053 '''Solve with LU matrix. 4054 ''' 4055 if isinstance(b,RingElem): 4056 b = b.elem; 4057 if isinstance(p,RingElem): 4058 p = p.elem; 4059 x = LinAlg().solveLU(self.elem, p, b); 4060 return RingElem(x);
4061
4062 - def determinant(self, p=None):
4063 '''Determinant from LU matrix. 4064 ''' 4065 a = self.elem; 4066 if p == None: 4067 p = LinAlg().decompositionLU(a); 4068 if isinstance(p,RingElem): 4069 p = p.elem; 4070 if p.isEmpty(): 4071 d = self.ring.coFac.getZERO(); 4072 d = LinAlg().determinantLU(a, p); 4073 return RingElem(d);
4074
4075 - def rowEchelon(self):
4076 '''Row echelon form matrix. 4077 ''' 4078 re = LinAlg().rowEchelonForm(self.elem); 4079 res = LinAlg().rowEchelonFormSparse(re); 4080 return RingElem(res);
4081
4082 - def rank(self):
4083 '''Rank from row echelon form matrix. 4084 ''' 4085 r = LinAlg().rankRE(self.elem); 4086 return r;
4087
4088 - def nullSpace(self):
4089 '''Null space basis. {v_i} with v_i * self = 0. 4090 ''' 4091 r = LinAlg().nullSpaceBasis(self.elem); 4092 return r;
4093
4094 - def coefficients(self):
4095 '''Get the coefficients of a polynomial. 4096 ''' 4097 a = self.elem; 4098 #L = [ c.toScriptFactory() for c in a.coefficientIterator() ]; 4099 L = [ RingElem(c) for c in a.coefficientIterator() ]; 4100 return L
4101
4102 - def cpp(self):
4103 '''(exterior) polynomial coefficient primitive part. 4104 ''' 4105 a = self.elem; 4106 r = a.coeffPrimitivePart(); 4107 p = RingElem(r); 4108 return p
4109
4110 - def interiorLeftProduct(self,other):
4111 '''Inner left product of two exterior vectors / polynomials. 4112 ''' 4113 [s,o] = coercePair(self,other); 4114 #print "self type(%s) = %s" % (s,type(s)); 4115 #print "other type(%s) = %s" % (o,type(o)); 4116 return RingElem( s.elem.interiorLeftProduct( o.elem ) );
4117
4118 - def interiorRightProduct(self,other):
4119 '''Inner right product of two exterior vectors / polynomials. 4120 ''' 4121 [s,o] = coercePair(self,other); 4122 #print "self type(%s) = %s" % (s,type(s)); 4123 #print "other type(%s) = %s" % (o,type(o)); 4124 return RingElem( s.elem.interiorRightProduct( o.elem ) );
4125 4126 #---------------- 4127 # Compatibility methods for Sage/Singular: 4128 # Note: the meaning of lt and lm is swapped compared to JAS. 4129 #---------------- 4130
4131 - def parent(self):
4132 '''Parent in Sage is factory in JAS. 4133 4134 Compatibility method for Sage/Singular. 4135 ''' 4136 return self.factory();
4137
4138 - def __call__(self,num):
4139 '''Apply this to num. 4140 ''' 4141 if num == 0: 4142 return self.zero(); 4143 if num == 1: 4144 return self.one(); 4145 return RingElem( self.ring.fromInteger(num) );
4146
4147 - def lm(self):
4148 '''Leading monomial of a polynomial. 4149 4150 Compatibility method for Sage/Singular. 4151 Note: the meaning of lt and lm is swapped compared to JAS. 4152 ''' 4153 ev = self.elem.leadingExpVector(); 4154 return ev;
4155
4156 - def lc(self):
4157 '''Leading coefficient of a polynomial. 4158 4159 Compatibility method for Sage/Singular. 4160 ''' 4161 c = self.elem.leadingBaseCoefficient(); 4162 return RingElem(c);
4163
4164 - def lt(self):
4165 '''Leading term of a polynomial. 4166 4167 Compatibility method for Sage/Singular. 4168 Note: the meaning of lt and lm is swapped compared to JAS. 4169 ''' 4170 ev = self.elem.leadingMonomial(); 4171 return Monomial(ev);
4172
4173 - def degree(self):
4174 '''Degree of a polynomial. 4175 ''' 4176 try: 4177 ev = self.elem.degree(); 4178 #ev = self.elem.totalDegree(); 4179 except: 4180 return None; 4181 return ev;
4182
4183 - def base_ring(self):
4184 '''Coefficient ring of a polynomial. 4185 ''' 4186 try: 4187 ev = self.elem.ring.coFac; 4188 except: 4189 return None; 4190 return RingElem(ev);
4191
4192 - def is_field(self):
4193 '''Test if this RingElem is field. 4194 ''' 4195 return self.elem.isField();
4196
4197 - def monomials(self):
4198 '''All monomials of a polynomial. 4199 4200 Compatibility method for Sage/Singular. 4201 ''' 4202 ev = self.elem.getMap().keySet(); 4203 return ev;
4204
4205 - def divides(self,other):
4206 '''Test if self divides other. 4207 4208 Compatibility method for Sage/Singular. 4209 ''' 4210 [s,o] = coercePair(self,other); 4211 return o.elem.remainder( s.elem ).isZERO();
4212
4213 - def ideal(self,list):
4214 '''Create an ideal. 4215 4216 Compatibility method for Sage/Singular. 4217 ''' 4218 r = Ring("",ring=self.ring,fast=True); 4219 return r.ideal("",list=list);
4220
4221 - def monomial_quotient(self,a,b,coeff=False):
4222 '''Quotient of ExpVectors. 4223 4224 Compatibility method for Sage/Singular. 4225 ''' 4226 if isinstance(a,RingElem): 4227 a = a.elem; 4228 if isinstance(b,RingElem): 4229 b = b.elem; 4230 if coeff == False: 4231 if isinstance(a,GenPolynomial): 4232 return RingElem( a.divide(b) ); 4233 else: 4234 return RingElem( GenPolynomial(self.ring, a.subtract(b)) ); 4235 else: 4236 # assume JAS Monomial 4237 c = a.coefficient().divide(b.coefficient()); 4238 e = a.exponent().subtract(b.exponent()) 4239 return RingElem( GenPolynomial(self.ring, c, e) );
4240
4241 - def monomial_divides(self,a,b):
4242 '''Test divide of ExpVectors. 4243 4244 Compatibility method for Sage/Singular. 4245 ''' 4246 #print "JAS a = " + str(a) + ", b = " + str(b); 4247 if isinstance(a,RingElem): 4248 a = a.elem; 4249 if isinstance(a,GenPolynomial): 4250 a = a.leadingExpVector(); 4251 if not isinstance(a,ExpVector): 4252 raise ValueError, "No ExpVector a given " + str(a) + ", " + str(b) 4253 if b == None: 4254 return False; 4255 if isinstance(b,RingElem): 4256 b = b.elem; 4257 if isinstance(b,GenPolynomial): 4258 b = b.leadingExpVector(); 4259 if not isinstance(b,ExpVector): 4260 raise ValueError, "No ExpVector b given " + str(a) + ", " + str(b) 4261 return a.divides(b);
4262
4263 - def monomial_pairwise_prime(self,e,f):
4264 '''Test if ExpVectors are pairwise prime. 4265 4266 Compatibility method for Sage/Singular. 4267 ''' 4268 if isinstance(e,RingElem): 4269 e = e.elem; 4270 if isinstance(f,RingElem): 4271 f = f.elem; 4272 # assume JAS ExpVector 4273 c = e.gcd(f); 4274 return c.isZERO();
4275
4276 - def monomial_lcm(self,e,f):
4277 '''Lcm of ExpVectors. 4278 4279 Compatibility method for Sage/Singular. 4280 ''' 4281 if isinstance(e,RingElem): 4282 e = e.elem; 4283 if isinstance(f,RingElem): 4284 f = f.elem; 4285 # assume JAS ExpVector 4286 c = e.lcm(f); 4287 return c;
4288
4289 - def reduce(self,F):
4290 '''Compute a normal form of self with respect to F. 4291 4292 Compatibility method for Sage/Singular. 4293 ''' 4294 s = self.elem; 4295 Fe = [ e.elem for e in F ]; 4296 if self.ring.coFac.isField(): 4297 n = ReductionSeq().normalform(Fe,s); 4298 else: 4299 n = PseudoReductionSeq().normalform(Fe,s); 4300 return RingElem(n);
4301 4302 #---------------- 4303 # End of compatibility methods 4304 #---------------- 4305 4306
4307 -class Order(TermOrderByName):
4308 '''Collection of JAS and other CAS term order names. 4309 4310 Defines names for TermOrders. 4311 See U{TermOrderByName<http://krum.rz.uni-mannheim.de/jas/doc/api/edu/jas/poly/TermOrderByName.html>}. 4312 '''
4313 # empty 4314 4315
4316 -class PolyRing(Ring):
4317 '''Represents a JAS polynomial ring: GenPolynomialRing. 4318 4319 Provides more convenient constructor. 4320 Then returns a Ring. 4321 4322 Example of the construction a polynomial ring over the 4323 rational numbers QQ() in the variables 'x' and 'y', together 4324 with the input of a polynomial (x+y)**3. 4325 >>> from jas import PolyRing, QQ 4326 >>> r = PolyRing(QQ(),"x,y") 4327 globally defined variables: one, x, y 4328 4329 The example works with p = (x+y)**3, but in doctests the 4330 full notation r.x and r.y must be used for x respectively y. 4331 >>> p = (r.x+r.y)**3 4332 >>> print p 4333 ( y**3 + 3 * x * y**2 + 3 * x**2 * y + x**3 ) 4334 4335 ''' 4336
4337 - def __init__(self,coeff,vars,order=Order.IGRLEX):
4338 '''Ring constructor. 4339 4340 coeff = factory for coefficients, 4341 vars = string with variable names, 4342 order = term order or weight matrix. 4343 ''' 4344 if coeff == None: 4345 raise ValueError, "No coefficient given." 4346 cf = coeff; 4347 if isinstance(coeff,RingElem): 4348 cf = coeff.elem.factory(); 4349 if isinstance(coeff,Ring): 4350 cf = coeff.ring; 4351 if vars == None: 4352 raise ValueError, "No variable names given." 4353 names = vars; 4354 if isinstance(vars,PyString): 4355 names = GenPolynomialTokenizer.variableList(vars); 4356 nv = len(names); 4357 to = Order.IGRLEX; 4358 if isinstance(order,TermOrder): 4359 to = order; 4360 if isinstance(order,PyList) or isinstance(order,PyTuple): 4361 #print "order = " + str(order); 4362 to = TermOrder.reverseWeight(order); 4363 tring = GenPolynomialRing(cf,nv,to,names); 4364 self.ring = tring; 4365 Ring.__init__(self,ring=tring)
4366
4367 - def __str__(self):
4368 '''Create a string representation. 4369 ''' 4370 return self.ring.toScript();
4371 4372 lex = Order.INVLEX 4373 '''Abreviation for INVLEX. 4374 ''' 4375 4376 grad = Order.IGRLEX 4377 '''Abreviation for IGRLEX. 4378 '''
4379 4380
4381 -class SolvPolyRing(SolvableRing):
4382 '''Represents a JAS solvable polynomial ring: GenSolvablePolynomialRing. 4383 4384 Provides more convenient constructor. 4385 Then returns a Ring. 4386 ''' 4387
4388 - def __init__(self,coeff,vars,order=Order.IGRLEX,rel=[]):
4389 '''Ring constructor. 4390 4391 coeff = factory for coefficients, 4392 vars = string with variable names, 4393 order = term order, 4394 rel = triple list of relations. (e,f,p,...) with e * f = p as relation 4395 and e, f and p are commutative polynomials. 4396 ''' 4397 if coeff == None: 4398 raise ValueError, "No coefficient given." 4399 cf = coeff; 4400 if isinstance(coeff,RingElem): 4401 cf = coeff.elem.factory(); 4402 if isinstance(coeff,Ring): 4403 cf = coeff.ring; 4404 if vars == None: 4405 raise ValueError, "No variable names given." 4406 names = vars; 4407 if isinstance(vars,PyString): 4408 names = GenPolynomialTokenizer.variableList(vars); 4409 nv = len(names); 4410 #to = PolyRing.lex; 4411 to = Order.IGRLEX; 4412 if isinstance(order,TermOrder): 4413 to = order; 4414 L = []; 4415 for x in rel: 4416 if isinstance(x,RingElem): 4417 x = x.elem; 4418 L.append(x); 4419 constSolv = False; 4420 for i in range(0,len(L),3): 4421 #print "L[i+1] = " + str(L[i+1]); 4422 if L[i+1].isConstant(): 4423 constSolv = True; 4424 cfs = cf.toScript(); 4425 if cfs[0] == "0": 4426 cfs = cf.toScriptFactory(); 4427 recSolv = isinstance(cf,GenPolynomialRing); 4428 recSolvWord = isinstance(cf,GenWordPolynomialRing); 4429 resWord = isinstance(cf,WordResidueRing); 4430 quotSolv = isinstance(cf,SolvableQuotientRing); 4431 resSolv = isinstance(cf,SolvableResidueRing); 4432 locSolv = isinstance(cf,SolvableLocalRing); 4433 locresSolv = isinstance(cf,SolvableLocalResidueRing); 4434 if recSolv and not constSolv: 4435 recSolv = False; 4436 #print "cf = " + str(cf.getClass().getSimpleName()) + ", quotSolv = " + str(quotSolv) + ", recSolv = " + str(recSolv); 4437 if recSolv: 4438 ring = RecSolvablePolynomialRing(cf,nv,to,names); 4439 table = ring.table; 4440 coeffTable = ring.coeffTable; 4441 else: 4442 if resSolv: 4443 #ring = ResidueSolvablePolynomialRing(cf,nv,to,names); 4444 ring = QLRSolvablePolynomialRing(cf,nv,to,names); 4445 table = ring.table; 4446 coeffTable = ring.polCoeff.coeffTable; 4447 else: 4448 if recSolvWord: 4449 print "RecSolvableWordPolynomialRing: " + cfs; 4450 ring = RecSolvableWordPolynomialRing(cf,nv,to,names); 4451 table = ring.table; 4452 coeffTable = ring.coeffTable; 4453 else: 4454 if resWord: 4455 print "ResWordSolvablePolynomialRing: " + cfs; 4456 #ring = GenSolvablePolynomialRing(cf,nv,to,names); # ?? 4457 #ring = RecSolvableWordPolynomialRing(cf,nv,to,names); 4458 ring = ResidueSolvableWordPolynomialRing(cf,nv,to,names); 4459 #print "ring = " + str(ring.toScript()); 4460 table = ring.table; 4461 coeffTable = ring.polCoeff.coeffTable; 4462 #coeffTable = ring.coeffTable; 4463 else: 4464 if quotSolv: 4465 #ring = QuotSolvablePolynomialRing(cf,nv,to,names); 4466 ring = QLRSolvablePolynomialRing(cf,nv,to,names); 4467 table = ring.table; 4468 coeffTable = ring.polCoeff.coeffTable; 4469 else: 4470 if locSolv: 4471 #ring = LocalSolvablePolynomialRing(cf,nv,to,names); 4472 ring = QLRSolvablePolynomialRing(cf,nv,to,names); 4473 table = ring.table; 4474 coeffTable = ring.polCoeff.coeffTable; 4475 else: 4476 if locresSolv: 4477 #ring = LocalResidueSolvablePolynomialRing(cf,nv,to,names); 4478 ring = QLRSolvablePolynomialRing(cf,nv,to,names); 4479 table = ring.table; 4480 coeffTable = ring.polCoeff.coeffTable; 4481 else: 4482 ring = GenSolvablePolynomialRing(cf,nv,to,names); 4483 table = ring.table; 4484 coeffTable = table; 4485 #print "rel = " + str(L); 4486 for i in range(0,len(L),3): 4487 print "adding relation: " + str(L[i]) + " * " + str(L[i+1]) + " = " + str(L[i+2]); 4488 if L[i+1].isConstant(): 4489 if recSolv or recSolvWord: 4490 coeffTable.update( L[i], L[i+1], L[i+2] ); 4491 else: 4492 if resSolv or resWord or quotSolv or locSolv or locresSolv: 4493 coeffTable.update(ring.toPolyCoefficients(L[i]), 4494 ring.toPolyCoefficients(L[i+1]), 4495 ring.toPolyCoefficients(L[i+2]) ); 4496 else: 4497 #print "L[i], L[i+1], L[i+2]: " + str(L[i]) + ", " + str(L[i+1]) + ", " + str(L[i+2]); 4498 table.update( L[i], L[i+1], L[i+2] ); 4499 if locresSolv or locSolv or quotSolv or resSolv or resWord: 4500 #print "ring.polCoeff.table " + str(ring.polCoeff.table.toScript()); 4501 ring.polCoeff.table.update( ring.toPolyCoefficients(L[i]), 4502 ring.toPolyCoefficients(L[i+1]), 4503 ring.toPolyCoefficients(L[i+2]) ); 4504 4505 self.ring = ring; 4506 SolvableRing.__init__(self,ring=self.ring)
4507
4508 - def __str__(self):
4509 '''Create a string representation. 4510 ''' 4511 return self.ring.toScript();
4512 4513
4514 -class EF:
4515 '''Extension field builder. 4516 4517 Construction of extension field towers according to the builder pattern. 4518 ''' 4519
4520 - def __init__(self,base):
4521 '''Constructor to set base field. 4522 ''' 4523 if isinstance(base,RingElem): 4524 #factory = base.elem; 4525 factory = base.ring; 4526 else: 4527 factory = base; 4528 try: 4529 factory = self.factory.factory(); 4530 except: 4531 pass 4532 print "extension field factory: " + factory.toScript(); # + " :: " + factory.toString(); 4533 #print "d type(%s) = %s" % (factory,type(factory)); 4534 if isinstance(factory,ExtensionFieldBuilder): 4535 self.builder = factory; 4536 else: 4537 self.builder = ExtensionFieldBuilder(factory);
4538
4539 - def __str__(self):
4540 '''Create a string representation. 4541 ''' 4542 return str(self.builder.toScript());
4543
4544 - def extend(self,vars,algebraic=None):
4545 '''Create an extension field. 4546 4547 If algebraic is given as string expression, then an algebraic 4548 extension field is constructed, else a transcendental 4549 extension field is constructed. 4550 ''' 4551 if algebraic == None: 4552 ef = self.builder.transcendentExtension(vars); 4553 else: 4554 if isinstance(algebraic,PyInteger) or isinstance(algebraic,PyLong): 4555 ef = self.builder.finiteFieldExtension(algebraic); 4556 else: 4557 ef = self.builder.algebraicExtension(vars,algebraic); 4558 return EF(ef.build());
4559
4560 - def realExtend(self,vars,algebraic,interval):
4561 '''Create a real extension field. 4562 4563 Construct a real algebraic extension field with an isolating interval for a real root. 4564 ''' 4565 ef = self.builder.realAlgebraicExtension(vars,algebraic,interval); 4566 return EF(ef.build());
4567
4568 - def complexExtend(self,vars,algebraic,rectangle):
4569 '''Create a complex extension field. 4570 4571 Construct a complex algebraic extension field with an isolating rectangle for a complex root. 4572 ''' 4573 ef = self.builder.complexAlgebraicExtension(vars,algebraic,rectangle); 4574 return EF(ef.build());
4575
4576 - def polynomial(self,vars):
4577 '''Create a polynomial ring extension. 4578 ''' 4579 ef = self.builder.polynomialExtension(vars); 4580 return EF(ef.build());
4581
4582 - def matrix(self,n):
4583 '''Create a matrix ring extension. 4584 ''' 4585 ef = self.builder.matrixExtension(n); 4586 return EF(ef.build());
4587
4588 - def build(self):
4589 '''Get extension field tower. 4590 4591 ''' 4592 rf = self.builder.build(); 4593 if isinstance(rf,GenPolynomialRing): 4594 return PolyRing(rf.coFac,rf.getVars(),rf.tord); 4595 else: 4596 return Ring("", rf);
4597 4598 4599 #------------------------------------ 4600
4601 -class ExtRing(Ring):
4602 '''Represents a JAS exterior form / vector / polynomial ring: GenExteriorPolynomialRing. 4603 4604 ''' 4605
4606 - def __init__(self,ringstr="",ring=None):
4607 '''Exterior vector / polynomial ring constructor. 4608 ''' 4609 print "ExtRing: "; 4610 if ring == None: 4611 #raise ValueError, "parse of exterior polynomials not implemented" 4612 sr = StringReader( ringstr ); 4613 tok = RingFactoryTokenizer(sr); 4614 pfac = tok.nextPolynomialRing(); 4615 wfac = GenExteriorPolynomialRing(pfac); 4616 list = tok.nextExteriorPolynomialList(wfac); 4617 self.ring = wfac; 4618 else: 4619 if isinstance(ring,Ring): 4620 self.ring = ring.ring; 4621 else: 4622 self.ring = ring; 4623 Ring.__init__(self,ring=self.ring)
4624
4625 - def __str__(self):
4626 '''Create a string representation. 4627 ''' 4628 return str(self.ring.toScript());
4629 4630 # def ideal(self,ringstr="",list=None): 4631 # '''Create a word ideal. 4632 # ''' 4633 # return ExtPolyIdeal(self,ringstr,list); 4634
4635 - def one(self):
4636 '''Get the one of the exterior vector / polynomial ring. 4637 ''' 4638 return RingElem( self.ring.getONE() );
4639
4640 - def zero(self):
4641 '''Get the zero of the exterior vector / polynomial ring. 4642 ''' 4643 return RingElem( self.ring.getZERO() );
4644
4645 - def random(self,n):
4646 '''Get a random exterior vector / polynomial. 4647 ''' 4648 return RingElem( self.ring.random(n) );
4649
4650 - def random(self,k,l,d):
4651 '''Get a random exterior vector / polynomial. 4652 ''' 4653 return RingElem( self.ring.random(k,l,d) );
4654
4655 - def element(self,poly):
4656 '''Create an element from a string or object. 4657 ''' 4658 if not isinstance(poly,str): 4659 try: 4660 if self.ring == poly.ring: 4661 return RingElem(poly); 4662 except Exception, e: 4663 pass 4664 poly = str(poly); 4665 sr = StringReader(poly); 4666 top = GenPolynomialTokenizer(sr); 4667 ep = tok.nextExteriorPolynomial(self.ring); 4668 if ep != None: 4669 return RingElem( ep );
4670 4671
4672 -class ExtPolyRing(ExtRing):
4673 '''Represents a JAS exterior form / vector / polynomial ring: GenExteriorPolynomialRing. 4674 4675 Provides more convenient constructor. 4676 Then returns a Ring. 4677 ''' 4678
4679 - def __init__(self,coeff,s,var="E"):
4680 '''Ring constructor. 4681 4682 coeff = factory for coefficients, 4683 s = size of index list, 4684 var = string with one variable name. 4685 ''' 4686 print "ExtPolyRing: "; 4687 if coeff == None: 4688 raise ValueError, "No coefficient given." 4689 cf = coeff; 4690 if isinstance(coeff,RingElem): 4691 cf = coeff.elem.factory(); 4692 if isinstance(coeff,Ring): 4693 cf = coeff.ring; 4694 if s == None: 4695 raise ValueError, "No variable size given." 4696 names = var; 4697 if not isinstance(var,PyString): 4698 names = GenPolynomialTokenizer.variableList(var); 4699 wf = IndexFactory(s, names); 4700 ring = GenExteriorPolynomialRing(cf,wf); 4701 self.ring = ring; 4702 Ring.__init__(self,ring=self.ring)
4703
4704 - def __str__(self):
4705 '''Create a string representation. 4706 ''' 4707 return self.ring.toScript();
4708 4709 4710 #------------------------------------ 4711 4712
4713 -class WordRing(Ring):
4714 '''Represents a JAS free non-commutative polynomial ring: GenWordPolynomialRing. 4715 4716 Has a method to create word ideals. 4717 <b>Note:</b> watch your step: check that jython does not reorder multiplication. 4718 ''' 4719
4720 - def __init__(self,ringstr="",ring=None):
4721 '''Word polynomial ring constructor. 4722 ''' 4723 if ring == None: 4724 #raise ValueError, "parse of word polynomials not implemented" 4725 sr = StringReader( ringstr ); 4726 tok = RingFactoryTokenizer(sr); 4727 pfac = tok.nextPolynomialRing(); 4728 wfac = GenWordPolynomialRing(pfac); 4729 #list = tok.nextWordPolynomialList(wfac); 4730 self.ring = wfac; 4731 else: 4732 if isinstance(ring,Ring): 4733 self.ring = ring.ring; 4734 else: 4735 self.ring = ring;
4736
4737 - def __str__(self):
4738 '''Create a string representation. 4739 ''' 4740 return str(self.ring.toScript());
4741
4742 - def ideal(self,ringstr="",list=None):
4743 '''Create a word ideal. 4744 ''' 4745 return WordPolyIdeal(self,ringstr,list);
4746
4747 - def one(self):
4748 '''Get the one of the word polynomial ring. 4749 ''' 4750 return RingElem( self.ring.getONE() );
4751
4752 - def zero(self):
4753 '''Get the zero of the word polynomial ring. 4754 ''' 4755 return RingElem( self.ring.getZERO() );
4756
4757 - def random(self,n):
4758 '''Get a random word polynomial. 4759 ''' 4760 return RingElem( self.ring.random(n) );
4761
4762 - def random(self,k,l,d):
4763 '''Get a random word polynomial. 4764 ''' 4765 return RingElem( self.ring.random(k,l,d) );
4766
4767 - def element(self,poly):
4768 '''Create an element from a string or object. 4769 ''' 4770 if not isinstance(poly,str): 4771 try: 4772 if self.ring == poly.ring: 4773 return RingElem(poly); 4774 except Exception, e: 4775 pass 4776 poly = str(poly); 4777 I = WordPolyIdeal(self, "( " + poly + " )"); # should work 4778 list = I.list; 4779 if len(list) > 0: 4780 return RingElem( list[0] );
4781 4782
4783 -class WordPolyRing(WordRing):
4784 '''Represents a JAS free non-commutative polynomial ring: GenWordPolynomialRing. 4785 4786 Provides more convenient constructor. 4787 Then returns a Ring. 4788 <b>Note:</b> watch your step: check that jython does not reorder multiplication. 4789 ''' 4790
4791 - def __init__(self,coeff,vars):
4792 '''Ring constructor. 4793 4794 coeff = factory for coefficients, 4795 vars = string with variable names. 4796 ''' 4797 if coeff == None: 4798 raise ValueError, "No coefficient given." 4799 cf = coeff; 4800 if isinstance(coeff,RingElem): 4801 cf = coeff.elem.factory(); 4802 if isinstance(coeff,Ring): 4803 cf = coeff.ring; 4804 if vars == None: 4805 raise ValueError, "No variable names given." 4806 names = vars; 4807 if isinstance(vars,PyString): 4808 names = GenPolynomialTokenizer.variableList(vars); 4809 wf = WordFactory(names); 4810 ring = GenWordPolynomialRing(cf,wf); 4811 self.ring = ring;
4812
4813 - def __str__(self):
4814 '''Create a string representation. 4815 ''' 4816 return self.ring.toScript();
4817 4818
4819 -class WordPolyIdeal:
4820 '''Represents a JAS word polynomial ideal. 4821 4822 Methods for two-sided Groebner basees and others. 4823 <b>Note:</b> watch your step: check that jython does not reorder multiplication. 4824 ''' 4825
4826 - def __init__(self,ring,ringstr="",list=None):
4827 '''Constructor for an ideal in a word polynomial ring. 4828 ''' 4829 self.ring = ring; 4830 if list == None: 4831 #raise ValueError, "parse of word polynomials not implemented" 4832 sr = StringReader( ringstr ); 4833 tok = GenPolynomialTokenizer(sr); 4834 self.list = tok.nextWordPolynomialList(ring.ring); 4835 else: 4836 if isinstance(list,WordPolyIdeal): 4837 self.list = list.list; 4838 self.ideal = list; 4839 else: 4840 self.list = pylist2arraylist(list,rec=1); 4841 self.ideal = jas.application.WordIdeal(ring.ring, self.list);
4842
4843 - def __str__(self):
4844 '''Create a string representation. 4845 ''' 4846 #ll = [ e.toScript() for e in self.list ] 4847 #return "( " + ", ".join(ll) + " )"; 4848 return self.ideal.toScript();
4849
4850 - def GB(self):
4851 '''Compute a two-sided Groebner base. 4852 ''' 4853 return self.twosidedGB();
4854
4855 - def twosidedGB(self):
4856 '''Compute a two-sided Groebner base. 4857 ''' 4858 cofac = self.ring.ring.coFac; 4859 F = self.ideal.list; 4860 kind = ""; 4861 t = System.currentTimeMillis(); 4862 if cofac.isField() or not cofac.isCommutative(): 4863 G = self.ideal.GB(); 4864 kind = "field|nocom" 4865 else: 4866 if isinstance(cofac,GenPolynomialRing): # and cofac.isCommutative(): 4867 G = WordGroebnerBasePseudoRecSeq(cofac).GB(F); 4868 self.ideal = WordIdeal(self.ring.ring, G); 4869 kind = "pseudorec" 4870 else: 4871 G = WordGroebnerBasePseudoSeq(cofac).GB(F); 4872 self.ideal = WordIdeal(self.ring.ring, G); 4873 kind = "pseudo" 4874 t = System.currentTimeMillis() - t; 4875 print "sequential(%s) twosidedGB executed in %s ms" % (kind, t); 4876 return self;
4877
4878 - def isGB(self):
4879 '''Test if this is a two-sided Groebner base. 4880 ''' 4881 return self.isTwosidedGB();
4882
4883 - def isTwosidedGB(self):
4884 '''Test if this is a two-sided Groebner base. 4885 ''' 4886 cofac = self.ring.ring.coFac; 4887 F = self.ideal.list; 4888 kind = ""; 4889 t = System.currentTimeMillis(); 4890 if cofac.isField() or not cofac.isCommutative(): 4891 b = self.ideal.isGB(); 4892 kind = "field|nocom" 4893 else: 4894 if isinstance(cofac,GenPolynomialRing): 4895 b = WordGroebnerBasePseudoRecSeq(cofac).isGB(F); 4896 kind = "pseudorec" 4897 else: 4898 b = WordGroebnerBasePseudoSeq(cofac).isGB(F); 4899 kind = "pseudo" 4900 t = System.currentTimeMillis() - t; 4901 print "sequential(%s) isTwosidedGB executed in %s ms" % (kind, t); 4902 return b;
4903
4904 - def __cmp__(self, other):
4905 '''Compare two ideals. 4906 ''' 4907 if not isinstance(other, WordPolyIdeal): 4908 return False; 4909 s = self.ideal; 4910 t = other.ideal; 4911 return s.compareTo(t);
4912
4913 - def __eq__(self,other):
4914 '''Test if two ideals are equal. 4915 ''' 4916 if not isinstance(other, WordPolyIdeal): 4917 return False; 4918 s = self.ideal; 4919 t = other.ideal; 4920 return s.equals(t)
4921
4922 - def sum(self,other):
4923 '''Compute the sum of this and the other ideal. 4924 ''' 4925 s = self.ideal; 4926 t = other.ideal; 4927 N = s.sum(t); 4928 return WordIdeal(self.ring, "", N);
4929 4930
4931 -def WRC(ideal,r=0):
4932 '''Create JAS polynomial WordResidue as ring element. 4933 ''' 4934 #print "ideal = " + str(ideal); 4935 if ideal == None: # does not work 4936 print "ideal = " + str(ideal); 4937 if False: 4938 raise ValueError, "No ideal given." 4939 if isinstance(ideal,WordPolyIdeal): 4940 ideal = ideal.ideal 4941 #ideal.doGB(); 4942 if not isinstance(ideal,WordIdeal): 4943 raise ValueError, "No ideal given." 4944 #print "ideal.getList().get(0).ring.ideal = %s" % ideal.getList().get(0).ring.ideal; 4945 if isinstance(ideal.getList().get(0).ring,WordResidueRing): 4946 rc = WordResidueRing( ideal.getList().get(0).ring.ideal ); 4947 else: 4948 rc = WordResidueRing(ideal); 4949 if isinstance(r,RingElem): 4950 r = r.elem; 4951 if r == 0: 4952 r = WordResidue(rc); 4953 else: 4954 r = WordResidue(rc,r); 4955 return RingElem(r);
4956 4957 4958 # doctest: 4959 if __name__ == '__main__': 4960 import doctest, sys 4961 doctest.testmod(sys.modules[__name__]) 4962