Skip to main content

Crate tensors

Crate tensors 

Source
Expand description

Compact dense tensor primitives for numerical algorithms.

The crate intentionally starts with a small surface area: row-major owned arrays, cheap strided views, checked shape errors, scalar kernels, and matrix primitives that can later delegate to accelerated backends.

§Examples

Matrix slicing and multiplication:

use tensors::{Array2, matmul};

let a = Array2::from_vec([2, 3], vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])?;
let gram = matmul(a.transpose_view(), a.view())?;

assert_eq!(gram.shape(), [3, 3]);
assert_eq!(gram[(0, 0)], 17.0);

Lazy preprocessing for PCA-style workflows:

use tensors::{Array2, LinearOperator, StandardizedOperator, column_means, column_variances};

let a = Array2::from_vec([3, 2], vec![1.0, 2.0, 3.0, 6.0, 5.0, 10.0])?;
let means = column_means(a.view());
let scales = column_variances(a.view(), &means)?
    .into_iter()
    .map(f64::sqrt)
    .collect::<Vec<_>>();
let standardized = StandardizedOperator::new(a, means, scales);

let mut y = [0.0; 3];
standardized.matvec(&[1.0, -1.0], &mut y)?;
assert_eq!(y.len(), 3);

Randomized SVD on a dense matrix:

use tensors::{Array2, RandomizedSvdOptions, randomized_svd};

let a = Array2::from_vec([3, 2], vec![3.0, 0.0, 4.0, 0.0, 0.0, 0.0])?;
let result = randomized_svd(
    &a,
    RandomizedSvdOptions {
        rank: 1,
        oversampling: 1,
        power_iterations: 1,
        seed: Some(7),
        ..RandomizedSvdOptions::default()
    },
)?;

assert_eq!(result.s.len(), 1);

Structs§

Array2
Owned 2D row-major array.
Array3
Owned 3D row-major array.
ArrayN
Owned dynamic-rank row-major array.
ArrayView2
Immutable 2D array view.
ArrayView3
Immutable 3D array view.
ArrayViewMut2
Mutable 2D array view.
ArrayViewMut3
Mutable 3D array view.
ArrayViewMutN
Mutable dynamic-rank strided view.
ArrayViewN
Immutable dynamic-rank strided view.
CenteredOperator
Lazy column-centered operator.
ColumnScaledOperator
Lazy column-scaled operator representing A * diag(scales).
CsrMatrix
Compressed sparse row matrix.
EighResult
Symmetric eigendecomposition result.
QrResult
Thin QR decomposition result.
RandomizedSvdOptions
Options for randomized SVD.
RowScaledOperator
Lazy row-scaled operator representing diag(scales) * A.
ScratchBuffer
Reusable one-dimensional scratch storage.
StandardizedOperator
Lazy column-standardized operator representing (A - means) * diag(1 / scales).
SvdResult
Singular value decomposition result.
Transpose
Lazy transpose wrapper.
Workspace
Workspace for repeated dense matrix algorithms.

Enums§

Axis3
Axis selector for 3D arrays.
Error
Checked operation failures.

Traits§

Float
Floating point scalar supported by numerical kernels.
LinearOperator
Matrix-like object usable by algorithms without requiring materialization.

Functions§

approx_reconstruction_error
Frobenius norm of A - U diag(S) Vt.
axpy
Compute y += alpha * x.
axpy_f32
Compute y += alpha * x using explicit SIMD for contiguous f32 slices.
axpy_f64
Compute y += alpha * x using explicit SIMD for contiguous f64 slices.
batch_randomized_svd
Run randomized SVD independently over each 2D slice of a 3D tensor.
batch_randomized_svd_parallel
Run randomized SVD independently over each 2D slice using Rayon.
center_columns_inplace
Subtract column means in place.
center_rows_inplace
Subtract row means in place.
centered_columns
Return a centered copy with column means removed.
centered_rows
Return a centered copy with row means removed.
column_means
Mean of each column.
column_variances
Population variance of each column.
dot
Compute a dot product.
dot_f32
Compute a dot product using explicit SIMD for contiguous f32 slices.
dot_f64
Compute a dot product using explicit SIMD for contiguous f64 slices.
eigh_small
Symmetric eigendecomposition for small dense matrices.
explained_variance_ratio
Fraction of squared singular value energy explained by each value.
fold_view
Fold a mode-unfolded matrix back into a 3D tensor with shape.
gemm
Checked matrix multiplication with transpose flags: C = alpha * op(A) * op(B) + beta * C.
gemm_with_workspace
Checked matrix multiplication using caller-provided reusable workspace: C = alpha * op(A) * op(B) + beta * C.
matmul
Return A * B.
norm_l2
Euclidean norm.
norm_l2_f32
Euclidean norm using explicit SIMD for contiguous f32 slices.
norm_l2_f64
Euclidean norm using explicit SIMD for contiguous f64 slices.
pack_block
Copy a rectangular view block into compact row-major storage.
qr
Modified Gram-Schmidt QR with one reorthogonalization pass.
randomized_range_finder
Randomized range finder returning an orthonormal basis Q.
randomized_svd
Pure-Rust randomized SVD entry point.
randomized_svd_with_error
Run randomized SVD on a dense view and return its reconstruction error.
reorthogonalize
Reorthogonalize approximate basis columns.
row_means
Mean of each row.
row_variances
Population variance of each row.
scale_columns_inplace
Multiply each column by the matching scale.
scale_rows_inplace
Multiply each row by the matching scale.
standardize_columns_inplace
Center and divide columns by their standard deviations in place.
standardize_rows_inplace
Center and divide rows by their standard deviations in place.
svd_small
Singular value decomposition for small dense matrices.
thin_qr
Modified Gram-Schmidt thin QR. Returns only Q.
unfold_view
Unfold a 3D tensor view along axis into a row-major matrix.
unpack_block
Copy a compact row-major block into a destination view at (row, col).

Type Aliases§

Result
Crate-wide result type.