</>
Skip to content
SciPy lessons (10/25)

SciPy — Linear Algebra

Solve linear system

from scipy import linalg
import numpy as np

A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])

x = linalg.solve(A, b)
print(f"Solution: {x}")

Matrix operations

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Matrix multiplication
C = A @ B
print(f"A * B =\n{C}")

# Inverse
A_inv = linalg.inv(A)
print(f"Inverse of A =\n{A_inv}")

# Determinant
det = linalg.det(A)
print(f"Determinant = {det:.2f}")

Eigenvalues and eigenvectors

A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = linalg.eig(A)
print(f"Eigenvalues: {eigenvalues}")
print(f"Eigenvectors:\n{eigenvectors}")

Singular Value Decomposition

A = np.array([[1, 2], [3, 4], [5, 6]])
U, s, Vh = linalg.svd(A)
print(f"U shape: {U.shape}")
print(f"Singular values: {s}")
print(f"Vh shape: {Vh.shape}")

LU decomposition

A = np.array([[1, 2], [3, 4]])
P, L, U = linalg.lu(A)
print(f"Permutation:\n{P}")
print(f"Lower:\n{L}")
print(f"Upper:\n{U}")

Cholesky decomposition

A = np.array([[4, 2], [2, 3]])
L = linalg.cholesky(A)
print(f"Cholesky factor:\n{L}")

Matrix norm

A = np.array([[1, 2], [3, 4]])
norm = linalg.norm(A)
print(f"Frobenius norm: {norm:.4f}")

Mini Practice

  1. Solve a linear system
  2. Compute eigenvalues
  3. Perform SVD
  4. Decompose a matrix

Up Next

Continue with FFT - Fast Fourier Transform.

Related Topics

Frequently Asked Questions about Linear Algebra

What is Linear Algebra in SciPy?

Linear Algebra is a fundamental concept in SciPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Linear Algebra?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Linear Algebra.

Why is Linear Algebra important in SciPy?

Linear Algebra is essential for SciPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.