Linear Algebra¶
Eigendecomposition, SVD, least squares, determinants, inverses, condition numbers and matrix summaries built on scipy.linalg.
compute_eigen(matrix)
¶
Compute eigenvalues and eigenvectors of a square matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Square numeric matrix. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
{'eigenvalues': np.ndarray, 'eigenvectors': np.ndarray} Eigenvectors are column vectors in the returned matrix. |
Notes
Uses numpy.linalg.eig which may return complex values for asymmetric matrices. For symmetric matrices, prefer numpy.linalg.eigh for real eigenvalues.
Source code in utils/linear_algebra_utils.py
compute_svd(matrix)
¶
Compute the full Singular Value Decomposition of a matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Input matrix of shape (m, n). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
{'U': np.ndarray (m x m), 'S': np.ndarray (min(m,n),), 'Vt': np.ndarray (n x n)} Satisfies matrix = U @ np.diag(S) @ Vt. |
Source code in utils/linear_algebra_utils.py
solve_least_squares(A, b)
¶
Solve the least-squares problem: minimize ||Ax - b||^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ndarray
|
Coefficient matrix of shape (m, n). |
required |
b
|
ndarray
|
Target vector of shape (m,). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
{'solution': np.ndarray, 'residuals': np.ndarray, 'rank': int, 'singular_values': np.ndarray} |
Source code in utils/linear_algebra_utils.py
compute_determinant(matrix)
¶
Compute the determinant of a square matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Square matrix. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Determinant value. Zero indicates a singular (non-invertible) matrix. |
Source code in utils/linear_algebra_utils.py
compute_inverse(matrix)
¶
Compute the inverse of a square matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Square, non-singular matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The inverse matrix such that matrix @ inverse ≈ I. |
Raises:
| Type | Description |
|---|---|
LinAlgError
|
If the matrix is singular (determinant = 0). |
Source code in utils/linear_algebra_utils.py
matrix_summary_df(matrix)
¶
Compute a summary of key matrix properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Input numeric matrix. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Single-row DataFrame with columns: Shape, Rank, Determinant, Condition Number. Determinant is None for non-square matrices. |
Source code in utils/linear_algebra_utils.py
generate_matrix(matrix_type='Random Symmetric', dim=4)
¶
Generates a matrix based on the selected type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix_type
|
str
|
Type of matrix ("Random Symmetric" or "Tall Matrix") |
'Random Symmetric'
|
dim
|
int
|
Matrix dimension (rows = dim, cols = dim or smaller) |
4
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Generated matrix as a DataFrame for display. |
Source code in utils/linear_algebra_utils.py
least_squares_solution(A, b)
¶
Solve the least-squares problem and return solution and residuals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ndarray
|
Coefficient matrix of shape (m, n). |
required |
b
|
ndarray
|
Target vector of shape (m,). |
required |
Returns:
| Type | Description |
|---|---|
tuple of (np.ndarray, np.ndarray)
|
(x, residuals) where x is the solution vector. |
Source code in utils/linear_algebra_utils.py
compute_eigendecomposition(matrix)
¶
Compute eigenvalues and eigenvectors, ensuring float output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Square numeric matrix. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (np.ndarray, np.ndarray)
|
(eigenvalues, eigenvectors) both as float64 arrays. Complex parts are discarded — use only for real-eigenvalue matrices (e.g., symmetric). |
Source code in utils/linear_algebra_utils.py
compute_svd_adv(matrix)
¶
Compute SVD with enforced float numeric output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray or DataFrame
|
Input matrix. DataFrames are converted to float numpy arrays. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (np.ndarray, np.ndarray, np.ndarray)
|
(U, S, Vt) matrices from the SVD decomposition. |