def zero(self): '''Zero element of this ring. ''' if self.isFactory(): return RingElem( self.elem.getZERO() ); else: return RingElem( self.elem.ring.getZERO() );
def one(self): '''One element of this ring. ''' if self.isFactory(): return RingElem( self.elem.getONE() ); else: return RingElem( self.elem.ring.getONE() );
def element(self,polystr): '''Create an element from a string. ''' I = Ideal(self, "( " + polystr + " )"); list = I.pset.list; if len(list) > 0: return RingElem( list[0] );
def CC(re=BigRational(),im=BigRational()): '''Create JAS BigComplex as ring element. ''' if isinstance(re,PyTuple) or isinstance(re,PyList): if isinstance(re[0],PyTuple) or isinstance(re[0],PyList): if len(re) > 1: im = QQ( re[1] ); re = QQ( re[0] ); else: re = QQ(re); # re = makeJasArith( re ); if isinstance(im,PyTuple) or isinstance(im,PyList): im = QQ( im ); # im = makeJasArith( im ); if isinstance(re,RingElem): re = re.elem; if isinstance(im,RingElem): im = im.elem; if im.isZERO(): if re.isZERO(): c = BigComplex(); else: c = BigComplex(re); else: c = BigComplex(re,im); return RingElem(c);
def random(self,k=5,l=7,d=3,q=0.3): '''Get a random polynomial. ''' r = self.ring.random(k,l,d,q); if self.ring.coFac.isField(): r = r.monic(); return RingElem( r );
def fixPoint(self,psmap): '''Create a power series as fixed point of the given mapping. psmap must implement the PowerSeriesMap interface. ''' ps = self.ring.fixPoint( psmap ); return RingElem( ps );
def differentiate(self): '''Differentiate a power series. ''' try: e = self.elem.differentiate(); except: e = None; return RingElem( e );
def gcd(self,a,b): '''Compute the greatest common divisor of a and b. ''' if isinstance(a,RingElem): a = a.elem; if isinstance(b,RingElem): b = b.elem; return RingElem( a.gcd(b) );
def coerce(self,other): '''Coerce other to self. ''' #print "self type(%s) = %s" % (self,type(self)); #print "other type(%s) = %s" % (other,type(other)); if isinstance(other,RingElem): if self.isPolynomial() and not other.isPolynomial(): o = self.ring.parse( str(other) ); return RingElem( o ); return other; #print "--1"; if isinstance(other,PyTuple) or isinstance(other,PyList): # assume BigRational or BigComplex # assume self will be compatible with them. todo: check this o = makeJasArith(other); return RingElem(o); #print "--2"; # test if self.elem is a factory itself if self.isFactory(): if isinstance(other,PyInteger) or isinstance(other,PyLong): o = self.elem.fromInteger(other); else: if isinstance(other,PyFloat): # ?? what to do ?? o = self.elem.fromInteger( int(other) ); else: print "unknown other type(%s) = %s" % (other,type(other)); o = other; return RingElem(o); #print "--3"; # self.elem has a ring factory if isinstance(other,PyInteger) or isinstance(other,PyLong): o = self.elem.ring.fromInteger(other); else: if isinstance(other,PyFloat): # ?? what to do ?? o = self.elem.ring.fromInteger( int(other) ); else: print "unknown other type(%s) = %s" % (other,type(other)); o = other; #print "--4"; return RingElem(o);
def DD(d=0): '''Create JAS BigDecimal as ring element. ''' if isinstance(d,RingElem): d = d.elem; if isinstance(d,PyFloat): d = str(d); #print "d type(%s) = %s" % (d,type(d)); if d == 0: r = BigDecimal(); else: r = BigDecimal(d); return RingElem(r);
def gcd(self,a,b): '''Compute the greatest common divisor of a and b. ''' if isinstance(a,RingElem): a = a.elem; else: a = self.element( str(a) ); a = a.elem; if isinstance(b,RingElem): b = b.elem; else: b = self.element( str(b) ); b = b.elem; return RingElem( self.engine.gcd(a,b) );
def integrate(self,a): '''Integrate a power series with constant a. ''' #print "self type(%s) = %s" % (self,type(self)); #print "a type(%s) = %s" % (a,type(a)); x = None; if isinstance(a,RingElem): x = a.elem; if isinstance(a,PyTuple) or isinstance(a,PyList): # assume BigRational or BigComplex # assume self will be compatible with them. todo: check this x = makeJasArith(a); try: e = self.elem.integrate(x); except: e = None; return RingElem( e );
def RF(d,n=1): '''Create JAS rational function Quotient as ring element. ''' if isinstance(d,PyTuple) or isinstance(d,PyList): if n != 1: print "%s ignored" % n; if len(d) > 1: n = d[1]; d = d[0]; if isinstance(d,RingElem): d = d.elem; if isinstance(n,RingElem): n = n.elem; qr = QuotientRing(d.ring); if n == 1: r = Quotient(qr,d); else: r = Quotient(qr,d,n); return RingElem(r);
def __pow__(self,other): '''Power of this to other. ''' #print "self type(%s) = %s" % (self,type(self)); #print "pow other type(%s) = %s" % (other,type(other)); if isinstance(other,PyInteger): n = other; else: if isinstance(other,RingElem): n = other.elem; if isinstance(n,BigRational): # does not work n = n.numerator().intValue(); if isinstance(n,BigInteger): # does not work n = n.intValue(); if self.isFactory(): p = Power(self.elem).power( self.elem, n ); else: p = Power(self.elem.ring).power( self.elem, n ); return RingElem( p );
def create(self,ifunc=None,jfunc=None,clazz=None): '''Create a power series with given generating function. ifunc(int i) must return a value which is used in RingFactory.fromInteger(). jfunc(int i) must return a value of type ring.coFac. clazz must implement the Coefficients abstract class. ''' class coeff( Coefficients ): def __init__(self,cofac): self.coFac = cofac; def generate(self,i): if jfunc == None: return self.coFac.fromInteger( ifunc(i) ); else: return jfunc(i); if clazz == None: ps = UnivPowerSeries( self.ring, coeff(self.ring.coFac) ); else: ps = UnivPowerSeries( self.ring, clazz ); return RingElem( ps );
def QQ(d=0,n=1): '''Create JAS BigRational as ring element. ''' if isinstance(d,PyTuple) or isinstance(d,PyList): if n != 1: print "%s ignored" % n; if len(d) > 1: n = d[1]; d = d[0]; if isinstance(d,RingElem): d = d.elem; if isinstance(n,RingElem): n = n.elem; if n == 1: if d == 0: r = BigRational(); else: r = BigRational(d); else: r = BigRational(d,n); return RingElem(r);
def gens(self): '''Get list of generators of the polynomial ring. ''' L = self.ring.univariateList(); c = self.ring.coFac; nv = None; try: nv = c.nvar; except: pass #print "type(coFac) = ", type(self.ring.coFac); #if isinstance(c,GenPolynomial): # does not work if nv: Lp = c.univariateList(); #Ls = [ GenPolynomial(self.ring,l) for l in Lp ]; i = 0; for l in Lp: L.add( i, GenPolynomial(self.ring,l) ); i += 1; N = [ RingElem(e) for e in L ]; return N;
def tan(self): '''Get the tangens power series. ''' return RingElem( self.ring.getTAN() );
def cos(self): '''Get the cosinus power series. ''' return RingElem( self.ring.getCOS() );
def sin(self): '''Get the sinus power series. ''' return RingElem( self.ring.getSIN() );
def exp(self): '''Get the exponential power series. ''' return RingElem( self.ring.getEXP() );
def random(self,n): '''Get a random power series. ''' return RingElem( self.ring.random(n) );
def zero(self): '''Get the zero of the power series ring. ''' return RingElem( self.ring.getZERO() );
def one(self): '''Get the one of the power series ring. ''' return RingElem( self.ring.getONE() );
def gens(self): '''Get the generator of the power series ring. ''' return RingElem( self.ring.getONE().shift(1) );
def zero(self): '''Get the zero of the solvable polynomial ring. ''' return RingElem( self.ring.getZERO() );
def one(self): '''Get the one of the solvable polynomial ring. ''' return RingElem( self.ring.getONE() );
def __sub__(self,other): '''Subtract two ring elements. ''' [s,o] = coercePair(self,other); return RingElem( s.elem.subtract( o.elem ) );
def __div__(self,other): '''Divide two ring elements. ''' [s,o] = coercePair(self,other); return RingElem( s.elem.divide( o.elem ) );
def __mod__(self,other): '''Modular remainder of two ring elements. ''' [s,o] = coercePair(self,other); return RingElem( s.elem.remainder( o.elem ) );