Expand description
Enjoyable, composable, hackable N-dimensional arrays for Rust.
rapl combines the familiar parts of NumPy with array programming
languages like APL and BQN. Ranks are part of the type, so rank mistakes
are compile errors, and every operation is built from a small set of
composable primitives.
use rapl::*;
let x = Ndarr::from([3, 1, 4, 1, 5, 9, 2, 6]);
let windows = x.slice(s![Win(3)])?; // a [6, 3] view, no copy
let peaks = windows.reduce(1, i32::max)?;
assert_eq!(peaks, Ndarr::from([4, 4, 5, 9, 9, 9]));
let a = Ndarr::from([1, 2, 3]);
let b = Ndarr::from([[1, 2, 3], [4, 5, 6]]);
assert_eq!(a + b - 1, Ndarr::from([[1, 3, 5], [4, 6, 8]])); // broadcasting
println!("{peaks}");
// ┌→──────────┐
// │4 4 5 9 9 9│
// └~──────────┘rapl is in early development: the API is still settling and speed is not
yet a priority. See the README for a
tour of the API.
Re-exports§
pub use complex::*;
Modules§
Macros§
- s
- Build a basic-indexing selection: every input axis is named by exactly one
non-
NewAxisselector. An integer removes its axis,NewAxisinserts a length-one axis, ranges andSlicekeep theirs, andWinturns its axis into two: positions then window.range;steptakes everystep-th element of a range (..;-1reverses). Indices, bounds, and steps may be any primitive integer type.
Structs§
- B0
- The type-level bit 0.
- B1
- The type-level bit 1.
- Dim
- DimError
- Dyn
- A rank whose number of axes is known only at runtime.
- Elem
Iter - Borrowed elements of an array in logical C-order.
- Keep
- A reduction axis that stays in the result with extent 1, so the result
broadcasts back against its input: NumPy’s
keepdims=True. A bareusizeaxis is removed instead. The axis is named once, so the reduction and the re-inserted axis cannot disagree. - Ndarr
- N-dimensional array of
Twith rankR. Values live inbuffer; the offset, shape, and strides describe the view onto it. - NewAxis
- Inserts a length-one axis in
Ndarr::slice; consumes no input axis. - Slice
- One rank-preserving axis selection:
start..endtaken everystepelements, normalized like NumPy (negative values count from the end, and out-of-range bounds clamp). Ins!it is writtenlo..hi;step,..;-1, and so on; ranges convert withSlice::from. - Win
- Sliding windows of extent
winNdarr::slice: one input axis (extentn, strides) becomes a position axis (n - w + 1, strides) then a window axis (w, strides), sowindows[p, k] == input[p + k]— an O(1) affine view, no copy. Windows alias one element into many windows, soNdarr::slice_mutrejectsWin.
Enums§
- Scan
Direction - Order in which
Ndarr::scan_axisvisits each lane. - Slice
Spec - One runtime selector of the basic-indexing grammar.
Traits§
- Broadcast
Rank - Buffer
- Read-only access to a contiguous buffer of
T. Sealed: the only buffers areVec<T>(owned),&[T], and&mut [T]. - Buffer
Mut - Mutable access to a contiguous buffer of
T. - Bump
Rank - Fold step for the output rank of a selection:
U0leaves the rank alone,U1appliesInsertAxisRankonce,U2twice (aWinselector contributes two output axes). - Cell
Axes - A cell count for
Ndarr::map_cells: typenum counts keep the cell rank and a fixed frame rank;usizecounts selectDynfor both. Sealed; generic callers may use this bound. - Contract
Axes - A contraction count:
kpairs trailingselfaxes with leadingotheraxes;(s, k)first sharessleading axes (a batch both operands walk together), then contractsk. Typenum values preserve fixed output ranks;usizecounts selectDyn. Sealed; generic callers may use this bound. - Contract
Rank - Rank resulting from contracting
Kaxis pairs while sharingSleading axes (L + R - S - 2K). Fixed operands retain compile-time count guards; a dynamic operand makes the output dynamic. - Frame
Rank - Rank of the frame left after taking
Ktrailing axes as cells. Fixed ranks checkKat compile time;DynstaysDyn. - Index
Int - A primitive integer usable as an index, bound, or step in
s!. Every type is accepted, so integer literals still infer (they fall back toi32) andusizeextents need no casts. Values beyondisizesaturate, which selects exactly what the out-of-range value would. - Insert
Axis Rank - Rank
- Selects storage for per-axis metadata. Every typenum rank uses an exact
inline
GenericArray; onlyDynuses aVec, so fixed rank and metadata length cannot disagree. - Rank
Store - Storage used for the shape or strides selected by a
Rank. - Reduce
Axis - The axis argument of
Ndarr::reduceandNdarr::fold_axis: ausizeremoves the axis (output rankRemoved<R>);Keepkeeps it with extent 1 (rankR). Sealed; generic callers may use this bound. - Remove
Axis Rank - Scalar
- Marker for element-like types, keeping blanket scalar operator impls coherent next to the array impls.
- Selector
- One typed selector of the basic-indexing grammar.
Outis the number of output axes it produces:U0for a scalar index,U2forWin,U1otherwise. - Slice
Args - A full selection for
Ndarr::slice/Ndarr::slice_mut: computes the output rank and lowers to runtimeSliceSpecs. Implemented for tuples ofSelectors (built bys!) with compile-time output rank, and for&[SliceSpec]as the runtime escape hatch (output rankDyn). - Static
Rank - A fixed typenum rank. Algorithms using typenum arithmetic may require this
stronger bound; ordinary structural APIs should prefer
Rank.