Matrix
26 functions for dense numeric matrices. Optimized for expression matrices, PCA, and linear algebra in bioinformatics pipelines.
Constructors
| Function | Signature | Description |
| matrix | matrix(rows) -> matrix | Create from nested lists (row-major) |
| zeros | zeros(rows, cols) -> matrix | All-zero matrix |
| ones | ones(rows, cols) -> matrix | All-one matrix |
| eye | eye(n) -> matrix | n x n identity matrix |
| diag | diag(list) -> matrix | Diagonal matrix from list |
let m = matrix([[1, 2, 3], [4, 5, 6]]) # 2x3 matrix
let z = zeros(3, 3) # 3x3 zero matrix
let id = eye(4) # 4x4 identity
let d = diag([1, 2, 3]) # 3x3 diagonal
Operations
transpose(mat) -> matrix
dot(a, b) -> float # vector dot product
mat_mul(a, b) -> matrix # matrix multiplication
inverse(mat) -> matrix # matrix inverse (Gauss-Jordan elimination)
determinant(mat) -> float # determinant via LU decomposition
solve(A, b) -> list # solve linear system Ax = b
trace(mat) -> float # sum of diagonal elements
norm(mat) -> float # Frobenius norm
rank(mat) -> int # matrix rank via SVD
let a = matrix([[1, 2], [3, 4]])
let b = matrix([[5, 6], [7, 8]])
transpose(a) # [[1, 3], [2, 4]]
mat_mul(a, b) # [[19, 22], [43, 50]]
determinant(a) # -2.0
inverse(a) # [[-2.0, 1.0], [1.5, -0.5]]
trace(a) # 5.0
norm(a) # 5.477...
rank(a) # 2
# Solve linear system
let A = matrix([[2, 1], [1, 3]])
b = [5, 10]
solve(A, b) # [1.0, 3.0]
Decompositions
| Function | Signature | Description |
| eigenvalues | eigenvalues(m) -> {values: List, vectors: Matrix} | Eigendecomposition via QR algorithm |
| svd | svd(m) -> {u: Matrix, d: List, vt: Matrix} | Singular value decomposition |
| pca | pca(m, n_components) -> {scores, loadings, variance_explained} | Principal component analysis |
# PCA on gene expression matrix (genes x samples)
let expr = csv("expression_matrix.csv") |> matrix_from_table("value")
let result = pca(expr, 3)
println("Variance explained:", result.variance_explained)
# [0.45, 0.23, 0.12]
# Plot first two principal components
scatter(result.scores, {
x_col: 0,
y_col: 1,
title: "PCA - Gene Expression",
x_label: format("PC1 ({}%)", round(result.variance_explained[0] * 100, 1)),
y_label: format("PC2 ({}%)", round(result.variance_explained[1] * 100, 1))
})
Linear Algebra Examples
# PCA on gene expression matrix
let expr = matrix([[10, 20, 15], [8, 25, 12], [11, 18, 20]])
let centered = mat_map(expr, |x| x - mean(expr))
let cov = mat_mul(transpose(centered), centered)
let pca_result = eigenvalues(cov)
println("PC1 variance:", first(pca_result.values))
# Solve linear regression: coefficients = (X'X)^-1 X'y
let X = matrix([[1, 2], [1, 4], [1, 6], [1, 8]])
let y = [3.1, 5.2, 6.8, 9.1]
let XtX = mat_mul(transpose(X), X)
let Xty = mat_mul(transpose(X), matrix([y]))
let coeffs = solve(XtX, Xty)
Matrix Statistics
cor_matrix(mat) -> matrix # correlation matrix
cov_matrix(mat) -> matrix # covariance matrix
row_sums(mat) -> list
col_sums(mat) -> list
let expr = matrix([
[10.2, 8.5, 12.1], # gene1 across 3 samples
[5.3, 4.8, 5.9], # gene2
[20.1, 18.7, 22.3] # gene3
])
let cor = cor_matrix(expr)
println("Gene correlation matrix:", cor)
let total_per_sample = col_sums(expr) # [35.6, 32.0, 40.3]
let total_per_gene = row_sums(expr) # [30.8, 16.0, 61.1]