Skip to main content

Tensor

Enum Tensor 

Source
pub enum Tensor {
    F32(TypedTensor<f32>),
    F64(TypedTensor<f64>),
    C32(TypedTensor<Complex<f32>>),
    C64(TypedTensor<Complex<f64>>),
}
Expand description

Dynamic tensor enum over the supported scalar types.

§Examples

use tenferro_tensor::{Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec(vec![2], vec![1.0, 2.0]));
assert_eq!(t.shape(), &[2]);

Variants§

Implementations§

Source§

impl Tensor

Source

pub fn from_vec<T: TensorScalar>(shape: Vec<usize>, data: Vec<T>) -> Self

Create a tensor from a shape and flat data.

This is the Tensor-level equivalent of TypedTensor::<T>::from_vec.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert_eq!(t.shape(), &[2, 3]);
assert_eq!(t.as_slice::<f64>().unwrap(), &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
Source

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

Tensor shape.

§Examples
use tenferro_tensor::{Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec(vec![2], vec![1.0, 2.0]));
assert_eq!(t.shape(), &[2]);
Source

pub fn dtype(&self) -> DType

Tensor dtype tag.

§Examples
use tenferro_tensor::{DType, Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec(vec![], vec![1.0]));
assert_eq!(t.dtype(), DType::F64);
Source

pub fn as_slice<T: TensorScalar>(&self) -> Option<&[T]>

Try to borrow the host data as a typed slice.

Returns None if the tensor dtype does not match T.

§Examples
use tenferro_tensor::{Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec(vec![3], vec![1.0, 2.0, 3.0]));
assert_eq!(t.as_slice::<f64>(), Some([1.0, 2.0, 3.0].as_slice()));
assert_eq!(t.as_slice::<f32>(), None);
Source

pub fn svd(&self, ctx: &mut impl TensorBackend) -> Result<(Self, Self, Self)>

Singular value decomposition: A = U diag(S) Vt.

Returns (U, S, Vt) using the thin/economy SVD.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![3, 2], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
let (u, s, vt) = a.svd(&mut ctx).unwrap();

assert_eq!(u.shape(), &[3, 2]);
assert_eq!(s.shape(), &[2]);
assert_eq!(vt.shape(), &[2, 2]);
Source

pub fn qr(&self, ctx: &mut impl TensorBackend) -> Result<(Self, Self)>

QR decomposition: A = Q R.

Returns (Q, R) using the thin/economy QR decomposition.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![3, 2], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
let (q, r) = a.qr(&mut ctx).unwrap();

assert_eq!(q.shape(), &[3, 2]);
assert_eq!(r.shape(), &[2, 2]);
Source

pub fn lu( &self, ctx: &mut impl TensorBackend, ) -> Result<(Self, Self, Self, Self)>

LU decomposition with partial pivoting: P A = L U.

Returns (P, L, U, parity).

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![0.0_f64, 1.0, 1.0, 0.0]);
let (p, l, u, parity) = a.lu(&mut ctx).unwrap();

assert_eq!(p.shape(), &[2, 2]);
assert_eq!(l.shape(), &[2, 2]);
assert_eq!(u.shape(), &[2, 2]);
assert_eq!(parity.shape(), &[] as &[usize]);
Source

pub fn cholesky(&self, ctx: &mut impl TensorBackend) -> Result<Self>

Cholesky decomposition: A = L L^T or A = L L^H for complex inputs.

Returns the lower-triangular factor L.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![4.0_f64, 1.0, 1.0, 3.0]);
let l = a.cholesky(&mut ctx).unwrap();

assert_eq!(l.shape(), &[2, 2]);
Source

pub fn eigh(&self, ctx: &mut impl TensorBackend) -> Result<(Self, Self)>

Symmetric or Hermitian eigendecomposition: A = V diag(W) V^T.

Returns (eigenvalues, eigenvectors).

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![4.0_f64, 1.0, 1.0, 3.0]);
let (w, v) = a.eigh(&mut ctx).unwrap();

assert_eq!(w.shape(), &[2]);
assert_eq!(v.shape(), &[2, 2]);
Source

pub fn eig(&self, ctx: &mut impl TensorBackend) -> Result<(Self, Self)>

General eigendecomposition.

Returns (eigenvalues, eigenvectors).

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 3.0]);
let (w, v) = a.eig(&mut ctx).unwrap();

assert_eq!(w.shape(), &[2]);
assert_eq!(v.shape(), &[2, 2]);
Source

pub fn solve(&self, b: &Self, ctx: &mut impl TensorBackend) -> Result<Self>

Solve A x = b for x.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![2.0_f64, 1.0, 1.0, 2.0]);
let b = Tensor::from_vec(vec![2, 1], vec![1.0_f64, 0.0]);
let x = a.solve(&b, &mut ctx).unwrap();

assert_eq!(x.shape(), &[2, 1]);
Source

pub fn triangular_solve( &self, b: &Self, left_side: bool, lower: bool, transpose_a: bool, unit_diagonal: bool, ctx: &mut impl TensorBackend, ) -> Result<Self>

Solve a triangular system.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0]);
let b = Tensor::from_vec(vec![2, 1], vec![2.0_f64, 7.0]);
let x = a
    .triangular_solve(&b, true, true, false, false, &mut ctx)
    .unwrap();

assert_eq!(x.shape(), &[2, 1]);
Source

pub fn add(&self, other: &Self, ctx: &mut impl TensorBackend) -> Result<Self>

Elementwise addition.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![3], vec![1.0_f64, 2.0, 3.0]);
let b = Tensor::from_vec(vec![3], vec![4.0_f64, 5.0, 6.0]);
let c = a.add(&b, &mut ctx).unwrap();

assert_eq!(c.as_slice::<f64>().unwrap(), &[5.0, 7.0, 9.0]);
Source

pub fn mul(&self, other: &Self, ctx: &mut impl TensorBackend) -> Result<Self>

Elementwise multiplication.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![3], vec![1.0_f64, 2.0, 3.0]);
let b = Tensor::from_vec(vec![3], vec![4.0_f64, 5.0, 6.0]);
let c = a.mul(&b, &mut ctx).unwrap();

assert_eq!(c.as_slice::<f64>().unwrap(), &[4.0, 10.0, 18.0]);
Source

pub fn neg(&self, ctx: &mut impl TensorBackend) -> Result<Self>

Negation.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![3], vec![1.0_f64, -2.0, 3.0]);
let b = a.neg(&mut ctx).unwrap();

assert_eq!(b.as_slice::<f64>().unwrap(), &[-1.0, 2.0, -3.0]);
Source

pub fn transpose( &self, perm: &[usize], ctx: &mut impl TensorBackend, ) -> Result<Self>

Transpose with an explicit permutation.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0]);
let b = a.transpose(&[1, 0], &mut ctx).unwrap();

assert_eq!(b.shape(), &[2, 2]);
assert_eq!(b.as_slice::<f64>().unwrap(), &[1.0, 3.0, 2.0, 4.0]);
Source

pub fn reshape( &self, shape: &[usize], ctx: &mut impl TensorBackend, ) -> Result<Self>

Reshape to a new shape with the same number of elements.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
let b = a.reshape(&[3, 2], &mut ctx).unwrap();

assert_eq!(b.shape(), &[3, 2]);
assert_eq!(b.as_slice::<f64>().unwrap(), &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
Source

pub fn reduce_sum( &self, axes: &[usize], ctx: &mut impl TensorBackend, ) -> Result<Self>

Reduce sum over the specified axes.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
let b = a.reduce_sum(&[1], &mut ctx).unwrap();

assert_eq!(b.shape(), &[2]);
assert_eq!(b.as_slice::<f64>().unwrap(), &[9.0, 12.0]);
Source

pub fn matmul(&self, other: &Self, ctx: &mut impl TensorBackend) -> Result<Self>

Matrix multiplication for rank-2 tensors.

This is a convenience wrapper around dot_general.

§Examples
use tenferro_tensor::{Tensor, TensorBackend, cpu::CpuBackend};

let mut ctx = CpuBackend::new();
let a = Tensor::from_vec(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
let b = Tensor::from_vec(vec![3, 2], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]);
let c = a.matmul(&b, &mut ctx).unwrap();

assert_eq!(c.shape(), &[2, 2]);
assert_eq!(c.as_slice::<f64>().unwrap(), &[22.0, 28.0, 49.0, 64.0]);

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<TypedTensor<Complex<f32>>> for Tensor

Wrap a Complex32 TypedTensor into the corresponding Tensor variant.

§Examples

use num_complex::Complex32;
use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec(
    vec![1],
    vec![Complex32::new(1.0, 2.0)],
);
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[1]);
Source§

fn from(t: TypedTensor<Complex<f32>>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<Complex<f64>>> for Tensor

Wrap a Complex64 TypedTensor into the corresponding Tensor variant.

§Examples

use num_complex::Complex64;
use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec(
    vec![1],
    vec![Complex64::new(1.0, 2.0)],
);
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[1]);
Source§

fn from(t: TypedTensor<Complex<f64>>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<f32>> for Tensor

Wrap an f32 TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec(vec![2], vec![1.0_f32, 2.0]);
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<f64>> for Tensor

Wrap an f64 TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec(vec![2], vec![1.0_f64, 2.0]);
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<f64>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

impl<T> MaybeSend for T

§

impl<T> MaybeSendSync for T

§

impl<T> MaybeSync for T