Array

Struct Array 

Source
pub struct Array { /* private fields */ }
Expand description

A multidimensional numeric array.

This is the core data type of jax-rs, equivalent to jax.Array in JAX or torch.Tensor in PyTorch. Unlike jax-js which uses manual reference counting (.ref and .dispose()), Rust’s ownership system provides automatic memory management.

§Memory Model

Arrays own their data through an Arc<Buffer>, allowing cheap cloning and zero-copy views. When the last reference to a buffer is dropped, the memory is automatically freed.

§Examples

// Create a 2x3 array of zeros
let a = Array::zeros(Shape::new(vec![2, 3]), DType::Float32);
assert_eq!(a.shape().as_slice(), &[2, 3]);

Implementations§

Source§

impl Array

Source

pub fn zeros(shape: Shape, dtype: DType) -> Self

Create a new array filled with zeros.

§Examples
let a = Array::zeros(Shape::new(vec![2, 3]), DType::Float32);
assert_eq!(a.shape().as_slice(), &[2, 3]);
assert_eq!(a.dtype(), DType::Float32);
Source

pub fn ones(shape: Shape, dtype: DType) -> Self

Create a new array filled with ones.

Source

pub fn full(value: f32, shape: Shape, dtype: DType) -> Self

Create a new array filled with a specific value.

Source

pub fn from_vec(data: Vec<f32>, shape: Shape) -> Self

Create an array from a flat Vec and shape.

§Panics

Panics if the shape size doesn’t match the data length.

§Examples
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let a = Array::from_vec(data, Shape::new(vec![2, 3]));
assert_eq!(a.shape().as_slice(), &[2, 3]);
Source

pub fn from_vec_i32(data: Vec<i32>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_i8(data: Vec<i8>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_u8(data: Vec<u8>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_i16(data: Vec<i16>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_u16(data: Vec<u16>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_i64(data: Vec<i64>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_u32(data: Vec<u32>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_u64(data: Vec<u64>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_f64(data: Vec<f64>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn from_vec_bool(data: Vec<bool>, shape: Shape) -> Self

Create an array from a Vec.

Source

pub fn shape(&self) -> &Shape

Get the shape of the array.

Source

pub fn dtype(&self) -> DType

Get the data type of the array.

Source

pub fn device(&self) -> Device

Get the device where this array lives.

Source

pub fn ndim(&self) -> usize

Get the number of dimensions.

Source

pub fn size(&self) -> usize

Get the total number of elements.

Source

pub fn id(&self) -> usize

Get the unique ID of this array (for tracing).

Source

pub fn is_scalar(&self) -> bool

Check if this is a scalar (0-dimensional array).

Source

pub fn to_vec(&self) -> Vec<f32>

Copy data to a Vec (synchronous).

This materializes the array and copies all data to CPU memory. Data is converted to f32 regardless of the array’s dtype.

Source

pub fn to_bool_vec(&self) -> Vec<bool>

Copy data to a Vec (for Bool dtype arrays).

Source

pub fn astype(&self, dtype: DType) -> Self

Cast array to a different dtype.

§Examples
let a = Array::from_vec(vec![1.0, 2.5, 3.9], Shape::new(vec![3]));
let b = a.astype(DType::Int32);
assert_eq!(b.dtype(), DType::Int32);
let data = b.to_vec();
assert_eq!(data, vec![1.0, 2.0, 3.0]); // truncated to integers
Source

pub fn to_device(&self, device: Device) -> Array

Transfer array to a different device.

If the array is already on the target device, returns a clone. Otherwise, transfers the data to the new device.

§Examples
let cpu_arr = Array::zeros(Shape::new(vec![10]), DType::Float32);
let gpu_arr = cpu_arr.to_device(Device::WebGpu);
assert_eq!(gpu_arr.device(), Device::WebGpu);
Source

pub fn reshape(&self, new_shape: Shape) -> Self

Reshape the array to a new shape.

§Panics

Panics if the total size doesn’t match.

Source

pub fn squeeze(&self) -> Self

Remove axes of length one from the array.

§Examples
let a = Array::zeros(Shape::new(vec![1, 3, 1, 4]), jax_rs::DType::Float32);
let b = a.squeeze();
assert_eq!(b.shape().as_slice(), &[3, 4]);
Source

pub fn squeeze_axis(&self, axis: usize) -> Self

Remove a single dimension at the specified axis.

The dimension at the given axis must be 1.

Source

pub fn expand_dims(&self, axis: usize) -> Self

Expand the shape of an array by inserting a new axis.

§Arguments
  • axis - Position where new axis is placed
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.expand_dims(0);
assert_eq!(b.shape().as_slice(), &[1, 3]);
let c = a.expand_dims(1);
assert_eq!(c.shape().as_slice(), &[3, 1]);
Source§

impl Array

Source

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

Add two arrays element-wise with broadcasting.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![10.0, 20.0, 30.0], Shape::new(vec![3]));
let c = a.add(&b);
assert_eq!(c.to_vec(), vec![11.0, 22.0, 33.0]);
Source

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

Subtract two arrays element-wise with broadcasting.

Source

pub fn mul(&self, other: &Array) -> Array

Multiply two arrays element-wise with broadcasting.

Source

pub fn div(&self, other: &Array) -> Array

Divide two arrays element-wise with broadcasting.

Source

pub fn pow(&self, other: &Array) -> Array

Raise elements to a power element-wise with broadcasting.

Source

pub fn minimum(&self, other: &Array) -> Array

Element-wise minimum.

Source

pub fn maximum(&self, other: &Array) -> Array

Element-wise maximum.

Source

pub fn divide_no_nan(&self, other: &Array) -> Array

Safe division that returns 0 where division by zero would occur.

Returns x / y where y != 0, and 0 where y == 0.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 0.0, 3.0], Shape::new(vec![3]));
let c = a.divide_no_nan(&b);
assert_eq!(c.to_vec(), vec![0.5, 0.0, 1.0]);
Source

pub fn squared_difference(&self, other: &Array) -> Array

Squared difference: (a - b)^2.

Useful for computing mean squared error and similar metrics.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 1.0], Shape::new(vec![3]));
let c = a.squared_difference(&b);
assert_eq!(c.to_vec(), vec![1.0, 0.0, 4.0]);
Source

pub fn mod_op(&self, other: &Array) -> Array

Element-wise modulo operation.

§Examples
let a = Array::from_vec(vec![5.0, 7.0, 9.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![3.0, 3.0, 3.0], Shape::new(vec![3]));
let c = a.mod_op(&b);
assert_eq!(c.to_vec(), vec![2.0, 1.0, 0.0]);
Source

pub fn atan2(&self, other: &Array) -> Array

Element-wise arctangent of a/b.

Correctly handles signs to determine quadrant.

§Examples
let y = Array::from_vec(vec![1.0, -1.0], Shape::new(vec![2]));
let x = Array::from_vec(vec![1.0, 1.0], Shape::new(vec![2]));
let angle = y.atan2(&x);
Source

pub fn hypot(&self, other: &Array) -> Array

Element-wise hypot: sqrt(a^2 + b^2).

Computes the hypotenuse in a numerically stable way.

§Examples
let a = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![4.0, 3.0], Shape::new(vec![2]));
let c = a.hypot(&b);
assert_eq!(c.to_vec(), vec![5.0, 5.0]);
Source

pub fn copysign(&self, other: &Array) -> Array

Element-wise copysign: magnitude of a with sign of b.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![-1.0, 1.0, -1.0], Shape::new(vec![3]));
let c = a.copysign(&b);
assert_eq!(c.to_vec(), vec![-1.0, 2.0, -3.0]);
Source

pub fn next_after(&self, other: &Array) -> Array

Element-wise next representable float in direction of b.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![2.0, 1.0], Shape::new(vec![2]));
let c = a.next_after(&b);
Source

pub fn logaddexp(&self, other: &Array) -> Array

Logarithm of sum of exponentials (numerically stable).

Computes log(exp(x) + exp(y)) in a numerically stable way.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let c = a.logaddexp(&b);
// Result: log(exp(1)+exp(2)), log(exp(2)+exp(3)), log(exp(3)+exp(4))
Source

pub fn logaddexp2(&self, other: &Array) -> Array

Base-2 logarithm of sum of exponentials.

Computes log2(2^x + 2^y) in a numerically stable way.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let c = a.logaddexp2(&b);
Source

pub fn heaviside(&self, h0: &Array) -> Array

Heaviside step function.

Returns 0 where x < 0, h0 where x == 0, and 1 where x > 0.

§Examples
let x = Array::from_vec(vec![-1.0, 0.0, 1.0], Shape::new(vec![3]));
let h0 = Array::from_vec(vec![0.5, 0.5, 0.5], Shape::new(vec![3]));
let h = x.heaviside(&h0);
assert_eq!(h.to_vec(), vec![0.0, 0.5, 1.0]);
Source

pub fn floor_divide(&self, other: &Array) -> Array

Floor division (division rounding toward negative infinity).

§Examples
let a = Array::from_vec(vec![7.0, 7.0, -7.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![3.0, -3.0, 3.0], Shape::new(vec![3]));
let c = a.floor_divide(&b);
assert_eq!(c.to_vec(), vec![2.0, -3.0, -3.0]);
Source

pub fn fma(&self, b: &Array, c: &Array) -> Array

Fused multiply-add: a * b + c.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let c = Array::from_vec(vec![1.0, 1.0, 1.0], Shape::new(vec![3]));
let result = a.fma(&b, &c);
assert_eq!(result.to_vec(), vec![3.0, 7.0, 13.0]); // [1*2+1, 2*3+1, 3*4+1]
Source

pub fn gcd(&self, other: &Array) -> Array

Greatest common divisor element-wise.

§Examples
let a = Array::from_vec(vec![12.0, 15.0, 24.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![8.0, 10.0, 18.0], Shape::new(vec![3]));
let c = a.gcd(&b);
assert_eq!(c.to_vec(), vec![4.0, 5.0, 6.0]);
Source

pub fn lcm(&self, other: &Array) -> Array

Least common multiple element-wise.

§Examples
let a = Array::from_vec(vec![12.0, 15.0, 24.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![8.0, 10.0, 18.0], Shape::new(vec![3]));
let c = a.lcm(&b);
assert_eq!(c.to_vec(), vec![24.0, 30.0, 72.0]);
Source

pub fn bitwise_and(&self, other: &Array) -> Array

Bitwise AND operation. Operates on the bit representation of Float32 values.

§Examples
let a = Array::from_vec(vec![15.0, 31.0, 63.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![7.0, 15.0, 31.0], Shape::new(vec![3]));
let c = a.bitwise_and(&b);
Source

pub fn bitwise_or(&self, other: &Array) -> Array

Bitwise OR operation. Operates on the bit representation of Float32 values.

§Examples
let a = Array::from_vec(vec![8.0, 16.0, 32.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 8.0, 16.0], Shape::new(vec![3]));
let c = a.bitwise_or(&b);
Source

pub fn bitwise_xor(&self, other: &Array) -> Array

Bitwise XOR operation. Operates on the bit representation of Float32 values.

§Examples
let a = Array::from_vec(vec![12.0, 15.0, 18.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![10.0, 5.0, 20.0], Shape::new(vec![3]));
let c = a.bitwise_xor(&b);
Source

pub fn left_shift(&self, other: &Array) -> Array

Left bit shift operation. Shifts the bit representation of Float32 values left.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let c = a.left_shift(&b);
Source

pub fn right_shift(&self, other: &Array) -> Array

Right bit shift operation. Shifts the bit representation of Float32 values right.

§Examples
let a = Array::from_vec(vec![4.0, 8.0, 16.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let c = a.right_shift(&b);
Source

pub fn fmax(&self, other: &Array) -> Array

Element-wise maximum, ignoring NaNs.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.fmax(&b);
assert_eq!(c.to_vec()[0], 2.0);
assert_eq!(c.to_vec()[1], 2.0);
assert_eq!(c.to_vec()[2], 3.0);
Source

pub fn fmin(&self, other: &Array) -> Array

Element-wise minimum, ignoring NaNs.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.fmin(&b);
assert_eq!(c.to_vec()[0], 1.0);
assert_eq!(c.to_vec()[1], 2.0);
assert_eq!(c.to_vec()[2], 2.0);
Source

pub fn arctan2(&self, other: &Array) -> Array

Element-wise arc tangent of x1/x2 choosing the quadrant correctly.

The quadrant (i.e., branch) is chosen so that arctan2(x1, x2) is the signed angle in radians between the ray ending at the origin and passing through the point (1,0), and the ray ending at the origin and passing through the point (x2, x1).

§Examples
let y = Array::from_vec(vec![1.0, -1.0, 1.0, -1.0], Shape::new(vec![4]));
let x = Array::from_vec(vec![1.0, 1.0, -1.0, -1.0], Shape::new(vec![4]));
let angles = y.arctan2(&x);
// First quadrant: pi/4, Second: -pi/4, Third: 3pi/4, Fourth: -3pi/4
Source

pub fn fmod(&self, other: &Array) -> Array

Element-wise remainder of division (fmod).

§Examples
let a = Array::from_vec(vec![5.0, 7.0, 10.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let c = a.fmod(&b);
assert_eq!(c.to_vec(), vec![1.0, 1.0, 2.0]);
Source

pub fn nextafter(&self, other: &Array) -> Array

Return the next floating-point value after x1 towards x2.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![2.0, 1.0], Shape::new(vec![2]));
let c = a.nextafter(&b);
// First element goes up slightly, second goes down
Source

pub fn safe_divide(&self, other: &Array) -> Array

Compute the safe element-wise division, returning 0 where denominator is 0.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 0.0, 3.0], Shape::new(vec![3]));
let c = a.safe_divide(&b);
assert_eq!(c.to_vec(), vec![1.0, 0.0, 1.0]);
Source

pub fn true_divide(&self, other: &Array) -> Array

Compute element-wise true division.

§Examples
let a = Array::from_vec(vec![5.0, 7.0, 9.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.true_divide(&b);
assert_eq!(c.to_vec(), vec![2.5, 3.5, 4.5]);
Source

pub fn remainder(&self, other: &Array) -> Array

Compute element-wise remainder, with the same sign as divisor.

§Examples
let a = Array::from_vec(vec![7.0, -7.0, 7.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![3.0, 3.0, -3.0], Shape::new(vec![3]));
let c = a.remainder(&b);
// Python-style modulo: result has same sign as divisor
Source

pub fn diff_pow(&self, other: &Array, power: f32) -> Array

Compute element-wise difference raised to a power.

§Examples
let a = Array::from_vec(vec![3.0, 5.0, 7.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let c = a.diff_pow(&b, 2.0);  // (a - b)^2
assert_eq!(c.to_vec(), vec![4.0, 9.0, 16.0]);
Source

pub fn squared_diff(&self, other: &Array) -> Array

Compute element-wise squared difference.

§Examples
let a = Array::from_vec(vec![3.0, 5.0, 7.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let c = a.squared_diff(&b);  // (a - b)^2
assert_eq!(c.to_vec(), vec![4.0, 9.0, 16.0]);
Source

pub fn average_with(&self, other: &Array) -> Array

Compute element-wise average of two arrays.

§Examples
let a = Array::from_vec(vec![2.0, 4.0, 6.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 6.0, 8.0], Shape::new(vec![3]));
let c = a.average_with(&b);
assert_eq!(c.to_vec(), vec![3.0, 5.0, 7.0]);
Source§

impl Array

Source

pub fn lt(&self, other: &Array) -> Array

Element-wise less than comparison.

Returns an array of 1.0 where condition is true, 0.0 otherwise.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.lt(&b);
assert_eq!(c.to_vec(), vec![1.0, 0.0, 0.0]);
Source

pub fn le(&self, other: &Array) -> Array

Element-wise less than or equal comparison.

Source

pub fn gt(&self, other: &Array) -> Array

Element-wise greater than comparison.

Source

pub fn ge(&self, other: &Array) -> Array

Element-wise greater than or equal comparison.

Source

pub fn eq(&self, other: &Array) -> Array

Element-wise equality comparison.

Note: For floating point, this is exact equality. Use allclose for approximate equality.

Source

pub fn eq_scalar(&self, value: f32) -> Array

Element-wise equality comparison with a scalar.

Returns an array where each element is 1.0 if equal to the scalar, 0.0 otherwise.

Source

pub fn ne(&self, other: &Array) -> Array

Element-wise inequality comparison.

Source

pub fn logical_not(&self) -> Array

Logical NOT element-wise.

Treats 0.0 as false, non-zero as true.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 0.0], Shape::new(vec![3]));
let b = a.logical_not();
assert_eq!(b.to_vec(), vec![1.0, 0.0, 1.0]);
Source

pub fn logical_and(&self, other: &Array) -> Array

Logical AND element-wise.

§Examples
let a = Array::from_vec(vec![1.0, 1.0, 0.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 0.0, 0.0], Shape::new(vec![3]));
let c = a.logical_and(&b);
assert_eq!(c.to_vec(), vec![1.0, 0.0, 0.0]);
Source

pub fn logical_or(&self, other: &Array) -> Array

Logical OR element-wise.

§Examples
let a = Array::from_vec(vec![1.0, 1.0, 0.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 0.0, 0.0], Shape::new(vec![3]));
let c = a.logical_or(&b);
assert_eq!(c.to_vec(), vec![1.0, 1.0, 0.0]);
Source

pub fn logical_xor(&self, other: &Array) -> Array

Logical XOR element-wise.

§Examples
let a = Array::from_vec(vec![1.0, 1.0, 0.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 0.0, 0.0], Shape::new(vec![3]));
let c = a.logical_xor(&b);
assert_eq!(c.to_vec(), vec![0.0, 1.0, 0.0]);
Source

pub fn all(&self) -> bool

Test if all elements are true (non-zero).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
assert!(a.all());
let b = Array::from_vec(vec![1.0, 0.0, 3.0], Shape::new(vec![3]));
assert!(!b.all());
Source

pub fn any(&self) -> bool

Test if any element is true (non-zero).

§Examples
let a = Array::from_vec(vec![0.0, 0.0, 1.0], Shape::new(vec![3]));
assert!(a.any());
let b = Array::from_vec(vec![0.0, 0.0, 0.0], Shape::new(vec![3]));
assert!(!b.any());
Source

pub fn count_nonzero(&self) -> usize

Count the number of true (non-zero) elements.

§Examples
let a = Array::from_vec(vec![1.0, 0.0, 1.0, 0.0, 1.0], Shape::new(vec![5]));
assert_eq!(a.count_nonzero(), 3);
Source

pub fn allclose(&self, other: &Array, rtol: f32, atol: f32) -> bool

Test if two arrays are element-wise equal within a tolerance.

Returns true if all elements satisfy: |a - b| <= atol + rtol * |b|

§Arguments
  • other - Array to compare with
  • rtol - Relative tolerance
  • atol - Absolute tolerance
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0001, 2.0001, 3.0001], Shape::new(vec![3]));
assert!(a.allclose(&b, 1e-3, 1e-3));
assert!(!a.allclose(&b, 1e-5, 1e-5));
Source

pub fn isclose(&self, other: &Array, rtol: f32, atol: f32) -> Array

Element-wise test if values are close within a tolerance.

Returns an array of 1.0 where |a - b| <= atol + rtol * |b|, 0.0 otherwise.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0001, 2.1, 3.0001], Shape::new(vec![3]));
let c = a.isclose(&b, 1e-3, 1e-3);
assert_eq!(c.to_vec(), vec![1.0, 0.0, 1.0]);
Source

pub fn array_equal(&self, other: &Array) -> bool

Test if two arrays have the same shape and elements.

This is exact equality - for approximate equality use allclose.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let c = Array::from_vec(vec![1.0, 2.0, 3.1], Shape::new(vec![3]));
assert!(a.array_equal(&b));
assert!(!a.array_equal(&c));
Source

pub fn array_equiv(&self, other: &Array) -> bool

Test if arrays can be broadcast to the same shape and are equal.

Unlike array_equal, this allows broadcasting.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![1, 3]));
assert!(a.array_equiv(&b));
Source

pub fn greater(&self, other: &Array) -> Array

Element-wise greater comparison (alias for gt).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.greater(&b);
assert_eq!(c.to_vec(), vec![0.0, 0.0, 1.0]);
Source

pub fn less(&self, other: &Array) -> Array

Element-wise less comparison (alias for lt).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.less(&b);
assert_eq!(c.to_vec(), vec![1.0, 0.0, 0.0]);
Source

pub fn greater_equal(&self, other: &Array) -> Array

Element-wise greater-or-equal comparison (alias for ge).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.greater_equal(&b);
assert_eq!(c.to_vec(), vec![0.0, 1.0, 1.0]);
Source

pub fn less_equal(&self, other: &Array) -> Array

Element-wise less-or-equal comparison (alias for le).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 2.0, 2.0], Shape::new(vec![3]));
let c = a.less_equal(&b);
assert_eq!(c.to_vec(), vec![1.0, 1.0, 0.0]);
Source

pub fn isreal(&self) -> Array

Test element-wise for real numbers (not infinity or NaN). For Float32, returns true for all finite values.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let r = a.isreal();
assert_eq!(r.to_vec(), vec![1.0, 1.0, 1.0]);
Source

pub fn iscomplex(&self) -> Array

Test element-wise for complex numbers. For Float32 arrays, always returns false (0.0).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let c = a.iscomplex();
assert_eq!(c.to_vec(), vec![0.0, 0.0, 0.0]);
Source

pub fn isin_range(&self, lower: f32, upper: f32) -> Array

Test element-wise if values are in an open interval. Returns 1.0 where lower < x < upper, 0.0 otherwise.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let b = a.isin_range(1.5, 4.5);
assert_eq!(b.to_vec(), vec![0.0, 1.0, 1.0, 1.0, 0.0]);
Source

pub fn issubnormal(&self) -> Array

Test element-wise if values are subnormal (denormalized). Returns 1.0 where value is subnormal, 0.0 otherwise.

§Examples
let a = Array::from_vec(vec![1.0, 0.0, 1e-40], Shape::new(vec![3]));
let b = a.issubnormal();
// Only 1e-40 is subnormal
assert_eq!(b.to_vec()[0], 0.0);
assert_eq!(b.to_vec()[1], 0.0);
assert_eq!(b.to_vec()[2], 1.0);
Source§

impl Array

Source

pub fn arange(start: f32, stop: f32, step: f32, dtype: DType) -> Self

Create an array with evenly spaced values within a given interval.

Equivalent to arange(start, stop, step) in NumPy/JAX.

§Arguments
  • start - Start of interval (inclusive)
  • stop - End of interval (exclusive)
  • step - Spacing between values
§Examples
let a = Array::arange(0.0, 10.0, 2.0, DType::Float32);
assert_eq!(a.to_vec(), vec![0.0, 2.0, 4.0, 6.0, 8.0]);
Source

pub fn linspace( start: f32, stop: f32, num: usize, endpoint: bool, dtype: DType, ) -> Self

Return evenly spaced numbers over a specified interval.

Returns num evenly spaced samples, calculated over the interval [start, stop].

§Arguments
  • start - Starting value
  • stop - End value
  • num - Number of samples to generate
  • endpoint - If true, stop is the last sample. Otherwise excluded.
§Examples
let a = Array::linspace(0.0, 1.0, 5, true, DType::Float32);
assert_eq!(a.to_vec(), vec![0.0, 0.25, 0.5, 0.75, 1.0]);
Source

pub fn eye(n: usize, m: Option<usize>, dtype: DType) -> Self

Return a 2-D array with ones on the diagonal and zeros elsewhere.

§Arguments
  • n - Number of rows
  • m - Number of columns (defaults to n if None)
§Examples
let i = Array::eye(3, None, DType::Float32);
// [[1, 0, 0],
//  [0, 1, 0],
//  [0, 0, 1]]
Source

pub fn identity(n: usize, dtype: DType) -> Self

Return the identity matrix (square matrix with ones on diagonal).

§Examples
let i = Array::identity(3, DType::Float32);
Source

pub fn diag(v: &Self, k: i32) -> Self

Extract diagonal or construct diagonal array.

If input is 1-D, constructs a 2-D array with the input on the diagonal. If input is 2-D, extracts the diagonal.

§Examples
// Construct diagonal matrix from 1-D array
let v = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let d = Array::diag(&v, 0);
assert_eq!(d.shape().as_slice(), &[3, 3]);
assert_eq!(d.to_vec(), vec![1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0]);
Source

pub fn tril(&self, k: i32) -> Self

Lower triangle of an array.

§Examples
let m = Array::from_vec(
    vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
    Shape::new(vec![3, 3])
);
let lower = m.tril(0);
assert_eq!(lower.to_vec(), vec![1.0, 0.0, 0.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0]);
Source

pub fn triu(&self, k: i32) -> Self

Upper triangle of an array.

§Examples
let m = Array::from_vec(
    vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
    Shape::new(vec![3, 3])
);
let upper = m.triu(0);
assert_eq!(upper.to_vec(), vec![1.0, 2.0, 3.0, 0.0, 5.0, 6.0, 0.0, 0.0, 9.0]);
Source

pub fn tri(n: usize, m: Option<usize>, k: i32, dtype: DType) -> Self

Lower triangular matrix with ones on the diagonal and below.

§Examples
let tri = Array::tri(3, None, 0, DType::Float32);
assert_eq!(tri.to_vec(), vec![1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0]);
Source

pub fn zeros_like(other: &Array) -> Array

Create array with same shape as another, filled with zeros.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::zeros_like(&a);
assert_eq!(b.to_vec(), vec![0.0, 0.0, 0.0]);
Source

pub fn ones_like(other: &Array) -> Array

Create array with same shape as another, filled with ones.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::ones_like(&a);
assert_eq!(b.to_vec(), vec![1.0, 1.0, 1.0]);
Source

pub fn full_like(other: &Array, value: f32) -> Array

Create array with same shape as another, filled with a constant value.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::full_like(&a, 42.0);
assert_eq!(b.to_vec(), vec![42.0, 42.0, 42.0]);
Source

pub fn repeat(&self, repeats: usize, axis: usize) -> Array

Repeat array along specified axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = a.repeat(3, 0);
assert_eq!(b.to_vec(), vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
Source

pub fn tile(&self, reps: usize) -> Array

Tile array by repeating it multiple times.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = a.tile(3);
assert_eq!(b.to_vec(), vec![1.0, 2.0, 1.0, 2.0, 1.0, 2.0]);
Source

pub fn meshgrid(x: &Array, y: &Array) -> (Array, Array)

Create coordinate matrices from coordinate vectors.

§Examples
let x = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let y = Array::from_vec(vec![3.0, 4.0, 5.0], Shape::new(vec![3]));
let (xx, yy) = Array::meshgrid(&x, &y);
assert_eq!(xx.shape().as_slice(), &[3, 2]);
assert_eq!(yy.shape().as_slice(), &[3, 2]);
Source

pub fn indices(dimensions: &[usize]) -> Vec<Array>

Generate arrays of indices for each dimension.

Returns a vector of arrays, one for each dimension, containing the indices.

§Examples
let indices = Array::indices(&[2, 3]);
assert_eq!(indices[0].shape().as_slice(), &[2, 3]);
assert_eq!(indices[1].shape().as_slice(), &[2, 3]);
Source

pub fn unravel_index(index: usize, shape: &Shape) -> Vec<usize>

Convert a flat index to multi-dimensional coordinates.

§Examples
let shape = Shape::new(vec![3, 4]);
let coords = Array::unravel_index(5, &shape);
assert_eq!(coords, vec![1, 1]);
Source

pub fn ravel_multi_index(multi_index: &[usize], shape: &Shape) -> usize

Convert multi-dimensional coordinates to a flat index.

§Examples
let shape = Shape::new(vec![3, 4]);
let index = Array::ravel_multi_index(&[1, 2], &shape);
assert_eq!(index, 6);
Source

pub fn diag_indices(n: usize) -> (Vec<usize>, Vec<usize>)

Return indices for the main diagonal of an n-by-n array.

§Examples
let (rows, cols) = Array::diag_indices(3);
assert_eq!(rows, vec![0, 1, 2]);
assert_eq!(cols, vec![0, 1, 2]);
Source

pub fn tril_indices(n: usize, k: isize) -> (Vec<usize>, Vec<usize>)

Return indices for the lower triangle of an n-by-n array.

§Arguments
  • n - Size of the arrays for which the indices are returned
  • k - Diagonal offset (0 for main diagonal, positive for above, negative for below)
§Examples
let (rows, cols) = Array::tril_indices(3, 0);
assert_eq!(rows, vec![0, 1, 1, 2, 2, 2]);
assert_eq!(cols, vec![0, 0, 1, 0, 1, 2]);
Source

pub fn triu_indices(n: usize, k: isize) -> (Vec<usize>, Vec<usize>)

Return indices for the upper triangle of an n-by-n array.

§Arguments
  • n - Size of the arrays for which the indices are returned
  • k - Diagonal offset (0 for main diagonal, positive for above, negative for below)
§Examples
let (rows, cols) = Array::triu_indices(3, 0);
assert_eq!(rows, vec![0, 0, 0, 1, 1, 2]);
assert_eq!(cols, vec![0, 1, 2, 1, 2, 2]);
Source

pub fn geomspace(start: f32, stop: f32, num: usize, dtype: DType) -> Self

Return numbers spaced evenly on a log scale (geometric progression).

§Examples
let a = Array::geomspace(1.0, 1000.0, 4, DType::Float32);
// Result: [1.0, 10.0, 100.0, 1000.0]
Source

pub fn logspace(start: f32, stop: f32, num: usize, dtype: DType) -> Self

Return numbers spaced evenly on a log scale.

§Examples
let a = Array::logspace(0.0, 3.0, 4, DType::Float32);
// Result: [1.0, 10.0, 100.0, 1000.0] (10^0 to 10^3)
Source

pub fn empty_like(&self) -> Array

Create empty array with same shape (uninitialized memory). Note: In this implementation, we return zeros.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.empty_like();
assert_eq!(b.shape().as_slice(), &[3]);
Source

pub fn is_contiguous(&self) -> bool

Check if array is C-contiguous (row-major order).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
assert!(a.is_contiguous());
Source

pub fn is_fortran_contiguous(&self) -> bool

Check if array is Fortran-contiguous (column-major order). Note: Our arrays are always C-contiguous.

Source

pub fn ascontiguousarray(&self) -> Array

Return a contiguous array in memory (C order).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let b = a.ascontiguousarray();
assert!(b.is_contiguous());
Source

pub fn hamming(n: usize) -> Array

Create a Hamming window of given length.

§Examples
let w = Array::hamming(5);
assert_eq!(w.shape().as_slice(), &[5]);
Source

pub fn hanning(n: usize) -> Array

Create a Hanning window of given length.

§Examples
let w = Array::hanning(5);
assert_eq!(w.shape().as_slice(), &[5]);
Source

pub fn blackman(n: usize) -> Array

Create a Blackman window of given length.

§Examples
let w = Array::blackman(5);
assert_eq!(w.shape().as_slice(), &[5]);
Source

pub fn kaiser(n: usize, beta: f32) -> Array

Create a Kaiser window of given length and beta parameter.

§Examples
let w = Array::kaiser(5, 5.0);
assert_eq!(w.shape().as_slice(), &[5]);
Source

pub fn bartlett(n: usize) -> Array

Create a Bartlett (triangular) window of given length.

§Examples
let w = Array::bartlett(5);
assert_eq!(w.shape().as_slice(), &[5]);
Source

pub fn flattop(n: usize) -> Array

Create a flat top window of given length.

§Examples
let w = Array::flattop(5);
assert_eq!(w.shape().as_slice(), &[5]);
Source

pub fn triang(n: usize) -> Array

Create a triangular window of given length.

§Examples
let w = Array::triang(5);
assert_eq!(w.shape().as_slice(), &[5]);
assert!((w.to_vec()[2] - 1.0).abs() < 1e-6); // Peak at center
Source§

impl Array

Source

pub fn transpose(&self) -> Array

Transpose the array by reversing its axes.

For 2D arrays, this swaps rows and columns. For 1D arrays, returns a copy. For higher dimensions, reverses all axes.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let b = a.transpose();
assert_eq!(b.shape().as_slice(), &[3, 2]);
Source

pub fn transpose_axes(&self, axes: &[usize]) -> Array

Transpose the array with a specified permutation of axes.

§Arguments
  • axes - The new order of axes
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let b = a.transpose_axes(&[1, 0]);
assert_eq!(b.shape().as_slice(), &[3, 2]);
Source

pub fn matmul(&self, other: &Array) -> Array

Matrix multiplication of two 2D arrays.

§Arguments
  • other - The right-hand array to multiply with
§Panics

Panics if shapes are incompatible or arrays are not 2D.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let b = Array::from_vec(vec![5.0, 6.0, 7.0, 8.0], Shape::new(vec![2, 2]));
let c = a.matmul(&b);
// [[1*5 + 2*7, 1*6 + 2*8],
//  [3*5 + 4*7, 3*6 + 4*8]]
// = [[19, 22], [43, 50]]
Source

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

Dot product of two arrays.

For 1D arrays, computes the inner product. For 2D arrays, equivalent to matmul.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 5.0, 6.0], Shape::new(vec![3]));
let c = a.dot(&b);
assert_eq!(c.to_vec(), vec![32.0]); // 1*4 + 2*5 + 3*6 = 32
Source

pub fn norm(&self, ord: f32) -> f32

Compute the norm of a vector or matrix.

§Arguments
  • ord - Order of the norm. Common values:
    • 1.0: L1 norm (sum of absolute values)
    • 2.0: L2 norm (Euclidean norm)
    • f32::INFINITY: L-infinity norm (maximum absolute value)
§Examples
let a = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let l2_norm = a.norm(2.0);
assert_eq!(l2_norm, 5.0); // sqrt(3^2 + 4^2) = 5
Source

pub fn det(&self) -> f32

Compute the determinant of a square matrix.

Uses LU decomposition for matrices larger than 3x3.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let det = a.det();
assert_eq!(det, -2.0); // 1*4 - 2*3 = -2
Source

pub fn inv(&self) -> Array

Compute the matrix inverse.

Uses Gauss-Jordan elimination.

§Examples
let a = Array::from_vec(vec![4.0, 7.0, 2.0, 6.0], Shape::new(vec![2, 2]));
let inv_a = a.inv();
// Verify A * A^-1 = I
let identity = a.matmul(&inv_a);
let expected = vec![1.0, 0.0, 0.0, 1.0];
for (i, &val) in identity.to_vec().iter().enumerate() {
    assert!((val - expected[i]).abs() < 1e-5);
}
Source

pub fn solve(&self, b: &Array) -> Array

Solve a linear system Ax = b.

Uses Gaussian elimination with partial pivoting.

§Examples
// Solve: 2x + y = 5, x + 3y = 6
let a = Array::from_vec(vec![2.0, 1.0, 1.0, 3.0], Shape::new(vec![2, 2]));
let b = Array::from_vec(vec![5.0, 6.0], Shape::new(vec![2]));
let x = a.solve(&b);
// Solution: x = [1.8, 1.4]
assert!((x.to_vec()[0] - 1.8).abs() < 1e-5);
assert!((x.to_vec()[1] - 1.4).abs() < 1e-5);
Source

pub fn outer(&self, other: &Array) -> Array

Compute the outer product of two 1D arrays.

Given two 1D arrays a and b, returns a 2D array of shape (a.len(), b.len()) where result[i, j] = a[i] * b[j].

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 5.0], Shape::new(vec![2]));
let c = a.outer(&b);
assert_eq!(c.shape().as_slice(), &[3, 2]);
// [[1*4, 1*5], [2*4, 2*5], [3*4, 3*5]]
// = [[4, 5], [8, 10], [12, 15]]
assert_eq!(c.to_vec(), vec![4.0, 5.0, 8.0, 10.0, 12.0, 15.0]);
Source

pub fn inner(&self, other: &Array) -> f32

Compute the inner product of two 1D arrays.

For 1D arrays, this is the same as dot product: sum of element-wise products.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 5.0, 6.0], Shape::new(vec![3]));
let result = a.inner(&b);
assert_eq!(result, 32.0); // 1*4 + 2*5 + 3*6 = 32
Source

pub fn cross(&self, other: &Array) -> Array

Compute the cross product of two 3D vectors.

Returns a vector perpendicular to both input vectors. Formula: a × b = [a1b2 - a2b1, a2b0 - a0b2, a0b1 - a1b0]

§Examples
let a = Array::from_vec(vec![1.0, 0.0, 0.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![0.0, 1.0, 0.0], Shape::new(vec![3]));
let c = a.cross(&b);
assert_eq!(c.to_vec(), vec![0.0, 0.0, 1.0]); // i × j = k
Source

pub fn trace(&self) -> f32

Compute the trace of a 2D array (sum of diagonal elements).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let tr = a.trace();
assert_eq!(tr, 5.0); // 1 + 4 = 5
Source

pub fn diagonal(&self) -> Array

Extract the diagonal of a 2D array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let diag = a.diagonal();
assert_eq!(diag.to_vec(), vec![1.0, 5.0]); // Elements at (0,0) and (1,1)
Source

pub fn vander(&self, n: usize) -> Array

Generate a Vandermonde matrix.

Creates a matrix where each row is the input vector raised to successive powers.

§Examples
let x = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let v = x.vander(4);
// [[1, 1, 1, 1], [1, 2, 4, 8], [1, 3, 9, 27]]
Source

pub fn qr(&self) -> (Array, Array)

QR decomposition of a matrix.

Decomposes matrix A into Q (orthogonal) and R (upper triangular) such that A = QR. Uses the Gram-Schmidt process.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let (q, r) = a.qr();
// Q is orthogonal, R is upper triangular
// Q * R ≈ A
Source

pub fn cholesky(&self) -> Array

Cholesky decomposition of a symmetric positive-definite matrix.

Decomposes matrix A into L such that A = L * L^T, where L is lower triangular.

§Panics

Panics if the matrix is not square or not positive-definite.

§Examples
// Symmetric positive-definite matrix
let a = Array::from_vec(vec![4.0, 2.0, 2.0, 3.0], Shape::new(vec![2, 2]));
let l = a.cholesky();
// L * L^T = A
Source

pub fn matrix_rank(&self) -> usize

Compute the rank of a matrix.

Uses SVD-like approach (actually QR with tolerance) to estimate rank.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 2.0, 4.0], Shape::new(vec![2, 2]));
let rank = a.matrix_rank();
assert_eq!(rank, 1); // Rows are linearly dependent
Source

pub fn eigvalsh(&self) -> Array

Compute eigenvalues of a symmetric matrix using the power method.

Returns approximate eigenvalues for symmetric matrices. For non-symmetric matrices, results may not be accurate.

§Examples
let a = Array::from_vec(vec![2.0, 1.0, 1.0, 2.0], Shape::new(vec![2, 2]));
let eigvals = a.eigvalsh();
// Eigenvalues of this symmetric matrix are 1 and 3
Source

pub fn pinv(&self) -> Array

Compute the pseudo-inverse of a matrix using the Moore-Penrose algorithm.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let pinv = a.pinv();
// pinv has shape [3, 2]
Source

pub fn cond(&self) -> f32

Compute the condition number of a matrix.

Uses the ratio of the largest to smallest singular value estimate.

§Examples
let a = Array::from_vec(vec![1.0, 0.0, 0.0, 1.0], Shape::new(vec![2, 2]));
let cond = a.cond();
// Identity matrix has condition number ~1 (within numerical tolerance)
assert!((cond - 1.0).abs() < 0.1);
Source

pub fn svd(&self) -> (Array, Array, Array)

Compute singular value decomposition (SVD).

Returns (U, S, Vt) where A = U @ diag(S) @ Vt. Uses power iteration to find singular values.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let (u, s, vt) = a.svd();
assert_eq!(s.shape().as_slice(), &[2]);
Source

pub fn lstsq(&self, b: &Array) -> Array

Solve least squares problem: minimize ||Ax - b||^2.

Returns the solution x that minimizes the squared error.

§Examples
let a = Array::from_vec(vec![1.0, 1.0, 1.0, 2.0, 1.0, 3.0], Shape::new(vec![3, 2]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let x = a.lstsq(&b);
assert_eq!(x.shape().as_slice(), &[2]);
Source

pub fn eigh(&self) -> (Array, Array)

Compute eigenvalues and eigenvectors of a symmetric matrix.

Returns (eigenvalues, eigenvectors) where each column of eigenvectors is an eigenvector corresponding to the eigenvalue at the same index.

§Examples
let a = Array::from_vec(vec![2.0, 1.0, 1.0, 2.0], Shape::new(vec![2, 2]));
let (vals, vecs) = a.eigh();
assert_eq!(vals.shape().as_slice(), &[2]);
assert_eq!(vecs.shape().as_slice(), &[2, 2]);
Source

pub fn eig(&self) -> Array

Compute eigenvalues of a general (non-symmetric) matrix.

Uses QR iteration to find eigenvalues.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 0.0, 3.0], Shape::new(vec![2, 2]));
let eigvals = a.eig();
assert_eq!(eigvals.shape().as_slice(), &[2]);
Source

pub fn tensordot(&self, other: &Array, axes: usize) -> Array

Compute the tensor dot product along specified axes.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let c = a.tensordot(&b, 1);
assert_eq!(c.shape().as_slice(), &[2, 2]);
Source

pub fn kron(&self, other: &Array) -> Array

Compute the Kronecker product of two arrays.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let b = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let c = a.kron(&b);
assert_eq!(c.shape().as_slice(), &[4, 4]);
Source§

impl Array

Source

pub fn concatenate(arrays: &[Array], axis: usize) -> Array

Concatenate arrays along an existing axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let c = Array::concatenate(&[a, b], 0);
assert_eq!(c.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
Source

pub fn stack(arrays: &[Array], axis: usize) -> Array

Stack arrays along a new axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let c = Array::stack(&[a, b], 0);
assert_eq!(c.shape().as_slice(), &[2, 2]);
assert_eq!(c.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
Source

pub fn split(array: &Array, num_sections: usize, axis: usize) -> Vec<Array>

Split an array into multiple sub-arrays along a specified axis.

§Arguments
  • array - The array to split
  • num_sections - Number of equal sections to split into
  • axis - The axis along which to split
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![6]));
let parts = Array::split(&a, 3, 0);
assert_eq!(parts.len(), 3);
assert_eq!(parts[0].to_vec(), vec![1.0, 2.0]);
assert_eq!(parts[1].to_vec(), vec![3.0, 4.0]);
assert_eq!(parts[2].to_vec(), vec![5.0, 6.0]);
Source

pub fn where_cond(condition: &Array, x: &Array, y: &Array) -> Array

Select elements from array based on condition with broadcasting support.

Returns elements from x where condition is true (non-zero), otherwise from y. All three arrays are broadcast to a common shape.

§Arguments
  • condition - Boolean array (non-zero = true, zero = false)
  • x - Array of values to select when condition is true
  • y - Array of values to select when condition is false
§Examples
let condition = Array::from_vec(vec![1.0, 0.0, 1.0], Shape::new(vec![3]));
let x = Array::from_vec(vec![10.0, 20.0, 30.0], Shape::new(vec![3]));
let y = Array::from_vec(vec![5.0, 5.0, 5.0], Shape::new(vec![3]));
let result = Array::where_cond(&condition, &x, &y);
assert_eq!(result.to_vec(), vec![10.0, 5.0, 30.0]);
Source

pub fn select(indices: &Array, choices: &[Array]) -> Array

Select values from multiple choice arrays based on index array.

For each element in indices, selects the corresponding element from the choice array at that index. Similar to a multi-way switch statement.

§Arguments
  • indices - Array of integer indices (as f32) specifying which choice to pick
  • choices - Slice of arrays to choose from
§Examples
let indices = Array::from_vec(vec![0.0, 1.0, 2.0, 1.0], Shape::new(vec![4]));
let choice0 = Array::from_vec(vec![10.0, 10.0, 10.0, 10.0], Shape::new(vec![4]));
let choice1 = Array::from_vec(vec![20.0, 20.0, 20.0, 20.0], Shape::new(vec![4]));
let choice2 = Array::from_vec(vec![30.0, 30.0, 30.0, 30.0], Shape::new(vec![4]));
let result = Array::select(&indices, &[choice0, choice1, choice2]);
assert_eq!(result.to_vec(), vec![10.0, 20.0, 30.0, 20.0]);
Source

pub fn clip(&self, min: f32, max: f32) -> Array

Clip (limit) values in an array.

§Examples
let a = Array::from_vec(vec![1.0, 5.0, 10.0, 15.0], Shape::new(vec![4]));
let clipped = a.clip(3.0, 12.0);
assert_eq!(clipped.to_vec(), vec![3.0, 5.0, 10.0, 12.0]);
Source

pub fn flip(&self, axis: usize) -> Array

Flip array along specified axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let flipped = a.flip(0);
assert_eq!(flipped.to_vec(), vec![3.0, 2.0, 1.0]);
Source

pub fn pad(&self, pad_width: &[(usize, usize)], constant_value: f32) -> Array

Pad array with a constant value.

§Arguments
  • pad_width - Number of values to pad on each side: [(before, after), …]
  • constant_value - Value to use for padding
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let padded = a.pad(&[(1, 1)], 0.0);
assert_eq!(padded.to_vec(), vec![0.0, 1.0, 2.0, 3.0, 0.0]);
Source

pub fn pad_edge(&self, pad_width: &[(usize, usize)]) -> Array

Pad array with edge values (repeat border elements).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let padded = a.pad_edge(&[(1, 1)]);
assert_eq!(padded.to_vec(), vec![1.0, 1.0, 2.0, 3.0, 3.0]);
Source

pub fn pad_reflect(&self, pad_width: &[(usize, usize)]) -> Array

Pad array with reflected values (mirror border elements).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let padded = a.pad_reflect(&[(1, 1)]);
assert_eq!(padded.to_vec(), vec![2.0, 1.0, 2.0, 3.0, 2.0]);
Source

pub fn nan_to_num(&self, nan: f32, posinf: f32, neginf: f32) -> Array

Replace NaN and infinity values with specified numbers.

§Arguments
  • nan - Value to replace NaN with (default 0.0)
  • posinf - Value to replace positive infinity with (default large positive value)
  • neginf - Value to replace negative infinity with (default large negative value)
§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, f32::INFINITY, -f32::INFINITY], Shape::new(vec![4]));
let result = a.nan_to_num(0.0, 1e10, -1e10);
assert_eq!(result.to_vec()[0], 1.0);
assert_eq!(result.to_vec()[1], 0.0);
assert_eq!(result.to_vec()[2], 1e10);
assert_eq!(result.to_vec()[3], -1e10);
Source

pub fn isnan(&self) -> Array

Check for NaN values element-wise.

Returns an array with 1.0 where NaN, 0.0 otherwise.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, f32::NAN], Shape::new(vec![4]));
let result = a.isnan();
assert_eq!(result.to_vec(), vec![0.0, 1.0, 0.0, 1.0]);
Source

pub fn isinf(&self) -> Array

Check for infinity values element-wise.

Returns an array with 1.0 where infinity (positive or negative), 0.0 otherwise.

§Examples
let a = Array::from_vec(vec![1.0, f32::INFINITY, -f32::INFINITY, 3.0], Shape::new(vec![4]));
let result = a.isinf();
assert_eq!(result.to_vec(), vec![0.0, 1.0, 1.0, 0.0]);
Source

pub fn isfinite(&self) -> Array

Check for finite values element-wise.

Returns an array with 1.0 where finite, 0.0 otherwise (NaN or infinity).

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, f32::INFINITY, 3.0], Shape::new(vec![4]));
let result = a.isfinite();
assert_eq!(result.to_vec(), vec![1.0, 0.0, 0.0, 1.0]);
Source

pub fn clip_by_norm(&self, max_norm: f32) -> Array

Clip array values by L2 norm.

If the L2 norm exceeds max_norm, scales the array down to have that norm. Useful for gradient clipping in neural networks.

§Examples
let a = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let clipped = a.clip_by_norm(2.0);
// Original norm is 5.0, should be scaled to 2.0
let result = clipped.to_vec();
assert!((result[0] - 1.2).abs() < 1e-5);
assert!((result[1] - 1.6).abs() < 1e-5);
Source

pub fn ravel(&self) -> Array

Flatten array to 1D.

Returns a 1D array containing all elements in row-major order.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let flat = a.ravel();
assert_eq!(flat.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
assert_eq!(flat.shape().as_slice(), &[4]);
Source

pub fn flatten(&self) -> Array

Flatten array to 1D (alias for ravel).

Returns a 1D array containing all elements in row-major order.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let flat = a.flatten();
assert_eq!(flat.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
Source

pub fn atleast_1d(&self) -> Array

View array with at least 1D.

Scalar (0D) arrays are converted to 1D arrays with shape [1].

§Examples
let a = Array::from_vec(vec![5.0], Shape::new(vec![]));
let b = a.atleast_1d();
assert_eq!(b.shape().as_slice(), &[1]);
Source

pub fn atleast_2d(&self) -> Array

View array with at least 2D.

Arrays with fewer than 2 dimensions are expanded.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.atleast_2d();
assert_eq!(b.shape().as_slice(), &[1, 3]);
Source

pub fn atleast_3d(&self) -> Array

View array with at least 3D.

Arrays with fewer than 3 dimensions are expanded.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = a.atleast_3d();
assert_eq!(b.shape().as_slice(), &[1, 2, 1]);
Source

pub fn broadcast_to(&self, new_shape: Shape) -> Array

Broadcast array to a new shape.

The new shape must be broadcast-compatible with the current shape.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.broadcast_to(Shape::new(vec![2, 3]));
assert_eq!(b.shape().as_slice(), &[2, 3]);
assert_eq!(b.to_vec(), vec![1.0, 2.0, 3.0, 1.0, 2.0, 3.0]);
Source

pub fn broadcast_arrays(arrays: &[Array]) -> Vec<Array>

Broadcast multiple arrays to a common shape.

All arrays are broadcast to a shape that is compatible with all inputs. The result shape is determined by the broadcast rules applied successively.

§Arguments
  • arrays - Slice of arrays to broadcast
§Returns

Vector of arrays, all broadcast to the same shape

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![10.0, 20.0], Shape::new(vec![2, 1]));
let broadcasted = Array::broadcast_arrays(&[a, b]);

// Both should have shape [2, 3]
assert_eq!(broadcasted[0].shape().as_slice(), &[2, 3]);
assert_eq!(broadcasted[1].shape().as_slice(), &[2, 3]);
Source

pub fn take(&self, indices: &[usize]) -> Array

Take elements from array along an axis at specified indices.

§Examples
let a = Array::from_vec(vec![10.0, 20.0, 30.0, 40.0], Shape::new(vec![4]));
let indices = vec![0, 2, 3];
let result = a.take(&indices);
assert_eq!(result.to_vec(), vec![10.0, 30.0, 40.0]);
Source

pub fn put(&self, indices: &[usize], values: &[f32]) -> Array

Put values into an array at specified indices.

Replaces elements at the given indices with the provided values. Returns a new array with the modifications.

§Arguments
  • indices - Flat indices where values should be placed
  • values - Values to place at those indices
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let result = a.put(&[0, 2, 4], &[10.0, 30.0, 50.0]);
assert_eq!(result.to_vec(), vec![10.0, 2.0, 30.0, 4.0, 50.0]);
Source

pub fn scatter(&self, indices: &[usize], updates: &[f32]) -> Array

Scatter update values into an array at specified indices.

Returns a new array with values from updates placed at positions specified by indices. This is equivalent to put() but follows the JAX/NumPy scatter naming convention.

§Arguments
  • indices - Flattened indices where updates should be placed
  • updates - Values to place at the specified indices
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let result = a.scatter(&[0, 2, 4], &[10.0, 30.0, 50.0]);
assert_eq!(result.to_vec(), vec![10.0, 2.0, 30.0, 4.0, 50.0]);
Source

pub fn scatter_add(&self, indices: &[usize], updates: &[f32]) -> Array

Scatter-add values into an array at specified indices.

Returns a new array with values from updates added to the values at positions specified by indices. If the same index appears multiple times, updates are accumulated.

§Arguments
  • indices - Flattened indices where updates should be added
  • updates - Values to add at the specified indices
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let result = a.scatter_add(&[0, 2, 4], &[10.0, 30.0, 50.0]);
assert_eq!(result.to_vec(), vec![11.0, 2.0, 33.0, 4.0, 55.0]);
Source

pub fn scatter_min(&self, indices: &[usize], updates: &[f32]) -> Array

Scatter-min values into an array at specified indices.

Returns a new array where each position specified by indices contains the minimum of the original value and the corresponding update value.

§Arguments
  • indices - Flattened indices where min operation should be applied
  • updates - Values to compare with current values
§Examples
let a = Array::from_vec(vec![5.0, 10.0, 15.0, 20.0, 25.0], Shape::new(vec![5]));
let result = a.scatter_min(&[1, 2, 3], &[8.0, 20.0, 15.0]);
assert_eq!(result.to_vec(), vec![5.0, 8.0, 15.0, 15.0, 25.0]);
Source

pub fn scatter_max(&self, indices: &[usize], updates: &[f32]) -> Array

Scatter-max values into an array at specified indices.

Returns a new array where each position specified by indices contains the maximum of the original value and the corresponding update value.

§Arguments
  • indices - Flattened indices where max operation should be applied
  • updates - Values to compare with current values
§Examples
let a = Array::from_vec(vec![5.0, 10.0, 15.0, 20.0, 25.0], Shape::new(vec![5]));
let result = a.scatter_max(&[1, 2, 3], &[12.0, 10.0, 25.0]);
assert_eq!(result.to_vec(), vec![5.0, 12.0, 15.0, 25.0, 25.0]);
Source

pub fn scatter_mul(&self, indices: &[usize], updates: &[f32]) -> Array

Scatter updates to specified indices using multiplication.

For each index, multiplies the existing value with the update value. When multiple updates target the same index, they are accumulated.

§Arguments
  • indices - Indices where updates should be applied
  • updates - Values to multiply at the corresponding indices
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let result = a.scatter_mul(&[1, 2, 3], &[2.0, 3.0, 0.5]);
assert_eq!(result.to_vec(), vec![1.0, 4.0, 9.0, 2.0, 5.0]);
Source

pub fn take_along_axis(&self, indices: &Array, axis: usize) -> Array

Take values from an array along an axis using indices.

This is similar to gather operations in other frameworks. For each position, it selects the element specified by the index array.

§Arguments
  • indices - Array of indices to take along the axis
  • axis - Axis along which to take values
§Examples
// For a 2D array, select different columns for each row
let a = Array::from_vec(
    vec![10.0, 20.0, 30.0,
         40.0, 50.0, 60.0],
    Shape::new(vec![2, 3])
);
let indices = Array::from_vec(vec![0.0, 2.0], Shape::new(vec![2]));
let result = a.take_along_axis(&indices, 1);
// Takes column 0 from row 0 (10.0) and column 2 from row 1 (60.0)
assert_eq!(result.to_vec(), vec![10.0, 60.0]);
Source

pub fn nonzero(&self) -> Vec<usize>

Return indices of non-zero elements.

Returns indices where elements are non-zero (not equal to 0.0).

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 0.0, 3.0, 0.0], Shape::new(vec![5]));
let indices = a.nonzero();
assert_eq!(indices, vec![1, 3]);
Source

pub fn argwhere(&self) -> Vec<usize>

Return indices where condition is true (non-zero).

Similar to nonzero but returns 2D array of indices for multi-dimensional arrays. For 1D arrays, returns a list of indices.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 0.0, 1.0], Shape::new(vec![4]));
let indices = a.argwhere();
assert_eq!(indices, vec![1, 3]);
Source

pub fn compress(&self, condition: &Array) -> Array

Select elements from array based on condition mask.

Returns a 1D array of elements where the condition is true (non-zero).

§Examples
let a = Array::from_vec(vec![10.0, 20.0, 30.0, 40.0], Shape::new(vec![4]));
let condition = Array::from_vec(vec![1.0, 0.0, 1.0, 0.0], Shape::new(vec![4]));
let result = a.compress(&condition);
assert_eq!(result.to_vec(), vec![10.0, 30.0]);
Source

pub fn choose(indices: &[usize], choices: &[Array]) -> Array

Choose elements from arrays based on index array.

For each element in the index array, select from the corresponding choice array.

§Examples
let choices = vec![
    Array::from_vec(vec![10.0, 20.0, 30.0], Shape::new(vec![3])),
    Array::from_vec(vec![100.0, 200.0, 300.0], Shape::new(vec![3])),
];
let indices = vec![0, 1, 0];
let result = Array::choose(&indices, &choices);
assert_eq!(result.to_vec(), vec![10.0, 200.0, 30.0]);
Source

pub fn extract(&self, condition: &Array) -> Array

Extract elements from array where condition is true.

Similar to compress, but condition can be a boolean-like array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let condition = Array::from_vec(vec![1.0, 0.0, 1.0, 0.0, 1.0], Shape::new(vec![5]));
let result = a.extract(&condition);
assert_eq!(result.to_vec(), vec![1.0, 3.0, 5.0]);
Source

pub fn roll(&self, shift: isize) -> Array

Roll array elements along a given axis.

Elements that roll beyond the last position are re-introduced at the first.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let rolled = a.roll(2);
assert_eq!(rolled.to_vec(), vec![4.0, 5.0, 1.0, 2.0, 3.0]);
Source

pub fn rot90(&self, k: isize) -> Array

Rotate array by 90 degrees in the plane specified by axes.

For 2D arrays, rotates counterclockwise.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let rotated = a.rot90(1);
assert_eq!(rotated.to_vec(), vec![2.0, 4.0, 1.0, 3.0]);
Source

pub fn swapaxes(&self, axis1: usize, axis2: usize) -> Array

Interchange two axes of an array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let swapped = a.swapaxes(0, 1);
assert_eq!(swapped.shape().as_slice(), &[3, 2]);
Source

pub fn moveaxis(&self, source: usize, destination: usize) -> Array

Move axes of an array to new positions.

Simplified version that only supports moving a single axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![1, 2, 3]));
let moved = a.moveaxis(2, 0);
assert_eq!(moved.shape().as_slice(), &[3, 1, 2]);
Source

pub fn interp(x: &Array, xp: &Array, fp: &Array) -> Array

One-dimensional linear interpolation.

Returns interpolated values at specified points using linear interpolation.

§Arguments
  • x - x-coordinates at which to evaluate the interpolated values
  • xp - x-coordinates of the data points (must be increasing)
  • fp - y-coordinates of the data points
§Examples
let xp = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let fp = Array::from_vec(vec![10.0, 20.0, 30.0], Shape::new(vec![3]));
let x = Array::from_vec(vec![1.5, 2.5], Shape::new(vec![2]));
let result = Array::interp(&x, &xp, &fp);
assert_eq!(result.to_vec(), vec![15.0, 25.0]);
Source

pub fn lerp(&self, other: &Array, weight: f32) -> Array

Linear interpolation between two arrays.

Returns a + weight * (b - a)

§Examples
let a = Array::from_vec(vec![0.0, 10.0, 20.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![100.0, 110.0, 120.0], Shape::new(vec![3]));
let result = a.lerp(&b, 0.5);
assert_eq!(result.to_vec(), vec![50.0, 60.0, 70.0]);
Source

pub fn lerp_array(&self, other: &Array, weights: &Array) -> Array

Linearly interpolate between two arrays element-wise with array weights.

Returns a + weight * (b - a) where weight is an array.

§Examples
let a = Array::from_vec(vec![0.0, 10.0, 20.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![100.0, 110.0, 120.0], Shape::new(vec![3]));
let weights = Array::from_vec(vec![0.0, 0.5, 1.0], Shape::new(vec![3]));
let result = a.lerp_array(&b, &weights);
assert_eq!(result.to_vec(), vec![0.0, 60.0, 120.0]);
Source

pub fn convolve(&self, kernel: &Array) -> Array

Compute the discrete 1D convolution of two arrays.

Returns the discrete linear convolution of the input array with a kernel. Uses ‘valid’ mode (only overlapping parts).

§Examples
let signal = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let kernel = Array::from_vec(vec![1.0, 0.0, -1.0], Shape::new(vec![3]));
let conv = signal.convolve(&kernel);
// Convolution flips the kernel: [-1, 0, 1]
// [1*(-1) + 2*0 + 3*1, 2*(-1) + 3*0 + 4*1, 3*(-1) + 4*0 + 5*1]
// = [-1+0+3, -2+0+4, -3+0+5] = [2, 2, 2]
assert_eq!(conv.to_vec(), vec![2.0, 2.0, 2.0]);
Source

pub fn correlate(&self, template: &Array) -> Array

Compute the cross-correlation of two 1D arrays.

Cross-correlation is similar to convolution but without flipping the kernel. Uses ‘valid’ mode (only overlapping parts).

§Examples
let signal = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let template = Array::from_vec(vec![1.0, 2.0, 1.0], Shape::new(vec![3]));
let corr = signal.correlate(&template);
// [1*1 + 2*2 + 3*1, 2*1 + 3*2 + 4*1, 3*1 + 4*2 + 5*1]
// = [1+4+3, 2+6+4, 3+8+5] = [8, 12, 16]
assert_eq!(corr.to_vec(), vec![8.0, 12.0, 16.0]);
Source

pub fn vstack(&self, other: &Array) -> Array

Stack arrays vertically (row-wise).

Equivalent to concatenation along axis 0 after promoting 1D arrays to 2D.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 5.0, 6.0], Shape::new(vec![3]));
let stacked = a.vstack(&b);
assert_eq!(stacked.shape().as_slice(), &[2, 3]);
assert_eq!(stacked.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
Source

pub fn hstack(&self, other: &Array) -> Array

Stack arrays horizontally (column-wise).

Equivalent to concatenation along axis 1.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2, 1]));
let b = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2, 1]));
let stacked = a.hstack(&b);
assert_eq!(stacked.shape().as_slice(), &[2, 2]);
assert_eq!(stacked.to_vec(), vec![1.0, 3.0, 2.0, 4.0]);
Source

pub fn vsplit(&self, num_sections: usize) -> Vec<Array>

Split array into multiple sub-arrays vertically (row-wise).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![6]));
let parts = a.vsplit(2);
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].to_vec(), vec![1.0, 2.0, 3.0]);
assert_eq!(parts[1].to_vec(), vec![4.0, 5.0, 6.0]);
Source

pub fn hsplit(&self, num_sections: usize) -> Vec<Array>

Split array into multiple sub-arrays horizontally (column-wise).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let parts = a.hsplit(2);
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].shape().as_slice(), &[2, 1]);
assert_eq!(parts[1].shape().as_slice(), &[2, 1]);
Source

pub fn append(&self, values: &Array) -> Array

Append values to the end of an array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 5.0], Shape::new(vec![2]));
let result = a.append(&b);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0]);
Source

pub fn insert(&self, index: usize, values: &Array) -> Array

Insert values at the given index.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 5.0, 6.0], Shape::new(vec![4]));
let values = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let result = a.insert(2, &values);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
Source

pub fn delete(&self, indices: &[usize]) -> Array

Delete elements at specified indices.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let result = a.delete(&[1, 3]);
assert_eq!(result.to_vec(), vec![1.0, 3.0, 5.0]);
Source

pub fn trim_zeros(&self) -> Array

Trim leading and trailing zeros.

§Examples
let a = Array::from_vec(vec![0.0, 0.0, 1.0, 2.0, 3.0, 0.0], Shape::new(vec![6]));
let trimmed = a.trim_zeros();
assert_eq!(trimmed.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn repeat_elements(&self, repeats: usize) -> Array

Repeat each element along axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let repeated = a.repeat_elements(2);
assert_eq!(repeated.to_vec(), vec![1.0, 1.0, 2.0, 2.0, 3.0, 3.0]);
Source

pub fn resize(&self, new_size: usize) -> Array

Resize array to new shape, repeating or truncating as needed.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let resized = a.resize(5);
assert_eq!(resized.to_vec(), vec![1.0, 2.0, 3.0, 1.0, 2.0]);
Source

pub fn corrcoef(&self, other: &Array) -> f32

Compute correlation coefficient between two 1D arrays.

§Examples
let x = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let y = Array::from_vec(vec![2.0, 4.0, 6.0, 8.0], Shape::new(vec![4]));
let corr = x.corrcoef(&y);
assert!((corr - 1.0).abs() < 1e-5); // Perfect correlation
Source

pub fn flatnonzero(&self) -> Vec<usize>

Return indices of non-zero elements in a flattened array.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 0.0, 3.0, 0.0, 5.0], Shape::new(vec![6]));
let indices = a.flatnonzero();
assert_eq!(indices, vec![1, 3, 5]);
Source

pub fn tile_1d(&self, reps: usize) -> Array

Tile the array by repeating it along each dimension.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = a.tile_1d(3);
assert_eq!(b.to_vec(), vec![1.0, 2.0, 1.0, 2.0, 1.0, 2.0]);
Source

pub fn column_stack(arrays: &[Array]) -> Array

Stack 1-D arrays as columns into a 2-D array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![4.0, 5.0, 6.0], Shape::new(vec![3]));
let c = Array::column_stack(&[a, b]);
assert_eq!(c.shape().as_slice(), &[3, 2]);
// [[1, 4], [2, 5], [3, 6]]
Source

pub fn row_stack(arrays: &[Array]) -> Array

Stack arrays in sequence vertically (row wise).

Alias for vstack.

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let c = Array::row_stack(&[a, b]);
assert_eq!(c.shape().as_slice(), &[2, 2]);
// [[1, 2], [3, 4]]
Source

pub fn dstack(arrays: &[Array]) -> Array

Stack arrays in sequence depth wise (along third axis).

§Examples
let a = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let b = Array::from_vec(vec![3.0, 4.0], Shape::new(vec![2]));
let c = Array::dstack(&[a, b]);
assert_eq!(c.shape().as_slice(), &[1, 2, 2]);
Source

pub fn absolute(&self) -> Array

Compute the absolute value and return as a new array (alias for abs).

§Examples
let a = Array::from_vec(vec![1.0, -2.0, 3.0], Shape::new(vec![3]));
let b = a.absolute();
assert_eq!(b.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn clamp(&self, min: f32, max: f32) -> Array

Clamp values to a specified range (alias for clip).

§Examples
let a = Array::from_vec(vec![1.0, 5.0, 10.0], Shape::new(vec![3]));
let b = a.clamp(2.0, 8.0);
assert_eq!(b.to_vec(), vec![2.0, 5.0, 8.0]);
Source

pub fn fill_diagonal(&self, value: f32) -> Array

Fill the diagonal of a 2D array with a scalar value.

§Examples
let a = Array::zeros(Shape::new(vec![3, 3]), DType::Float32);
let filled = a.fill_diagonal(5.0);
assert_eq!(filled.to_vec(), vec![5.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 5.0]);
Source

pub fn polyval(&self, coeffs: &Array) -> Array

Evaluate a polynomial at specific values. Polynomial coefficients are in decreasing order (highest degree first).

§Examples
// Evaluate p(x) = 2x^2 + 3x + 1 at x = [1, 2, 3]
let x = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let coeffs = Array::from_vec(vec![2.0, 3.0, 1.0], Shape::new(vec![3]));
let result = x.polyval(&coeffs);
// At x=1: 2(1)^2 + 3(1) + 1 = 6
// At x=2: 2(4) + 3(2) + 1 = 15
// At x=3: 2(9) + 3(3) + 1 = 28
assert_eq!(result.to_vec(), vec![6.0, 15.0, 28.0]);
Source

pub fn polyadd(&self, other: &Array) -> Array

Add two polynomials. Polynomial coefficients are in decreasing order (highest degree first).

§Examples
// Add p(x) = 2x^2 + 3x + 1 and q(x) = x^2 + 2x + 3
let p = Array::from_vec(vec![2.0, 3.0, 1.0], Shape::new(vec![3]));
let q = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let sum = p.polyadd(&q);
assert_eq!(sum.to_vec(), vec![3.0, 5.0, 4.0]);
Source

pub fn polymul(&self, other: &Array) -> Array

Multiply two polynomials. Polynomial coefficients are in decreasing order (highest degree first).

§Examples
// Multiply (x + 1) * (x + 2) = x^2 + 3x + 2
let p = Array::from_vec(vec![1.0, 1.0], Shape::new(vec![2]));
let q = Array::from_vec(vec![1.0, 2.0], Shape::new(vec![2]));
let prod = p.polymul(&q);
assert_eq!(prod.to_vec(), vec![1.0, 3.0, 2.0]);
Source

pub fn polyder(&self) -> Array

Differentiate a polynomial. Returns the polynomial representing the derivative.

§Examples
// d/dx (2x^2 + 3x + 1) = 4x + 3
let p = Array::from_vec(vec![2.0, 3.0, 1.0], Shape::new(vec![3]));
let dp = p.polyder();
assert_eq!(dp.to_vec(), vec![4.0, 3.0]);
Source

pub fn polysub(&self, other: &Array) -> Array

Subtract two polynomials. Polynomial coefficients are in decreasing order (highest degree first).

§Examples
let p = Array::from_vec(vec![3.0, 5.0, 4.0], Shape::new(vec![3]));
let q = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let diff = p.polysub(&q);
assert_eq!(diff.to_vec(), vec![2.0, 3.0, 1.0]);
Source

pub fn piecewise(&self, conditions: &[Array], functions: &[Array]) -> Array

Evaluate a piecewise-defined function.

Applies different functions based on conditions. For each element, the first true condition determines which function to apply.

§Arguments
  • conditions - Vector of condition arrays (booleans as 0.0/1.0)
  • functions - Vector of function output arrays corresponding to conditions
§Examples
let x = Array::from_vec(vec![-2.0, -1.0, 0.0, 1.0, 2.0], Shape::new(vec![5]));
// Condition: x < 0
let cond1 = Array::from_vec(vec![1.0, 1.0, 0.0, 0.0, 0.0], Shape::new(vec![5]));
// Condition: x >= 0
let cond2 = Array::from_vec(vec![0.0, 0.0, 1.0, 1.0, 1.0], Shape::new(vec![5]));
// Function outputs (pre-computed)
let func1 = Array::from_vec(vec![-2.0, -1.0, 0.0, 1.0, 2.0], Shape::new(vec![5])); // identity
let func2 = Array::from_vec(vec![4.0, 1.0, 0.0, 1.0, 4.0], Shape::new(vec![5])); // x^2
let result = x.piecewise(&[cond1, cond2], &[func1, func2]);
// For x<0: use identity, for x>=0: use x^2
assert_eq!(result.to_vec(), vec![-2.0, -1.0, 0.0, 1.0, 4.0]);
Source

pub fn place(&self, mask: &Array, values: &[f32]) -> Array

Place values into array at specified indices.

Returns a new array with values inserted at the specified indices.

§Examples
let a = Array::from_vec(vec![0.0, 0.0, 0.0, 0.0, 0.0], Shape::new(vec![5]));
let mask = Array::from_vec(vec![0.0, 1.0, 0.0, 1.0, 0.0], Shape::new(vec![5]));
let values = vec![10.0, 20.0];
let result = a.place(&mask, &values);
assert_eq!(result.to_vec(), vec![0.0, 10.0, 0.0, 20.0, 0.0]);
Source

pub fn copyto(&self, src: &Array, mask: &Array) -> Array

Copy values from source to destination array.

Returns a new array with values from source copied to corresponding positions.

§Examples
let dst = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let src = Array::from_vec(vec![10.0, 20.0, 30.0, 40.0, 50.0], Shape::new(vec![5]));
let mask = Array::from_vec(vec![0.0, 1.0, 1.0, 0.0, 1.0], Shape::new(vec![5]));
let result = dst.copyto(&src, &mask);
assert_eq!(result.to_vec(), vec![1.0, 20.0, 30.0, 4.0, 50.0]);
Source

pub fn argmax_with_value(&self) -> (usize, f32)

Return the index of the maximum element along an axis and the max value.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 1.0, 5.0], Shape::new(vec![5]));
let (idx, val) = a.argmax_with_value();
assert_eq!(idx, 4);
assert!((val - 5.0).abs() < 1e-6);
Source

pub fn argmin_with_value(&self) -> (usize, f32)

Return the index of the minimum element along an axis and the min value.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 1.0, 5.0], Shape::new(vec![5]));
let (idx, val) = a.argmin_with_value();
assert_eq!(idx, 1);
assert!((val - 1.0).abs() < 1e-6);
Source

pub fn permute(&self, axes: &[usize]) -> Array

Return an array with axes transposed to the given permutation.

§Examples
let a = Array::from_vec(
    vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
    Shape::new(vec![2, 3])
);
let b = a.permute(&[1, 0]);
assert_eq!(b.shape().as_slice(), &[3, 2]);
Source

pub fn gather(&self, indices: &Array, axis: usize) -> Array

Gather values along an axis using indices.

This is a generalized form of indexing that allows selecting arbitrary indices along a specified axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let indices = Array::from_vec(vec![0.0, 2.0], Shape::new(vec![2]));
let result = a.gather(&indices, 1);
// Gathers columns 0 and 2 from each row
Source

pub fn gather_nd(&self, indices: &[(usize, usize)]) -> Array

Gather values using n-dimensional indices.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let indices = vec![(0, 1), (1, 2)]; // (row, col) pairs
let result = a.gather_nd(&indices);
// Returns values at [0,1] and [1,2]
Source

pub fn segment_sum(&self, segment_ids: &Array, num_segments: usize) -> Array

Segment sum - sum elements by segment ID.

§Examples
let data = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let segment_ids = Array::from_vec(vec![0.0, 0.0, 1.0, 1.0, 2.0], Shape::new(vec![5]));
let result = data.segment_sum(&segment_ids, 3);
// segment 0: 1+2=3, segment 1: 3+4=7, segment 2: 5
Source

pub fn segment_mean(&self, segment_ids: &Array, num_segments: usize) -> Array

Segment mean - compute mean of elements by segment ID.

Source

pub fn segment_max(&self, segment_ids: &Array, num_segments: usize) -> Array

Segment max - compute max of elements by segment ID.

Source

pub fn segment_min(&self, segment_ids: &Array, num_segments: usize) -> Array

Segment min - compute min of elements by segment ID.

Source

pub fn flip_axes(&self, axes: &[usize]) -> Array

Flip array along multiple axes.

Source

pub fn moveaxis_multiple( &self, sources: &[usize], destinations: &[usize], ) -> Array

Move multiple axes to new positions.

Source

pub fn expand_dims_multiple(&self, axes: &[usize]) -> Array

Expand dimensions at multiple positions.

Source

pub fn squeeze_all(&self) -> Array

Squeeze all axes with size 1.

Source

pub fn unflatten(&self, dim: usize, sizes: &[usize]) -> Array

Unflatten array - reshape the first axis into multiple dimensions.

Source

pub fn repeat_axis(&self, repeats: usize, axis: usize) -> Array

Repeat array elements along each axis.

Source

pub fn tile_nd(&self, reps: &[usize]) -> Array

N-dimensional tile - repeat array along each axis.

Source§

impl Array

Source

pub fn sum_all(&self) -> f32

Sum of all elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let sum = a.sum_all();
assert_eq!(sum, 10.0);
Source

pub fn sum_all_array(&self) -> Array

Sum of all elements, returned as a scalar Array.

This is a convenience method for autodiff that wraps sum_all().

Source

pub fn sum(&self, axis: usize) -> Array

Sum along a specific axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let sum_axis0 = a.sum(0);
assert_eq!(sum_axis0.to_vec(), vec![5.0, 7.0, 9.0]);
let sum_axis1 = a.sum(1);
assert_eq!(sum_axis1.to_vec(), vec![6.0, 15.0]);
Source

pub fn mean_all(&self) -> f32

Mean of all elements.

Source

pub fn mean_all_array(&self) -> Array

Mean of all elements, returning a scalar array.

Source

pub fn mean(&self, axis: usize) -> Array

Mean along a specific axis.

Source

pub fn max_all(&self) -> f32

Maximum of all elements.

Source

pub fn max(&self, axis: usize) -> Array

Maximum along a specific axis.

Source

pub fn min_all(&self) -> f32

Minimum of all elements.

Source

pub fn min(&self, axis: usize) -> Array

Minimum along a specific axis.

Source

pub fn prod_all(&self) -> f32

Product of all elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let prod = a.prod_all();
assert_eq!(prod, 24.0);
Source

pub fn prod(&self, axis: usize) -> Array

Product along a specific axis.

Source

pub fn argmin(&self) -> usize

Index of minimum element.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let idx = a.argmin();
assert_eq!(idx, 1);
Source

pub fn argmax(&self) -> usize

Index of maximum element.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let idx = a.argmax();
assert_eq!(idx, 2);
Source

pub fn var(&self) -> f32

Variance of all elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let var = a.var();
assert!((var - 1.25).abs() < 1e-6);
Source

pub fn std(&self) -> f32

Standard deviation of all elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let std = a.std();
assert!((std - 1.118).abs() < 0.01);
Source

pub fn var_axis(&self, axis: usize) -> Array

Variance along a specific axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let var_axis0 = a.var_axis(0);
assert_eq!(var_axis0.shape().as_slice(), &[2]);
Source

pub fn std_axis(&self, axis: usize) -> Array

Standard deviation along a specific axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![2, 2]));
let std_axis0 = a.std_axis(0);
assert_eq!(std_axis0.shape().as_slice(), &[2]);
Source

pub fn median(&self) -> f32

Median of all elements.

§Examples
let a = Array::from_vec(vec![1.0, 3.0, 2.0], Shape::new(vec![3]));
let med = a.median();
assert_eq!(med, 2.0);
Source

pub fn percentile(&self, q: f32) -> f32

Percentile of all elements.

§Arguments
  • q - Percentile to compute (0-100)
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let p50 = a.percentile(50.0);
assert_eq!(p50, 3.0);
Source

pub fn cumsum(&self) -> Array

Cumulative sum of array elements.

Returns an array of the same shape with cumulative sums.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let cumsum = a.cumsum();
assert_eq!(cumsum.to_vec(), vec![1.0, 3.0, 6.0, 10.0]);
Source

pub fn cumprod(&self) -> Array

Cumulative product of array elements.

Returns an array of the same shape with cumulative products.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let cumprod = a.cumprod();
assert_eq!(cumprod.to_vec(), vec![1.0, 2.0, 6.0, 24.0]);
Source

pub fn cummax(&self) -> Array

Cumulative maximum of array elements.

Returns an array of the same shape with cumulative maximums.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let cummax = a.cummax();
assert_eq!(cummax.to_vec(), vec![3.0, 3.0, 4.0, 4.0]);
Source

pub fn cummin(&self) -> Array

Cumulative minimum of array elements.

Returns an array of the same shape with cumulative minimums.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let cummin = a.cummin();
assert_eq!(cummin.to_vec(), vec![3.0, 1.0, 1.0, 1.0]);
Source

pub fn diff(&self) -> Array

Calculate the discrete difference along the array.

Computes the difference between consecutive elements.

§Examples
let a = Array::from_vec(vec![1.0, 3.0, 6.0, 10.0], Shape::new(vec![4]));
let diff = a.diff();
assert_eq!(diff.to_vec(), vec![2.0, 3.0, 4.0]);
Source

pub fn diff_n(&self, n: usize) -> Array

Calculate the n-th discrete difference.

Recursively applies diff n times.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let diff2 = a.diff_n(2);
assert_eq!(diff2.to_vec(), vec![0.0, 0.0, 0.0]);
Source

pub fn nansum(&self) -> f32

Sum of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 4.0], Shape::new(vec![4]));
let sum = a.nansum();
assert_eq!(sum, 8.0);
Source

pub fn nanmean(&self) -> f32

Mean of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 4.0], Shape::new(vec![4]));
let mean = a.nanmean();
assert_eq!(mean, 8.0 / 3.0);
Source

pub fn nanmax(&self) -> f32

Maximum of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 4.0, 2.0], Shape::new(vec![4]));
let max = a.nanmax();
assert_eq!(max, 4.0);
Source

pub fn nanmin(&self) -> f32

Minimum of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 4.0, 2.0], Shape::new(vec![4]));
let min = a.nanmin();
assert_eq!(min, 1.0);
Source

pub fn nanstd(&self) -> f32

Standard deviation of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 5.0], Shape::new(vec![4]));
let std = a.nanstd();
assert!((std - 2.0).abs() < 1e-5);
Source

pub fn nanvar(&self) -> f32

Variance of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 5.0], Shape::new(vec![4]));
let var = a.nanvar();
assert!((var - 4.0).abs() < 1e-5);
Source

pub fn nanmedian(&self) -> f32

Median of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 5.0, 2.0], Shape::new(vec![5]));
let median = a.nanmedian();
assert_eq!(median, 2.5);
Source

pub fn ptp(&self) -> f32

Peak-to-peak (maximum - minimum) value.

§Examples
let a = Array::from_vec(vec![1.0, 5.0, 2.0, 8.0], Shape::new(vec![4]));
assert_eq!(a.ptp(), 7.0);
Source

pub fn ptp_axis(&self, axis: usize) -> Array

Peak-to-peak (maximum - minimum) along an axis.

§Examples
let a = Array::from_vec(vec![1.0, 5.0, 2.0, 8.0], Shape::new(vec![2, 2]));
let ptp = a.ptp_axis(0);
assert_eq!(ptp.to_vec(), vec![1.0, 3.0]);
Source

pub fn quantile(&self, q: f32) -> f32

Compute the q-th quantile of the data.

§Arguments
  • q - Quantile to compute (between 0.0 and 1.0)
§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], Shape::new(vec![5]));
let q = a.quantile(0.5); // Median
assert!((q - 3.0).abs() < 1e-6);
Source

pub fn quantile_axis(&self, q: f32, axis: usize) -> Array

Compute the q-th quantile along an axis.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let q = a.quantile_axis(0.5, 0);
assert_eq!(q.shape().as_slice(), &[3]);
Source

pub fn trapz(&self) -> f32

Integrate along the array using the composite trapezoidal rule.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let integral = a.trapz();
assert_eq!(integral, 4.0); // (1+2)/2 + (2+3)/2 = 1.5 + 2.5 = 4.0
Source

pub fn trapz_axis(&self, axis: usize) -> Array

Integrate along an axis using the composite trapezoidal rule.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], Shape::new(vec![2, 3]));
let integral = a.trapz_axis(1);
assert_eq!(integral.shape().as_slice(), &[2]);
Source

pub fn gradient(&self) -> Array

Compute the gradient (numerical derivative) of an array.

For an array [a, b, c, d], returns [b-a, (c-a)/2, (d-b)/2, d-c]. Uses forward differences at the start, backward differences at the end, and central differences in the middle.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 4.0, 7.0], Shape::new(vec![4]));
let grad = a.gradient();
// [2-1, (4-1)/2, (7-2)/2, 7-4] = [1.0, 1.5, 2.5, 3.0]
assert_eq!(grad.to_vec(), vec![1.0, 1.5, 2.5, 3.0]);
Source

pub fn ediff1d(&self) -> Array

Compute differences between consecutive elements (edge differences).

This is equivalent to diff but specifically meant for edge detection. Returns an array of length n-1 where result[i] = array[i+1] - array[i].

§Examples
let a = Array::from_vec(vec![1.0, 3.0, 6.0, 10.0], Shape::new(vec![4]));
let edges = a.ediff1d();
assert_eq!(edges.to_vec(), vec![2.0, 3.0, 4.0]);
Source

pub fn nanargmax(&self) -> usize

Find the index of the maximum value, ignoring NaN.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 5.0, 3.0], Shape::new(vec![4]));
let idx = a.nanargmax();
assert_eq!(idx, 2); // Index of 5.0
Source

pub fn nanargmin(&self) -> usize

Find the index of the minimum value, ignoring NaN.

§Examples
let a = Array::from_vec(vec![5.0, f32::NAN, 1.0, 3.0], Shape::new(vec![4]));
let idx = a.nanargmin();
assert_eq!(idx, 2); // Index of 1.0
Source

pub fn average(&self, weights: &Array) -> f32

Compute the weighted average of an array.

§Examples
let values = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let weights = Array::from_vec(vec![1.0, 1.0, 1.0, 1.0], Shape::new(vec![4]));
let avg = values.average(&weights);
assert_eq!(avg, 2.5);
Source

pub fn cov(&self, other: &Array) -> f32

Compute covariance between two arrays.

§Examples
let x = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let y = Array::from_vec(vec![2.0, 4.0, 6.0, 8.0], Shape::new(vec![4]));
let cov = x.cov(&y);
// Covariance should be positive since both increase together
Source

pub fn nanprod(&self) -> f32

Product of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 4.0], Shape::new(vec![4]));
let prod = a.nanprod();
assert_eq!(prod, 12.0); // 1 * 3 * 4
Source

pub fn nancumsum(&self) -> Array

Cumulative sum of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 4.0], Shape::new(vec![4]));
let cumsum = a.nancumsum();
// Treats NaN as 0
assert_eq!(cumsum.to_vec(), vec![1.0, 1.0, 4.0, 8.0]);
Source

pub fn nancumprod(&self) -> Array

Cumulative product of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 4.0], Shape::new(vec![4]));
let cumprod = a.nancumprod();
// Treats NaN as 1
assert_eq!(cumprod.to_vec(), vec![1.0, 1.0, 3.0, 12.0]);
Source

pub fn agmean(&self) -> f32

Compute the arithmetic-geometric mean.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let agm = a.agmean();
// AGM is between arithmetic and geometric means
Source

pub fn rms(&self) -> f32

Root mean square of array elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let rms = a.rms();
// sqrt((1 + 4 + 9) / 3) = sqrt(14/3)
assert!((rms - (14.0_f32 / 3.0).sqrt()).abs() < 1e-6);
Source

pub fn harmonic_mean(&self) -> f32

Harmonic mean of array elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 4.0], Shape::new(vec![3]));
let hm = a.harmonic_mean();
// 3 / (1/1 + 1/2 + 1/4) = 3 / 1.75
Source

pub fn geometric_mean(&self) -> f32

Geometric mean of array elements.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 4.0, 8.0], Shape::new(vec![4]));
let gm = a.geometric_mean();
// (1 * 2 * 4 * 8)^(1/4) = 64^(1/4) = 2.83...
Source

pub fn nanpercentile(&self, q: f32) -> f32

Compute percentile of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 5.0, 7.0], Shape::new(vec![5]));
let p = a.nanpercentile(50.0);
assert!((p - 4.0).abs() < 1e-6);  // median of [1, 3, 5, 7] = 4
Source

pub fn nanquantile(&self, q: f32) -> f32

Compute quantile of array elements, ignoring NaN values.

§Examples
let a = Array::from_vec(vec![1.0, f32::NAN, 3.0, 5.0, 7.0], Shape::new(vec![5]));
let q = a.nanquantile(0.5);
assert!((q - 4.0).abs() < 1e-6);  // median of [1, 3, 5, 7] = 4
Source§

impl Array

Source

pub fn sort(&self) -> Array

Sort array elements.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let sorted = a.sort();
assert_eq!(sorted.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
Source

pub fn sort_descending(&self) -> Array

Sort array elements in descending order.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let sorted = a.sort_descending();
assert_eq!(sorted.to_vec(), vec![4.0, 3.0, 2.0, 1.0]);
Source

pub fn argsort(&self) -> Vec<usize>

Return indices that would sort the array.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let indices = a.argsort();
assert_eq!(indices, vec![1, 3, 0, 2]);
Source

pub fn argsort_descending(&self) -> Vec<usize>

Return indices that would sort the array in descending order.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0], Shape::new(vec![4]));
let indices = a.argsort_descending();
assert_eq!(indices, vec![2, 0, 3, 1]);
Source

pub fn top_k_smallest(&self, k: usize) -> Vec<usize>

Find the k smallest elements and return their indices.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0, 5.0], Shape::new(vec![5]));
let top2 = a.top_k_smallest(2);
assert_eq!(top2, vec![1, 3]);
Source

pub fn top_k_largest(&self, k: usize) -> Vec<usize>

Find the k largest elements and return their indices.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 2.0, 5.0], Shape::new(vec![5]));
let top2 = a.top_k_largest(2);
assert_eq!(top2, vec![4, 2]);
Source

pub fn searchsorted(&self, value: f32) -> usize

Find indices where elements should be inserted to maintain order.

§Examples
let a = Array::from_vec(vec![1.0, 3.0, 5.0, 7.0], Shape::new(vec![4]));
let idx = a.searchsorted(4.0);
assert_eq!(idx, 2);
Source

pub fn unique(&self) -> Array

Find unique elements in the array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 1.0, 3.0, 2.0], Shape::new(vec![5]));
let unique = a.unique();
assert_eq!(unique.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn unique_counts(&self) -> (Array, Vec<usize>)

Count occurrences of each unique value.

Returns (unique_values, counts).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 1.0, 3.0, 2.0, 1.0], Shape::new(vec![6]));
let (values, counts) = a.unique_counts();
assert_eq!(values.to_vec(), vec![1.0, 2.0, 3.0]);
assert_eq!(counts, vec![3, 2, 1]);
Source

pub fn setdiff1d(&self, other: &Array) -> Array

Find the set difference of two arrays.

Returns the unique values in the first array that are not in the second array.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let b = Array::from_vec(vec![2.0, 4.0, 5.0], Shape::new(vec![3]));
let diff = a.setdiff1d(&b);
assert_eq!(diff.to_vec(), vec![1.0, 3.0]);
Source

pub fn union1d(&self, other: &Array) -> Array

Find the union of two arrays.

Returns the unique values that are in either of the two arrays.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let union = a.union1d(&b);
assert_eq!(union.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
Source

pub fn intersect1d(&self, other: &Array) -> Array

Find the intersection of two arrays.

Returns the unique values that are in both arrays.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let intersect = a.intersect1d(&b);
assert_eq!(intersect.to_vec(), vec![2.0, 3.0]);
Source

pub fn setxor1d(&self, other: &Array) -> Array

Find the exclusive-or of two arrays.

Returns the unique values that are in exactly one of the two arrays.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = Array::from_vec(vec![2.0, 3.0, 4.0], Shape::new(vec![3]));
let xor = a.setxor1d(&b);
assert_eq!(xor.to_vec(), vec![1.0, 4.0]);
Source

pub fn in1d(&self, test_elements: &Array) -> Array

Test whether each element of a 1D array is also present in a second array.

Returns a boolean-like array (1.0 for true, 0.0 for false).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let b = Array::from_vec(vec![2.0, 4.0], Shape::new(vec![2]));
let result = a.in1d(&b);
assert_eq!(result.to_vec(), vec![0.0, 1.0, 0.0, 1.0]);
Source

pub fn digitize(&self, bins: &Array) -> Vec<usize>

Return the indices of the bins to which each value belongs.

§Examples
let x = Array::from_vec(vec![0.2, 6.4, 3.0, 1.6], Shape::new(vec![4]));
let bins = Array::from_vec(vec![0.0, 1.0, 2.5, 4.0, 10.0], Shape::new(vec![5]));
let indices = x.digitize(&bins);
assert_eq!(indices, vec![1, 4, 3, 2]);
Source

pub fn histogram( &self, bins: usize, range_min: f32, range_max: f32, ) -> (Vec<usize>, Vec<f32>)

Compute the histogram of a dataset.

Returns (hist, bin_edges) where hist contains the counts and bin_edges contains the bin boundaries.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 1.0, 3.0, 2.0, 1.0], Shape::new(vec![6]));
let (hist, edges) = a.histogram(3, 0.0, 4.0);
assert_eq!(hist, vec![3, 2, 1]);
Source

pub fn bincount(&self) -> Vec<usize>

Count number of occurrences of each value in array of non-negative integers.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 1.0, 3.0, 2.0, 1.0, 7.0], Shape::new(vec![7]));
let counts = a.bincount();
assert_eq!(counts, vec![1, 3, 1, 1, 0, 0, 0, 1]);
Source

pub fn bincount_weighted(&self, weights: &Array) -> Vec<f32>

Count number of occurrences with optional weights.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 1.0, 2.0], Shape::new(vec![4]));
let weights = Array::from_vec(vec![0.3, 0.5, 0.2, 0.7], Shape::new(vec![4]));
let counts = a.bincount_weighted(&weights);
assert!((counts[0] - 0.3).abs() < 1e-6);
assert!((counts[1] - 0.7).abs() < 1e-6);
assert!((counts[2] - 0.7).abs() < 1e-6);
Source

pub fn partition(&self, kth: usize) -> Array

Partially sort array so that the k-th element is in sorted position.

Elements smaller than the k-th element are moved before it, and elements larger are moved after it.

§Examples
let a = Array::from_vec(vec![3.0, 4.0, 2.0, 1.0], Shape::new(vec![4]));
let partitioned = a.partition(2);
// Element at index 2 is in correct sorted position
let data = partitioned.to_vec();
assert!(data[0] <= data[2] && data[1] <= data[2]);
assert!(data[2] <= data[3]);
Source

pub fn argpartition(&self, kth: usize) -> Vec<usize>

Return indices that would partition the array.

§Examples
let a = Array::from_vec(vec![3.0, 4.0, 2.0, 1.0], Shape::new(vec![4]));
let indices = a.argpartition(2);
// Indices are such that a[indices[0]] and a[indices[1]] are smaller than a[indices[2]]
Source

pub fn lexsort(keys: &[&Array]) -> Vec<usize>

Perform indirect stable sort using a sequence of keys.

Sort by the last key first, then by second-to-last, etc. This is equivalent to numpy’s lexsort.

§Examples
// Sort by surname, then by first name
let surnames = Array::from_vec(vec![1.0, 2.0, 1.0, 2.0], Shape::new(vec![4]));  // Hertz, Move, Hertz, Newton
let first_names = Array::from_vec(vec![1.0, 2.0, 2.0, 1.0], Shape::new(vec![4])); // Heinrich, Never, Lansen, Isaac
let indices = Array::lexsort(&[&first_names, &surnames]);
// Should be sorted by surname first, then first_name
Source

pub fn median_select(&self) -> f32

Return the median element without full sorting.

Uses quickselect algorithm for O(n) average performance.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 1.0, 5.0], Shape::new(vec![5]));
let med = a.median_select();
assert_eq!(med, 3.0);
Source

pub fn select_kth(&self, k: usize) -> f32

Return the k-th smallest element using selection algorithm.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 4.0, 1.0, 5.0], Shape::new(vec![5]));
let kth = a.select_kth(2); // 0-indexed, so 3rd smallest
assert_eq!(kth, 3.0);
Source

pub fn sort_axis(&self, _axis: i32) -> Array

Sort array along the last axis.

For 2D arrays, sorts each row independently.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 2.0, 6.0, 4.0, 5.0], Shape::new(vec![2, 3]));
let sorted = a.sort_axis(-1);
assert_eq!(sorted.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
Source

pub fn stable_argsort(&self) -> Vec<usize>

Return indices that would sort the array in stable order.

Unlike argsort, stable_argsort preserves the relative order of equal elements.

§Examples
let a = Array::from_vec(vec![3.0, 1.0, 1.0, 2.0], Shape::new(vec![4]));
let indices = a.stable_argsort();
assert_eq!(indices, vec![1, 2, 3, 0]); // The two 1.0s maintain order
Source§

impl Array

Source

pub fn neg(&self) -> Array

Negate the array element-wise.

§Examples
let a = Array::from_vec(vec![1.0, -2.0, 3.0], Shape::new(vec![3]));
let b = a.neg();
assert_eq!(b.to_vec(), vec![-1.0, 2.0, -3.0]);
Source

pub fn abs(&self) -> Array

Absolute value element-wise.

Source

pub fn sin(&self) -> Array

Sine element-wise.

Source

pub fn cos(&self) -> Array

Cosine element-wise.

Source

pub fn tan(&self) -> Array

Tangent element-wise.

Source

pub fn tanh(&self) -> Array

Hyperbolic tangent element-wise.

Source

pub fn exp(&self) -> Array

Natural exponential (e^x) element-wise.

Source

pub fn log(&self) -> Array

Natural logarithm element-wise.

Source

pub fn sqrt(&self) -> Array

Square root element-wise.

Source

pub fn reciprocal(&self) -> Array

Reciprocal (1/x) element-wise.

Source

pub fn square(&self) -> Array

Square (x^2) element-wise.

Source

pub fn sign(&self) -> Array

Sign function element-wise (-1, 0, or 1).

Source

pub fn sinh(&self) -> Array

Hyperbolic sine element-wise.

§Examples
let a = Array::from_vec(vec![0.0], Shape::new(vec![1]));
let b = a.sinh();
assert_eq!(b.to_vec()[0], 0.0);
Source

pub fn cosh(&self) -> Array

Hyperbolic cosine element-wise.

§Examples
let a = Array::from_vec(vec![0.0], Shape::new(vec![1]));
let b = a.cosh();
assert_eq!(b.to_vec()[0], 1.0);
Source

pub fn asin(&self) -> Array

Arcsine element-wise.

§Examples
let a = Array::from_vec(vec![0.0, 1.0], Shape::new(vec![2]));
let b = a.asin();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
assert!((b.to_vec()[1] - std::f32::consts::FRAC_PI_2).abs() < 1e-6);
Source

pub fn acos(&self) -> Array

Arccosine element-wise.

§Examples
let a = Array::from_vec(vec![1.0], Shape::new(vec![1]));
let b = a.acos();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
Source

pub fn atan(&self) -> Array

Arctangent element-wise.

§Examples
let a = Array::from_vec(vec![0.0, 1.0], Shape::new(vec![2]));
let b = a.atan();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
assert!((b.to_vec()[1] - std::f32::consts::FRAC_PI_4).abs() < 1e-6);
Source

pub fn asinh(&self) -> Array

Inverse hyperbolic sine element-wise.

§Examples
let a = Array::from_vec(vec![0.0], Shape::new(vec![1]));
let b = a.asinh();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
Source

pub fn acosh(&self) -> Array

Inverse hyperbolic cosine element-wise.

§Examples
let a = Array::from_vec(vec![1.0], Shape::new(vec![1]));
let b = a.acosh();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
Source

pub fn atanh(&self) -> Array

Inverse hyperbolic tangent element-wise.

§Examples
let a = Array::from_vec(vec![0.0], Shape::new(vec![1]));
let b = a.atanh();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
Source

pub fn ceil(&self) -> Array

Ceiling function element-wise.

§Examples
let a = Array::from_vec(vec![1.2, 2.7, -0.5], Shape::new(vec![3]));
let b = a.ceil();
assert_eq!(b.to_vec(), vec![2.0, 3.0, 0.0]);
Source

pub fn floor(&self) -> Array

Floor function element-wise.

§Examples
let a = Array::from_vec(vec![1.2, 2.7, -0.5], Shape::new(vec![3]));
let b = a.floor();
assert_eq!(b.to_vec(), vec![1.0, 2.0, -1.0]);
Source

pub fn round(&self) -> Array

Round to nearest integer element-wise.

§Examples
let a = Array::from_vec(vec![1.2, 2.7, -0.5], Shape::new(vec![3]));
let b = a.round();
assert_eq!(b.to_vec(), vec![1.0, 3.0, -1.0]);
Source

pub fn trunc(&self) -> Array

Truncate to integer element-wise (round toward zero).

§Examples
let a = Array::from_vec(vec![1.7, 2.3, -1.7], Shape::new(vec![3]));
let b = a.trunc();
assert_eq!(b.to_vec(), vec![1.0, 2.0, -1.0]);
Source

pub fn expm1(&self) -> Array

Exponential minus 1 (e^x - 1) element-wise.

More accurate than exp(x) - 1 for small values of x.

§Examples
let a = Array::from_vec(vec![0.0], Shape::new(vec![1]));
let b = a.expm1();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
Source

pub fn log1p(&self) -> Array

Natural logarithm of 1 + x element-wise.

More accurate than log(1 + x) for small values of x.

§Examples
let a = Array::from_vec(vec![0.0], Shape::new(vec![1]));
let b = a.log1p();
assert!((b.to_vec()[0] - 0.0).abs() < 1e-6);
Source

pub fn reciprocal_no_nan(&self) -> Array

Safe reciprocal that returns 0 where x == 0.

Returns 1/x where x != 0, and 0 where x == 0.

§Examples
let a = Array::from_vec(vec![2.0, 0.0, 4.0], Shape::new(vec![3]));
let b = a.reciprocal_no_nan();
assert_eq!(b.to_vec(), vec![0.5, 0.0, 0.25]);
Source

pub fn deg2rad(&self) -> Array

Convert degrees to radians.

§Examples
let degrees = Array::from_vec(vec![0.0, 90.0, 180.0], Shape::new(vec![3]));
let radians = degrees.deg2rad();
assert!((radians.to_vec()[1] - std::f32::consts::PI / 2.0).abs() < 1e-5);
Source

pub fn rad2deg(&self) -> Array

Convert radians to degrees.

§Examples
let radians = Array::from_vec(vec![0.0, std::f32::consts::PI / 2.0, std::f32::consts::PI], Shape::new(vec![3]));
let degrees = radians.rad2deg();
assert!((degrees.to_vec()[1] - 90.0).abs() < 1e-5);
Source

pub fn sinc(&self) -> Array

Compute the sinc function: sin(x) / x.

§Examples
let x = Array::from_vec(vec![0.0, 1.0, 2.0], Shape::new(vec![3]));
let y = x.sinc();
assert_eq!(y.to_vec()[0], 1.0); // sinc(0) = 1
Source

pub fn cbrt(&self) -> Array

Compute the cube root.

§Examples
let a = Array::from_vec(vec![8.0, 27.0, 64.0], Shape::new(vec![3]));
let b = a.cbrt();
assert_eq!(b.to_vec(), vec![2.0, 3.0, 4.0]);
Source

pub fn arcsin(&self) -> Array

Compute the inverse sine (arcsine) element-wise.

Returns values in the range [-π/2, π/2].

§Examples
let a = Array::from_vec(vec![0.0, 0.5, 1.0], Shape::new(vec![3]));
let b = a.arcsin();
// Result: [0.0, ~0.524, ~1.571] (radians)
Source

pub fn arccos(&self) -> Array

Compute the inverse cosine (arccosine) element-wise.

Returns values in the range [0, π].

§Examples
let a = Array::from_vec(vec![1.0, 0.5, 0.0], Shape::new(vec![3]));
let b = a.arccos();
// Result: [0.0, ~1.047, ~1.571] (radians)
Source

pub fn arctan(&self) -> Array

Compute the inverse tangent (arctangent) element-wise.

Returns values in the range [-π/2, π/2].

§Examples
let a = Array::from_vec(vec![0.0, 1.0, -1.0], Shape::new(vec![3]));
let b = a.arctan();
// Result: [0.0, ~0.785, ~-0.785] (radians)
Source

pub fn arcsinh(&self) -> Array

Compute the inverse hyperbolic sine element-wise.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 2.0], Shape::new(vec![3]));
let b = a.arcsinh();
// Result: [0.0, ~0.881, ~1.444]
Source

pub fn arccosh(&self) -> Array

Compute the inverse hyperbolic cosine element-wise.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.arccosh();
// Result: [0.0, ~1.317, ~1.763]
Source

pub fn arctanh(&self) -> Array

Compute the inverse hyperbolic tangent element-wise.

§Examples
let a = Array::from_vec(vec![0.0, 0.5, -0.5], Shape::new(vec![3]));
let b = a.arctanh();
// Result: [0.0, ~0.549, ~-0.549]
Source

pub fn log10(&self) -> Array

Compute the base-10 logarithm element-wise.

§Examples
let a = Array::from_vec(vec![1.0, 10.0, 100.0, 1000.0], Shape::new(vec![4]));
let b = a.log10();
assert_eq!(b.to_vec(), vec![0.0, 1.0, 2.0, 3.0]);
Source

pub fn log2(&self) -> Array

Compute the base-2 logarithm element-wise.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 4.0, 8.0], Shape::new(vec![4]));
let b = a.log2();
assert_eq!(b.to_vec(), vec![0.0, 1.0, 2.0, 3.0]);
Source

pub fn around(&self, decimals: i32) -> Array

Round to n decimal places.

§Examples
let a = Array::from_vec(vec![1.234, 5.678, 9.012], Shape::new(vec![3]));
let b = a.around(1);
// Result: [1.2, 5.7, 9.0]
Source

pub fn fix(&self) -> Array

Round toward zero (truncate decimal part).

§Examples
let a = Array::from_vec(vec![1.7, -2.3, 3.9], Shape::new(vec![3]));
let b = a.fix();
assert_eq!(b.to_vec(), vec![1.0, -2.0, 3.0]);
Source

pub fn signbit(&self) -> Array

Check if sign bit is set (negative number).

Returns 1.0 for negative numbers, 0.0 for positive.

§Examples
let a = Array::from_vec(vec![1.0, -2.0, 0.0, -0.0], Shape::new(vec![4]));
let b = a.signbit();
// Result: [0.0, 1.0, 0.0, 1.0]
Source

pub fn positive(&self) -> Array

Unary positive (identity operation).

§Examples
let a = Array::from_vec(vec![1.0, -2.0, 3.0], Shape::new(vec![3]));
let b = a.positive();
assert_eq!(b.to_vec(), vec![1.0, -2.0, 3.0]);
Source

pub fn negative(&self) -> Array

Unary negative (same as neg).

§Examples
let a = Array::from_vec(vec![1.0, -2.0, 3.0], Shape::new(vec![3]));
let b = a.negative();
assert_eq!(b.to_vec(), vec![-1.0, 2.0, -3.0]);
Source

pub fn invert(&self) -> Array

Inverse (1/x) with safe handling of zeros.

Returns infinity for zero values instead of panicking.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 4.0, 0.5], Shape::new(vec![4]));
let b = a.invert();
assert_eq!(b.to_vec(), vec![1.0, 0.5, 0.25, 2.0]);
Source

pub fn degrees(&self) -> Array

Convert angles from radians to degrees (alias).

§Examples
let a = Array::from_vec(vec![0.0, std::f32::consts::PI, std::f32::consts::PI * 2.0], Shape::new(vec![3]));
let b = a.degrees();
// Result: [0.0, 180.0, 360.0]
Source

pub fn radians(&self) -> Array

Convert angles from degrees to radians (alias).

§Examples
let a = Array::from_vec(vec![0.0, 180.0, 360.0], Shape::new(vec![3]));
let b = a.radians();
// Result: [0.0, π, 2π]
Source

pub fn spacing(&self) -> Array

Return the spacing to the next representable float.

For simplicity, returns a constant small value.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.spacing();
// Returns small epsilon values
Source

pub fn copy(&self) -> Array

Return a copy of the array (alias for clone).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.copy();
assert_eq!(b.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn ln(&self) -> Array

Return element-wise natural logarithm (alias for log).

§Examples
let a = Array::from_vec(vec![1.0, std::f32::consts::E, std::f32::consts::E * std::f32::consts::E], Shape::new(vec![3]));
let b = a.ln();
// Result: [0.0, 1.0, 2.0]
Source

pub fn clip_min(&self, min: f32) -> Array

Return element-wise maximum with zero.

§Examples
let a = Array::from_vec(vec![-1.0, 0.0, 1.0, 2.0], Shape::new(vec![4]));
let b = a.clip_min(0.0);
assert_eq!(b.to_vec(), vec![0.0, 0.0, 1.0, 2.0]);
Source

pub fn clip_max(&self, max: f32) -> Array

Return element-wise minimum with a maximum bound.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], Shape::new(vec![4]));
let b = a.clip_max(2.5);
assert_eq!(b.to_vec(), vec![1.0, 2.0, 2.5, 2.5]);
Source

pub fn conj(&self) -> Array

Return the conjugate of the array (identity for real numbers).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.conj();
assert_eq!(b.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn conjugate(&self) -> Array

Return the conjugate (alias for conj).

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.conjugate();
assert_eq!(b.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn angle(&self) -> Array

Return the angle of complex numbers (phase). For real numbers, returns 0 for positive, PI for negative.

§Examples
let a = Array::from_vec(vec![1.0, -1.0, 0.0], Shape::new(vec![3]));
let angles = a.angle();
// Positive: 0, Negative: PI, Zero: 0
Source

pub fn real(&self) -> Array

Return the real part of complex numbers. For real arrays, this is the identity function.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let r = a.real();
assert_eq!(r.to_vec(), vec![1.0, 2.0, 3.0]);
Source

pub fn imag(&self) -> Array

Return the imaginary part of complex numbers. For real arrays, returns zeros.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let im = a.imag();
assert_eq!(im.to_vec(), vec![0.0, 0.0, 0.0]);
Source

pub fn bitwise_not(&self) -> Array

Bitwise NOT operation. Inverts all bits in the bit representation of Float32 values.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.bitwise_not();
Source

pub fn rsqrt(&self) -> Array

Return the reciprocal of the square root (1/sqrt(x)).

§Examples
let a = Array::from_vec(vec![1.0, 4.0, 9.0], Shape::new(vec![3]));
let b = a.rsqrt();
assert!((b.to_vec()[0] - 1.0).abs() < 1e-6);
assert!((b.to_vec()[1] - 0.5).abs() < 1e-6);
Source

pub fn modf(&self) -> (Array, Array)

Return the fractional and integer parts of an array element-wise. Returns a tuple of (fractional_part, integer_part).

§Examples
let a = Array::from_vec(vec![1.5, 2.7, -3.2], Shape::new(vec![3]));
let (frac, int) = a.modf();
assert!((frac.to_vec()[0] - 0.5).abs() < 1e-6);
assert!((int.to_vec()[0] - 1.0).abs() < 1e-6);
Source

pub fn ldexp(&self, exp: i32) -> Array

Compute x * 2^exp for each element. Equivalent to ldexp function from C math library.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.ldexp(2);
assert_eq!(b.to_vec(), vec![4.0, 8.0, 12.0]); // multiply by 2^2 = 4
Source

pub fn frexp(&self) -> (Array, Array)

Decompose x into mantissa and exponent: x = m * 2^e. Returns (mantissa, exponent) where mantissa is in [0.5, 1.0).

§Examples
let a = Array::from_vec(vec![4.0, 8.0, 0.5], Shape::new(vec![3]));
let (mantissa, exp) = a.frexp();
// 4.0 = 0.5 * 2^3, 8.0 = 0.5 * 2^4, 0.5 = 0.5 * 2^0
Source

pub fn safe_divide_scalar(&self, divisor: f32) -> Array

Divide arrays element-wise with safe handling of division by zero. Returns 0 when dividing by zero instead of NaN/Inf.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 3.0], Shape::new(vec![3]));
let b = a.safe_divide_scalar(2.0);
assert_eq!(b.to_vec(), vec![0.5, 1.0, 1.5]);
let c = a.safe_divide_scalar(0.0);
assert_eq!(c.to_vec(), vec![0.0, 0.0, 0.0]); // Returns 0 instead of Inf
Source

pub fn i0(&self) -> Array

Compute the modified Bessel function of the first kind, order 0. Approximation using polynomial expansion.

§Examples
let a = Array::from_vec(vec![0.0, 1.0, 2.0], Shape::new(vec![3]));
let b = a.i0();
assert!((b.to_vec()[0] - 1.0).abs() < 1e-4);  // i0(0) = 1
Source

pub fn lgamma(&self) -> Array

Compute the natural logarithm of the absolute value of the gamma function.

§Examples
let a = Array::from_vec(vec![1.0, 2.0, 5.0], Shape::new(vec![3]));
let b = a.lgamma();
assert!((b.to_vec()[0]).abs() < 1e-6);  // lgamma(1) = 0
assert!((b.to_vec()[1]).abs() < 1e-6);  // lgamma(2) = 0

Trait Implementations§

Source§

impl Clone for Array

Source§

fn clone(&self) -> Array

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Array

Source§

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

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

impl Display for Array

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Array

§

impl !RefUnwindSafe for Array

§

impl Send for Array

§

impl Sync for Array

§

impl Unpin for Array

§

impl !UnwindSafe for Array

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

Source§

fn downcast(&self) -> &T

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
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,