pub struct Tensor { /* private fields */ }Expand description
An immutable, strided tensor whose packed backing storage is shared by views.
Construction accepts at most MAX_RANK axes and MAX_ELEMENTS elements.
Slices and transposes retain the same storage; reshaping a noncontiguous view
materializes row-major bytes. Binary operations require matching types and use
trailing-axis broadcasting. Serialization is {dtype, shape, data}, where
every data entry is an exact decimal scalar string.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn inspect(
&self,
options: TensorInspectionOptions,
) -> Result<TensorInspection, DataError>
pub fn inspect( &self, options: TensorInspectionOptions, ) -> Result<TensorInspection, DataError>
Inspects this tensor without cloning or materializing its backing storage.
Views are visited in logical row-major order through their shape and
strides. The inspection reads no more than options.max_samples, capped
by MAX_INSPECTION_SAMPLES.
Source§impl Tensor
impl Tensor
Sourcepub fn from_scalars(
dtype: DType,
shape: Vec<usize>,
values: Vec<Scalar>,
) -> Result<Self, DataError>
pub fn from_scalars( dtype: DType, shape: Vec<usize>, values: Vec<Scalar>, ) -> Result<Self, DataError>
Creates a contiguous tensor from scalars of dtype.
Sourcepub fn from_le_bytes(
dtype: DType,
shape: Vec<usize>,
bytes: Vec<u8>,
) -> Result<Self, DataError>
pub fn from_le_bytes( dtype: DType, shape: Vec<usize>, bytes: Vec<u8>, ) -> Result<Self, DataError>
Creates a contiguous tensor from packed little-endian scalar bytes.
Sourcepub fn zeros(dtype: DType, shape: Vec<usize>) -> Result<Self, DataError>
pub fn zeros(dtype: DType, shape: Vec<usize>) -> Result<Self, DataError>
Creates a zero-filled contiguous tensor.
Sourcepub fn storage_byte_len(&self) -> usize
pub fn storage_byte_len(&self) -> usize
Returns the physical byte length of the shared backing allocation.
Sourcepub fn get(&self, indices: &[isize]) -> Result<Scalar, DataError>
pub fn get(&self, indices: &[isize]) -> Result<Scalar, DataError>
Reads one scalar using Python-style negative indices.
Sourcepub fn slice(&self, indices: &[AxisIndex]) -> Result<Self, DataError>
pub fn slice(&self, indices: &[AxisIndex]) -> Result<Self, DataError>
Produces a shared-storage slice using Python indexing rules.
Sourcepub fn with_slice(
&self,
indices: &[AxisIndex],
replacement: &Self,
) -> Result<Self, DataError>
pub fn with_slice( &self, indices: &[AxisIndex], replacement: &Self, ) -> Result<Self, DataError>
Replaces a selection in a new contiguous tensor of the original shape.
Replacement values broadcast across the selected shape using trailing axes. A rank-zero replacement fills the selection with a single scalar. Source and replacement may share storage: every replacement value is read from its original immutable allocation, so overlapping assignment is safe. Noncontiguous source views are copied in logical row-major order.
§Errors
Rejects invalid selectors, different element types, or replacement shapes that cannot broadcast into exactly the selection’s shape. On error, source and replacement remain unchanged.
use konjure_sdk::data::{AxisIndex, DType, Scalar, Tensor};
let original = Tensor::zeros(DType::I16, vec![2, 3])?;
let row = Tensor::from_scalars(DType::I16, vec![3],
["1", "2", "3"].into_iter().map(|text| Scalar::parse(DType::I16, text))
.collect::<Result<Vec<_>, _>>()?)?;
let updated = original.with_slice(&[AxisIndex::Index(-1)], &row)?;
assert_eq!(updated.get(&[1, 2])?.to_string(), "3");
assert_eq!(original.get(&[1, 2])?.to_string(), "0");Sourcepub fn with_index_path(
&self,
path: &[Vec<AxisIndex>],
replacement: &Self,
) -> Result<Self, DataError>
pub fn with_index_path( &self, path: &[Vec<AxisIndex>], replacement: &Self, ) -> Result<Self, DataError>
Replaces a selection formed by successive indexing expressions.
Each path entry indexes the preceding view. All entries are composed
before one logical payload copy, including for a noncontiguous source.
An empty path selects the entire tensor. Broadcasting and immutability
follow Self::with_slice.
§Errors
Rejects paths longer than MAX_RANK, invalid selectors, mixed types,
and replacement shapes that cannot broadcast into the final selection.
Sourcepub fn reshape(&self, shape: Vec<usize>) -> Result<Self, DataError>
pub fn reshape(&self, shape: Vec<usize>) -> Result<Self, DataError>
Changes dimensions while preserving logical row-major order.
Sourcepub fn transpose(&self, axes: &[usize]) -> Result<Self, DataError>
pub fn transpose(&self, axes: &[usize]) -> Result<Self, DataError>
Reorders axes; each old axis must occur exactly once.
Sourcepub fn binary(&self, operation: BinaryOp, rhs: &Self) -> Result<Self, DataError>
pub fn binary(&self, operation: BinaryOp, rhs: &Self) -> Result<Self, DataError>
Applies a scalar operation with NumPy trailing-axis broadcasting.
Sourcepub fn scalar_binary(
&self,
operation: BinaryOp,
rhs: Scalar,
) -> Result<Self, DataError>
pub fn scalar_binary( &self, operation: BinaryOp, rhs: Scalar, ) -> Result<Self, DataError>
Applies a scalar operation to every element.
Sourcepub fn sum(&self) -> Result<Scalar, DataError>
pub fn sum(&self) -> Result<Scalar, DataError>
Returns the additive reduction, or zero for an empty tensor.
Sourcepub fn matmul(&self, rhs: &Self) -> Result<Self, DataError>
pub fn matmul(&self, rhs: &Self) -> Result<Self, DataError>
Multiplies rank-two-or-greater tensors, broadcasting leading batch axes.
Sourcepub fn to_scalars(&self) -> Vec<Scalar>
pub fn to_scalars(&self) -> Vec<Scalar>
Materializes logical elements in row-major order.
Sourcepub fn to_le_bytes(&self) -> Vec<u8> ⓘ
pub fn to_le_bytes(&self) -> Vec<u8> ⓘ
Materializes logical elements as packed little-endian scalar bytes.