pub struct Tensor { /* private fields */ }Expand description
The uniform tensor value: an n-dimensional array of scalar number cells.
A tensor is row-major (last axis varies fastest) and homogeneous: every cell
shares the dtype number domain. An empty shape
denotes a rank-0 scalar holding a single cell. Tensors are the value backing
the numbers/tensor domain and are constructed through
build_tensor_value rather than parsed from literals.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn new_checked(
cx: &mut Cx,
shape: Vec<usize>,
dtype: Symbol,
data: Vec<Value>,
) -> Result<Self>
pub fn new_checked( cx: &mut Cx, shape: Vec<usize>, dtype: Symbol, data: Vec<Value>, ) -> Result<Self>
Builds a tensor after checking shape, scalar-cell, and promotion invariants against the loaded number-domain registry.
Sourcepub fn new_exact(
shape: Vec<usize>,
dtype: Symbol,
data: Vec<Value>,
) -> Result<Self>
pub fn new_exact( shape: Vec<usize>, dtype: Symbol, data: Vec<Value>, ) -> Result<Self>
Builds a tensor whose cells already exactly match dtype.
Specialized tensor backends use this when converting packed storage into
uniform tensor cells without needing a loaded registry. It is stricter
than Tensor::new_checked: every cell must report exactly dtype.
Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
The tensor shape, outermost axis first. Empty means a rank-0 scalar.
Sourcepub fn flat_offset(shape: &[usize], indices: &[usize]) -> Result<usize>
pub fn flat_offset(shape: &[usize], indices: &[usize]) -> Result<usize>
Computes the row-major flat offset into data for a
multi-dimensional indices coordinate against shape.
Returns an error if the index rank does not match shape or any
component is out of bounds.
§Examples
use sim_lib_numbers_tensor::Tensor;
// Row-major 2x3 tensor: element (1, 2) is at flat offset 5.
assert_eq!(Tensor::flat_offset(&[2, 3], &[1, 2]).unwrap(), 5);
assert_eq!(Tensor::flat_offset(&[2, 3], &[0, 0]).unwrap(), 0);
// Out-of-bounds and rank-mismatched indices are rejected.
assert!(Tensor::flat_offset(&[2, 3], &[2, 0]).is_err());
assert!(Tensor::flat_offset(&[2, 3], &[0]).is_err());Trait Implementations§
Source§impl Citizen for Tensor
impl Citizen for Tensor
Source§fn citizen_symbol() -> Symbol
fn citizen_symbol() -> Symbol
namespace/name class symbol.Source§fn citizen_version() -> u32
fn citizen_version() -> u32
Source§fn citizen_arity() -> usize
fn citizen_arity() -> usize
Source§fn citizen_fields() -> &'static [&'static str]
fn citizen_fields() -> &'static [&'static str]
Source§impl NumberValue for Tensor
impl NumberValue for Tensor
Source§fn number_domain(&self, _cx: &mut Cx) -> Result<Symbol>
fn number_domain(&self, _cx: &mut Cx) -> Result<Symbol>
Source§fn number_literal(&self, _cx: &mut Cx) -> Result<Option<NumberLiteral>, Error>
fn number_literal(&self, _cx: &mut Cx) -> Result<Option<NumberLiteral>, Error>
Ok(None) when not representable.Source§impl Object for Tensor
impl Object for Tensor
Source§fn display(&self, cx: &mut Cx) -> Result<String>
fn display(&self, cx: &mut Cx) -> Result<String>
Source§fn header(&self) -> &ObjectHeader
fn header(&self) -> &ObjectHeader
Source§fn op(&self, _key: &OpKey) -> Option<&dyn Op>
fn op(&self, _key: &OpKey) -> Option<&dyn Op>
key, if any.Source§impl ObjectCompat for Tensor
impl ObjectCompat for Tensor
Source§fn class(&self, cx: &mut Cx) -> Result<ClassRef>
fn class(&self, cx: &mut Cx) -> Result<ClassRef>
Source§fn as_expr(&self, cx: &mut Cx) -> Result<Expr>
fn as_expr(&self, cx: &mut Cx) -> Result<Expr>
Source§fn as_table(&self, cx: &mut Cx) -> Result<Value>
fn as_table(&self, cx: &mut Cx) -> Result<Value>
Source§fn as_number_value(&self) -> Option<&dyn NumberValue>
fn as_number_value(&self) -> Option<&dyn NumberValue>
Source§fn as_object_encoder(&self) -> Option<&dyn ObjectEncode>
fn as_object_encoder(&self) -> Option<&dyn ObjectEncode>
Source§fn as_callable(&self) -> Option<&dyn Callable>
fn as_callable(&self) -> Option<&dyn Callable>
Source§fn as_read_constructor(&self) -> Option<&dyn ReadConstructor>
fn as_read_constructor(&self) -> Option<&dyn ReadConstructor>
Source§fn as_number_domain(&self) -> Option<&(dyn NumberDomain + 'static)>
fn as_number_domain(&self) -> Option<&(dyn NumberDomain + 'static)>
Source§fn as_eval_fabric(&self) -> Option<&dyn EvalFabric>
fn as_eval_fabric(&self) -> Option<&dyn EvalFabric>
Source§fn as_sequence(&self) -> Option<&dyn Sequence>
fn as_sequence(&self) -> Option<&dyn Sequence>
Source§fn as_list(&self) -> Option<&(dyn ListValue + 'static)>
fn as_list(&self) -> Option<&(dyn ListValue + 'static)>
Source§fn as_table_impl(&self) -> Option<&(dyn Table + 'static)>
fn as_table_impl(&self) -> Option<&(dyn Table + 'static)>
Source§fn as_dir(&self) -> Option<&(dyn Dir + 'static)>
fn as_dir(&self) -> Option<&(dyn Dir + 'static)>
Source§impl ObjectEncode for Tensor
impl ObjectEncode for Tensor
Source§fn object_encoding(&self, cx: &mut Cx) -> Result<ObjectEncoding>
fn object_encoding(&self, cx: &mut Cx) -> Result<ObjectEncoding>
ObjectEncoding this object should be rendered as.