# Compute the determinant for a stack of matrices in linear algebra in Python

PythonNumpyServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To compute the determinant for a stack of matrices in linear algebra, use the np.linalg.det() in Python Numpy. The 1st parameter, a is the input array to compute determinants for. The method returns the determinant of a.

## Steps

At first, import the required libraries -

import numpy as np

Create an array −

arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)


Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To compute the determinant for a stack of matrices in linear algebra, use the np.linalg.det() in Python −

print("\nResult (determinant)...\n",np.linalg.det(arr))

## Example

import numpy as np

# Create an array
arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To compute the determinant for a stack of matrices in linear algebra, use the np.linalg.det() in Python Numpy.
print("\nResult (determinant)...\n",np.linalg.det(arr))

## Output

Our Array...
[[[1 2]
[3 4]]

[[1 2]
[2 1]]

[[1 3]
[3 1]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

Shape of our Array object...
(3, 2, 2)

Result (determinant)...
[-2. -3. -8.]