Array

Struct Array 

Source
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: usize

Offset into data buffer for view (0 = no offset)

Implementations§

Source§

impl<T: DTypeValue> Array<T>

Source

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.

Source

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]);
Source

pub fn to_f32(&self) -> Array<f32>

Helper to convert any array to f32 (useful for tests and interoperability)

Source

pub fn zeros(shape: Vec<usize>) -> Self

Create an array initialized with zeros for a shape.

Source

pub fn ones(shape: Vec<usize>) -> Self

Create an array initialized with ones.

Source

pub fn shape(&self) -> &[usize]

Return shape as a slice

Source

pub fn dtype(&self) -> DType

Return the dtype of this array

Source

pub fn len(&self) -> usize

Helper to get length

Source

pub fn is_contiguous(&self) -> bool

Check if array is C-contiguous (no custom strides)

Source

pub fn compute_default_strides(&self) -> Vec<isize>

Compute C-contiguous strides for current shape

Source

pub fn get_strides(&self) -> Vec<isize>

Get effective strides (either custom or computed default)

Source

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

Source

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.

Source

pub fn to_hlo_const(&self) -> HloNode

Build an HLO graph node representing a constant array

Trait Implementations§

Source§

impl Add<&Array> for Array

Source§

fn add(self, rhs: &Array) -> Self::Output

Addition: a + &b

Source§

type Output = Array

The resulting type after applying the + operator.
Source§

impl Add<Array> for &Array

Source§

fn add(self, rhs: Array) -> Self::Output

Addition: &a + b

Source§

type Output = Array

The resulting type after applying the + operator.
Source§

impl Add for &Array

Source§

fn add(self, rhs: Self) -> Self::Output

Addition: &a + &b (most common, avoids moves)

Source§

type Output = Array

The resulting type after applying the + operator.
Source§

impl Add for Array

Source§

fn add(self, rhs: Self) -> Self::Output

Addition: a + b

Source§

type Output = Array

The resulting type after applying the + operator.
Source§

impl<T: Clone + DTypeValue> Clone for Array<T>

Source§

fn clone(&self) -> Array<T>

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + DTypeValue> Debug for Array<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<&Array> for Array

Source§

type Output = Array

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Array) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Array> for &Array

Source§

type Output = Array

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Array) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for &Array

Source§

fn div(self, rhs: Self) -> Self::Output

Element-wise division: &a / &b

Source§

type Output = Array

The resulting type after applying the / operator.
Source§

impl Div for Array

Source§

fn div(self, rhs: Self) -> Self::Output

Element-wise division: a / b

Source§

type Output = Array

The resulting type after applying the / operator.
Source§

impl From<Array<bool>> for DynArray

Source§

fn from(arr: Array<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<Array<f64>> for DynArray

Source§

fn from(arr: Array<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<Array<i32>> for DynArray

Source§

fn from(arr: Array<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<Array<i8>> for DynArray

Source§

fn from(arr: Array<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<Array<u8>> for DynArray

Source§

fn from(arr: Array<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<Array> for DynArray

Source§

fn from(arr: Array<f32>) -> Self

Converts to this type from the input type.
Source§

impl Mul<&Array> for Array

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Array) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Array> for &Array

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Array) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for &Array

Source§

fn mul(self, rhs: Self) -> Self::Output

Element-wise multiplication: &a * &b

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

impl Mul for Array

Source§

fn mul(self, rhs: Self) -> Self::Output

Element-wise multiplication: a * b

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

impl Neg for &Array

Source§

fn neg(self) -> Self::Output

Negation: -&a

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

impl Neg for Array

Source§

fn neg(self) -> Self::Output

Negation: -a

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

impl Sub<&Array> for Array

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Array) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Array> for &Array

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Array) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for &Array

Source§

fn sub(self, rhs: Self) -> Self::Output

Subtraction: &a - &b

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

impl Sub for Array

Source§

fn sub(self, rhs: Self) -> Self::Output

Subtraction: a - b

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

impl TryFrom<DynArray> for Array<f32>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(dyn_arr: DynArray) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<DynArray> for Array<bool>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(dyn_arr: DynArray) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<DynArray> for Array<f64>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(dyn_arr: DynArray) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<DynArray> for Array<i32>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(dyn_arr: DynArray) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<DynArray> for Array<i8>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(dyn_arr: DynArray) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<DynArray> for Array<u8>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(dyn_arr: DynArray) -> Result<Self>

Performs the conversion.

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,