Skip to main content

Array

Struct Array 

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

Fixed-size numeric vector backed by [T; N].

Unlike a heap-backed Vec, the length is part of the type, so no allocation or bounds checks are needed and the whole value can live on the stack or in a static. Works in no_std environments.

§Example

use mathverse_core::arrays::Array;

let a = Array::from([1.0f64, 2.0, 3.0]);
let b = Array::ones();
let c = a + b;
assert_eq!(c, Array::from([2.0, 3.0, 4.0]));
assert!((a.dot(&b) - 6.0).abs() < 1e-12);

Implementations§

Source§

impl<T: Num, const N: usize> Array<T, N>

Source

pub const fn new(data: [T; N]) -> Self

Construct from a raw array.

Source

pub fn from_fn<F: FnMut(usize) -> T>(f: F) -> Self

Build from a mapping function f(i).

use mathverse_core::arrays::Array;
let a = Array::from_fn(|i| i as f64 * 2.0);
assert_eq!(a, Array::from([0.0, 2.0, 4.0]));
Source

pub fn zeros() -> Self

All elements set to T::zero().

use mathverse_core::arrays::Array;
assert_eq!(Array::<f64, 3>::zeros(), Array::from([0.0; 3]));
Source

pub fn ones() -> Self

All elements set to T::one().

Source

pub fn as_slice(&self) -> &[T]

The backing slice (read-only).

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

The backing mutable slice.

Source

pub fn get(&self, i: usize) -> T

Get element i.

§Panics

Panics if i >= N.

Source

pub fn set(&mut self, i: usize, value: T)

Set element i in place.

§Panics

Panics if i >= N.

Source

pub fn try_get(&self, i: usize) -> Option<T>

Element at index i without panicking.

Returns None if i >= N.

Source

pub fn sum(&self) -> T

Sum of all elements using Kahan-compensated summation for numerical stability with floating-point types.

Source

pub fn dot(&self, other: &Self) -> T

Dot (scalar) product with another array of the same length. Uses Kahan-compensated summation for numerical stability.

use mathverse_core::arrays::Array;
let a = Array::from([1.0f64, 2.0, 3.0]);
let b = Array::from([4.0, 5.0, 6.0]);
assert!((a.dot(&b) - 32.0).abs() < 1e-12);
Source

pub fn map<U: Num, F: FnMut(T) -> U>(&self, f: F) -> Array<U, N>

Element-wise apply of f.

Source

pub fn zip_with<U: Num, V: Num, F: FnMut(T, U) -> V>( &self, other: &Array<U, N>, f: F, ) -> Array<V, N>

Zip with another array and combine with f.

Source

pub fn component_mul(&self, other: &Self) -> Self

Element-wise a * b.

Source

pub fn component_div(&self, other: &Self) -> Self
where T: Div<Output = T>,

Element-wise a / b.

Source

pub fn cumulative_sum(&self) -> Self

Cumulative sum (prefix sum).

use mathverse_core::arrays::Array;
let a = Array::from([1.0f64, 2.0, 3.0]);
assert_eq!(a.cumulative_sum(), Array::from([1.0, 3.0, 6.0]));
Source

pub fn reversed(&self) -> Self

Reverse the elements.

Source

pub fn all_eq(&self, other: &Self) -> bool
where T: PartialEq,

True when every element compares equal to the corresponding one in other.

Source§

impl<T: Num, const N: usize> Array<T, N>

Source

pub fn add(&self, other: &Self) -> Self

Element-wise addition.

Source

pub fn sub(&self, other: &Self) -> Self

Element-wise subtraction.

Source

pub fn scalar_mul(&self, s: T) -> Self

Scalar multiply: self * s.

Source

pub fn scalar_add(&self, s: T) -> Self

Scalar addition: self + s.

Source

pub fn scalar_div(&self, s: T) -> Self
where T: Div<Output = T>,

Divide every element by s.

Source§

impl<T: Real, const N: usize> Array<T, N>

Source

pub fn l2_norm(&self) -> T

Euclidean (L2) norm.

use mathverse_core::arrays::Array;
let a = Array::from([3.0f64, 4.0]);
assert!((a.l2_norm() - 5.0).abs() < 1e-12);
Source

pub fn l1_norm(&self) -> T

Manhattan (L1) norm.

Source

pub fn max_norm(&self) -> T

Maximum (L∞ / Chebyshev) norm.

Source

pub fn try_normalize(&self) -> Option<Self>

Normalize to unit length; returns None for a zero vector.

Source

pub fn normalize(&self) -> Self

Normalize to unit length; a zero vector stays zero.

Source

pub fn mean(&self) -> T

Mean of the elements.

Trait Implementations§

Source§

impl<T: Num, const N: usize> Add for Array<T, N>

Source§

type Output = Array<T, N>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T, const N: usize> AsRef<[T]> for Array<T, N>

Source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

Source§

fn clone(&self) -> Self

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: Copy, const N: usize> Copy for Array<T, N>

Source§

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

Source§

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

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

impl<T: Num, const N: usize> Default for Array<T, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Num + Div<Output = T>, const N: usize> Div<T> for Array<T, N>

Source§

type Output = Array<T, N>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T: Num + Eq, const N: usize> Eq for Array<T, N>

Source§

impl<T, const N: usize> From<Array<T, N>> for [T; N]

Source§

fn from(arr: Array<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: usize> From<[T; N]> for Array<T, N>

Source§

fn from(data: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: usize> Index<usize> for Array<T, N>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: Num, const N: usize> Mul<T> for Array<T, N>

Source§

type Output = Array<T, N>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialEq, const N: usize> StructuralPartialEq for Array<T, N>

Source§

impl<T: Num, const N: usize> Sub for Array<T, N>

Source§

type Output = Array<T, N>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Array<T, N>
where [T; N]: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Array<T, N>

§

impl<T, const N: usize> Send for Array<T, N>
where [T; N]: Send,

§

impl<T, const N: usize> Sync for Array<T, N>
where [T; N]: Sync,

§

impl<T, const N: usize> Unpin for Array<T, N>
where [T; N]: Unpin,

§

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

§

impl<T, const N: usize> UnwindSafe for Array<T, N>

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.