Skip to main content

EagerLinalgOps

Trait EagerLinalgOps 

Source
pub trait EagerLinalgOps {
Show 31 methods // Required methods fn svd(&mut self, a: &EagerTensor) -> Result<Svd<EagerTensor, EagerTensor>>; fn svd_with( &mut self, a: &EagerTensor, options: &SvdOptions, ) -> Result<Svd<EagerTensor, EagerTensor>>; fn svdvals(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn svdvals_with( &mut self, a: &EagerTensor, options: &SvdvalsOptions, ) -> Result<EagerTensor>; fn qr(&mut self, a: &EagerTensor) -> Result<Qr<EagerTensor>>; fn qr_with( &mut self, a: &EagerTensor, options: &QrOptions, ) -> Result<Qr<EagerTensor>>; fn householder_qr( &mut self, a: &EagerTensor, ) -> Result<HouseholderQr<EagerTensor>>; fn rank_revealing_qr( &mut self, a: &EagerTensor, ) -> Result<RankRevealingQrResult<EagerTensor, EagerTensor>>; fn rank_revealing_qr_with( &mut self, a: &EagerTensor, options: &RankRevealingQrOptions, ) -> Result<RankRevealingQrResult<EagerTensor, EagerTensor>>; fn lu(&mut self, a: &EagerTensor) -> Result<Lu<EagerTensor>>; fn full_piv_lu( &mut self, a: &EagerTensor, ) -> Result<FullPivLu<EagerTensor, EagerTensor>>; fn full_piv_lu_solve( &mut self, a: &EagerTensor, b: &EagerTensor, ) -> Result<EagerTensor>; fn solve(&mut self, a: &EagerTensor, b: &EagerTensor) -> Result<EagerTensor>; fn lstsq(&mut self, a: &EagerTensor, b: &EagerTensor) -> Result<EagerTensor>; fn triangular_solve( &mut self, a: &EagerTensor, b: &EagerTensor, ) -> Result<EagerTensor>; fn triangular_solve_with( &mut self, a: &EagerTensor, b: &EagerTensor, options: &TriangularSolveOptions, ) -> Result<EagerTensor>; fn cholesky(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn cholesky_with( &mut self, a: &EagerTensor, options: &CholeskyOptions, ) -> Result<EagerTensor>; fn eig(&mut self, a: &EagerTensor) -> Result<Eig<EagerTensor>>; fn eigvals(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn eigh( &mut self, a: &EagerTensor, ) -> Result<Eigh<EagerTensor, EagerTensor>>; fn eigh_with( &mut self, a: &EagerTensor, options: &EighOptions, ) -> Result<Eigh<EagerTensor, EagerTensor>>; fn eigvalsh(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn eigvalsh_with( &mut self, a: &EagerTensor, options: &EigvalshOptions, ) -> Result<EagerTensor>; fn slogdet( &mut self, a: &EagerTensor, ) -> Result<Slogdet<EagerTensor, EagerTensor>>; fn det(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn inv(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn pinv(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn pinv_with( &mut self, a: &EagerTensor, options: &PinvOptions, ) -> Result<EagerTensor>; fn norm(&mut self, a: &EagerTensor) -> Result<EagerTensor>; fn norm_with( &mut self, a: &EagerTensor, options: &NormOptions, ) -> Result<EagerTensor>;
}
Expand description

Linear algebra on eager tensors, with the session as receiver. Eager operations execute immediately and record the tape for reverse-mode differentiation.

Generated by api_ops! from the linalg registry.

Required Methods§

Source

fn svd(&mut self, a: &EagerTensor) -> Result<Svd<EagerTensor, EagerTensor>>

Singular value decomposition a = u diag(s) vh; SvdShape::Full returns the full-matrices factors.

Uses the default SvdOptions; same as svd_with with &SvdOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • SvdShape::Full combined with a non-default gauge, derivative_eps or driver returns Unsupported: the full-matrices route has no gauge or derivative regularization yet
  • SvdShape::Full has no derivative: differentiating through it is unsupported
  • lanes (CpuEngine::with_lanes): SvdShape::Thin, SvdShape::Full and the gauge are available
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, SvdOptions, SvdShape};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 2.0, 0.0])?)?;
    let r = s.svd(&a)?;
    let w = s.svd_with(&a, &SvdOptions::default().shape(SvdShape::Full))?;
    Ok::<_, tenferro_ad::Error>((r.s, w.u))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert_eq!(rt.shape(), &[2]);
let s = rt.as_slice::<f64>()?;
assert!((s[0] - 2.0).abs() < 1e-12 && (s[1] - 1.0).abs() < 1e-12);
// `SvdShape::Full` returns the full-matrices `u`.
assert_eq!(wt.shape(), &[3, 3]);
Source

fn svd_with( &mut self, a: &EagerTensor, options: &SvdOptions, ) -> Result<Svd<EagerTensor, EagerTensor>>

Singular value decomposition a = u diag(s) vh; SvdShape::Full returns the full-matrices factors.

With explicit SvdOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • SvdShape::Full combined with a non-default gauge, derivative_eps or driver returns Unsupported: the full-matrices route has no gauge or derivative regularization yet
  • SvdShape::Full has no derivative: differentiating through it is unsupported
  • lanes (CpuEngine::with_lanes): SvdShape::Thin, SvdShape::Full and the gauge are available
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, SvdOptions, SvdShape};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 2.0, 0.0])?)?;
    let r = s.svd(&a)?;
    let w = s.svd_with(&a, &SvdOptions::default().shape(SvdShape::Full))?;
    Ok::<_, tenferro_ad::Error>((r.s, w.u))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert_eq!(rt.shape(), &[2]);
let s = rt.as_slice::<f64>()?;
assert!((s[0] - 2.0).abs() < 1e-12 && (s[1] - 1.0).abs() < 1e-12);
// `SvdShape::Full` returns the full-matrices `u`.
assert_eq!(wt.shape(), &[3, 3]);
Source

fn svdvals(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Singular values only, in descending order.

Uses the default SvdvalsOptions; same as svdvals_with with &SvdvalsOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • driver is a hint for providers that can choose a driver; the current CPU provider ignores it
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, SvdvalsOptions, SvdDriver};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.svdvals(&a)?;
    let w = s.svdvals_with(&a, &SvdvalsOptions::default().driver(SvdDriver::QrBased))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert_eq!(rt.shape(), &[2]);
let s = rt.as_slice::<f64>()?;
assert!(s[0] > s[1] && s[1] > 0.0);
// The CPU provider ignores the driver hint.
assert_eq!(wt.as_slice::<f64>()?, s);
Source

fn svdvals_with( &mut self, a: &EagerTensor, options: &SvdvalsOptions, ) -> Result<EagerTensor>

Singular values only, in descending order.

With explicit SvdvalsOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • driver is a hint for providers that can choose a driver; the current CPU provider ignores it
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, SvdvalsOptions, SvdDriver};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.svdvals(&a)?;
    let w = s.svdvals_with(&a, &SvdvalsOptions::default().driver(SvdDriver::QrBased))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert_eq!(rt.shape(), &[2]);
let s = rt.as_slice::<f64>()?;
assert!(s[0] > s[1] && s[1] > 0.0);
// The CPU provider ignores the driver hint.
assert_eq!(wt.as_slice::<f64>()?, s);
Source

fn qr(&mut self, a: &EagerTensor) -> Result<Qr<EagerTensor>>

Thin QR factorization a = q r.

Uses the default QrOptions; same as qr_with with &QrOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): only the default QrOptions run in a lane; a non-default gauge returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, QrOptions, QrGauge};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.qr(&a)?;
    let w = s.qr_with(&a, &QrOptions::default().gauge(QrGauge::PositiveDiagonal))?;
    Ok::<_, tenferro_ad::Error>((r.r, w.r))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert_eq!(rt.shape(), &[2, 2]);
// `PositiveDiagonal` makes the diagonal of `r` positive.
let r = wt.as_slice::<f64>()?;
assert!(r[0] > 0.0 && r[3] > 0.0);
Source

fn qr_with( &mut self, a: &EagerTensor, options: &QrOptions, ) -> Result<Qr<EagerTensor>>

Thin QR factorization a = q r.

With explicit QrOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): only the default QrOptions run in a lane; a non-default gauge returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, QrOptions, QrGauge};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.qr(&a)?;
    let w = s.qr_with(&a, &QrOptions::default().gauge(QrGauge::PositiveDiagonal))?;
    Ok::<_, tenferro_ad::Error>((r.r, w.r))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert_eq!(rt.shape(), &[2, 2]);
// `PositiveDiagonal` makes the diagonal of `r` positive.
let r = wt.as_slice::<f64>()?;
assert!(r[0] > 0.0 && r[3] > 0.0);
Source

fn householder_qr( &mut self, a: &EagerTensor, ) -> Result<HouseholderQr<EagerTensor>>

Compact Householder QR: an opaque factorization state from which r and q_columns are extracted.

This operation has no options.

§Layout

The compact factorization takes a compact operand: an owned tensor is read in place, any other operand (a view or a typed tensor) is first made compact by the backend (same placement and dtype). Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, QrOptions};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.householder_qr(&a)?;
    Ok::<_, tenferro_ad::Error>(r.r(QrOptions::default(), s)?)
})?;
let rt = r.value()?;
assert_eq!(rt.shape(), &[2, 2]);
Source

fn rank_revealing_qr( &mut self, a: &EagerTensor, ) -> Result<RankRevealingQrResult<EagerTensor, EagerTensor>>

Column-pivoted QR with a numerical rank: q, r, column_permutation and rank.

Uses the default RankRevealingQrOptions; same as rank_revealing_qr_with with &RankRevealingQrOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, RankRevealingQrOptions};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.rank_revealing_qr(&a)?;
    let w = s.rank_revealing_qr_with(&a, &RankRevealingQrOptions::default().rtol(1.0e-10))?;
    Ok::<_, tenferro_ad::Error>((r.rank, w.rank))
})?;
let rt = r.value()?;
let wt = w.value()?;
// A full-rank matrix has numerical rank 2.
assert_eq!(rt.as_slice::<i64>()?, &[2]);
assert_eq!(wt.as_slice::<i64>()?, &[2]);
Source

fn rank_revealing_qr_with( &mut self, a: &EagerTensor, options: &RankRevealingQrOptions, ) -> Result<RankRevealingQrResult<EagerTensor, EagerTensor>>

Column-pivoted QR with a numerical rank: q, r, column_permutation and rank.

With explicit RankRevealingQrOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, RankRevealingQrOptions};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.rank_revealing_qr(&a)?;
    let w = s.rank_revealing_qr_with(&a, &RankRevealingQrOptions::default().rtol(1.0e-10))?;
    Ok::<_, tenferro_ad::Error>((r.rank, w.rank))
})?;
let rt = r.value()?;
let wt = w.value()?;
// A full-rank matrix has numerical rank 2.
assert_eq!(rt.as_slice::<i64>()?, &[2]);
assert_eq!(wt.as_slice::<i64>()?, &[2]);
Source

fn lu(&mut self, a: &EagerTensor) -> Result<Lu<EagerTensor>>

Partial-pivot LU a = p l u; p is a permutation matrix and parity is the real sign of the permutation.

This operation has no options.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.lu(&a)?;
    Ok::<_, tenferro_ad::Error>(r.l)
})?;
let rt = r.value()?;
assert_eq!(rt.shape(), &[2, 2]);
// `l` is unit lower triangular.
assert_eq!(rt.as_slice::<f64>()?[0], 1.0);
Source

fn full_piv_lu( &mut self, a: &EagerTensor, ) -> Result<FullPivLu<EagerTensor, EagerTensor>>

Complete-pivot LU a = p l u q; p and q are permutation matrices and parity is the real sign of the permutations.

This operation has no options.

§Layout

A rank-2 matrix with non-negative strides is read in place; a negative stride or a batched input (rank >= 3) is gathered once into a compact buffer of the same placement and dtype. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
  • typed Complex32/Complex64 input: returns Unsupported; the borrowed route returns parity in the input dtype while the owned route and the typed result type have it real (#2552 Q21); erased inputs keep the dtype their route returns
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.full_piv_lu(&a)?;
    Ok::<_, tenferro_ad::Error>(r.u)
})?;
let rt = r.value()?;
assert_eq!(rt.shape(), &[2, 2]);
Source

fn full_piv_lu_solve( &mut self, a: &EagerTensor, b: &EagerTensor, ) -> Result<EagerTensor>

Solves a x = b through the complete-pivot LU of a; a rank-deficient system is rejected.

This operation has no options.

§Layout

a and b that are not owned compact tensors (views, typed tensors) are first made compact (same placement and dtype); owned tensors are read in place. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(Singular)
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?)?;
    let r = s.full_piv_lu_solve(&a, &b)?;
    Ok::<_, tenferro_ad::Error>(r)
})?;
let rt = r.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 1]);
// [[4, 2], [2, 3]] x = [1, 2] has the solution [-1/8, 3/4].
let x = rt.as_slice::<f64>()?;
assert!(close(x[0], -0.125) && close(x[1], 0.75));
Source

fn solve(&mut self, a: &EagerTensor, b: &EagerTensor) -> Result<EagerTensor>

Solves a x = b with a partial-pivot LU of a; solve_into overwrites a destination.

This operation has no options.

§Layout

A rank-2 a with a rank-1 or rank-2 non-empty b is read in place (b is gathered straight into the right-hand side); any other shape or layout is gathered once into compact buffers of the same placement and dtype. solve_into writes the solution into dest directly when dest is a column-major matrix of the shape of b; otherwise it computes the solution and copies it in. Operands and dest must be on the same device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(Singular)
§Omissions
  • solve_into accepts only Dest::Overwrite; Accumulate and Scaled return Unsupported before dest is touched
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?)?;
    let r = s.solve(&a, &b)?;
    Ok::<_, tenferro_ad::Error>(r)
})?;
let rt = r.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 1]);
// [[4, 2], [2, 3]] x = [1, 2] has the solution [-1/8, 3/4].
let x = rt.as_slice::<f64>()?;
assert!(close(x[0], -0.125) && close(x[1], 0.75));
Source

fn lstsq(&mut self, a: &EagerTensor, b: &EagerTensor) -> Result<EagerTensor>

Least-squares solution of a x = b for a tall or square, full-column-rank a (thin QR, then a triangular solve).

This operation has no options.

§Layout

a is read like qr reads it (rank-2 with non-negative strides in place, otherwise gathered once into a compact buffer of the same placement and dtype); b is read in place by the contraction. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • an underdetermined (wide, rows < cols) system returns a Validation error
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 1.0, 0.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![3, 1], vec![1.0_f64, 2.0, 3.0])?)?;
    let r = s.lstsq(&a, &b)?;
    Ok::<_, tenferro_ad::Error>(r)
})?;
let rt = r.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 1]);
// The columns of `a` are the first two unit vectors, so x = [1, 2].
let x = rt.as_slice::<f64>()?;
assert!(close(x[0], 1.0) && close(x[1], 2.0));
Source

fn triangular_solve( &mut self, a: &EagerTensor, b: &EagerTensor, ) -> Result<EagerTensor>

Solves a triangular system with a (or its transpose) on the chosen side; only the selected triangle of a is read.

Uses the default TriangularSolveOptions; same as triangular_solve_with with &TriangularSolveOptions::default().

§Layout

A rank-2 a with non-negative strides is read in place and a rank-2 b is gathered straight into the right-hand side; any other shape or layout is gathered once into compact buffers of the same placement and dtype. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(Singular)
§Omissions
  • MatOp::Adjoint on a complex matrix returns Unsupported; for a real matrix it equals MatOp::Transpose
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, TriangularSolveOptions, MatOp};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![2, 1], vec![2.0_f64, 5.0])?)?;
    let r = s.triangular_solve(&a, &b)?;
    let w = s.triangular_solve_with(&a, &b, &TriangularSolveOptions::default().op(MatOp::Transpose))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
// The lower triangle of `a` is [[2, 0], [1, 3]]: solving `l x = b` gives [1, 4/3].
let x = rt.as_slice::<f64>()?;
assert!(close(x[0], 1.0) && close(x[1], 4.0 / 3.0));
// With `MatOp::Transpose` the system is `l^T x = b`: x = [1/6, 5/3].
let y = wt.as_slice::<f64>()?;
assert!(close(y[0], 1.0 / 6.0) && close(y[1], 5.0 / 3.0));
Source

fn triangular_solve_with( &mut self, a: &EagerTensor, b: &EagerTensor, options: &TriangularSolveOptions, ) -> Result<EagerTensor>

Solves a triangular system with a (or its transpose) on the chosen side; only the selected triangle of a is read.

With explicit TriangularSolveOptions.

§Layout

A rank-2 a with non-negative strides is read in place and a rank-2 b is gathered straight into the right-hand side; any other shape or layout is gathered once into compact buffers of the same placement and dtype. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(Singular)
§Omissions
  • MatOp::Adjoint on a complex matrix returns Unsupported; for a real matrix it equals MatOp::Transpose
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, TriangularSolveOptions, MatOp};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![2, 1], vec![2.0_f64, 5.0])?)?;
    let r = s.triangular_solve(&a, &b)?;
    let w = s.triangular_solve_with(&a, &b, &TriangularSolveOptions::default().op(MatOp::Transpose))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
// The lower triangle of `a` is [[2, 0], [1, 3]]: solving `l x = b` gives [1, 4/3].
let x = rt.as_slice::<f64>()?;
assert!(close(x[0], 1.0) && close(x[1], 4.0 / 3.0));
// With `MatOp::Transpose` the system is `l^T x = b`: x = [1/6, 5/3].
let y = wt.as_slice::<f64>()?;
assert!(close(y[0], 1.0 / 6.0) && close(y[1], 5.0 / 3.0));
Source

fn cholesky(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Lower Cholesky factor l of a Hermitian positive-definite matrix, a = l l^H.

Uses the default CholeskyOptions; same as cholesky_with with &CholeskyOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • Uplo::Upper returns Unsupported: only the lower factor is computed
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, CholeskyOptions, Uplo};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.cholesky(&a)?;
    let w = s.cholesky_with(&a, &CholeskyOptions::default().uplo(Uplo::Lower))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 2]);
// l = [[2, 0], [1, sqrt(2)]].
let l = rt.as_slice::<f64>()?;
assert!(close(l[0], 2.0) && close(l[1], 1.0) && close(l[3], 2.0_f64.sqrt()));
assert_eq!(wt.as_slice::<f64>()?, l);
Source

fn cholesky_with( &mut self, a: &EagerTensor, options: &CholeskyOptions, ) -> Result<EagerTensor>

Lower Cholesky factor l of a Hermitian positive-definite matrix, a = l l^H.

With explicit CholeskyOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • Uplo::Upper returns Unsupported: only the lower factor is computed
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, CholeskyOptions, Uplo};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.cholesky(&a)?;
    let w = s.cholesky_with(&a, &CholeskyOptions::default().uplo(Uplo::Lower))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 2]);
// l = [[2, 0], [1, sqrt(2)]].
let l = rt.as_slice::<f64>()?;
assert!(close(l[0], 2.0) && close(l[1], 1.0) && close(l[3], 2.0_f64.sqrt()));
assert_eq!(wt.as_slice::<f64>()?, l);
Source

fn eig(&mut self, a: &EagerTensor) -> Result<Eig<EagerTensor>>

General eigendecomposition a v = v diag(values); eigenvectors have unit 2-norm.

This operation has no options.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
  • NumericalFailure(NonFinite)
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor, Complex64};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.eig(&a)?;
    Ok::<_, tenferro_ad::Error>(r.values)
})?;
let rt = r.value()?;
assert_eq!(rt.shape(), &[2]);
Source

fn eigvals(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Eigenvalues of a general matrix, as complex numbers.

This operation has no options.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
  • NumericalFailure(NonFinite)
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor, Complex64};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.eigvals(&a)?;
    Ok::<_, tenferro_ad::Error>(r)
})?;
let rt = r.value()?;
assert_eq!(rt.shape(), &[2]);
Source

fn eigh(&mut self, a: &EagerTensor) -> Result<Eigh<EagerTensor, EagerTensor>>

Eigendecomposition of a Hermitian matrix (the lower triangle is read); values are real and ascending.

Uses the default EighOptions; same as eigh_with with &EighOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • driver is a hint for providers that can choose a driver; the current CPU provider ignores it
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, EighOptions, EighGauge};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.eigh(&a)?;
    let w = s.eigh_with(&a, &EighOptions::default().gauge(EighGauge::CanonicalPivot))?;
    Ok::<_, tenferro_ad::Error>((r.values, w.values))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2]);
// The eigenvalues of [[4, 2], [2, 3]] are (7 -+ sqrt(17)) / 2, ascending.
let v = rt.as_slice::<f64>()?;
assert!(close(v[0], (7.0 - 17.0_f64.sqrt()) / 2.0) && close(v[1], (7.0 + 17.0_f64.sqrt()) / 2.0));
assert_eq!(wt.shape(), &[2]);
Source

fn eigh_with( &mut self, a: &EagerTensor, options: &EighOptions, ) -> Result<Eigh<EagerTensor, EagerTensor>>

Eigendecomposition of a Hermitian matrix (the lower triangle is read); values are real and ascending.

With explicit EighOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • driver is a hint for providers that can choose a driver; the current CPU provider ignores it
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, EighOptions, EighGauge};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.eigh(&a)?;
    let w = s.eigh_with(&a, &EighOptions::default().gauge(EighGauge::CanonicalPivot))?;
    Ok::<_, tenferro_ad::Error>((r.values, w.values))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2]);
// The eigenvalues of [[4, 2], [2, 3]] are (7 -+ sqrt(17)) / 2, ascending.
let v = rt.as_slice::<f64>()?;
assert!(close(v[0], (7.0 - 17.0_f64.sqrt()) / 2.0) && close(v[1], (7.0 + 17.0_f64.sqrt()) / 2.0));
assert_eq!(wt.shape(), &[2]);
Source

fn eigvalsh(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Eigenvalues of a Hermitian matrix (the lower triangle is read), real and ascending.

Uses the default EigvalshOptions; same as eigvalsh_with with &EigvalshOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • driver is a hint for providers that can choose a driver; the current CPU provider ignores it
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, EigvalshOptions, EighDriver};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.eigvalsh(&a)?;
    let w = s.eigvalsh_with(&a, &EigvalshOptions::default().driver(EighDriver::Jacobi))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2]);
let v = rt.as_slice::<f64>()?;
assert!(close(v[0], (7.0 - 17.0_f64.sqrt()) / 2.0) && close(v[1], (7.0 + 17.0_f64.sqrt()) / 2.0));
// The CPU provider ignores the driver hint.
assert_eq!(wt.as_slice::<f64>()?, v);
Source

fn eigvalsh_with( &mut self, a: &EagerTensor, options: &EigvalshOptions, ) -> Result<EagerTensor>

Eigenvalues of a Hermitian matrix (the lower triangle is read), real and ascending.

With explicit EigvalshOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred. In a lane (CpuEngine::with_lanes), a negative-stride input is gathered once into the lane’s pool; zero steady-state allocation is promised only for non-negative strides. A batched operand is read in place in a lane.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • driver is a hint for providers that can choose a driver; the current CPU provider ignores it
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, EigvalshOptions, EighDriver};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.eigvalsh(&a)?;
    let w = s.eigvalsh_with(&a, &EigvalshOptions::default().driver(EighDriver::Jacobi))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2]);
let v = rt.as_slice::<f64>()?;
assert!(close(v[0], (7.0 - 17.0_f64.sqrt()) / 2.0) && close(v[1], (7.0 + 17.0_f64.sqrt()) / 2.0));
// The CPU provider ignores the driver hint.
assert_eq!(wt.as_slice::<f64>()?, v);
Source

fn slogdet( &mut self, a: &EagerTensor, ) -> Result<Slogdet<EagerTensor, EagerTensor>>

Sign and natural log of the absolute determinant of a square matrix.

This operation has no options.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.slogdet(&a)?;
    Ok::<_, tenferro_ad::Error>(r.log_abs_det)
})?;
let rt = r.value()?;
assert!(rt.shape().is_empty());
// det = 4 * 3 - 2 * 2 = 8.
assert!((rt.as_slice::<f64>()?[0] - 8.0_f64.ln()).abs() < 1e-12);
Source

fn det(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Determinant of a square matrix, computed as sign * exp(log_abs_det).

This operation has no options.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the computation. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.det(&a)?;
    Ok::<_, tenferro_ad::Error>(r)
})?;
let rt = r.value()?;
assert!(rt.shape().is_empty());
assert!((rt.as_slice::<f64>()?[0] - 8.0).abs() < 1e-10);
Source

fn inv(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Inverse of a square matrix (a solve against the identity).

This operation has no options.

§Layout

A rank-2 a is read in place by the solve (the identity right-hand side is allocated compact); any other shape or layout is gathered once into a compact buffer of the same placement and dtype. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(Singular)
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let r = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.inv(&a)?;
    Ok::<_, tenferro_ad::Error>(r)
})?;
let rt = r.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 2]);
// inv([[4, 2], [2, 3]]) = [[3, -2], [-2, 4]] / 8.
let m = rt.as_slice::<f64>()?;
assert!(close(m[0], 0.375) && close(m[1], -0.25) && close(m[3], 0.5));
Source

fn pinv(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Moore-Penrose pseudoinverse through the SVD; singular values below rtol * max(s) are dropped.

Uses the default PinvOptions; same as pinv_with with &PinvOptions::default().

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the SVD. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, PinvOptions};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.pinv(&a)?;
    let w = s.pinv_with(&a, &PinvOptions::default().rtol(Some(1.0e-10)))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 2]);
// The matrix is invertible, so the pseudoinverse is the inverse.
let m = rt.as_slice::<f64>()?;
assert!(close(m[0], 0.375) && close(m[1], -0.25) && close(m[3], 0.5));
assert!(close(wt.as_slice::<f64>()?[0], 0.375));
Source

fn pinv_with( &mut self, a: &EagerTensor, options: &PinvOptions, ) -> Result<EagerTensor>

Moore-Penrose pseudoinverse through the SVD; singular values below rtol * max(s) are dropped.

With explicit PinvOptions.

§Layout

A rank-2 matrix with non-negative strides (compact, transposed or any strided view) is read in place; a negative stride, or a batched input (rank >= 3), is gathered once into a compact buffer of the same placement and dtype before the SVD. Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • NumericalFailure(NonConvergence)
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, PinvOptions};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.pinv(&a)?;
    let w = s.pinv_with(&a, &PinvOptions::default().rtol(Some(1.0e-10)))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
let close = |x: f64, y: f64| (x - y).abs() < 1e-9;
assert_eq!(rt.shape(), &[2, 2]);
// The matrix is invertible, so the pseudoinverse is the inverse.
let m = rt.as_slice::<f64>()?;
assert!(close(m[0], 0.375) && close(m[1], -0.25) && close(m[3], 0.5));
assert!(close(wt.as_slice::<f64>()?[0], 0.375));
Source

fn norm(&mut self, a: &EagerTensor) -> Result<EagerTensor>

Vector or matrix norm over the chosen axes, selected by NormOrd; NormOrd::Zero counts entries != 0 (NaN counts as nonzero).

Uses the default NormOptions; same as norm_with with &NormOptions::default().

§Layout

NormOrd::Frobenius over all axes of a strided operand is one fused pass that reads it in place; the other orders read the operand through strided reductions, and a matrix norm over axes that are not the leading two first moves them to the front (a layout change of the same placement and dtype). Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, NormOptions, NormOrd};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.norm(&a)?;
    let w = s.norm_with(&a, &NormOptions::default().ord(NormOrd::Inf))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert!(rt.shape().is_empty());
// Frobenius norm: sqrt(4^2 + 2^2 + 2^2 + 3^2).
assert!((rt.as_slice::<f64>()?[0] - 33.0_f64.sqrt()).abs() < 1e-12);
// The infinity norm is the largest absolute row sum.
assert!((wt.as_slice::<f64>()?[0] - 6.0).abs() < 1e-12);
Source

fn norm_with( &mut self, a: &EagerTensor, options: &NormOptions, ) -> Result<EagerTensor>

Vector or matrix norm over the chosen axes, selected by NormOrd; NormOrd::Zero counts entries != 0 (NaN counts as nonzero).

With explicit NormOptions.

§Layout

NormOrd::Frobenius over all axes of a strided operand is one fused pass that reads it in place; the other orders read the operand through strided reductions, and a matrix norm over axes that are not the leading two first moves them to the front (a layout change of the same placement and dtype). Operands stay on their device; nothing is transferred.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): not available; returns Unsupported
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_linalg::{EagerLinalgOps, NormOptions, NormOrd};
use tenferro_tensor::{Tensor};

let runtime = EagerRuntime::new()?;
let (r, w) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?)?;
    let r = s.norm(&a)?;
    let w = s.norm_with(&a, &NormOptions::default().ord(NormOrd::Inf))?;
    Ok::<_, tenferro_ad::Error>((r, w))
})?;
let rt = r.value()?;
let wt = w.value()?;
assert!(rt.shape().is_empty());
// Frobenius norm: sqrt(4^2 + 2^2 + 2^2 + 3^2).
assert!((rt.as_slice::<f64>()?[0] - 33.0_f64.sqrt()).abs() < 1e-12);
// The infinity norm is the largest absolute row sum.
assert!((wt.as_slice::<f64>()?[0] - 6.0).abs() < 1e-12);

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl EagerLinalgOps for EagerSession<'_>

Available on crate feature autodiff only.