10

The following code generates a 3x3 matrix in terms of x,y,z. I want to generate the determinant of the matrix. But unable to do so.

import numpy as np
import sympy as sp
from sympy import *
from sympy.matrices import Matrix
x,y,z =sp.symbols('x,y,z')
H1=np.array([[x,y,z]])
H2=np.array([[0.0630,0.0314,-0.0001],[0.0314,96.1659,-0.0001],[-0.0001,-0.0001,0.0001]])
H3=H1.T
H=H1*H2*H3
print H

To find the determinant of the above matrix, I am using the following command.

H.det()

But it is showing error

AttributeError: 'numpy.ndarray' object has no attribute 'det' 
Chikorita Rai
  • 851
  • 5
  • 13
  • 17

2 Answers2

14

You first need to convert your numpy n-dimensional array to a sympy matrix, and then perform the calculation of the symbolic determinant. What @hildensia said won't work, because H is a numpy object, which won't work with symbolic entities.

>>> M = sp.Matrix(H)
>>> M
Matrix([
[ 0.063*x**2,   0.0314*x*y, -0.0001*x*z],
[ 0.0314*x*y, 96.1659*y**2, -0.0001*y*z],
[-0.0001*x*z,  -0.0001*y*z, 0.0001*z**2]])
>>> M.det()
0.000604784913*x**2*y**2*z**2
Oliver W.
  • 13,169
  • 3
  • 37
  • 50
  • Glad to hear. What about [your older sympy related questions](https://stackoverflow.com/search?q=user%3A4053821+[sympy]+hasaccepted%3Ano), are those solved too? – Oliver W. Nov 07 '14 at 11:56
  • Thanks for asking. I don't remember clearly enough to tell you whether they were addressed positively. If they were not, I would have reposted anyway. =D – Chikorita Rai Nov 07 '14 at 12:01
  • 1
    There's really no reason to use a numpy array in the first place if you have symbolic entries. – asmeurer Nov 15 '14 at 21:03
  • @asmeurer, That's true, but perhaps the OP has specific reasons for first performing some calculations in numpy (speed, result of a complex simulation, ..). That's not for me to comment on. – Oliver W. Nov 16 '14 at 00:13
0

The determinant in numpy is not a member of the array class but a function in the linalg module. So simply use:

np.linalg.det(H)
hildensia
  • 1,650
  • 2
  • 12
  • 24