# Formal determinant of symbolic matrix

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

M.determinant()


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 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...

( 2019-05-22 10:50:58 -0500 )edit
1

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

( 2019-05-22 15:29:15 -0500 )edit

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

( 2019-05-22 15:38:58 -0500 )edit

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

( 2019-05-22 15:55:45 -0500 )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, ....

( 2019-05-28 11:15:16 -0500 )edit

Sort by » oldest newest most voted

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)
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()
1*x*y


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

more

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

( 2019-05-22 17:05:02 -0500 )edit

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

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

( 2019-05-22 15:27:53 -0500 )edit