pub struct Tensor {
pub shape: Vec<usize>,
pub data: Vec<f64>,
}Expand description
N-dimensional tensor with row-major (C-contiguous) data.
Fields§
§shape: Vec<usize>Tensor shape in row-major order.
data: Vec<f64>Flat row-major data buffer.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn new(shape: &[usize], data: &[f64]) -> MathResult<Self>
pub fn new(shape: &[usize], data: &[f64]) -> MathResult<Self>
Create a tensor from shape + data. Validates numel matches.
Sourcepub fn from_vec(shape: &[usize], data: Vec<f64>) -> MathResult<Self>
pub fn from_vec(shape: &[usize], data: Vec<f64>) -> MathResult<Self>
Create from shape + owned data.
Sourcepub fn arange(start: f64, stop: f64, step: f64) -> MathResult<Self>
pub fn arange(start: f64, stop: f64, step: f64) -> MathResult<Self>
1-D range: [start, stop) with step. Negative steps are supported.
Sourcepub fn randn(shape: &[usize]) -> Self
pub fn randn(shape: &[usize]) -> Self
Pseudo-random normal via xorshift64. Uses persistent thread-local state so successive calls produce different values.
Sourcepub fn randn_seeded(shape: &[usize], seed: u64) -> Self
pub fn randn_seeded(shape: &[usize], seed: u64) -> Self
Pseudo-random normal with explicit seed. Uses a local RNG state so it
does not interfere with the shared thread-local state used by [randn].
Sourcepub fn index_to_flat(&self, coords: &[usize]) -> MathResult<usize>
pub fn index_to_flat(&self, coords: &[usize]) -> MathResult<usize>
Multi-index to flat offset.
Sourcepub fn flat_to_index(&self, flat: usize) -> MathResult<Vec<usize>>
pub fn flat_to_index(&self, flat: usize) -> MathResult<Vec<usize>>
Flat offset to multi-index.
Sourcepub fn get(&self, coords: &[usize]) -> MathResult<f64>
pub fn get(&self, coords: &[usize]) -> MathResult<f64>
Get element at multi-index.
Sourcepub fn set(&mut self, coords: &[usize], val: f64) -> MathResult<()>
pub fn set(&mut self, coords: &[usize], val: f64) -> MathResult<()>
Set element at multi-index.
Sourcepub fn reshape(&self, new_shape: &[usize]) -> MathResult<Self>
pub fn reshape(&self, new_shape: &[usize]) -> MathResult<Self>
Reshape (numel must match).
Sourcepub fn transpose(&self) -> MathResult<Self>
pub fn transpose(&self) -> MathResult<Self>
2-D transpose.
Sourcepub fn permute(&self, axes: &[usize]) -> MathResult<Self>
pub fn permute(&self, axes: &[usize]) -> MathResult<Self>
General axis permutation.
Sourcepub fn squeeze(&self, axis: Option<usize>) -> Self
pub fn squeeze(&self, axis: Option<usize>) -> Self
Remove size-1 dimensions (or a specific axis).
Sourcepub fn unsqueeze(&self, axis: usize) -> MathResult<Self>
pub fn unsqueeze(&self, axis: usize) -> MathResult<Self>
Add a size-1 dimension at axis.
Sourcepub fn broadcast_to(&self, target_shape: &[usize]) -> MathResult<Self>
pub fn broadcast_to(&self, target_shape: &[usize]) -> MathResult<Self>
Expand to a target shape (broadcast semantics, clones data).
Sourcepub fn add(&self, other: &Tensor) -> MathResult<Tensor>
pub fn add(&self, other: &Tensor) -> MathResult<Tensor>
Element-wise add with broadcasting.
Sourcepub fn sub(&self, other: &Tensor) -> MathResult<Tensor>
pub fn sub(&self, other: &Tensor) -> MathResult<Tensor>
Element-wise sub.
Sourcepub fn mul(&self, other: &Tensor) -> MathResult<Tensor>
pub fn mul(&self, other: &Tensor) -> MathResult<Tensor>
Element-wise mul.
Sourcepub fn div(&self, other: &Tensor) -> MathResult<Tensor>
pub fn div(&self, other: &Tensor) -> MathResult<Tensor>
Element-wise div (safe: returns NaN for zero denominators instead of corrupting sign).
Sourcepub fn add_scalar(&self, s: f64) -> Tensor
pub fn add_scalar(&self, s: f64) -> Tensor
Add a scalar to every element.
Sourcepub fn sub_scalar(&self, s: f64) -> Tensor
pub fn sub_scalar(&self, s: f64) -> Tensor
Subtract a scalar from every element.
Sourcepub fn mul_scalar(&self, s: f64) -> Tensor
pub fn mul_scalar(&self, s: f64) -> Tensor
Multiply every element by a scalar.
Sourcepub fn div_scalar(&self, s: f64) -> Tensor
pub fn div_scalar(&self, s: f64) -> Tensor
Divide every element by a scalar (returns NaN for zero denominator).
Sourcepub fn sum_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn sum_axis(&self, axis: usize) -> MathResult<Tensor>
Sum along axis (removes that axis).
Sourcepub fn mean_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn mean_axis(&self, axis: usize) -> MathResult<Tensor>
Mean along axis.
Sourcepub fn max_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn max_axis(&self, axis: usize) -> MathResult<Tensor>
Max along axis.
Sourcepub fn min_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn min_axis(&self, axis: usize) -> MathResult<Tensor>
Min along axis.
Sourcepub fn argmax_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn argmax_axis(&self, axis: usize) -> MathResult<Tensor>
Argmax along axis (returns flat index per slice).
Sourcepub fn argmin_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn argmin_axis(&self, axis: usize) -> MathResult<Tensor>
Argmin along axis.
Sourcepub fn matmul(&self, other: &Tensor) -> MathResult<Tensor>
pub fn matmul(&self, other: &Tensor) -> MathResult<Tensor>
Matrix multiply: 2-D @ 2-D or batched 3-D @ 3-D.
Sourcepub fn layer_norm(&self, eps: f64) -> Tensor
pub fn layer_norm(&self, eps: f64) -> Tensor
Layer normalization (over last dimension).
Sourcepub fn batch_norm(&self, eps: f64) -> MathResult<Tensor>
pub fn batch_norm(&self, eps: f64) -> MathResult<Tensor>
Batch normalization (over first dimension).
For 2-D tensors [N, features], normalizes each feature across the batch.
For 4-D tensors [N, C, H, W], normalizes per channel across N, H, W
(standard image batch norm behavior).
Sourcepub fn rms_norm(&self, eps: f64) -> Tensor
pub fn rms_norm(&self, eps: f64) -> Tensor
RMS normalization: x / sqrt(mean(x²) + eps) along last dim.
Sourcepub fn where_tensor(
condition: &Tensor,
a: &Tensor,
b: &Tensor,
) -> MathResult<Tensor>
pub fn where_tensor( condition: &Tensor, a: &Tensor, b: &Tensor, ) -> MathResult<Tensor>
Element-wise where: condition ? a : b.
Sourcepub fn gather(&self, axis: usize, indices: &Tensor) -> MathResult<Tensor>
pub fn gather(&self, axis: usize, indices: &Tensor) -> MathResult<Tensor>
Gather along axis: selects from self using indices.
indices has same shape as output, values are indices along axis.
§Errors
Returns MathError::OutOfRange if any index is outside [0, axis_size).
Sourcepub fn scatter_add(
&self,
axis: usize,
indices: &Tensor,
src: &Tensor,
) -> MathResult<Tensor>
pub fn scatter_add( &self, axis: usize, indices: &Tensor, src: &Tensor, ) -> MathResult<Tensor>
Scatter add: adds src into a zero tensor at positions given by indices.
§Errors
Returns MathError::OutOfRange if any index is outside [0, axis_size).
Sourcepub fn topk(&self, k: usize, axis: usize) -> MathResult<(Tensor, Tensor)>
pub fn topk(&self, k: usize, axis: usize) -> MathResult<(Tensor, Tensor)>
Top-k along axis: returns (values, indices) tensors.
Sourcepub fn concat(tensors: &[Tensor], axis: usize) -> MathResult<Tensor>
pub fn concat(tensors: &[Tensor], axis: usize) -> MathResult<Tensor>
Concatenate tensors along an axis.
Sourcepub fn split(&self, num_chunks: usize, axis: usize) -> MathResult<Vec<Tensor>>
pub fn split(&self, num_chunks: usize, axis: usize) -> MathResult<Vec<Tensor>>
Split into chunks along axis. Last chunk absorbs any remainder.
Sourcepub fn dot(&self, other: &Tensor) -> MathResult<f64>
pub fn dot(&self, other: &Tensor) -> MathResult<f64>
Dot product (flattened). Errors if the tensors have different sizes.
Sourcepub fn cross(&self, other: &Tensor) -> MathResult<Tensor>
pub fn cross(&self, other: &Tensor) -> MathResult<Tensor>
Cross product (3-vectors only).
Sourcepub fn var_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn var_axis(&self, axis: usize) -> MathResult<Tensor>
Variance along axis.
Sourcepub fn std_axis(&self, axis: usize) -> MathResult<Tensor>
pub fn std_axis(&self, axis: usize) -> MathResult<Tensor>
Standard deviation along axis.
Sourcepub fn sort(&self, axis: usize) -> MathResult<(Tensor, Tensor)>
pub fn sort(&self, axis: usize) -> MathResult<(Tensor, Tensor)>
Sort along axis (ascending), returns (sorted_values, original_indices).
Sourcepub fn unique(&self) -> Vec<f64>
pub fn unique(&self) -> Vec<f64>
Unique elements (sorted, deduplicated). Exact equality only — near-but-not-equal values are kept.
Sourcepub fn count_nonzero(&self) -> usize
pub fn count_nonzero(&self) -> usize
Count non-zero elements.