Ask Your Question

Formal determinant of symbolic matrix

asked 2019-05-22 16:32:20 +0200

anonymous user


I have some sparse symbolic matrices, and want to compute their formal determinant (without cancellation of terms). In other words, if I have the matrix

x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])

I would like the result of


to be xy - xy, rather than just 0. The variables in each monomial are allowed to commute with each other, but on the other hand I would like all monomials containing a 0 to vanish (i.e if in the example above M = Matrix(SR, [[x,0],[x,y]])), then the determinant should be just xy, rather than xy - 0*x). Is there a way to achieve this (without using the expansion of the determinant as permutations, since the dimension of the matrices gets quite big!)? Thanks in advance!

edit retag flag offensive close merge delete


You might be interested by this tutorial : the quaternions are a good example of a non-commutative ring. Matrices of such elements would have the properties you seek.

You may find inspiration in this free textbook and its Sagemath supplement...

Also, ISTR that Maxima allows you to define non-commutating sets of variables ; however, I have ni way to dive in its doc now, and I can't remember if it allows to define matrices of such elements...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-05-22 17:50:58 +0200 )edit

the properties asked do not seem to be related to the non-commutativity of variables if I guess correctly

danieleC gravatar imagedanieleC ( 2019-05-22 22:29:15 +0200 )edit

DanieleC : I agree. I misread/misunderstood the whishes of the anonymous OP. I'm not sure it makes sense...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-05-22 22:38:58 +0200 )edit

the question seems something like: how many nonzero terms are there in the determinant, if we don't allow for cancellations?

danieleC gravatar imagedanieleC ( 2019-05-22 22:55:45 +0200 )edit

I do not know how suitable this is for your needs, but my idea here would be to use several distinct variables x1, x2, ....

B r u n o gravatar imageB r u n o ( 2019-05-28 18:15:16 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-05-22 22:58:31 +0200

rburing gravatar image

updated 2019-05-22 23:29:44 +0200

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):
            x_iter = iter(x)
            self.x = list(x_iter)
            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()

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

edit flag offensive delete link more


Easier would be to implement the determinant separately, for matrices with ordinary entries, but as a formal sum.

rburing gravatar imagerburing ( 2019-05-23 00:05:02 +0200 )edit

answered 2019-05-22 22:09:09 +0200

nbruin gravatar image

I'm not so sure that xy-xy can ever be non-zero and still provide arithmetic in which linear algebra operations make sense. For non-commuting variables, one can get at least something:

sage: A.<x,y>=FreeAlgebra(QQ)
sage: M=matrix([[x,y],[y*x,y^2]])
sage: M
[  x   y]
[y*x y^2]
sage: M.determinant()
x*y^2 - y*x*y
edit flag offensive delete link more


I had a similar problem: one needs to keep track of the non-zero monomials appearing, so if there's cancellations it does not work

danieleC gravatar imagedanieleC ( 2019-05-22 22:27:53 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-05-22 16:32:20 +0200

Seen: 1,478 times

Last updated: May 22 '19