pub trait FpScalar:
Sized
+ Clone
+ Send
+ Sync
+ Debug
+ Display
+ IntoInner<InnerType: RawScalarTrait>
+ PartialEq
+ Arithmetic
+ Abs<Output = Self::RealType>
+ Sqrt
+ PowIntExponent<RawType = Self::InnerType>
+ TrigonometricFunctions
+ HyperbolicFunctions
+ LogarithmFunctions
+ Exp
+ Zero
+ One
+ FpChecks
+ Neg<Output = Self>
+ MulAddRef
+ Reciprocal
+ NeumaierAddable
+ RandomSampleFromF64
+ 'static {
type Kind: Sealed;
type RealType: RealScalar<RealType = Self::RealType>;
}
Expand description
§Core trait for a floating-point scalar number (real or complex)
FpScalar
is a fundamental trait in num-valid
that provides a common interface
for all floating-point scalar types, whether they are real or complex, and regardless
of their underlying numerical kernel (e.g., native f64
or arbitrary-precision rug
types).
This trait serves as a primary bound for generic functions and structures that need to
operate on scalar values capable of floating-point arithmetic. It aggregates a large
number of mathematical function traits (e.g., Sin
, Cos
, Exp
, Sqrt
) and
core properties.
§Key Responsibilities and Associated Types:
-
Scalar Kind (
FpScalar::Kind
): This associated type specifies the fundamental nature of the scalar, which can be eitherscalar_kind::Real
orscalar_kind::Complex
. It is bound by the private, sealed traitscalar_kind::Sealed
. This design enforces mutual exclusion: since a type can only implementFpScalar
once, it can only have oneKind
, making it impossible for a type to be both aRealScalar
and aComplexScalar
simultaneously. -
Real Type Component (
FpScalar::RealType
): Specifies the real number type corresponding to this scalar viaRealType
.- For real scalars (e.g.,
f64
,RealNative64StrictFinite
,RealNative64StrictFiniteInDebug
,RealRugStrictFinite
, etc.),FpScalar::RealType
isSelf
. - For complex scalars (e.g.,
num::Complex<f64>
,ComplexNative64StrictFinite
,ComplexNative64StrictFiniteInDebug
,ComplexRugStrictFinite
, etc.),FpScalar::RealType
is their underlying real component type (e.g.,f64
,RealNative64StrictFinite
,RealNative64StrictFiniteInDebug
,RealRugStrictFinite
, etc.).
Crucially, this
FpScalar::RealType
is constrained to implementRealScalar
, ensuring consistency. - For real scalars (e.g.,
-
Core Floating-Point Properties:
FpScalar
requires implementations of the traitFpChecks
, which provides fundamental floating-point checks:is_finite()
: Checks if the number is finite.is_infinite()
: Checks if the number is positive or negative infinity.is_nan()
: Checks if the number is “Not a Number”.is_normal()
: Checks if the number is a normal (not zero, subnormal, infinite, or NaN).
§Trait Bounds:
FpScalar
itself has several important trait bounds:
Sized + Clone + Debug + Display + PartialEq
: Standard utility traits. Note that scalar types areClone
but not necessarilyCopy
.Zero
+One
: Fromnum_traits
, ensuring the type has additive and multiplicative identities.Neg<Output = Self>
: Ensures the type can be negated.Arithmetic
: A custom aggregate trait innum-valid
that bundles standard arithmetic operator traits (likeAdd
,Sub
,Mul
,Div
).RandomSampleFromF64
: Allows the type to be randomly generated from any distribution that producesf64
, integrating with therand
crate.Send + Sync + 'static
: Makes the scalar types suitable for use in concurrent contexts.- A comprehensive suite of mathematical function traits like
Sin
,Cos
,Exp
,Ln
,Sqrt
, etc.
§Relationship to Other Traits:
RealScalar
: A sub-trait ofFpScalar
for real numbers, defined by the constraintFpScalar<Kind = scalar_kind::Real>
.ComplexScalar
: A sub-trait ofFpScalar
for complex numbers, defined by the constraintFpScalar<Kind = scalar_kind::Complex>
.NumKernel
: Policies likeStrictFinitePolicy
are used to validate the raw values that are wrapped inside types implementingFpScalar
.
§Example: Generic Function
Here is how you can write a generic function that works with any scalar type
implementing FpScalar
.
use num_valid::{FpScalar, functions::{MulAddRef, Sqrt}};
use num::Zero;
// This function works with any FpScalar type.
fn norm_and_sqrt<T: FpScalar>(a: T, b: T) -> T {
let val = a.clone().mul_add_ref(&a, &b.pow(2)); // a*a + b*b
val.sqrt()
}
// Also this function works with any FpScalar type.
fn multiply_if_not_zero<T: FpScalar>(a: T, b: T) -> T {
if !a.is_zero() && !b.is_zero() {
a * b
} else {
T::zero()
}
}
Required Associated Types§
Sourcetype Kind: Sealed
type Kind: Sealed
The kind of scalar this is, e.g., Real
or Complex
.
This is a sealed trait to prevent external implementations.
Sourcetype RealType: RealScalar<RealType = Self::RealType>
type RealType: RealScalar<RealType = Self::RealType>
The real number type corresponding to this scalar.
- For real scalars (e.g.,
f64
),RealType
isSelf
. - For complex scalars (e.g.,
Complex<f64>
),RealType
is their underlying real component type (e.g.,f64
).
This RealType
is guaranteed to implement RealScalar
and belong to the
same numerical kernel as Self
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl FpScalar for f64
impl FpScalar for f64
This trait provides a common interface for floating-point scalar types within
the num-valid
ecosystem.
Source§impl FpScalar for Complex<f64>
Implements the FpScalar
trait for num::Complex<f64>
.
impl FpScalar for Complex<f64>
Implements the FpScalar
trait for num::Complex<f64>
.
This trait provides a common interface for floating-point scalar types within
the num-valid
ecosystem.