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>
impl<T: Num, const N: usize> Array<T, N>
Sourcepub fn from_fn<F: FnMut(usize) -> T>(f: F) -> Self
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]));Sourcepub fn zeros() -> Self
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]));Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
The backing mutable slice.
Sourcepub fn try_get(&self, i: usize) -> Option<T>
pub fn try_get(&self, i: usize) -> Option<T>
Element at index i without panicking.
Returns None if i >= N.
Sourcepub fn sum(&self) -> T
pub fn sum(&self) -> T
Sum of all elements using Kahan-compensated summation for numerical stability with floating-point types.
Sourcepub fn dot(&self, other: &Self) -> T
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);Sourcepub fn zip_with<U: Num, V: Num, F: FnMut(T, U) -> V>(
&self,
other: &Array<U, N>,
f: F,
) -> Array<V, N>
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.
Sourcepub fn component_mul(&self, other: &Self) -> Self
pub fn component_mul(&self, other: &Self) -> Self
Element-wise a * b.
Sourcepub fn component_div(&self, other: &Self) -> Selfwhere
T: Div<Output = T>,
pub fn component_div(&self, other: &Self) -> Selfwhere
T: Div<Output = T>,
Element-wise a / b.
Sourcepub fn cumulative_sum(&self) -> Self
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§impl<T: Num, const N: usize> Array<T, N>
impl<T: Num, const N: usize> Array<T, N>
Sourcepub fn scalar_mul(&self, s: T) -> Self
pub fn scalar_mul(&self, s: T) -> Self
Scalar multiply: self * s.
Sourcepub fn scalar_add(&self, s: T) -> Self
pub fn scalar_add(&self, s: T) -> Self
Scalar addition: self + s.
Sourcepub fn scalar_div(&self, s: T) -> Selfwhere
T: Div<Output = T>,
pub fn scalar_div(&self, s: T) -> Selfwhere
T: Div<Output = T>,
Divide every element by s.
Source§impl<T: Real, const N: usize> Array<T, N>
impl<T: Real, const N: usize> Array<T, N>
Sourcepub fn l2_norm(&self) -> T
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);Sourcepub fn try_normalize(&self) -> Option<Self>
pub fn try_normalize(&self) -> Option<Self>
Normalize to unit length; returns None for a zero vector.