Reputation: 111
I have given a sympy matrix, so a matrix consisting of real values and symbolic values and I want to calculate the product of diagonal entries of the matrix; An example:
import numpy as np
import sympy as sp
from sympy import *
from sympy.matrices import Matrix
A=sp.Matrix([[1,0,1],[2,3,1],[y,5,x]])
Now the desired result would be 3x; Of course I could do that with a for loop but is there some other, cleaner solution?
Upvotes: 0
Views: 240
Reputation: 140
Oscar Benjamin provided a slick solution. This will work as well:
np.product(np.diag(A))
Upvotes: 0
Reputation: 14470
You can use diagonal
to get the diagonal elements and prod
to multiply them:
In [46]: A.diagonal()
Out[46]: [1 3 x]
In [47]: prod(_)
Out[47]: 3⋅x
Upvotes: 1