How do you calculate the determinant of a matrix in NumPy?

QuestionsQuestions8 SkillsProNumPy Advanced TopicsSep, 08 2025
0210

To calculate the determinant of a matrix in NumPy, you can use the numpy.linalg.det() function. Here’s a simple example:

import numpy as np

def determinant_matrix(matrix: np.ndarray) -> float:
    """
    Returns the determinant of the input matrix using NumPy.

    Args:
    matrix: A NumPy matrix for which to calculate the determinant.

    Returns:
    The determinant of the input matrix as a float.
    """
    return np.linalg.det(matrix)

# Example usage
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

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

print("Determinant of A:", determinant_matrix(A))
print("Determinant of B:", determinant_matrix(B))

In this example, determinant_matrix function calculates the determinant of the given NumPy matrix.

0 Comments

no data
Be the first to share your comment!