Polynomial ring over self-defined coefficient ring
Is it possible to define an Univariate Polynomial Ring over a user-defined ring in sagemath?
Here is sample code (only an example, not my real use case):
class Rationals:
def __init__(self,nom,denom):
if (denom==0):
print("Error")
print(quit)
quit()
else:
divisor=gcd(nom,denom)
if (divisor!=0):
self.nom=nom/divisor
self.denom=denom/divisor
else:
self.nom=nom
self.denom=denom
def __add__(self,element2):
denom=self.denom*element2.denom
nom=self.denom*element2.nom+self.nom*element2.denom
divisor=gcd(nom,denom)
nom=nom/divisor
denom=denom/divisor
return Rationals(nom,denom)
def __mul__(self,element2):
nom=self.nom*element2.nom
denom=self.denom*element2.denom
divisor=gcd(nom,denom)
nom=nom/divisor
denom=denom/divisor
return Rationals(nom,denom)
Now if I type in RR = PolynomialRing(Rationals,'y') I get an error:
TypeError Traceback (most recent call last) <ipython-input-112-6f1923f0a0bd> in <module> ----> 1 RR = PolynomialRing(Rationals,'y')
/usr/lib/python3/dist-packages/sage/rings/polynomial/polynomial_ring_constructor.py in PolynomialRing(base_ring, args, *kwds) 553 """ 554 if not ring.is_Ring(base_ring): --> 555 raise TypeError("base_ring {!r} must be a ring".format(base_ring)) 556 557 n = -1 # Unknown number of variables
TypeError: base_ring <class '__main__.rationals'=""> must be a ring
Note that this is only an example, not my real use case. Of course, I could use QQ in sagemath, but I consider a more complex ring which does not exist in sagemath.
How do I tell sage that I am confident that my domain is actually a ring?
Can you give us a concrete example of such a "self-defined ring" ?
I think you should mimic the construction of a ring within Sage. Take an example from
sage/algebras/
orsage/rings/
and modify it to suit you. In particular, your class should probably inherit from a Sage class likeParent
, and the__init__
method should set the category to something involving rings or algebras.