Skip to main content

Module raw

Module raw 

Source
Expand description

Raw trait implementations for f32/f64 and their complex counterparts.

§Native Floating-Point Kernels

This module provides a numerical kernel implementation based on Rust’s native floating-point numbers (f32 and f64) and complex numbers (num::Complex<T>). It serves as the standard, high-performance native backend for the num-valid library.

§Purpose and Role

The primary role of this module is to act as a bridge between the abstract traits defined by num-valid (like RealScalar and ComplexScalar) and Rust’s concrete, hardware-accelerated numeric types. It adapts f32, f64, and their complex counterparts so they can be used seamlessly in generic code written against the library’s traits.

§Native floats as RealScalar

By implementing RealScalar for f32 and f64, this module makes Rust’s native floats a “first-class citizen” in the num-valid ecosystem. This implementation provides concrete logic for all the operations required by RealScalar and its super-traits (e.g., FpScalar, Rounding, Sign, Constants). Most of these implementations simply delegate to the highly optimized methods available in Rust’s standard library (e.g., <f32 as Float>::sin and <f64 as Float>::sqrt).

This allows generic functions bounded by T: RealScalar to be instantiated with f32 or f64, leveraging direct CPU floating-point support for maximum performance.

§Native complex floats as ComplexScalar

Similarly, this module implements ComplexScalar for num::Complex<f32> and num::Complex<f64>. This makes the standard complex number type from the num crate compatible with the num-valid abstraction for complex numbers. It implements all required traits, including FpScalar, ComplexScalarGetParts, ComplexScalarSetParts, and ComplexScalarMutateParts, by delegating to the methods of num::Complex.

This enables generic code written against T: ComplexScalar to operate on Complex<f32> and Complex<f64> values, providing a performant backend for complex arithmetic.

§Core Components

  • Native<T>: A marker struct that implements the RawKernel trait. It is used in generic contexts to select this native kernel, specifying T as its RawReal type and num::Complex<T> as its RawComplex type.
  • Trait Implementations: This module is primarily composed of impl blocks that satisfy the contracts of core traits (FpScalar, RealScalar, ComplexScalar, Arithmetic, MulAddRef, etc.) for f32/f64 and their complex counterparts.

§Validation

It is crucial to understand that the base types f64 and Complex<f64> do not inherently enforce properties like finiteness (they can be NaN or Infinity).

The num-valid library introduces validation at the trait and wrapper level, not at the base type level. For instance, RealScalar::try_from_f64 for f64 uses a StrictFinitePolicy to ensure the input is finite before creating an instance. This design separates the raw, high-performance types from the validated, safer abstractions built on top of them.

For scenarios requiring types that are guaranteed to be valid by construction, consider using the validated wrappers from the validated module.

Structs§

Native
Numerical kernel specifier for Rust’s native floating-point type T (i.e., f64 or f32).

Type Aliases§

Native32
Native64