SKILL.md
Applied Linear Algebra for Research
A skill for applying linear algebra to research computing, data analysis, and scientific modeling. Covers matrix decompositions, eigenvalue problems, least squares, dimensionality reduction, and practical implementation in NumPy/SciPy.
Essential Operations
Matrix Multiplication and Solving Systems
import numpy as np
from scipy import linalg
def solve_linear_system(A: np.ndarray, b: np.ndarray) -> dict:
"""
Solve Ax = b and analyze the system.
Args:
A: Coefficient matrix (n x n)
b: Right-hand side vector (n,)
"""
n = A.shape[0]
# Check condition number (sensitivity to perturbations)
cond = np.linalg.cond(A)
result = {
"shape": A.shape,
"rank": np.linalg.matrix_rank(A),
"condition_number": cond,
"well_conditioned": cond < 1e10,
}
if result["rank"] == n:
x = np.linalg.solve(A, b)
result["solution"] = x
result["residual_norm"] = np.linalg.norm(A @ x - b)
else:
# Underdetermined or singular -- use least-squares
x, residuals, rank, sv = np.linalg.lstsq(A, b, rcond=None)
result["least_squares_solution"] = x
result["note"] = "System is rank-deficient; least-squares solution returned"
return result
Matrix Decompositions
LU Decomposition (Solving Multiple Systems)
def lu_factorization(A: np.ndarray) -> dict:
"""
LU decomposition for efficiently solving Ax=b for multiple b.
"""
P, L, U = linalg.lu(A)
return {
"P": P, # Permutation matrix
"L": L, # Lower triangular
"U": U, # Upper triangular
"usage": (
"Once computed, solve for any new right-hand side b "
"in O(n^2) instead of O(n^3). Use scipy.linalg.lu_solve()."
)
}
Singular Value Decomposition (SVD)
def svd_analysis(A: np.ndarray) -> dict:
"""
SVD of matrix A = U S V^T and its applications.
Args:
A: Input matrix (m x n)
"""
U, s, Vt = np.linalg.svd(A, full_matrices=False)
return {
"U_shape": U.shape, # Left singular vectors (m x k)
"singular_values": s, # Sorted descending
"Vt_shape": Vt.shape, # Right singular vectors (k x n)
"rank": np.sum(s > 1e-10),
"condition_number": s[0] / s[-1] if s[-1] > 0 else float("inf"),
"energy_ratio": np.cumsum(s ** 2) / np.sum(s ** 2),
"applications": [
"Low-rank approximation (truncated SVD)",
"Principal Component Analysis (PCA)",
"Pseudoinverse computation",
"Latent Semantic Analysis (LSA) in text mining",
"Image compression",
"Noise reduction"
]
}
