pub fn norm_on_session(
session: &mut Session<'_>,
tensor: &IdxTensor,
) -> Result<f64, IdxTensorError>Expand description
Compute the Euclidean norm of a dense host tensor on a caller-held explicit session.
The square root of norm_squared_on_session, exactly as the compatibility route defines
IdxTensor::norm as the square root of IdxTensor::norm_squared.
§Errors
Returns IdxTensorError::NaNInput when an element or the accumulated result is NaN, and
IdxTensorError::Operation for the representation and dtype refusals the shared admission applies (a
tracked value, a device-resident operand, a diagonal, structured or compact payload, a deferred storage
error, or a non-floating dtype) and for a materialization, session-copy or host-read failure. f32 and
c32 operands are served by this route, promoted into the same f64 accumulation the compatibility
route uses.
Both carry norm_squared_on_session as their operation name, because the computation is delegated - the
same shape as slice 5’s delegation and as the compatibility route’s own norm delegating to
norm_squared.
§Examples
use tensor4all_core::index::{DefaultIndex as Index, DynId};
use tensor4all_core::{norm_on_session, IdxTensor};
use tensor4all_tensorbackend::CpuExecutionContext;
use tensor4all_tensorbackend::CpuBackend;
let context = CpuExecutionContext::from_backend(CpuBackend::with_threads(1)?);
let i = Index::new_dyn(2);
let tensor: IdxTensor = IdxTensor::from_dense(vec![i.clone()], vec![3.0_f64, 4.0])?;
let norm = context.with_concrete_session(|session| norm_on_session(session, &tensor))??;
assert!((norm - 5.0).abs() < 1e-12);