Skip to main content

TensorBase

Trait TensorBase 

Source
pub trait TensorBase:
    Clone
    + Send
    + Sync
    + Debug {
    // Required methods
    fn shape(&self) -> &[usize];
    fn dtype(&self) -> DType;
    fn device(&self) -> Device;
    fn to_dense(&self) -> DenseTensor;
    fn to_sparse(&self) -> Option<SparseTensor>;

    // Provided methods
    fn ndim(&self) -> usize { ... }
    fn numel(&self) -> usize { ... }
    fn is_scalar(&self) -> bool { ... }
    fn is_vector(&self) -> bool { ... }
    fn is_matrix(&self) -> bool { ... }
}
Expand description

Tensor 基础 trait:所有张量必须实现的核心操作

提供形状、数据类型、设备等基本信息查询

Required Methods§

Source

fn shape(&self) -> &[usize]

获取张量的形状(各维度大小)

Source

fn dtype(&self) -> DType

获取数据类型

Source

fn device(&self) -> Device

获取设备类型

Source

fn to_dense(&self) -> DenseTensor

转换为密集张量

Source

fn to_sparse(&self) -> Option<SparseTensor>

转换为稀疏张量(如果适用)

Provided Methods§

Source

fn ndim(&self) -> usize

获取张量的维度数(rank)

Source

fn numel(&self) -> usize

获取总元素数量

Source

fn is_scalar(&self) -> bool

检查是否为标量(0 维张量)

Source

fn is_vector(&self) -> bool

检查是否为向量(1 维张量)

Source

fn is_matrix(&self) -> bool

检查是否为矩阵(2 维张量)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl TensorBase for SparseTensor

Available on crate feature tensor only.
Source§

impl TensorBase for DenseTensor

Available on crate feature tensor only.