Skip to main content

LinalgOps

Trait LinalgOps 

Source
pub trait LinalgOps: AsView {
Show 32 methods // Required methods fn svd( &self, exec: &mut dyn Engine, ) -> Result<Svd<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>> where Self::Repr: LinalgRepr; fn svd_with( &self, options: &SvdOptions, exec: &mut dyn Engine, ) -> Result<Svd<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>> where Self::Repr: LinalgRepr; fn svdvals( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real> where Self::Repr: LinalgRepr; fn svdvals_with( &self, options: &SvdvalsOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real> where Self::Repr: LinalgRepr; fn qr( &self, exec: &mut dyn Engine, ) -> Result<Qr<<Self::Repr as Repr>::Value>> where Self::Repr: LinalgRepr; fn qr_with( &self, options: &QrOptions, exec: &mut dyn Engine, ) -> Result<Qr<<Self::Repr as Repr>::Value>> where Self::Repr: LinalgRepr; fn householder_qr( &self, exec: &mut dyn Engine, ) -> Result<HouseholderQr<<Self::Repr as Repr>::Value>> where Self::Repr: LinalgRepr; fn rank_revealing_qr( &self, exec: &mut dyn Engine, ) -> Result<RankRevealingQrResult<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Index>> where Self::Repr: LinalgRepr; fn rank_revealing_qr_with( &self, options: &RankRevealingQrOptions, exec: &mut dyn Engine, ) -> Result<RankRevealingQrResult<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Index>> where Self::Repr: LinalgRepr; fn lu( &self, exec: &mut dyn Engine, ) -> Result<Lu<<Self::Repr as Repr>::Value>> where Self::Repr: LinalgRepr; fn full_piv_lu( &self, exec: &mut dyn Engine, ) -> Result<FullPivLu<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>> where Self::Repr: LinalgRepr; fn full_piv_lu_solve( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn solve( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn solve_into( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, dest: Dest<<Self::Repr as Repr>::Write<'_>>, ) -> Result<()> where Self::Repr: LinalgRepr; fn lstsq( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn triangular_solve( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn triangular_solve_with( &self, b: &impl AsView<Repr = Self::Repr>, options: &TriangularSolveOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn cholesky( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn cholesky_with( &self, options: &CholeskyOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn eig( &self, exec: &mut dyn Engine, ) -> Result<Eig<<Self::Repr as LinalgRepr>::Complex>> where Self::Repr: LinalgRepr; fn eigvals( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Complex> where Self::Repr: LinalgRepr; fn eigh( &self, exec: &mut dyn Engine, ) -> Result<Eigh<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>> where Self::Repr: LinalgRepr; fn eigh_with( &self, options: &EighOptions, exec: &mut dyn Engine, ) -> Result<Eigh<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>> where Self::Repr: LinalgRepr; fn eigvalsh( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real> where Self::Repr: LinalgRepr; fn eigvalsh_with( &self, options: &EigvalshOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real> where Self::Repr: LinalgRepr; fn slogdet( &self, exec: &mut dyn Engine, ) -> Result<Slogdet<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>> where Self::Repr: LinalgRepr; fn det(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn inv(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn pinv(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn pinv_with( &self, options: &PinvOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value> where Self::Repr: LinalgRepr; fn norm( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real> where Self::Repr: LinalgRepr; fn norm_with( &self, options: &NormOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real> where Self::Repr: LinalgRepr;
}
Expand description

Linear algebra on anything that implements AsView: owned and borrowed tensors, typed tensors and strided views. Results are Tensors for erased inputs and typed tensors for typed inputs. Every operation takes the options last-but-one and the engine last.

Generated by api_ops! from the linalg registry.

Required Methods§

Source

fn svd( &self, exec: &mut dyn Engine, ) -> Result<Svd<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, SvdOptions, SvdShape};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 2.0, 0.0])?;
let r = a.svd(&mut engine)?;
let w = a.svd_with(&SvdOptions::default().shape(SvdShape::Full), &mut engine)?;
let rt = &r.s;
let wt = &w.u;
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( &self, options: &SvdOptions, exec: &mut dyn Engine, ) -> Result<Svd<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, SvdOptions, SvdShape};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 2.0, 0.0])?;
let r = a.svd(&mut engine)?;
let w = a.svd_with(&SvdOptions::default().shape(SvdShape::Full), &mut engine)?;
let rt = &r.s;
let wt = &w.u;
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( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, SvdvalsOptions, SvdDriver};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.svdvals(&mut engine)?;
let w = a.svdvals_with(&SvdvalsOptions::default().driver(SvdDriver::QrBased), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, options: &SvdvalsOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, SvdvalsOptions, SvdDriver};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.svdvals(&mut engine)?;
let w = a.svdvals_with(&SvdvalsOptions::default().driver(SvdDriver::QrBased), &mut engine)?;
let rt = &r;
let wt = &w;
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(&self, exec: &mut dyn Engine) -> Result<Qr<<Self::Repr as Repr>::Value>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, QrOptions, QrGauge};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.qr(&mut engine)?;
let w = a.qr_with(&QrOptions::default().gauge(QrGauge::PositiveDiagonal), &mut engine)?;
let rt = &r.r;
let wt = &w.r;
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( &self, options: &QrOptions, exec: &mut dyn Engine, ) -> Result<Qr<<Self::Repr as Repr>::Value>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, QrOptions, QrGauge};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.qr(&mut engine)?;
let w = a.qr_with(&QrOptions::default().gauge(QrGauge::PositiveDiagonal), &mut engine)?;
let rt = &r.r;
let wt = &w.r;
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( &self, exec: &mut dyn Engine, ) -> Result<HouseholderQr<<Self::Repr as Repr>::Value>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, QrOptions};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.householder_qr(&mut engine)?;
let rt = r.r(QrOptions::default(), &mut engine)?;
assert_eq!(rt.shape(), &[2, 2]);
Source

fn rank_revealing_qr( &self, exec: &mut dyn Engine, ) -> Result<RankRevealingQrResult<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Index>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, RankRevealingQrOptions};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.rank_revealing_qr(&mut engine)?;
let w = a.rank_revealing_qr_with(&RankRevealingQrOptions::default().rtol(1.0e-10), &mut engine)?;
let rt = &r.rank;
let wt = &w.rank;
// 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( &self, options: &RankRevealingQrOptions, exec: &mut dyn Engine, ) -> Result<RankRevealingQrResult<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Index>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, RankRevealingQrOptions};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.rank_revealing_qr(&mut engine)?;
let w = a.rank_revealing_qr_with(&RankRevealingQrOptions::default().rtol(1.0e-10), &mut engine)?;
let rt = &r.rank;
let wt = &w.rank;
// 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(&self, exec: &mut dyn Engine) -> Result<Lu<<Self::Repr as Repr>::Value>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.lu(&mut engine)?;
let rt = &r.l;
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( &self, exec: &mut dyn Engine, ) -> Result<FullPivLu<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.full_piv_lu(&mut engine)?;
let rt = &r.u;
assert_eq!(rt.shape(), &[2, 2]);
Source

fn full_piv_lu_solve( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let b = Tensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
let r = a.full_piv_lu_solve(&b, &mut engine)?;
let rt = &r;
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( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor, Dest};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let b = Tensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
let r = a.solve(&b, &mut engine)?;
let rt = &r;
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));

// `solve_into` overwrites the destination, whatever it held before.
let mut out = Tensor::from_vec_col_major(vec![2, 1], vec![f64::NAN; 2])?;
a.solve_into(&b, &mut engine, Dest::overwrite(&mut out))?;
assert_eq!(out.as_slice::<f64>()?, rt.as_slice::<f64>()?);
Source

fn solve_into( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, dest: Dest<<Self::Repr as Repr>::Write<'_>>, ) -> Result<()>
where Self::Repr: LinalgRepr,

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

Writes into dest instead of returning a new tensor. Accepted destination modes: Overwrite; any other mode returns an Unsupported error before any data is read or written.

§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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor, Dest};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let b = Tensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
let r = a.solve(&b, &mut engine)?;
let rt = &r;
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));

// `solve_into` overwrites the destination, whatever it held before.
let mut out = Tensor::from_vec_col_major(vec![2, 1], vec![f64::NAN; 2])?;
a.solve_into(&b, &mut engine, Dest::overwrite(&mut out))?;
assert_eq!(out.as_slice::<f64>()?, rt.as_slice::<f64>()?);
Source

fn lstsq( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 1.0, 0.0])?;
let b = Tensor::from_vec_col_major(vec![3, 1], vec![1.0_f64, 2.0, 3.0])?;
let r = a.lstsq(&b, &mut engine)?;
let rt = &r;
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( &self, b: &impl AsView<Repr = Self::Repr>, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, TriangularSolveOptions, MatOp};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0])?;
let b = Tensor::from_vec_col_major(vec![2, 1], vec![2.0_f64, 5.0])?;
let r = a.triangular_solve(&b, &mut engine)?;
let w = a.triangular_solve_with(&b, &TriangularSolveOptions::default().op(MatOp::Transpose), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, b: &impl AsView<Repr = Self::Repr>, options: &TriangularSolveOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, TriangularSolveOptions, MatOp};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0])?;
let b = Tensor::from_vec_col_major(vec![2, 1], vec![2.0_f64, 5.0])?;
let r = a.triangular_solve(&b, &mut engine)?;
let w = a.triangular_solve_with(&b, &TriangularSolveOptions::default().op(MatOp::Transpose), &mut engine)?;
let rt = &r;
let wt = &w;
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(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, CholeskyOptions, Uplo};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.cholesky(&mut engine)?;
let w = a.cholesky_with(&CholeskyOptions::default().uplo(Uplo::Lower), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, options: &CholeskyOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, CholeskyOptions, Uplo};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.cholesky(&mut engine)?;
let w = a.cholesky_with(&CholeskyOptions::default().uplo(Uplo::Lower), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, exec: &mut dyn Engine, ) -> Result<Eig<<Self::Repr as LinalgRepr>::Complex>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor, Complex64};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eig(&mut engine)?;
let rt = &r.values;
assert_eq!(rt.shape(), &[2]);
Source

fn eigvals( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Complex>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor, Complex64};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigvals(&mut engine)?;
let rt = &r;
assert_eq!(rt.shape(), &[2]);
Source

fn eigh( &self, exec: &mut dyn Engine, ) -> Result<Eigh<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, EighOptions, EighGauge};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigh(&mut engine)?;
let w = a.eigh_with(&EighOptions::default().gauge(EighGauge::CanonicalPivot), &mut engine)?;
let rt = &r.values;
let wt = &w.values;
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( &self, options: &EighOptions, exec: &mut dyn Engine, ) -> Result<Eigh<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, EighOptions, EighGauge};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigh(&mut engine)?;
let w = a.eigh_with(&EighOptions::default().gauge(EighGauge::CanonicalPivot), &mut engine)?;
let rt = &r.values;
let wt = &w.values;
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( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, EigvalshOptions, EighDriver};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigvalsh(&mut engine)?;
let w = a.eigvalsh_with(&EigvalshOptions::default().driver(EighDriver::Jacobi), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, options: &EigvalshOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, EigvalshOptions, EighDriver};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigvalsh(&mut engine)?;
let w = a.eigvalsh_with(&EigvalshOptions::default().driver(EighDriver::Jacobi), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, exec: &mut dyn Engine, ) -> Result<Slogdet<<Self::Repr as Repr>::Value, <Self::Repr as LinalgRepr>::Real>>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.slogdet(&mut engine)?;
let rt = &r.log_abs_det;
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(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.det(&mut engine)?;
let rt = &r;
assert!(rt.shape().is_empty());
assert!((rt.as_slice::<f64>()?[0] - 8.0).abs() < 1e-10);
Source

fn inv(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.inv(&mut engine)?;
let rt = &r;
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(&self, exec: &mut dyn Engine) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, PinvOptions};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.pinv(&mut engine)?;
let w = a.pinv_with(&PinvOptions::default().rtol(Some(1.0e-10)), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, options: &PinvOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as Repr>::Value>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, PinvOptions};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.pinv(&mut engine)?;
let w = a.pinv_with(&PinvOptions::default().rtol(Some(1.0e-10)), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, NormOptions, NormOrd};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.norm(&mut engine)?;
let w = a.norm_with(&NormOptions::default().ord(NormOrd::Inf), &mut engine)?;
let rt = &r;
let wt = &w;
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( &self, options: &NormOptions, exec: &mut dyn Engine, ) -> Result<<Self::Repr as LinalgRepr>::Real>
where Self::Repr: LinalgRepr,

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_cpu::CpuEngine;
use tenferro_linalg::{LinalgOps, NormOptions, NormOrd};
use tenferro_tensor::{Tensor};

let mut engine = CpuEngine::serial();
let a = Tensor::from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.norm(&mut engine)?;
let w = a.norm_with(&NormOptions::default().ord(NormOrd::Inf), &mut engine)?;
let rt = &r;
let wt = &w;
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 not dyn compatible.

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

Implementors§

Source§

impl<X: AsView + ?Sized> LinalgOps for X