pub struct Tensor<St, L>{ /* private fields */ }Expand description
Tensor wrapping a TensorData bundle.
§Type Parameters
St- Storage half (DenseStorage<T>orBlockSparseStorage<T>)L- Layout half (DenseLayoutorBlockSparseLayout<S>)
§Examples
use ariadnetor_tensor::DenseTensor;
let a = DenseTensor::<f64>::zeros(vec![2, 2]);
assert_eq!(a.shape(), &[2, 2]);Implementations§
Source§impl<S> Tensor<DenseStorage<S>, DenseLayout>
impl<S> Tensor<DenseStorage<S>, DenseLayout>
Sourcepub fn data_slice(&self) -> &[S]
pub fn data_slice(&self) -> &[S]
Get a reference to the underlying contiguous data buffer.
Sourcepub fn data_slice_mut(&mut self) -> &mut [S]where
S: Clone,
pub fn data_slice_mut(&mut self) -> &mut [S]where
S: Clone,
Get a mutable reference to the underlying data buffer (CoW-aware).
Sourcepub fn reshape(&self, new_shape: Vec<usize>) -> Self
pub fn reshape(&self, new_shape: Vec<usize>) -> Self
Reshape to new_shape (zero-copy). Preserves the layout’s memory
order. The flat data buffer is Arc-shared via
DenseStorage::Clone, so the result aliases the same allocation
as self.
Under non-adjacent axis fusion the logical mapping differs between row-major and column-major; callers fusing such axes must reorder the flat buffer to the appropriate order first.
§Panics
Panics if new_shape.iter().product() != self.len(), via
TensorData::new’s storage.flat_len() == layout.storage_extent()
assert.
Source§impl<S: Scalar> Tensor<DenseStorage<S>, DenseLayout>
impl<S: Scalar> Tensor<DenseStorage<S>, DenseLayout>
Sourcepub fn order(&self) -> MemoryOrder
pub fn order(&self) -> MemoryOrder
Memory order this tensor’s flat data is laid out in.
Sourcepub fn get(&self, indices: impl AsRef<[usize]>) -> S
pub fn get(&self, indices: impl AsRef<[usize]>) -> S
Get element at the given indices.
indices accepts any AsRef<[usize]>, so an array literal can be
passed without a borrow: t.get([i, j]) as well as t.get(&coords).
§Panics
Panics if indices.len() != rank or any index exceeds the
corresponding axis dimension.
Sourcepub fn set(&mut self, indices: impl AsRef<[usize]>, value: S)
pub fn set(&mut self, indices: impl AsRef<[usize]>, value: S)
Set element at the given indices.
indices accepts any AsRef<[usize]>, so an array literal can be
passed without a borrow: t.set([i, j], v) as well as t.set(&coords, v).
§Panics
Panics if indices.len() != rank or any index exceeds the
corresponding axis dimension.
Source§impl<S: Clone> Tensor<DenseStorage<S>, DenseLayout>
impl<S: Clone> Tensor<DenseStorage<S>, DenseLayout>
Source§impl<S> Tensor<DenseStorage<S>, DenseLayout>where
S: Scalar,
impl<S> Tensor<DenseStorage<S>, DenseLayout>where
S: Scalar,
Sourcepub fn normalize(&mut self) -> S::Real
pub fn normalize(&mut self) -> S::Real
Normalize to unit norm (in-place). Returns the original norm.
§Panics
Panics if the tensor has zero norm.
Sourcepub fn normalized(&self) -> (Self, S::Real)
pub fn normalized(&self) -> (Self, S::Real)
Normalize and return a new tensor (out-of-place).
Sourcepub fn conj(&self) -> Self
pub fn conj(&self) -> Self
Element-wise complex conjugate. Symmetric with
[BlockSparseTensor::conj].
Sourcepub fn reordered(&self, to: MemoryOrder) -> Self
pub fn reordered(&self, to: MemoryOrder) -> Self
Return a tensor with flat data reordered to to. When
self.data().order() == to, the underlying buffer is shared via
Arc rather than copied.
This is a workspace-internal escape hatch, not a user entry
point. The public Tensor surface hides memory layout: constructors
take no order, and the linalg / algorithm layers normalize to the
backend’s preferred order internally. The only in-tree callers are
that internal plumbing (and the order-mismatch rejection tests).
End users should never need to choose a MemoryOrder; as an inherent
method on a re-exported type it cannot be hidden from umbrella users,
hence this note.
Sourcepub fn reshape_logical(&self, new_shape: Vec<usize>) -> Self
pub fn reshape_logical(&self, new_shape: Vec<usize>) -> Self
General logical (C-order) reshape to an arbitrary target shape,
preserving the tensor’s memory order. The buffer is routed
through row-major so the logical axis grouping is independent of
the physical layout, then restored to the original order; for a
row-major tensor each step is a zero-copy Arc share, for a
column-major tensor it costs one round-trip transpose.
This is the low-level escape hatch for multi-leg regroupings that
fuse_legs / split_leg cannot express in a single
operation — e.g. fusing two disjoint leg groups at once. Prefer
fuse_legs / split_leg for single-leg fuse / split: they
constrain which axis changes and read as intent. Like
reshape, only the total element count is validated.
§Panics
Panics if new_shape’s total element count differs from the
tensor’s, via reshape.
Sourcepub fn fuse_legs(&self, range: Range<usize>) -> Self
pub fn fuse_legs(&self, range: Range<usize>) -> Self
Fuse a contiguous range of axes into a single leg, grouping them in row-major (C-order) logical order regardless of the tensor’s physical memory order.
The fused leg’s extent is the product of the fused axes’
extents and its logical index runs fastest over the last fused
axis. The result keeps self’s memory order. Use reshape
instead when a raw, order-preserving buffer reinterpretation is
wanted. Inverse of split_leg over the same range. Convenience
over reshape_logical for the single-leg case; for multi-group
regroupings call reshape_logical directly.
§Panics
Panics unless range.start < range.end <= rank.
Sourcepub fn split_leg(&self, axis: usize, into: &[usize]) -> Self
pub fn split_leg(&self, axis: usize, into: &[usize]) -> Self
Split one axis into multiple axes, distributing the extent in row-major (C-order) logical order regardless of the tensor’s physical memory order.
into lists the resulting extents from slowest- to
fastest-varying. The result keeps self’s memory order.
Inverse of fuse_legs for a contiguous range. Convenience over
reshape_logical for the single-leg case; for multi-group
regroupings call reshape_logical directly.
§Panics
Panics unless axis < rank, into is non-empty, and
into.iter().product() == shape[axis].
Source§impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>where
S: Sector,
impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>where
S: Sector,
Sourcepub fn order(&self) -> MemoryOrder
pub fn order(&self) -> MemoryOrder
Memory order this tensor’s flat block data is laid out in.
Mirror of DenseTensor::order — saves block-sparse callers
from reaching through .data().layout().order() for a basic
layout property.
Sourcepub fn num_blocks(&self) -> usize
pub fn num_blocks(&self) -> usize
Number of flux-allowed blocks.
Sourcepub fn block_metas(&self) -> &[BlockMeta]
pub fn block_metas(&self) -> &[BlockMeta]
Block metadata table (one entry per flux-allowed block).
Sourcepub fn block_shape(&self, coord: &BlockCoord) -> Option<Vec<usize>>
pub fn block_shape(&self, coord: &BlockCoord) -> Option<Vec<usize>>
Per-leg block shape for a stored block coordinate.
Returns None if the coord is outside the layout’s enumerated
block set.
Sourcepub fn block_data(&self, coord: &BlockCoord) -> Option<&[T]>
pub fn block_data(&self, coord: &BlockCoord) -> Option<&[T]>
Data slice for a block identified by coordinate.
Returns None if the block is not stored (zero by symmetry).
Sourcepub fn block_data_mut(&mut self, coord: &BlockCoord) -> Option<&mut [T]>where
T: Clone,
pub fn block_data_mut(&mut self, coord: &BlockCoord) -> Option<&mut [T]>where
T: Clone,
Mutable data slice for a block identified by coordinate (CoW-aware).
Source§impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
Source§impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
Source§impl<St, L> Tensor<St, L>
impl<St, L> Tensor<St, L>
Sourcepub fn from_data(data: TensorData<St, L>) -> Self
pub fn from_data(data: TensorData<St, L>) -> Self
Build a tensor from a pre-bundled TensorData.
Sourcepub fn data(&self) -> &TensorData<St, L>
pub fn data(&self) -> &TensorData<St, L>
Internal escape hatch: reference to the joined TensorData
bundle.
Intended for cross-crate kernel-access paths inside ariadnetor-linalg
and ariadnetor-mps; user code should reach for the inherent methods
on DenseTensor / BlockSparseTensor instead.
Sourcepub fn data_mut(&mut self) -> &mut TensorData<St, L>
pub fn data_mut(&mut self) -> &mut TensorData<St, L>
Internal escape hatch: mutable reference to the joined
TensorData bundle.
Same audience as Tensor::data — cross-crate kernel paths
that need to mutate raw storage / layout state.
Source§impl<S: Scalar> Tensor<DenseStorage<S>, DenseLayout>
impl<S: Scalar> Tensor<DenseStorage<S>, DenseLayout>
Sourcepub fn random<R: Rng>(shape: Vec<usize>, rng: &mut R) -> Selfwhere
StandardUniform: Distribution<S>,
pub fn random<R: Rng>(shape: Vec<usize>, rng: &mut R) -> Selfwhere
StandardUniform: Distribution<S>,
Create a Dense tensor filled with values drawn from the standard distribution via the supplied RNG.
Source§impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
Source§impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
Source§impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
impl<T, S> Tensor<BlockSparseStorage<T>, BlockSparseLayout<S>>
Sourcepub fn from_block_fn<F>(indices: Vec<QNIndex<S>>, flux: S, f: F) -> Self
pub fn from_block_fn<F>(indices: Vec<QNIndex<S>>, flux: S, f: F) -> Self
Construct a BlockSparseTensor by populating each flux-allowed
block from a closure receiving the block coordinate and its
dense block shape.
Trait Implementations§
Source§impl<S> Mul<S> for Tensor<DenseStorage<S>, DenseLayout>
impl<S> Mul<S> for Tensor<DenseStorage<S>, DenseLayout>
Source§fn mul(self, rhs: S) -> Self::Output
fn mul(self, rhs: S) -> Self::Output
Scale by rhs, consuming self. Reuses the owned buffer in
place (no extra allocation when the storage is uniquely owned;
a buffer still shared via copy-on-write is cloned first).
Source§type Output = Tensor<DenseStorage<S>, DenseLayout>
type Output = Tensor<DenseStorage<S>, DenseLayout>
* operator.Source§impl<S> Mul<S> for &Tensor<DenseStorage<S>, DenseLayout>
impl<S> Mul<S> for &Tensor<DenseStorage<S>, DenseLayout>
Source§type Output = Tensor<DenseStorage<S>, DenseLayout>
type Output = Tensor<DenseStorage<S>, DenseLayout>
* operator.Source§impl<S> MulAssign<S> for Tensor<DenseStorage<S>, DenseLayout>
impl<S> MulAssign<S> for Tensor<DenseStorage<S>, DenseLayout>
Source§fn mul_assign(&mut self, rhs: S)
fn mul_assign(&mut self, rhs: S)
Scale every element by rhs in place.
Auto Trait Implementations§
impl<St, L> Freeze for Tensor<St, L>
impl<St, L> RefUnwindSafe for Tensor<St, L>where
St: RefUnwindSafe,
L: RefUnwindSafe,
impl<St, L> Send for Tensor<St, L>
impl<St, L> Sync for Tensor<St, L>
impl<St, L> Unpin for Tensor<St, L>
impl<St, L> UnsafeUnpin for Tensor<St, L>where
St: UnsafeUnpin,
L: UnsafeUnpin,
impl<St, L> UnwindSafe for Tensor<St, L>where
St: UnwindSafe,
L: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more