Calculate the determinant of a 4x4 random array using NumPy

NumPy: Advanced Exercise-18 with Solution

In mathematics, the determinant is a scalar value that is a certain function of the entries of a square matrix. The determinant of a matrix A is commonly denoted det(A), det A, or |A|. Its value characterizes some properties of the matrix and the linear map represented, on a given basis, by the matrix.

Write a NumPy program to create a 4x4 array with random values and calculate the determinant.

The task involves creating a 4x4 array with random values using NumPy and then calculating its determinant. Determinants are crucial in linear algebra for solving systems of linear equations, finding matrix inverses, and understanding matrix properties such as singularity and invertibility.

Sample Solution:

Python Code:

# Importing the necessary NumPy library
import numpy as np
# Create a 4x4 array with random values
array = np.random.rand(4, 4)
# Calculate the determinant of the array
determinant = np.linalg.det(array)
# Printing the array and its determinant
print("Array:\n", array)
print("Determinant of the array:\n", determinant)


 [[0.16599069 0.27064193 0.24692337 0.36708756]
 [0.947476   0.90895607 0.80675682 0.31148671]
 [0.98497524 0.07407102 0.39445196 0.85262454]
 [0.63139044 0.63265701 0.06777085 0.51663395]]
Determinant of the array:


  • Import NumPy library: This step imports the NumPy library, which is essential for numerical operations.
  • Create a 4x4 array: We use np.random.rand(4, 4) to generate a 4x4 matrix with random values between 0 and 1.
  • Calculate the determinant of the array: The np.linalg.det function computes the determinant of the given array.
  • Print results: This step prints the original array and its determinant.

Python-Numpy Code Editor:

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

Previous: Compute the inverse of a 6x6 random array using NumPy.
Next: Flattening a 3x3x3 array to a 1D array using NumPy.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.