OpenBLAS linear algebra — matrix multiply, solve linear systems, LU/Cholesky/QR factorizations, SVD, and eigenvalues at near-hardware peak throughput.
ppm install openblas
Requires libopenblas-dev and liblapacke-dev:
sudo apt install libopenblas-dev liblapacke-dev
Overview
OpenBLAS is a highly optimized BLAS + LAPACK implementation used by NumPy, SciPy, Julia, R, and virtually every scientific computing stack.
It selects optimal CPU-specific kernels (AVX2, AVX-512, NEON) at runtime for maximum throughput.
Matrix storage: column-major (Fortran order). Element (i, j) of an M×N matrix is at index i + j*M (0-based). This matches LAPACK, MATLAB, and Julia conventions.
Basic Operations
uses openblas;
{ 3x3 matrix multiply C = A * B }
var A := MatFromArray([1,0,0, 0,2,0, 0,0,3], 3, 3); { diagonal }
var B := Eye(3);
var C := MatMul(A, 3, 3, B, 3);
{ Transpose }
var At := Transpose(A, 3, 3);
{ Determinant and trace }
Writeln('det = ' + FloatToStr(Det(A, 3)));
Writeln('tr = ' + FloatToStr(Trace(A, 3)));
Solve a Linear System
{ Solve Ax = b (A is 3x3, b is 3x1) }
var A := MatFromArray([2,1,-1, -3,-1,2, -2,1,2], 3, 3);
var b := MatFromArray([8, -11, -3], 3, 1);
var x := Solve(A, 3, b, 1);
{ x = [2, 3, -1] }
{ Least-squares regression (overdetermined system) }
var design : TMatrix; { M x 2 design matrix [1, x_i] }
var targets: TMatrix; { M x 1 observations }
var coeffs := LeastSquares(design, M, 2, targets, 1);
{ coeffs[0] = intercept, coeffs[1] = slope }
SVD & Eigenvalues
{ Full SVD of a 4x3 matrix }
var svd := SVD(A, 4, 3);
Writeln('Singular values: ' + FloatToStr(svd.S[0]));
Writeln('Numerical rank: ' + IntToStr(svd.Rank));
{ Truncated SVD — top 10 components (fast for large matrices) }
var tsvd := TruncatedSVD(A, M, N, 10);
{ Eigenvalues of symmetric matrix (all real) }
var eig := EigSymmetric(A, 3);
for v in eig.Values do
Writeln(FloatToStr(v));
BLAS Level 1 — Vectors
var x: TVector := [1.0, 2.0, 3.0];
var y: TVector := [4.0, 5.0, 6.0];
Writeln(Dot(x, y, 3)); { 32.0 }
Writeln(Norm2(x, 3)); { 3.742 }
Axpy(2.0, x, y, 3); { y := 2*x + y = [6, 9, 12] }
Scale(0.5, x, 3); { x := 0.5*x = [0.5, 1.0, 1.5] }
API Reference
Matrix Creation
| Function | Description |
| Zeros(M, N) | M×N zero matrix. |
| Ones(M, N) | M×N all-ones matrix. |
| Eye(N) | N×N identity matrix. |
| MatFromArray(Data, M, N) | Matrix from flat column-major array. |
| Diag(V) | Diagonal matrix from vector. |
| Transpose(A, M, N) | Matrix transpose. |
| MatAdd / MatSub / MatScale | Element-wise add, subtract, scale. |
| MatMul(A, MA, KA, B, NB) | Matrix multiply A (MA×KA) × B (KA×NB). |
BLAS Level 1 — Vectors
| Function | Description |
| Dot(X, Y, N) | Dot product. |
| Norm2(X, N) | Euclidean norm. |
| Asum(X, N) | Sum of absolute values (L1 norm). |
| Axpy(Alpha, X, Y, N) | Y := Alpha*X + Y. |
| Scale(Alpha, X, N) | X := Alpha*X. |
| Iamax(X, N) | Index of max absolute element. |
LAPACK — Solve & Factorize
| Function | Description |
| Solve(A, N, B, NRHS) | Solve A·X=B using LU (DGESV). |
| SolveSPD(A, N, B, NRHS) | Solve symmetric positive definite system (DPOSV). |
| LeastSquares(A, M, N, B, NRHS) | Minimum-norm least-squares solution (DGELS). |
| LU(A, M, N) | LU decomposition with pivoting. |
| Cholesky(A, N) | Cholesky A = L·Lᵀ. |
| QR(A, M, N) | QR decomposition. |
Eigenvalues & SVD
| Function | Description |
| Eig(A, N) | Eigenvalues + eigenvectors (general matrix, DGEEV). |
| EigSymmetric(A, N) | Real eigenvalues + orthonormal eigenvectors (DSYEV). |
| SVD(A, M, N) | Full SVD: U, S, Vᵀ. |
| TruncatedSVD(A, M, N, K) | Top-K singular values/vectors. |
| Det(A, N) | Determinant. |
| Rank(A, M, N, Tol) | Numerical rank. |
| Inv(A, N) | Matrix inverse. |
| PseudoInv(A, M, N) | Moore-Penrose pseudo-inverse. |
| FrobeniusNorm(A, M, N) | Frobenius norm. |
| Trace(A, N) | Sum of diagonal. |