pub struct Array<T: DTypeValue = f32> {
pub shape: Vec<usize>,
pub dtype: DType,
pub data: Vec<T>,
pub strides: Option<Vec<isize>>,
pub offset: usize,
}Expand description
A lightweight n-dimensional array with generic dtype support.
Arrays are memory contiguous (row-major) and can hold different data types.
The type parameter T must implement DTypeValue trait.
Arrays now support zero-copy views with strides and offset:
strides: Step size in each dimension (None = C-contiguous)offset: Starting offset in data buffer (0 = start at beginning)
Fields§
§shape: Vec<usize>§dtype: DType§data: Vec<T>§strides: Option<Vec<isize>>Strides for each dimension (None = C-contiguous layout)
offset: usizeOffset into data buffer for view (0 = no offset)
Implementations§
Source§impl<T: DTypeValue> Array<T>
impl<T: DTypeValue> Array<T>
Sourcepub fn new(shape: Vec<usize>, data: Vec<T>) -> Self
pub fn new(shape: Vec<usize>, data: Vec<T>) -> Self
Create a new array given a shape and a flat data vector. The dtype is automatically inferred from type T.
Sourcepub fn from_vec<U: DTypeValue>(shape: Vec<usize>, data: Vec<U>) -> Self
pub fn from_vec<U: DTypeValue>(shape: Vec<usize>, data: Vec<U>) -> Self
Create a new array with implicit type conversion from any DTypeValue.
This allows you to create arrays with automatic casting from input data to the target dtype using the promotion engine.
§Examples
use numrs::Array;
// Create f32 array from i32 data - automatic conversion
let arr = Array::<f32>::from_vec(vec![3], vec![1i32, 2, 3]);
assert_eq!(arr.data, vec![1.0f32, 2.0, 3.0]);
// Create i32 array from f32 data - automatic conversion
let arr = Array::<i32>::from_vec(vec![2], vec![1.5f32, 2.7]);
assert_eq!(arr.data, vec![1, 2]);Sourcepub fn to_f32(&self) -> Array<f32>
pub fn to_f32(&self) -> Array<f32>
Helper to convert any array to f32 (useful for tests and interoperability)
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if array is C-contiguous (no custom strides)
Sourcepub fn compute_default_strides(&self) -> Vec<isize>
pub fn compute_default_strides(&self) -> Vec<isize>
Compute C-contiguous strides for current shape
Sourcepub fn get_strides(&self) -> Vec<isize>
pub fn get_strides(&self) -> Vec<isize>
Get effective strides (either custom or computed default)
Sourcepub fn broadcast_view(&self, target_shape: &[usize]) -> Result<Self>
pub fn broadcast_view(&self, target_shape: &[usize]) -> Result<Self>
Create a broadcast view without copying data
This creates a zero-copy view that logically broadcasts the array to a new shape by adjusting strides (setting stride to 0 for broadcast dims).
§Arguments
target_shape- The shape to broadcast to
§Returns
A new Array that shares the same data but appears to have the target shape
Sourcepub fn to_contiguous(&self) -> Self
pub fn to_contiguous(&self) -> Self
Materialize a view into a contiguous array
If the array is already contiguous, returns a clone. Otherwise, creates a new contiguous array with the data arranged properly.
Sourcepub fn to_hlo_const(&self) -> HloNode
pub fn to_hlo_const(&self) -> HloNode
Build an HLO graph node representing a constant array
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Array<T>
impl<T> RefUnwindSafe for Array<T>where
T: RefUnwindSafe,
impl<T> Send for Array<T>
impl<T> Sync for Array<T>
impl<T> Unpin for Array<T>where
T: Unpin,
impl<T> UnwindSafe for Array<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more