0
0
NumpyHow-ToBeginner ยท 4 min read

How to Use np.linalg in NumPy for Linear Algebra

Use np.linalg in NumPy to perform linear algebra tasks such as matrix inversion, solving linear systems, and finding eigenvalues. Import NumPy and call functions like np.linalg.inv() for inverse or np.linalg.solve() to solve equations.
๐Ÿ“

Syntax

The np.linalg module contains functions for linear algebra operations. Common functions include:

  • np.linalg.inv(a): Computes the inverse of a square matrix a.
  • np.linalg.solve(a, b): Solves the linear system ax = b for x.
  • np.linalg.det(a): Calculates the determinant of matrix a.
  • np.linalg.eig(a): Returns eigenvalues and eigenvectors of a.

Each function takes arrays as input and returns arrays or scalars as output.

python
import numpy as np

# Inverse of matrix a
inv_a = np.linalg.inv(a)

# Solve linear system ax = b
x = np.linalg.solve(a, b)

# Determinant of matrix a
det_a = np.linalg.det(a)

# Eigenvalues and eigenvectors of a
eigenvalues, eigenvectors = np.linalg.eig(a)
๐Ÿ’ป

Example

This example shows how to invert a matrix, solve a system of equations, and compute the determinant using np.linalg.

python
import numpy as np

# Define a 2x2 matrix
A = np.array([[3, 1], [1, 2]])

# Define vector b
b = np.array([9, 8])

# Calculate inverse of A
A_inv = np.linalg.inv(A)

# Solve Ax = b
x = np.linalg.solve(A, b)

# Calculate determinant of A
det_A = np.linalg.det(A)

print("Inverse of A:\n", A_inv)
print("Solution x of Ax = b:\n", x)
print("Determinant of A:", det_A)
Output
Inverse of A: [[ 0.4 -0.2] [-0.2 0.6]] Solution x of Ax = b: [2. 3.] Determinant of A: 5.0
โš ๏ธ

Common Pitfalls

Common mistakes when using np.linalg include:

  • Trying to invert a non-square or singular matrix, which causes errors.
  • Passing arrays with wrong shapes to np.linalg.solve.
  • Ignoring that some functions expect float or complex types for accurate results.

Always check matrix shape and properties before applying linear algebra functions.

python
import numpy as np

# Wrong: Non-square matrix inversion
try:
    A = np.array([[1, 2, 3], [4, 5, 6]])
    np.linalg.inv(A)
except np.linalg.LinAlgError as e:
    print("Error:", e)

# Right: Square matrix inversion
A = np.array([[1, 2], [3, 4]])
inv_A = np.linalg.inv(A)
print("Inverse of A:\n", inv_A)
Output
Error: Last 2 dimensions of the array must be square Inverse of A: [[-2. 1. ] [ 1.5 -0.5]]
๐Ÿ“Š

Quick Reference

Here is a quick reference for common np.linalg functions:

FunctionDescriptionInputOutput
np.linalg.inv(a)Inverse of square matrix a2D square array2D array (inverse)
np.linalg.solve(a, b)Solve linear system ax = b2D square array a, 1D or 2D array bArray x solution
np.linalg.det(a)Determinant of matrix a2D square arrayScalar determinant
np.linalg.eig(a)Eigenvalues and eigenvectors2D square arrayTuple (eigenvalues, eigenvectors)
np.linalg.norm(x)Vector or matrix normArrayScalar norm
โœ…

Key Takeaways

Use np.linalg functions for common linear algebra tasks like inversion, solving, and eigen computations.
Ensure matrices are square and non-singular before inverting or solving systems.
np.linalg.solve is preferred over manual inversion for solving linear equations.
Check input shapes carefully to avoid errors.
np.linalg provides fast, reliable linear algebra tools integrated with NumPy arrays.