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

Source Code for Module jas

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