w3resource

NumPy: Compute the determinant of an array

NumPy: Linear Algebra Exercise-11 with Solution

Write a NumPy program to compute the determinant of an array.

From Wikipedia: In linear algebra, the determinant is a value that can be computed from the elements of a square matrix. The determinant of a matrix A is denoted det(A), det A, or |A|. Geometrically, it can be viewed as the scaling factor of the linear transformation described by the matrix.

NumPy Linear algebra: Compute the determinant of an array

Sample Solution :

Python Code :

import numpy as np
a = np.array([[1,2],[3,4]])
print("Original array:")
print(a)
result =  np.linalg.det(a)
print("Determinant of the said array:")
print(result)

Sample Output:

Original array:
[[1 2]
 [3 4]]
Determinant of the said array:
-2.0000000000000004

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find a matrix or vector norm.
Next: Write a NumPy program to compute the inverse of a given matrix.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Finding the most common elements in an iterable:

Example:

# collections.Counter lets you find the most common
# elements in an iterable:

import collections
c = collections.Counter('helloworld')

print(c)

print (c.most_common(3))

Output:

Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1})
[('l', 3), ('o', 2), ('h', 1)]