ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 28 May 2019 18:15:16 +0200Formal determinant of symbolic matrixhttps://ask.sagemath.org/question/46624/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 x*y - x*y, 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 x*y, rather than x*y - 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!
Wed, 22 May 2019 16:32:20 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/Comment by Emmanuel Charpentier for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46625#post-id-46625You might be interested by [this tutorial](http://doc.sagemath.org/html/en/prep/Quickstarts/Abstract-Algebra.html) : 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](http://abstract.ups.edu/) and its Sagemath [supplement](http://abstract.ups.edu/download/aata-20111223-sage-4.8.pdf)...
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...Wed, 22 May 2019 17:50:58 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46625#post-id-46625Comment by danieleC for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46632#post-id-46632the properties asked do not seem to be related to the non-commutativity of variables if I guess correctlyWed, 22 May 2019 22:29:15 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46632#post-id-46632Comment by Emmanuel Charpentier for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46633#post-id-46633DanieleC : I agree. I misread/misunderstood the whishes of the anonymous OP. I'm not sure it makes sense...Wed, 22 May 2019 22:38:58 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46633#post-id-46633Comment by danieleC for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46634#post-id-46634the question seems something like: how many nonzero terms are there in the determinant, if we don't allow for cancellations?Wed, 22 May 2019 22:55:45 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46634#post-id-46634Comment by B r u n o for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46691#post-id-46691I do not know how suitable this is for your needs, but my idea here would be to use several distinct variables `x1, x2, ...`.Tue, 28 May 2019 18:15:16 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46691#post-id-46691Answer by nbruin for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?answer=46629#post-id-46629I'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
Wed, 22 May 2019 22:09:09 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?answer=46629#post-id-46629Comment by danieleC for <p>I'm not so sure that <code>xy-xy</code> 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:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46631#post-id-46631I had a similar problem: one needs to keep track of the non-zero monomials appearing, so if there's cancellations it does not workWed, 22 May 2019 22:27:53 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46631#post-id-46631Answer by rburing for <p>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 </p>
<pre><code>x,y = var('x,y')
M = Matrix(SR, [[x,y],[x,y]])
</code></pre>
<p>I would like the result of </p>
<pre><code>M.determinant()
</code></pre>
<p>to be x<em>y - x</em>y, 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 x<em>y, rather than x</em>y - 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!</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?answer=46636#post-id-46636You can implement your own "ring" as described in [
How to implement new algebraic structures in Sage](http://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html).
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()
1*x*y
Note that this is a hack because the thing is not actually a ring, e.g. `TestSuite(S).run()` fails.Wed, 22 May 2019 22:58:31 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?answer=46636#post-id-46636Comment by rburing for <p>You can implement your own "ring" as described in <a href="http://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html">
How to implement new algebraic structures in Sage</a>.</p>
<p>Here's a start:</p>
<pre><code>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()
</code></pre>
<p>Now you can do:</p>
<pre><code>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
</code></pre>
<p>Note that this is a hack because the thing is not actually a ring, e.g. <code>TestSuite(S).run()</code> fails.</p>
https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46638#post-id-46638Easier would be to implement the determinant separately, for matrices with ordinary entries, but as a formal sum.Thu, 23 May 2019 00:05:02 +0200https://ask.sagemath.org/question/46624/formal-determinant-of-symbolic-matrix/?comment=46638#post-id-46638