Skip to main content

TracedLinalgOps

Trait TracedLinalgOps 

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

Linear algebra on traced tensors: each call records graph nodes and returns traced results; execution happens when the graph is prepared and run. Shape and dtype problems that are known at record time are reported by the call; the rest surface when the graph is compiled or run.

Generated by api_ops! from the linalg registry.

Required Methods§

Source

fn svd(&self) -> Result<Svd<TracedTensor, TracedTensor>>

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::{TracedLinalgOps, SvdOptions, SvdShape};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.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()?;
let w = a.svd_with(&SvdOptions::default().shape(SvdShape::Full))?;
let rt = r.s.clone();
let wt = w.u.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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, ) -> Result<Svd<TracedTensor, TracedTensor>>

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::{TracedLinalgOps, SvdOptions, SvdShape};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.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()?;
let w = a.svd_with(&SvdOptions::default().shape(SvdShape::Full))?;
let rt = r.s.clone();
let wt = w.u.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, SvdvalsOptions, SvdDriver};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.svdvals()?;
let w = a.svdvals_with(&SvdvalsOptions::default().driver(SvdDriver::QrBased))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, SvdvalsOptions, SvdDriver};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.svdvals()?;
let w = a.svdvals_with(&SvdvalsOptions::default().driver(SvdDriver::QrBased))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<Qr<TracedTensor>>

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::{TracedLinalgOps, QrOptions, QrGauge};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.qr()?;
let w = a.qr_with(&QrOptions::default().gauge(QrGauge::PositiveDiagonal))?;
let rt = r.r.clone();
let wt = w.r.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<Qr<TracedTensor>>

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::{TracedLinalgOps, QrOptions, QrGauge};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.qr()?;
let w = a.qr_with(&QrOptions::default().gauge(QrGauge::PositiveDiagonal))?;
let rt = r.r.clone();
let wt = w.r.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<HouseholderQr<TracedTensor>>

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::{TracedLinalgOps, QrOptions};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.householder_qr()?;
let rt = r.r(QrOptions::default())?;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
assert_eq!(rt.shape(), &[2, 2]);
Source

fn rank_revealing_qr( &self, ) -> Result<RankRevealingQrResult<TracedTensor, TracedTensor>>

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::{TracedLinalgOps, RankRevealingQrOptions};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.rank_revealing_qr()?;
let w = a.rank_revealing_qr_with(&RankRevealingQrOptions::default().rtol(1.0e-10))?;
let rt = r.rank.clone();
let wt = w.rank.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
// 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, ) -> Result<RankRevealingQrResult<TracedTensor, TracedTensor>>

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::{TracedLinalgOps, RankRevealingQrOptions};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.rank_revealing_qr()?;
let w = a.rank_revealing_qr_with(&RankRevealingQrOptions::default().rtol(1.0e-10))?;
let rt = r.rank.clone();
let wt = w.rank.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
// 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) -> Result<Lu<TracedTensor>>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.lu()?;
let rt = r.l.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
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) -> Result<FullPivLu<TracedTensor, TracedTensor>>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.full_piv_lu()?;
let rt = r.u.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
assert_eq!(rt.shape(), &[2, 2]);
Source

fn full_piv_lu_solve(&self, b: &TracedTensor) -> Result<TracedTensor>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let b = trace.from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
let r = a.full_piv_lu_solve(&b)?;
let rt = r;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
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: &TracedTensor) -> Result<TracedTensor>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let b = trace.from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
let r = a.solve(&b)?;
let rt = r;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
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(&self, b: &TracedTensor) -> Result<TracedTensor>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![3, 2], vec![1.0_f64, 0.0, 0.0, 0.0, 1.0, 0.0])?;
let b = trace.from_vec_col_major(vec![3, 1], vec![1.0_f64, 2.0, 3.0])?;
let r = a.lstsq(&b)?;
let rt = r;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
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: &TracedTensor) -> Result<TracedTensor>

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::{TracedLinalgOps, TriangularSolveOptions, MatOp};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0])?;
let b = trace.from_vec_col_major(vec![2, 1], vec![2.0_f64, 5.0])?;
let r = a.triangular_solve(&b)?;
let w = a.triangular_solve_with(&b, &TriangularSolveOptions::default().op(MatOp::Transpose))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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: &TracedTensor, options: &TriangularSolveOptions, ) -> Result<TracedTensor>

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::{TracedLinalgOps, TriangularSolveOptions, MatOp};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![2.0_f64, 1.0, 0.0, 3.0])?;
let b = trace.from_vec_col_major(vec![2, 1], vec![2.0_f64, 5.0])?;
let r = a.triangular_solve(&b)?;
let w = a.triangular_solve_with(&b, &TriangularSolveOptions::default().op(MatOp::Transpose))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, CholeskyOptions, Uplo};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.cholesky()?;
let w = a.cholesky_with(&CholeskyOptions::default().uplo(Uplo::Lower))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, CholeskyOptions, Uplo};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.cholesky()?;
let w = a.cholesky_with(&CholeskyOptions::default().uplo(Uplo::Lower))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<Eig<TracedTensor>>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};
use tenferro_tensor::{Complex64};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eig()?;
let rt = r.values.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
assert_eq!(rt.shape(), &[2]);
Source

fn eigvals(&self) -> Result<TracedTensor>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};
use tenferro_tensor::{Complex64};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigvals()?;
let rt = r;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
assert_eq!(rt.shape(), &[2]);
Source

fn eigh(&self) -> Result<Eigh<TracedTensor, TracedTensor>>

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::{TracedLinalgOps, EighOptions, EighGauge};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigh()?;
let w = a.eigh_with(&EighOptions::default().gauge(EighGauge::CanonicalPivot))?;
let rt = r.values.clone();
let wt = w.values.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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, ) -> Result<Eigh<TracedTensor, TracedTensor>>

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::{TracedLinalgOps, EighOptions, EighGauge};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigh()?;
let w = a.eigh_with(&EighOptions::default().gauge(EighGauge::CanonicalPivot))?;
let rt = r.values.clone();
let wt = w.values.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, EigvalshOptions, EighDriver};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigvalsh()?;
let w = a.eigvalsh_with(&EigvalshOptions::default().driver(EighDriver::Jacobi))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, EigvalshOptions, EighDriver};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.eigvalsh()?;
let w = a.eigvalsh_with(&EigvalshOptions::default().driver(EighDriver::Jacobi))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<Slogdet<TracedTensor, TracedTensor>>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.slogdet()?;
let rt = r.log_abs_det.clone();

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.det()?;
let rt = r;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
assert!(rt.shape().is_empty());
assert!((rt.as_slice::<f64>()?[0] - 8.0).abs() < 1e-10);
Source

fn inv(&self) -> Result<TracedTensor>

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::{TracedLinalgOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.inv()?;
let rt = r;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, PinvOptions};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.pinv()?;
let w = a.pinv_with(&PinvOptions::default().rtol(Some(1.0e-10)))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, PinvOptions};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.pinv()?;
let w = a.pinv_with(&PinvOptions::default().rtol(Some(1.0e-10)))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, NormOptions, NormOrd};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.norm()?;
let w = a.norm_with(&NormOptions::default().ord(NormOrd::Inf))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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) -> Result<TracedTensor>

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::{TracedLinalgOps, NormOptions, NormOrd};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![4.0_f64, 2.0, 2.0, 3.0])?;
let r = a.norm()?;
let w = a.norm_with(&NormOptions::default().ord(NormOrd::Inf))?;
let rt = r;
let wt = w;

// Build the graph, then run it.
let program = GraphCompiler::new().compile_many(&[&rt, &wt])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_linalg::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
let mut outputs = outputs.into_iter();
let rt = outputs.next().ok_or("missing first output")?.into_tensor(&mut engine)?;
let wt = outputs.next().ok_or("missing second output")?.into_tensor(&mut engine)?;
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§