strided_view/
lib.rs

1//! Device-agnostic strided view types and metadata operations.
2//!
3//! This crate is a Rust port of Julia's [StridedViews.jl](https://github.com/Jutho/StridedViews.jl),
4//! providing strided multidimensional array view types with zero-copy metadata transformations.
5//!
6//! # Core Types
7//!
8//! - [`StridedView`] / [`StridedViewMut`]: Dynamic-rank strided views over existing data
9//! - [`StridedArray`]: Owned strided multidimensional array
10//! - [`ElementOp`] trait and implementations ([`Identity`], [`Conj`], [`Transpose`], [`Adjoint`]):
11//!   Type-level element operations applied lazily on access
12//!
13//! # Metadata Transformations
14//!
15//! These operate only on dims/strides/offset and never access the underlying data:
16//! - `permute`: Reorder dimensions
17//! - `transpose_2d`, `adjoint_2d`: 2D matrix transformations
18//! - `conj`: Compose conjugation operation
19//! - `broadcast`: Expand size-1 dimensions
20
21pub mod auxiliary;
22mod element_op;
23pub mod view;
24
25// ============================================================================
26// Element operations
27// ============================================================================
28pub use element_op::{
29    Adjoint, ComposableElementOp, Compose, Conj, ElementOp, ElementOpApply, Identity, Transpose,
30};
31
32// ============================================================================
33// View-based types
34// ============================================================================
35pub use view::{col_major_strides, row_major_strides, StridedArray, StridedView, StridedViewMut};
36
37// ============================================================================
38// Error types
39// ============================================================================
40
41/// Errors that can occur during strided array operations.
42#[derive(Debug, thiserror::Error)]
43pub enum StridedError {
44    /// Array ranks do not match.
45    #[error("rank mismatch: {0} vs {1}")]
46    RankMismatch(usize, usize),
47
48    /// Array shapes are incompatible for the operation.
49    #[error("shape mismatch: {0:?} vs {1:?}")]
50    ShapeMismatch(Vec<usize>, Vec<usize>),
51
52    /// Invalid axis index for the given array rank.
53    #[error("invalid axis {axis} for rank {rank}")]
54    InvalidAxis { axis: usize, rank: usize },
55
56    /// Stride array length doesn't match dimensions.
57    #[error("stride and dims length mismatch")]
58    StrideLengthMismatch,
59
60    /// Integer overflow while computing array offset.
61    #[error("offset overflow while computing pointer")]
62    OffsetOverflow,
63
64    /// Failed to convert a scalar value for scaling operation.
65    #[error("failed to convert scalar for scaling")]
66    ScalarConversion,
67
68    /// Matrix is not square when a square matrix was required.
69    #[error("non-square matrix: rows={rows}, cols={cols}")]
70    NonSquare { rows: usize, cols: usize },
71}
72
73/// Result type for strided array operations.
74pub type Result<T> = std::result::Result<T, StridedError>;