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