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