Ask Your Question

Revision history [back]

You can implement your own "ring" as described in How to implement new algebraic structures in Sage.

Here's a start:

class MyFormalSum(sage.structure.element.Element):
    def __init__(self, parent, x):
        if isinstance(x,list):
            self.x = x
        else:
            self.x = [(1,x)]
        # simplify:
        self.x = [(a,b) for (a,b) in self.x if not (a == 0 or b == 0)]
    def _repr_(self):
        return " + ".join('%s*%s' % (a,b) for (a,b) in self.x)
    def _add_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + other.x)
    def _sub_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + [(-a,b) for (a,b) in other.x])
    def _mul_(self, other):
        C = self.__class__
        import itertools
        return C(self.parent(), [(a*c, b*d) for ((a,b),(c,d)) in itertools.product(self.x, other.x)])

class MyFormalSums(sage.structure.unique_representation.UniqueRepresentation, Ring):
    Element = MyFormalSum
    def __init__(self, base):
        Ring.__init__(self, base)
    def _repr_(self):
        return "MyFormalSums(%s)"%repr(self.base())
    def base_ring(self):
        return self.base().base_ring()
    def characteristic(self):
        return self.base().characteristic()

Now you can do:

sage: R = MyFormalSums(QQ)
sage: var('x,y')
sage: Matrix(R, [[x,y], [x,y]]).determinant()
1*x*y + -1*x*y
sage: Matrix(R, [[x,0],[x,y]]).determinant()
1*x*y

Note that this is a hack because the thing is not actually a ring, e.g. TestSuite(R).run() fails.

You can implement your own "ring" as described in How to implement new algebraic structures in Sage.

Here's a start:

class MyFormalSum(sage.structure.element.Element):
    def __init__(self, parent, x):
        if isinstance(x,list):
            self.x = x
        else:
            self.x = [(1,x)]
        # simplify:
        self.x = [(a,b) for (a,b) in self.x if not (a == 0 or b == 0)]
        sage.structure.element.Element.__init__(self, parent)
    def _repr_(self):
        return " + ".join('%s*%s' % (a,b) for (a,b) in self.x)
    def _add_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + other.x)
    def _sub_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + [(-a,b) for (a,b) in other.x])
    def _mul_(self, other):
        C = self.__class__
        import itertools
        return C(self.parent(), [(a*c, b*d) for ((a,b),(c,d)) in itertools.product(self.x, other.x)])

class MyFormalSums(sage.structure.unique_representation.UniqueRepresentation, Ring):
    Element = MyFormalSum
    def __init__(self, base):
        Ring.__init__(self, base)
    def _repr_(self):
        return "MyFormalSums(%s)"%repr(self.base())
    def base_ring(self):
        return self.base().base_ring()
    def characteristic(self):
        return self.base().characteristic()

Now you can do:

sage: R = MyFormalSums(QQ)
sage: var('x,y')
sage: Matrix(R, [[x,y], [x,y]]).determinant()
1*x*y + -1*x*y
sage: Matrix(R, [[x,0],[x,y]]).determinant()
1*x*y

Note that this is a hack because the thing is not actually a ring, e.g. TestSuite(R).run() fails.

You can implement your own "ring" as described in How to implement new algebraic structures in Sage.

Here's a start:

class MyFormalSum(sage.structure.element.Element):
    def __init__(self, parent, x):
        if isinstance(x,list):
            self.x = x
        else:
try:
            x_iter = iter(x)
            self.x = list(x_iter)
        except:
            self.x = [(1,x)]
        # simplify:
        self.x = [(a,b) for (a,b) in self.x if not (a == 0 or b == 0)]
        sage.structure.element.Element.__init__(self, parent)
    def _repr_(self):
        return " + ".join('%s*%s' % (a,b) for (a,b) in self.x)
    def _add_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + other.x)
    def _sub_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + [(-a,b) for (a,b) in other.x])
    def _mul_(self, other):
        C = self.__class__
        import itertools
        C = self.__class__
        return C(self.parent(), [(a*c, b*d) for ((a,b),(c,d)) in itertools.product(self.x, other.x)])

class MyFormalSums(sage.structure.unique_representation.UniqueRepresentation, Ring):
    Element = MyFormalSum
    def __init__(self, base):
        Ring.__init__(self, base)
    def _repr_(self):
        return "MyFormalSums(%s)"%repr(self.base())
    def base_ring(self):
        return self.base().base_ring()
    def characteristic(self):
        return self.base().characteristic()

Now you can do:

sage: R = MyFormalSums(QQ)
R.<x,y> = PolynomialRing(QQ)
sage: var('x,y')
S = MyFormalSums(R)
sage: Matrix(R, Matrix(S, [[x,y], [x,y]]).determinant()
1*x*y + -1*x*y
sage: Matrix(R, [[x,0],[x,y]]).determinant()
Matrix(S, [[x,0],[x,y]]).determinant().parent()
1*x*y

Note that this is a hack because the thing is not actually a ring, e.g. TestSuite(R).run() fails.

You can implement your own "ring" as described in How to implement new algebraic structures in Sage.

Here's a start:

class MyFormalSum(sage.structure.element.Element):
    def __init__(self, parent, x):
        try:
            x_iter = iter(x)
            self.x = list(x_iter)
        except:
            self.x = [(1,x)]
        # simplify:
        self.x = [(a,b) for (a,b) in self.x if not (a == 0 or b == 0)]
        sage.structure.element.Element.__init__(self, parent)
    def _repr_(self):
        return " + ".join('%s*%s' % (a,b) for (a,b) in self.x)
    def _add_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + other.x)
    def _sub_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + [(-a,b) for (a,b) in other.x])
    def _mul_(self, other):
        import itertools
        C = self.__class__
        return C(self.parent(), [(a*c, b*d) for ((a,b),(c,d)) in itertools.product(self.x, other.x)])

class MyFormalSums(sage.structure.unique_representation.UniqueRepresentation, Ring):
    Element = MyFormalSum
    def __init__(self, base):
        Ring.__init__(self, base)
    def _repr_(self):
        return "MyFormalSums(%s)"%repr(self.base())
    def base_ring(self):
        return self.base().base_ring()
    def characteristic(self):
        return self.base().characteristic()

Now you can do:

sage: R.<x,y> = PolynomialRing(QQ)
sage: S = MyFormalSums(R)
sage: Matrix(S, [[x,y], [x,y]]).determinant()
1*x*y + -1*x*y
sage: Matrix(S, [[x,0],[x,y]]).determinant().parent()
1*x*y

Note that this is a hack because the thing is not actually a ring, e.g. TestSuite(R).run()TestSuite(S).run() fails.

You can implement your own "ring" as described in How to implement new algebraic structures in Sage.

Here's a start:

class MyFormalSum(sage.structure.element.Element):
    def __init__(self, parent, x):
        try:
            x_iter = iter(x)
            self.x = list(x_iter)
        except:
            self.x = [(1,x)]
        # simplify:
        self.x = [(a,b) for (a,b) in self.x if not (a == 0 or b == 0)]
        sage.structure.element.Element.__init__(self, parent)
    def _repr_(self):
        return " + ".join('%s*%s' % (a,b) for (a,b) in self.x)
    def _add_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + other.x)
    def _sub_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + [(-a,b) for (a,b) in other.x])
    def _mul_(self, other):
        import itertools
        C = self.__class__
        return C(self.parent(), [(a*c, b*d) for ((a,b),(c,d)) in itertools.product(self.x, other.x)])

class MyFormalSums(sage.structure.unique_representation.UniqueRepresentation, Ring):
    Element = MyFormalSum
    def __init__(self, base):
        Ring.__init__(self, base)
    def _repr_(self):
        return "MyFormalSums(%s)"%repr(self.base())
    def base_ring(self):
        return self.base().base_ring()
    def characteristic(self):
        return self.base().characteristic()

Now you can do:

sage: R.<x,y> = PolynomialRing(QQ)
sage: S = MyFormalSums(R)
MyFormalSums(QQ)
sage: Matrix(S, [[x,y], [x,y]]).determinant()
1*x*y + -1*x*y
sage: Matrix(S, [[x,0],[x,y]]).determinant().parent()
1*x*y

Note that this is a hack because the thing is not actually a ring, e.g. TestSuite(S).run() fails.

You can implement your own "ring" as described in How to implement new algebraic structures in Sage.

Here's a start:

class MyFormalSum(sage.structure.element.Element):
    def __init__(self, parent, x):
        try:
            x_iter = iter(x)
            self.x = list(x_iter)
        except:
            self.x = [(1,x)]
        # simplify:
        self.x = [(a,b) for (a,b) in self.x if not (a == 0 or b == 0)]
        sage.structure.element.Element.__init__(self, parent)
    def _repr_(self):
        return " + ".join('%s*%s' % (a,b) for (a,b) in self.x)
    def _add_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + other.x)
    def _sub_(self, other):
        C = self.__class__
        return C(self.parent(), self.x + [(-a,b) for (a,b) in other.x])
    def _mul_(self, other):
        import itertools
        C = self.__class__
        return C(self.parent(), [(a*c, b*d) for ((a,b),(c,d)) in itertools.product(self.x, other.x)])

class MyFormalSums(sage.structure.unique_representation.UniqueRepresentation, Ring):
    Element = MyFormalSum
    def __init__(self, base):
        Ring.__init__(self, base)
    def _repr_(self):
        return "MyFormalSums(%s)"%repr(self.base())
    def base_ring(self):
        return self.base().base_ring()
    def characteristic(self):
        return self.base().characteristic()

Now you can do:

sage: R.<x,y> = PolynomialRing(QQ)
sage: S = MyFormalSums(QQ)
sage: Matrix(S, [[x,y], [x,y]]).determinant()
1*x*y + -1*x*y
sage: Matrix(S, [[x,0],[x,y]]).determinant().parent()
[[x,0],[x,y]]).determinant()
1*x*y

Note that this is a hack because the thing is not actually a ring, e.g. TestSuite(S).run() fails.