Skip to main content

NDArray

Struct NDArray 

Source
pub struct NDArray<T, const N: usize> { /* private fields */ }
Expand description

A simple N-dimensional array type with shape, strides, and contiguous data storage.

§Example

use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 3], vec![1, 2, 3, 4, 5, 6]).unwrap();
assert_eq!(arr[[1, 2]], 6);

Implementations§

Source§

impl<T, const N: usize> NDArray<T, N>

Source

pub fn new(shape: [usize; N], data: Vec<T>) -> Result<Self, NDArrayError>

Creates a new NDArray with the given shape and data.

§Arguments
  • shape - The shape of the array as an array of dimension sizes.
  • data - The data to fill the array, must match the product of shape dimensions.
§Errors

Returns TimsUtilsError if the shape and data length are incompatible.

§Example
use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
assert_eq!(arr.shape(), [2, 2]);
assert_eq!(arr[[1, 1]], 4);
Source

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

Returns the shape of the array.

§Example
use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 3], vec![1, 2, 3, 4, 5, 6]).unwrap();
assert_eq!(arr.shape(), [2, 3]);
Source

pub fn index(&self, indices: [usize; N]) -> usize

Computes the flat index in the data vector for the given N-dimensional indices.

§Arguments
  • indices - The N-dimensional indices.
§Example
use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
let idx = arr.index([1, 1]);
assert_eq!(idx, 3);
Source

pub fn inverted_index(&self, idx: usize) -> [usize; N]

Converts a flat index into N-dimensional indices.

§Arguments
  • idx - The flat index.
§Example
use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
let indices = arr.inverted_index(3);
assert_eq!(indices, [1, 1]);
Source§

impl<T: Default + Copy + AddAssign, const N: usize> NDArray<T, N>

Source

pub fn project_axis(&self, axis: usize) -> Vec<T>

Projects the array along the specified axis, summing over all other axes.

§Arguments
  • axis - The axis to project onto.
§Returns

A vector of values, one for each index along the specified axis.

§Panics

Panics if the axis is out of bounds.

§Example
use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
let proj = arr.project_axis(0);
assert_eq!(proj, vec![1+2, 3+4]);
Source§

impl<T: Default, const N: usize> NDArray<T, N>

Source

pub fn empty(shape: [usize; N]) -> Self

Creates an empty NDArray with the given shape, filled with default values.

§Arguments
  • shape - The shape of the array.
§Example
use timsrust_utils::ndarray::NDArray;
let arr: NDArray<i32, 2> = NDArray::empty([2, 2]);
assert_eq!(arr.shape(), [2, 2]);
assert_eq!(arr[[1,1]], 0);

Trait Implementations§

Source§

impl<T: AddAssign + Copy, const N: usize> AddAssign for NDArray<T, N>

Source§

fn add_assign(&mut self, other: Self)

Adds another NDArray to this one, elementwise.

§Panics

Panics if the shapes do not match.

§Example
use timsrust_utils::ndarray::NDArray;
let mut a = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
let b = NDArray::new([2, 2], vec![10, 20, 30, 40]).unwrap();
a += b;
assert_eq!(a[[0, 0]], 11);
assert_eq!(a[[1, 1]], 44);
Source§

impl<T: Clone, const N: usize> Clone for NDArray<T, N>

Source§

fn clone(&self) -> NDArray<T, N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const N: usize> Debug for NDArray<T, N>

Source§

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

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

impl<T: Eq, const N: usize> Eq for NDArray<T, N>

Source§

impl<T: Hash, const N: usize> Hash for NDArray<T, N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, const N: usize> Index<[usize; N]> for NDArray<T, N>

Source§

fn index(&self, indices: [usize; N]) -> &Self::Output

Indexes the array using N-dimensional indices.

§Example
use timsrust_utils::ndarray::NDArray;
let arr = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
assert_eq!(arr[[1, 1]], 4);
Source§

type Output = T

The returned type after indexing.
Source§

impl<T, const N: usize> IndexMut<[usize; N]> for NDArray<T, N>

Source§

fn index_mut(&mut self, indices: [usize; N]) -> &mut Self::Output

Mutable indexing using N-dimensional indices.

§Example
use timsrust_utils::ndarray::NDArray;
let mut arr = NDArray::new([2, 2], vec![1, 2, 3, 4]).unwrap();
arr[[0, 0]] = 10;
assert_eq!(arr[[0, 0]], 10);
Source§

impl<T: PartialEq, const N: usize> PartialEq for NDArray<T, N>

Source§

fn eq(&self, other: &NDArray<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const N: usize> StructuralPartialEq for NDArray<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for NDArray<T, N>

§

impl<T, const N: usize> RefUnwindSafe for NDArray<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for NDArray<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for NDArray<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for NDArray<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for NDArray<T, N>

§

impl<T, const N: usize> UnwindSafe for NDArray<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.