Skip to main content

TypedTensor

Struct TypedTensor 

Source
pub struct TypedTensor<T> {
    pub buffer: Buffer<T>,
    pub shape: Vec<usize>,
    pub placement: Placement,
}
Expand description

Contiguous column-major typed tensor storage.

§Examples

use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::from_vec(vec![2, 2], vec![1.0, 2.0, 3.0, 4.0]);
assert_eq!(t.shape, vec![2, 2]);

Fields§

§buffer: Buffer<T>§shape: Vec<usize>§placement: Placement

Implementations§

Source§

impl<T: TensorScalar> TypedTensor<T>

Source

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

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

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

For complex inputs, this wrapper expects the backend to return the singular values as TypedTensor<T::Real>. If the backend still returns a complex tensor for S, this method returns Error::DTypeMismatch.

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

let mut ctx = CpuBackend::new();
let a = TypedTensor::<f64>::from_vec(vec![2, 2], vec![1.0, 0.0, 0.0, 2.0]);
let (u, s, vt) = a.svd(&mut ctx).unwrap();

assert_eq!(u.shape, vec![2, 2]);
assert_eq!(s.shape, vec![2]);
assert_eq!(vt.shape, vec![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::{cpu::CpuBackend, TensorBackend, TypedTensor};

let mut ctx = CpuBackend::new();
let a = TypedTensor::<f64>::from_vec(vec![2, 2], vec![1.0, 2.0, 3.0, 4.0]);
let (q, r) = a.qr(&mut ctx).unwrap();

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

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

Cholesky factorization: A = L L^T or A = L L^H.

Returns the lower-triangular factor L.

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

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

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

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

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

Returns (eigenvalues, eigenvectors).

For complex inputs, this wrapper expects the backend to return the eigenvalues as TypedTensor<T::Real>. If the backend still returns a complex tensor for W, this method returns Error::DTypeMismatch.

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

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

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

impl<T: Clone + Zero> TypedTensor<T>

Source

pub fn zeros(shape: Vec<usize>) -> Self

Allocate a zero-filled tensor.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::zeros(vec![2, 3]);
assert_eq!(t.n_elements(), 6);
Source§

impl<T: Clone + One + Zero> TypedTensor<T>

Source

pub fn ones(shape: Vec<usize>) -> Self

Allocate a one-filled tensor.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::ones(vec![2]);
assert_eq!(t.host_data(), &[1.0, 1.0]);
Source§

impl<T: Clone> TypedTensor<T>

Source

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

Create a tensor from a column-major buffer.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::from_vec(vec![2, 2], vec![1.0, 2.0, 3.0, 4.0]);
assert_eq!(t.get(&[1, 0]), &2.0);
Source

pub fn n_elements(&self) -> usize

Number of elements in the tensor.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::from_vec(vec![2, 3], vec![0.0; 6]);
assert_eq!(t.n_elements(), 6);
Source

pub fn host_data(&self) -> &[T]

Borrow the host buffer.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::from_vec(vec![2], vec![1.0, 2.0]);
assert_eq!(t.host_data(), &[1.0, 2.0]);
Source

pub fn as_slice(&self) -> &[T]

View the tensor data as a flat slice.

This is an alias for host_data() for API consistency with Tensor::as_slice.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::from_vec(vec![2], vec![1.0, 2.0]);
assert_eq!(t.as_slice(), &[1.0, 2.0]);
Source

pub fn host_data_mut(&mut self) -> &mut [T]

Mutably borrow the host buffer.

§Examples
use tenferro_tensor::TypedTensor;

let mut t = TypedTensor::<f64>::zeros(vec![2]);
t.host_data_mut()[0] = 3.0;
assert_eq!(t.host_data(), &[3.0, 0.0]);
Source

pub fn linear_offset(&self, indices: &[usize]) -> usize

Compute the linear column-major offset for an index.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::zeros(vec![2, 3]);
assert_eq!(t.linear_offset(&[1, 2]), 5);
Source

pub fn get(&self, indices: &[usize]) -> &T

Borrow a single element by multi-index.

§Examples
use tenferro_tensor::TypedTensor;

let t = TypedTensor::<f64>::from_vec(vec![2], vec![1.0, 2.0]);
assert_eq!(t.get(&[1]), &2.0);
Source

pub fn get_mut(&mut self, indices: &[usize]) -> &mut T

Mutably borrow a single element by multi-index.

§Examples
use tenferro_tensor::TypedTensor;

let mut t = TypedTensor::<f64>::zeros(vec![1]);
*t.get_mut(&[0]) = 7.0;
assert_eq!(t.host_data(), &[7.0]);

Trait Implementations§

Source§

impl<T: Clone> Clone for TypedTensor<T>

Source§

fn clone(&self) -> TypedTensor<T>

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<T: Debug> Debug for TypedTensor<T>

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§

§

impl<T> Freeze for TypedTensor<T>

§

impl<T> RefUnwindSafe for TypedTensor<T>
where T: RefUnwindSafe,

§

impl<T> Send for TypedTensor<T>
where T: Send,

§

impl<T> Sync for TypedTensor<T>
where T: Sync,

§

impl<T> Unpin for TypedTensor<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for TypedTensor<T>

§

impl<T> UnwindSafe for TypedTensor<T>
where T: UnwindSafe,

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