Skip to main content

Module indexing

Module indexing 

Source
Expand description

NumPy-style indexing: Array::i reads elements and Array::at returns an array with some elements updated, like x[...] and x.at[...].set(...) in JAX.

An index is an integer, a range (1..3, ..2, ..), a Slice with a step, a run-time index (an integer Scalar), NewAxis, or Ellipsis, or a tuple of them. Dimensions the index does not mention are taken whole. The computations are those of JAX: reading with static indices uses slice, reading with run-time indices dynamic_slice, and updates scatter (dropping out-of-bounds updates).

§Examples

use ruxla_core::{Array, I32, Native, indexing::Slice};

let x = Array::<Native, i32>::from_vec((0..12).collect(), [3, 4]);
assert_eq!(x.i(1).to_vec(), [4, 5, 6, 7]);
assert_eq!(x.i((.., 0)).to_vec(), [0, 4, 8]);
assert_eq!(x.i((1..3, -1)).to_vec(), [7, 11]);
assert_eq!(x.i((0, Slice::new(None, None).step(-2))).to_vec(), [3, 1]);

// A run-time index, such as a position in an environment's state.
let row = I32::<Native>::new(2);
assert_eq!(x.get((row, 1)).value(), 9);
assert_eq!(x.at((0, 1..3)).set(-1).i(0).to_vec(), [0, -1, -1, 3]);
assert_eq!(x.at(row).add(100).i(2).to_vec(), [108, 109, 110, 111]);

Structs§

At
Updates of the elements of an array at an index (Array::at).
AtMut
Updates of the elements of an array that replace it (Array::at_mut).
Ellipsis
As many whole dimensions as needed (... in NumPy).
Indices
The components of an index: up to four inline, more on the heap.
NewAxis
Inserts a dimension of size 1 (None in NumPy).
Slice
A slice with an optional start and stop and a step, like Python’s start:stop:step. Rust ranges convert to slices with step 1.

Enums§

Index
One component of an index.

Traits§

IntoIndices
Something that can index an array: one Index component or a tuple.