Types
An N-dimensional numeric array (NdArray<T>) over dense native storage.
The element type parameter is the storage dtype: bool, an integer width, f32, or float. Produced by the std.ndarray constructors (zeros, ones, full, arange, linspace, from_array, from_nested, from_bytes); reshaped, transposed, and sliced in place; consumed by elementwise arithmetic and comparisons, reductions (sum, mean, min, max and their _axis forms), and the matrix products matmul and dot.
use std.ndarray
use std.ndarray.NdArray
fn main() {
let grid: NdArray<i32> = ndarray.zeros<i32>([2, 3])
grid.fill(2)
println(grid.shape())
println(grid.rank())
println(grid.sum())
println(grid.mean_axis(1).to_array())
println(ndarray.arange(0, 4, 1).to_array())
}A contiguous rank-one view of an NdArray<T>'s elements (NdSlice<T>), returned by view_contiguous.
The view shares the array's storage rather than copying it: writes through either are visible to both, and the view borrows the array for as long as it is live. Its surface is the three operations an element loop needs — len, get and set — because everything else is a question for the NdArray it came from, which is still there and still the owner.
It exists so a loop over elements costs what a loop over [T] costs. The checker knows the rank is one and the dtype is T, so a read is one scalar index and a typed load, with no stride arithmetic, no contiguity test and no dtype switch in the loop body.
use std.ndarray
fn main() {
let grid = ndarray.zeros<f32>([2, 3])
match grid.view_contiguous() {
ok(cells) => {
for i in 0..cells.len() { cells.set(i, (i * 2) as f32) }
println(cells.get(5))
},
err(e) => println(e),
}
}