Dot Product, Matmul, Matrix Inversion, and Determinant in NumPy

NumPy provides various functions for performing linear algebra operations efficiently.

1. Importing NumPy

Before using NumPy, it needs to be imported.

            import numpy as np
        

2. Dot Product

The dot product of two arrays is computed using np.dot().

            a = np.array([1, 2, 3])
            b = np.array([4, 5, 6])
            
            result = np.dot(a, b)
            print(result)  # Output: 32
        

3. Matrix Multiplication

Matrix multiplication can be performed using np.matmul() or the @ operator.

            matrix1 = np.array([[1, 2], [3, 4]])
            matrix2 = np.array([[5, 6], [7, 8]])
            
            result = np.matmul(matrix1, matrix2)
            print(result)
            # Output: 
            # [[19 22]
            #  [43 50]]
        

4. Matrix Inversion

The inverse of a matrix is computed using np.linalg.inv().

            matrix = np.array([[1, 2], [3, 4]])
            
            inverse_matrix = np.linalg.inv(matrix)
            print(inverse_matrix)
            # Output:
            # [[-2.   1. ]
            #  [ 1.5 -0.5]]
        

5. Determinant

The determinant of a matrix can be calculated using np.linalg.det().

            matrix = np.array([[1, 2], [3, 4]])
            
            determinant = np.linalg.det(matrix)
            print(determinant)  # Output: -2.0
        

Conclusion

NumPy provides efficient methods for computing dot products, matrix multiplication, inversion, and determinants, which are essential for linear algebra applications.





Advertisement