Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Apply linear algebra concepts to research computing and data analysis
.claude/skills/brycewang-stanford-linear-algebra-applications/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-07 | ✗→✓ | ▲ Improved | 98% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 102% | 0% |
| case-11 | ✓→✗ | ▼ Worse | 95% | 0% |
| case-08 | ✓→✓ | = Same ✓ | 104% | 0% |
| case-09 | ✓→✓ | = Same ✓ | 76% | 0% |
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.
pythonimport 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
pythondef 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()." ) }
pythondef 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" ] }
pythondef eigen_analysis(A: np.ndarray) -> dict: """ Eigenvalue decomposition of a square matrix. """ eigenvalues, eigenvectors = np.linalg.eig(A) # Sort by magnitude idx = np.argsort(np.abs(eigenvalues))[::-1] return { "eigenvalues": eigenvalues[idx], "eigenvectors": eigenvectors[:, idx], "is_symmetric": np.allclose(A, A.T), "is_positive_definite": ( np.all(np.real(eigenvalues) > 0) if np.allclose(A, A.T) else "N/A (not symmetric)" ), "spectral_radius": np.max(np.abs(eigenvalues)), "trace_check": ( f"Sum of eigenvalues: {np.sum(eigenvalues):.4f}, " f"Trace of A: {np.trace(A):.4f}" ) }
pythondef pca_from_scratch(X: np.ndarray, n_components: int = 2) -> dict: """ PCA using eigendecomposition of the covariance matrix. Args: X: Data matrix (n_samples x n_features), centered n_components: Number of principal components to retain """ # Center the data X_centered = X - X.mean(axis=0) # Covariance matrix C = np.cov(X_centered, rowvar=False) # Eigendecomposition (symmetric matrix -> use eigh for stability) eigenvalues, eigenvectors = np.linalg.eigh(C) # Sort descending idx = np.argsort(eigenvalues)[::-1] eigenvalues = eigenvalues[idx] eigenvectors = eigenvectors[:, idx] # Select top components components = eigenvectors[:, :n_components] explained_variance = eigenvalues[:n_components] total_variance = eigenvalues.sum() # Project data X_projected = X_centered @ components return { "components": components, "explained_variance_ratio": explained_variance / total_variance, "cumulative_variance": np.cumsum(explained_variance) / total_variance, "projected_data": X_projected }
pythondef least_squares_fit(X: np.ndarray, y: np.ndarray) -> dict: """ Solve the normal equations: beta = (X^T X)^{-1} X^T y """ # Using the numerically stable QR decomposition Q, R = np.linalg.qr(X) beta = linalg.solve_triangular(R, Q.T @ y) y_hat = X @ beta residuals = y - y_hat return { "coefficients": beta, "r_squared": 1 - np.sum(residuals ** 2) / np.sum((y - y.mean()) ** 2), "residual_norm": np.linalg.norm(residuals), "method": "QR decomposition (more stable than normal equations)" }
1. Avoid explicitly computing matrix inverses:
BAD: x = np.linalg.inv(A) @ b
GOOD: x = np.linalg.solve(A, b)
2. Use specialized routines for structured matrices:
- Symmetric positive definite: Cholesky (linalg.cho_solve)
- Sparse: scipy.sparse.linalg.spsolve
- Banded: scipy.linalg.solve_banded
3. Check condition numbers before solving:
- cond(A) > 10^10 suggests the solution may be unreliable
- Consider regularization (Tikhonov/ridge) for ill-conditioned systems
4. Use appropriate precision:
- float64 for most research computing
- float32 for large-scale GPU computations (monitor for precision loss)When working with very large matrices, leverage sparse matrix representations (scipy.sparse), iterative solvers (conjugate gradient, GMRES), and randomized algorithms (randomized SVD) to keep computation tractable.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-05 | fail→fail | 19,895 | 19,653 | -1% | 1 | 1 | 0% | 3,912 | 5,983 | +53% | 0 | 0 | — |
case-01 | fail→fail | 20,811 | 52,415 | +152% | 1 | 1 | 0% | 4,419 | 6,494 | +47% | 0 | 0 | — |
case-02 | fail→fail | 26,881 | 19,860 | -26% | 1 | 1 | 0% | 4,150 | 5,623 | +35% | 0 | 0 | — |
case-03 | fail→fail | 15,434 | 44,846 | +191% | 1 | 1 | 0% | 2,450 | 4,118 | +68% | 0 | 0 | — |
case-04 | fail→fail | 19,373 | 23,168 | +20% | 1 | 1 | 0% | 3,834 | 5,592 | +46% | 0 | 0 | — |
case-06 | fail→fail | 13,896 | 18,809 | +35% | 1 | 1 | 0% | 2,758 | 5,584 | +102% | 0 | 0 | — |
case-07 | fail→pass | 14,415 | 14,089 | -2% | 1 | 1 | 0% | 2,313 | 4,569 | +98% | 0 | 0 | — |
case-08 | pass→pass | 11,368 | 12,406 | +9% | 1 | 1 | 0% | 1,990 | 4,059 | +104% | 0 | 0 | — |
case-09 | pass→pass | 12,199 | 12,922 | +6% | 1 | 1 | 0% | 2,215 | 3,906 | +76% | 0 | 0 | — |
case-15 | pass→pass | 4,904 | 5,458 | +11% | 1 | 1 | 0% | 819 | 2,759 | +237% | 0 | 0 | — |
case-10 | pass→pass | 13,005 | 13,667 | +5% | 1 | 1 | 0% | 2,438 | 3,930 | +61% | 0 | 0 | — |
case-11 | pass→fail | 13,844 | 15,617 | +13% | 1 | 1 | 0% | 2,429 | 4,726 | +95% | 0 | 0 | — |
case-12 | fail→fail | 21,718 | 28,217 | +30% | 1 | 1 | 0% | 3,497 | 6,392 | +83% | 0 | 0 | — |
case-13 | pass→pass | 18,555 | 17,945 | -3% | 1 | 1 | 0% | 3,201 | 5,207 | +63% | 0 | 0 | — |
case-14 | fail→pass | 13,225 | 16,855 | +27% | 1 | 1 | 0% | 2,291 | 4,638 | +102% | 0 | 0 | — |
case-16 | pass→pass | 8,749 | 9,232 | +6% | 1 | 1 | 0% | 1,767 | 3,290 | +86% | 0 | 0 | — |
case-17 | pass→pass | 12,512 | 10,886 | -13% | 1 | 1 | 0% | 2,040 | 3,385 | +66% | 0 | 0 | — |
case-18 | pass→pass | 10,282 | 13,778 | +34% | 1 | 1 | 0% | 2,134 | 4,131 | +94% | 0 | 0 | — |
case-19 | pass→pass | 7,698 | 13,705 | +78% | 1 | 1 | 0% | 1,528 | 4,092 | +168% | 0 | 0 | — |
case-20 | pass→pass | 11,670 | 16,870 | +45% | 1 | 1 | 0% | 2,502 | 4,654 | +86% | 0 | 0 | — |
case-21 | pass→pass | 11,518 | 16,352 | +42% | 1 | 1 | 0% | 2,486 | 5,213 | +110% | 0 | 0 | — |
case-22 | pass→pass | 14,204 | 15,745 | +11% | 1 | 1 | 0% | 2,798 | 4,888 | +75% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases. 2 cases got worse with the skill loaded, and they are included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.