mAPL Scientific Computing Library
Pure APL implementations of standard mathematical and numerical algorithms.
All functions use well-known textbook methods.
Import with import { Fn } from 'sci/module.mapl'.
Core Utilities sci/core.mapl
Array generation, cumulative operations, indexing utilities, and meshgrid construction.
| Function | Type | Signature | Description | Example |
|---|---|---|---|---|
| Linspace | ⍺ f ⍵ | scalar, vector → vector | n linearly spaced points between (a,b) | 5 Linspace (0,1) → 0 0.25 0.5 0.75 1 |
| Logspace | ⍺ f ⍵ | scalar, vector → vector | n log-spaced points between 10^a and 10^b | 4 Logspace (0,3) → 1 10 100 1000 |
| Colon | ⍺ f ⍵ | vector, scalar → vector | Range from start to end with step | (0,0.5) Colon 2 → 0 0.5 1 1.5 2 |
| CumSum | f ⍵ | vector → vector | Cumulative sum (+\) | CumSum 1 2 3 4 5 → 1 3 6 10 15 |
| CumProd | f ⍵ | vector → vector | Cumulative product (×\) | CumProd 1 2 3 4 5 → 1 2 6 24 120 |
| CumMax | f ⍵ | vector → vector | Running maximum (⌈\) | CumMax 3 1 4 1 5 → 3 3 4 4 5 |
| CumMin | f ⍵ | vector → vector | Running minimum (⌊\) | CumMin 5 3 4 1 2 → 5 3 3 1 1 |
| Diff | f ⍵ | vector → vector | First differences (length n → n−1) | Diff 1 4 9 16 → 3 5 7 |
| Find | f ⍵ | vector → vector | Indices of nonzero elements | Find 0 3 0 5 → 2 4 |
| Any | f ⍵ | vector → scalar | 1 if any element nonzero | Any 0 0 1 0 → 1 |
| All | f ⍵ | vector → scalar | 1 if all elements nonzero | All 1 1 1 1 → 1 |
| Numel | f ⍵ | any → scalar | Total number of elements | Numel 3 4 ⍴ ⍳12 → 12 |
| Sub2Ind | ⍺ f ⍵ | vector, vector → scalar | Subscripts → linear index | (3,4) Sub2Ind (1,2) → 6 |
| Ind2Sub | ⍺ f ⍵ | vector, scalar → vector | Linear index → subscripts | (3,4) Ind2Sub 6 → 1 2 |
| Meshgrid | ⍺ f ⍵ | vector, vector → dict | Coordinate matrices {X, Y} | (1 2 3) Meshgrid (4 5) → {X:…, Y:…} |
| Accumarray | ⍺ f ⍵ | vector, vector → vector | Accumulate values by group keys | (1 1 2 2 3) Accumarray (10 20 30 40 50) → 30 70 50 |
Polynomials sci/poly.mapl
Horner evaluation, differentiation, integration, root-finding, and polynomial arithmetic.
| Function | Type | Signature | Description | Example |
|---|---|---|---|---|
| Polyval | ⍺ f ⍵ | vector, any → any | Evaluate polynomial (Horner's method) | (1 2 3) Polyval 5 → 38 |
| Polyder | f ⍵ | vector → vector | Differentiate polynomial (power rule) | Polyder 3 2 1 → 6 2 |
| Polyint | f ⍵ | vector → vector | Integrate polynomial | Polyint 6 2 → 2 2 0 |
| Roots2 | f ⍵ | vector → vector | Quadratic formula roots | Roots2 (1 ¯3 2) → 2 1 |
| Poly | f ⍵ | vector → vector | Roots → polynomial coefficients | Poly 2 1 → 1 ¯3 2 |
| Polyadd | ⍺ f ⍵ | vector, vector → vector | Add two polynomials | (1 2) Polyadd (3 4 5) → 3 5 7 |
| Polymul | ⍺ f ⍵ | vector, vector → vector | Multiply polynomials (convolution) | (1 1) Polymul (1 1) → 1 2 1 |
Special Matrices sci/special.mapl
Classic matrix families: magic squares, Hilbert, Vandermonde, Pascal, Toeplitz, Hankel, circulant, companion.
| Function | Type | Signature | Description | Example |
|---|---|---|---|---|
| Magic | f ⍵ | scalar → matrix | n×n magic square (Siamese method, odd n) | Magic 3 → rows/cols/diags all sum to 15 |
| Hilb | f ⍵ | scalar → matrix | Hilbert matrix H[i,j]=1/(i+j+1) | Hilb 3 → 1 0.5 0.333 / … |
| InvHilb | f ⍵ | scalar → matrix | Inverse of Hilbert matrix | InvHilb 3 → exact inverse |
| Vander | f ⍵ | vector → matrix | Vandermonde matrix v^(n-1..0) | Vander 1 2 3 → power matrix |
| Toeplitz | f ⍵ | vector → matrix | Symmetric Toeplitz matrix | Toeplitz 1 2 3 → 1 2 3 / 2 1 2 / 3 2 1 |
| Hankel | f ⍵ | vector → matrix | Hankel matrix H[i,j]=c[i+j] | Hankel 1 2 3 → 1 2 3 / 2 3 0 / 3 0 0 |
| Pascal | f ⍵ | scalar → matrix | Pascal/binomial coefficient matrix | Pascal 4 → 1 1 1 1 / 1 2 3 4 / … |
| Companion | f ⍵ | vector → matrix | Companion matrix from polynomial | Companion 1 ¯6 11 ¯6 → … |
| Circulant | f ⍵ | vector → matrix | Circulant matrix (cyclic rows) | Circulant 1 2 3 → 1 2 3 / 3 1 2 / 2 3 1 |
Linear Algebra sci/linalg.mapl
Decompositions (LU, QR, eigen), norms, vector products, matrix operations, pseudoinverse.
| Function | Type | Signature | Description | Example |
|---|---|---|---|---|
| Inv | f ⍵ | matrix → matrix | Matrix inverse (via ⌹) | Inv 2 2⍴4 7 2 6 → 0.6 ¯0.7… |
| Det | f ⍵ | matrix → scalar | Determinant (cofactor expansion) | Det 2 2⍴1 2 3 4 → ¯2 |
| Dot | ⍺ f ⍵ | vector, vector → scalar | Dot product (+/⍺×⍵) | (1 2 3) Dot (4 5 6) → 32 |
| Cross | ⍺ f ⍵ | vector, vector → vector | Cross product (3D vectors) | (1 2 3) Cross (4 5 6) → ¯3 6 ¯3 |
| Norm | f ⍵ | vector → scalar | Euclidean (L²) norm | Norm 3 4 → 5 |
| Norm1 | f ⍵ | vector → scalar | Manhattan (L¹) norm | Norm1 (¯3 4) → 7 |
| NormInf | f ⍵ | vector → scalar | Infinity norm (max |x|) | NormInf (¯3 4 ¯7) → 7 |
| NormFro | f ⍵ | matrix → scalar | Frobenius norm | NormFro 2 2⍴1 2 3 4 → √30 |
| Triu | f ⍵ | matrix → matrix | Upper triangular part | Triu M → zeros below diagonal |
| Tril | f ⍵ | matrix → matrix | Lower triangular part | Tril M → zeros above diagonal |
| FlipUD | f ⍵ | matrix → matrix | Flip upside-down (⊖) | FlipUD M → reversed rows |
| FlipLR | f ⍵ | matrix → matrix | Flip left-right (⌽) | FlipLR M → reversed columns |
| Rot90 | f ⍵ | matrix → matrix | Rotate 90° counter-clockwise | Rot90 M → ⍉⊖M |
| Repmat | ⍺ f ⍵ | vector, matrix → matrix | Tile matrix into m×n grid | (2,3) Repmat M → 2×3 copies |
| Kron | ⍺ f ⍵ | matrix, matrix → matrix | Kronecker (tensor) product | I₂ Kron σₓ → 4×4 block matrix |
| LU | f ⍵ | matrix → dict | LU decomposition {L, U} (Doolittle) | lu←LU A ⋄ lu.L +.× lu.U ≡ A |
| QR | f ⍵ | matrix → dict | QR decomposition {Q, R} (Gram-Schmidt) | qr←QR A ⋄ qr.Q +.× qr.R ≡ A |
| Eig | f ⍵ | matrix → dict | Dominant eigenvalue {val, vec} (power iter) | Eig 2 2⍴2 1 1 3 → {val:3.618…} |
| EigAll | f ⍵ | matrix → dict | All eigenvalues {vals} (deflation) | EigAll M → {vals: …} |
| Rank | f ⍵ | matrix → scalar | Matrix rank | Rank 3 3⍴1 2 3 4 5 6 7 8 9 → 2 |
| Cond | f ⍵ | matrix → scalar | Condition number | Cond (Hilb 4) → large number |
| Pinv | f ⍵ | matrix → matrix | Pseudoinverse (AᵀA)⁻¹Aᵀ | Pinv A → A⁺ |
Signal Processing sci/signal.mapl
Cooley-Tukey FFT, convolution, deconvolution, cross-correlation, and IIR/FIR digital filtering.
| Function | Type | Signature | Description | Example |
|---|---|---|---|---|
| FFT | f ⍵ | vector → vector | Fast Fourier Transform (radix-2) | FFT 1 1 1 1 0 0 0 0 → complex spectrum |
| IFFT | f ⍵ | vector → vector | Inverse FFT | IFFT FFT signal → signal (roundtrip) |
| Conv | ⍺ f ⍵ | vector, vector → vector | Discrete convolution | (1 2 3) Conv (4 5) → 4 13 22 15 |
| Deconv | ⍺ f ⍵ | vector, vector → dict | Deconvolution {Q: quotient, R: remainder} | (4 13 22 15) Deconv (4 5) → {Q:1 2 3} |
| XCorr | ⍺ f ⍵ | vector, vector → vector | Cross-correlation | signal XCorr template → lag peaks |
| Filter | ⍺ f ⍵ | dict, vector → vector | IIR/FIR digital filter {b, a} | {b:(1 1),a:(1 ¯0.5)} Filter impulse |
Numerical Methods sci/numerical.mapl
ODE solvers (Euler, RK4), root finding (bisection), numerical integration (Simpson, trapezoidal), differentiation, interpolation.
| Function | Type | Signature | Description | Example |
|---|---|---|---|---|
| OdeEuler | ⍺ f ⍵ | dict, scalar → vector | Euler ODE solver {f, h, n} | {f:{-⍵},h:0.1,n:10} OdeEuler 1 |
| OdeRK4 | ⍺ f ⍵ | dict, scalar → vector | Runge-Kutta 4th order {f, h, n} | {f:{-⍵},h:0.1,n:10} OdeRK4 1 |
| Fzero | ⍺ f ⍵ | dict, scalar → scalar | Root finder (bisection) {f, a, b} | {f:{(⍵*2)-2},a:0,b:2} Fzero 1E¯10 → √2 |
| Quad | ⍺ f ⍵ | dict, scalar → scalar | Numerical integration (Simpson's rule) | {f:{⍵*2},a:0,b:1} Quad 100 → 0.333… |
| Trapz | f ⍵ | vector → scalar | Trapezoidal integration from data | Trapz 0 1 4 9 → area under curve |
| HTrapz | ⍺ f ⍵ | scalar, vector → scalar | Trapezoidal with custom spacing h | 0.5 HTrapz data → area |
| Gradient | f ⍵ | vector → vector | Numerical differentiation (central diff) | Gradient 0 1 4 9 16 → approx 2x |
| Interp1 | ⍺ f ⍵ | dict, vector → vector | Linear interpolation {x, y} | {x:(0 1 2),y:(0 10 20)} Interp1 (0.5 1.5) |