Reputation: 207
Thank you in advance and sorry for the bad English! sympy determinant 3x3 a11 ---> a[1,1]
from sympy import *
var('a11 a12 a13')
var('a21 a22 a23')
var('a31 a32 a33')
print("#",det(Matrix([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]])))
# a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31
how convert?
from sympy import *
var('a')
# ?????
# ?????
print("#",det(Matrix([[a[1,1], a[1,2], a[1,3]], [a[2,1], a[2,2], a[2,3]], [a[3,1], a[3,2], a[3,3]]])))
# I want
# a[1,1]*a[2,2]*a[3,3] - a[1,1]*a[2,3]*a[3,2] - a[1,2]*a[2,1]*a[3,3] + a[1,2]*a[2,3]*a[3,1] + a[1,3]*a[2,1]*a[3,2] - a[1,3]*a[2,2]*a[3,1]
# TypeError: 'Symbol' object is not subscriptable
Upvotes: 0
Views: 78
Reputation: 207
from sympy import *
a = MatrixSymbol('a', 3, 3)
print("#",type(a))
print("#",a)
b=a.as_explicit()
print("#",type(b))
print("#",b)
c=det(a.as_explicit())
print("#",type(c))
print("#",c)
# <class 'sympy.matrices.expressions.matexpr.MatrixSymbol'>
# a
# <class 'sympy.matrices.immutable.ImmutableDenseMatrix'>
# Matrix([[a[0, 0], a[0, 1], a[0, 2]], [a[1, 0], a[1, 1], a[1, 2]], [a[2, 0], a[2, 1], a[2, 2]]])
# <class 'sympy.core.add.Add'>
# a[0, 0]*a[1, 1]*a[2, 2] - a[0, 0]*a[1, 2]*a[2, 1] - a[0, 1]*a[1, 0]*a[2, 2] + a[0, 1]*a[1, 2]*a[2, 0] + a[0, 2]*a[1, 0]*a[2, 1] - a[0, 2]*a[1, 1]*a[2, 0]
Upvotes: 0
Reputation: 14500
You should use MatrixSymbol
if you want a symbol representing a matrix:
In [40]: a = MatrixSymbol('a', 3, 3)
In [41]: a
Out[41]: a
In [42]: a[0, 0]
Out[42]: a₀₀
In [43]: a.as_explicit()
Out[43]:
⎡a₀₀ a₀₁ a₀₂⎤
⎢ ⎥
⎢a₁₀ a₁₁ a₁₂⎥
⎢ ⎥
⎣a₂₀ a₂₁ a₂₂⎦
In [44]: det(a.as_explicit())
Out[44]: a₀₀⋅a₁₁⋅a₂₂ - a₀₀⋅a₁₂⋅a₂₁ - a₀₁⋅a₁₀⋅a₂₂ + a₀₁⋅a₁₂⋅a₂₀ + a₀₂⋅a₁₀⋅a₂₁ - a₀₂⋅a₁₁⋅a₂₀
https://docs.sympy.org/latest/modules/matrices/expressions.html
Upvotes: 2