Tensors, element-wise math, reductions, linear algebra, random numbers, signal processing, sparse arrays, and numeric loading.
The math module is built in — no file needed. It provides tensor creation, element-wise operations, reductions, shape operations, and linear algebra. After use math, all functions are called directly (e.g. zeros[3], not math.zeros[3]).
| Function | Description | Example |
|---|---|---|
zeros[d1, d2, ...] | Tensor of zeros with given shape | Automatic differentiation · Math: tensors |
ones[d1, d2, ...] | Tensor of ones with given shape | Math: tensors |
full[d1, d2, ..., value] | Tensor filled with value (last arg) | Math: tensors |
eye[n] | n×n identity matrix | Math: reductions and linear algebra · Math: tensors |
onehot[targets, classes] | A row per target, 1 in its class's column and 0 elsewhere: the targets a vector of whole numbers from 0 to classes − 1 (a target that is no class stops the program: onehot: 7 is not a class of 3 (0 to 2)) | Math: tensors |
diag[tensor] | Extract diagonal from a 2D tensor | Math: tensors |
linspace[start, end, n] | n evenly spaced values from start to end | Math: random, signal, special, and sparse · Math: tensors |
arange[start, end, step] | Values from start to end by step | Math: tensors · The standard library's layer: use sys |
randn[d1, d2, ...] | Normal random values (mean=0, std=1); written in Crow (see Random Numbers) | Math: tensors |
tensor[array] | Convert an array to a tensor | Math: tensors |
| Function | Description | Example |
|---|---|---|
exp[t] | Element-wise exponential | Automatic differentiation · Math: tensors |
ln[t] | Element-wise natural log | Math: tensors |
log2[t] | Element-wise base-2 logarithm | Math: tensors |
log10[t] | Element-wise base-10 logarithm | Math: tensors |
sqrt[t] | Element-wise square root | Math: tensors · Numbers and conversion · Data Types |
abs[t] | Element-wise absolute value | Frames: use frame · Math: tensors · Numbers and conversion · The Element Vocabulary |
sign[t] | Element-wise sign (-1, 0, or 1) | Math: tensors · Web: responses and submodules |
floor[t] | Element-wise floor | Math: tensors · Numbers and conversion |
ceil[t] | Element-wise ceiling | Numbers and conversion |
round[t] | Element-wise round to nearest | Math: reductions and linear algebra · Math: random, signal, special, and sparse · Math: tensors · Numbers and conversion · Data Types |
clip[t, lo, hi] | Clamp values to [lo, hi] | Math: tensors |
mod[t, divisor] | Element-wise modulo | Math: tensors |
sin[t] | Element-wise sine | Automatic differentiation · Math: tensors |
cos[t] | Element-wise cosine | Math: tensors |
tan[t] | Element-wise tangent | Math: tensors |
asin[t] | Element-wise arcsine | Math: tensors |
acos[t] | Element-wise arccosine | Math: tensors |
atan[t] | Element-wise arctangent | Math: tensors |
atan2[y, x] | Element-wise two-argument arctangent | Math: tensors |
sinh[t] | Element-wise hyperbolic sine | Math: tensors |
cosh[t] | Element-wise hyperbolic cosine | Math: tensors |
tanh[t] | Element-wise hyperbolic tangent | Math: tensors |
All reductions except argmax/argmin accept an optional axis parameter: sum[t, axis] reduces along that axis and returns a tensor instead of a scalar.
| Function | Returns | Example |
|---|---|---|
argsort[t] | The indices that would sort the numbers, ascending: a list of ints, as range answers one, so xs[order[0]] reads the smallest (it had answered a tensor, whose items read as floats and indexed nothing) | Math: reductions and linear algebra |
topk[t, k] | Top k largest values, sorted descending (NumArray) | Math: reductions and linear algebra |
unique[t] | Unique values, sorted (NumArray) | Frames: use frame · Math: reductions and linear algebra |
where[cond, a, b] | Element-wise: a where cond≠0, else b (NumArray); cond a mask too — where[v gt 15.0, v, 0.0 * v] — as a mask is numbers where numbers are asked (true 1, false 0; sum[v gt 15.0] counts) | Frames: use frame · Math: reductions and linear algebra · Math: tensors · Project: Analyzing Data |
Note: sort[t] is a core builtin (not in the math module) that works on both arrays and tensors.
| Function | Returns | Example |
|---|---|---|
cumsum[t] | Cumulative sum (NumArray, same shape) | Math: reductions and linear algebra |
cumprod[t] | Cumulative product (NumArray, same shape) | Math: reductions and linear algebra |
| Function | Returns | Example |
|---|---|---|
stack[tensors] | Stack tensors along new axis 0 (NumArray) | Frames: use frame · Math: tensors · Views: the whole element vocabulary · View Blocks · Reactive State and Events · Slow Work and the Live Loop · Testing a Live App · Charts and Styling · The Element Vocabulary · Derivations |
stack[tensors, axis] | Stack tensors along new axis (NumArray) | Frames: use frame · Math: tensors · Views: the whole element vocabulary · View Blocks · Reactive State and Events · Slow Work and the Live Loop · Testing a Live App · Charts and Styling · The Element Vocabulary · Derivations |
Note: concat[a, b, axis] is a core builtin (not in the math module) — one merge operation for all arrays and tensors. It replaced the former append.
Note: slice[t, start, end] is a core builtin (not in the math module) that works on arrays, tensors, and strings.
Element-wise operations on tensors with different shapes automatically broadcast following NumPy rules:
use math
a = reshape[linspace[1, 3, 3], 3, 1] --- shape [3, 1]
b = reshape[linspace[10, 40, 4], 1, 4] --- shape [1, 4]
c = a + b --- shape [3, 4]
Broadcasting works with all element-wise operators: +, -, .*, ./, .^.
Incompatible shapes (e.g. [3] + [4]) are refused before the program runs where both shapes are known (see Tensor Arithmetic), and stop the program where it runs where they are not, with the same words in both modes.
Tensors can be indexed with another tensor to gather elements by index:
a = linspace[10, 50, 5]
indices = linspace[0, 2, 3]
y = a[indices] --- gathers elements at indices 0, 1, 2
Negative indices are supported (wrap around from the end). The result tensor has the same shape as the index tensor.
| Function | Description | Example |
|---|---|---|
reshape[t, d1, d2, ...] | Reshape to new dimensions | Math: tensors |
flatten[t] | Flatten to 1D | Automatic differentiation · Math: tensors |
transpose[t] | Transpose (swap last two axes) | Math: reductions and linear algebra · Math: tensors |
| Function | Description | Example |
|---|---|---|
seed[n] | Set the program's RNG seed: every later draw, tensors and the core rand[] alike, is repeatable and identical in both modes. Statements run in the order written, and a loop that draws runs in order (§14), so a seeded program draws the same numbers on every run | Math: random, signal, special, and sparse · Math: tensors |
randn[d1, d2, ...] | Standard normal (mean=0, std=1): Box–Muller over two uniform draws a value, written in Crow (src/modules/math.co, design rule 20); a negative size stops the program | Math: tensors |
normal[mean, std, d1, d2, ...] | Normal with custom mean/std: mean + std · randn, written in Crow; a negative std stops the program | Math: random, signal, special, and sparse |
uniform[low, high, d1, d2, ...] | Uniform in [low, high): uniform[0.0, 1.0, 2, 3] is a 2×3 tensor of values in [0, 1) | Math: random, signal, special, and sparse |
poisson[lambda, d1, d2, ...] | Poisson distribution | Math: random, signal, special, and sparse |
bernoulli[p, d1, d2, ...] | Bernoulli (0.0 or 1.0) | Math: random, signal, special, and sparse |
gamma_dist[alpha, beta, d1, ...] | Gamma distribution | Math: random, signal, special, and sparse |
beta_dist[alpha, beta, d1, ...] | Beta distribution | Math: random, signal, special, and sparse |
exponential[lambda, d1, ...] | Exponential distribution | Math: random, signal, special, and sparse |
shuffle[x] | Random permutation of tensor elements | Math: random, signal, special, and sparse |
choice[x, n] | Random sample of n elements (no replacement) | Math: random, signal, special, and sparse |
permutation[n] | Random permutation of 0..n | Math: random, signal, special, and sparse |
rand is core's: rand[] is one float in [0, 1), and rand[n] and rand[min, max] are ints (see Core Builtins). A tensor of values in [0, 1) is uniform[0.0, 1.0, d1, …].
Design Note: no tensor rand. Considered: rand[d1, d2, …] for a tensor of uniform values in [0, 1), as NumPy spells it. The math module had it until March 2026, when its functions became callable without math. and core's rand kept the name; this table went on listing it, and its examples made ints (rand[2, 3] is 2 or 3). Rejected: rand[5] and rand[2, 3] are core's, an int below 5 and an int from 2 to 3, so the same call would mean two things, and use math would change what a program computes. Decided: uniform[0.0, 1.0, …] makes the tensor, the one way to.
| Function | Description | Example |
|---|---|---|
fft[x] | 1D FFT → returns [real, imag] tensors | Math: random, signal, special, and sparse |
ifft[real, imag] | Inverse 1D FFT → returns real tensor | Math: random, signal, special, and sparse |
fft2[m] | 2D FFT → returns [real, imag] tensors | Math: random, signal, special, and sparse |
conv[a, b] | 1D convolution (full), length = len(a)+len(b)-1 | Math: random, signal, special, and sparse |
conv2d[a, b] | 2D convolution (full) | Math: random, signal, special, and sparse |
hamming[n] | Hamming window of length n | Math: random, signal, special, and sparse |
hanning[n] | Hanning (Hann) window of length n | Math: random, signal, special, and sparse |
stft[x, window_size, hop_size] | Short-Time Fourier Transform → [real, imag] 2D tensors [frames × window_size] | Math: random, signal, special, and sparse |
spectrogram[x, window_size, hop_size] | Power spectrogram (magnitude² of STFT) → 2D tensor | Math: random, signal, special, and sparse |
lowpass[x, cutoff_hz, sample_rate] | FFT-based lowpass filter | Math: random, signal, special, and sparse |
highpass[x, cutoff_hz, sample_rate] | FFT-based highpass filter | Math: random, signal, special, and sparse |
bandpass[x, low_hz, high_hz, sample_rate] | FFT-based bandpass filter | Math: random, signal, special, and sparse |
| Function | Description | Example |
|---|---|---|
gamma[x] | Gamma function Γ(x), element-wise | Math: random, signal, special, and sparse · The standard library's layer: use sys |
erf[x] | Error function erf(x), of a number or element-wise over a tensor (it had taken a tensor only) | Math: random, signal, special, and sparse |
fma[a, b, c] | a × b + c rounded once: the fused multiply-add a compiled loop accumulates with (pandas's rolling kernels, scikit-learn's row norms, scipy's sparse products), so a sum held to one of them runs with it — total = fma[x, x, total] | Math: random, signal, special, and sparse |
bessel[x, n] | Bessel function J_n(x), element-wise | Math: random, signal, special, and sparse |
factorial[x] | Factorial x! = Γ(x+1), element-wise | Math: random, signal, special, and sparse |
choose[n, k] | Binomial coefficient C(n,k), element-wise | Math: random, signal, special, and sparse · The Element Vocabulary |
Complex values are represented as [real_tensor, imag_tensor] pairs.
| Function | Description | Example |
|---|---|---|
complex[re, im] | Create complex value → [real, imag] | Math: random, signal, special, and sparse |
real[c] | Extract real part tensor | Math: random, signal, special, and sparse |
imag[c] | Extract imaginary part tensor | Math: random, signal, special, and sparse |
magnitude[c] | Magnitude |c| = sqrt(re² + im²) | Math: random, signal, special, and sparse |
phase[c] | Phase angle = atan2(im, re) | Math: random, signal, special, and sparse |
conjugate[c] | Complex conjugate → [re, -im] | Math: random, signal, special, and sparse |
Sparse matrices store only non-zero entries, using CSR (Compressed Sparse Row) format internally. They use the same operators as dense arrays — the runtime dispatches to sparse-optimized algorithms automatically.
use math
--- dense to sparse
s = sparse[eye[3]]
log[s] --- sparse[3x3, nnz=3]
--- COO creation
s2 = sparse[[0, 1, 2], [0, 1, 2], [5, 6, 7], [3, 3]]
--- sparse matmul
r = s * [1, 2, 3]
--- sparse add
s3 = s + s2
--- back to dense
d = dense[s3]
--- sparse solve
x = solve[s2, [10, 12, 14]]
| Function | Description | Example |
|---|---|---|
load[path] | Load a table of numbers directly into a 2D tensor: a CSV file, or JSON records or Parquet, from a file, a URL or an S3 object (error-value on failure) | Databases: use db · Frames: use frame · Web: responses and submodules · Project: Analyzing Data · Reactive State and Events |
load[path] reads a CSV file with a header row and parses all values as floats into a 2D tensor (rows × columns). This is much faster than csv[path] (io module) for numeric data because it avoids creating intermediate string maps.
use math
data = load['data.csv']
log[data.shape] --- [1000, 10]
log[sum[data]] --- total of all values
use math
a = zeros[3, 3]
b = eye[3]
c = a * b
d = det[b] --- 1.0
--- constants
log[pi] --- 3.141592653589793
--- properties
log[a.shape] --- [3, 3]
log[a.dtype] --- float
log[a.device] --- cpu
Generated by crow test --docs --write from the language specification, section 11 and 13. Edit the spec, not this page.