Skip to main content

Abs

Trait Abs 

Source
pub trait Abs: Sized {
    type Output: Sized;
    type Error: Error;

    // Required methods
    fn try_abs(self) -> Result<Self::Output, Self::Error>;
    fn abs(self) -> Self::Output;
    fn try_abs_squared(self) -> Result<Self::Output, Self::Error>;
    fn abs_squared(self) -> Self::Output;
}
Expand description

This trait provides the interface for the function used to compute the absolute value (also known as modulus or magnitude) of a number, which can be real or complex.

The absolute value of a real number x is |x|, which is x if x is non-negative, and -x if x is negative. The absolute value (or modulus) of a complex number z = a + b*i is sqrt(a^2 + b^2). In both cases, the result is always a non-negative real number.

This trait provides four methods:

  • try_abs: A fallible version that performs validation on the input (and potentially the output) and returns a Result. This is the preferred method when robust error handling is required.
  • abs: A convenient infallible version that panics if try_abs would return an error in debug builds, or directly computes the value in release builds. Use this when the input is known to be valid or when panicking on error is acceptable.
  • try_abs_squared: A fallible version that computes the squared absolute value (avoiding the square root). For complex numbers, this is a^2 + b^2.
  • abs_squared: A convenient infallible version of try_abs_squared.

§Associated types

  • Output: The type of the result of the absolute value operation. This is constrained by RealScalar to ensure it’s a real number type.
  • Error : The error type that can be returned by the try_abs method.

Required Associated Types§

Source

type Output: Sized

The output type of the absolute value function.

This is always a real number type, even when the input Self is a complex number. It must implement the RealScalar trait.

Source

type Error: Error

The error type that can be returned by the try_abs method.

This type must implement std::error::Error. Specific implementations will use types like AbsRealErrors or AbsComplexErrors to provide detailed information about the failure.

Required Methods§

Source

fn try_abs(self) -> Result<Self::Output, Self::Error>

Attempts to compute the absolute value of self.

Source

fn abs(self) -> Self::Output

Returns the absolute value of self.

Source

fn try_abs_squared(self) -> Result<Self::Output, Self::Error>

Attempts to compute the squared absolute value of self.

For real numbers, this computes x^2. For complex numbers z = a + b*i, this computes a^2 + b^2 (equivalent to |z|^2).

This method is useful when you need the squared magnitude without the expense of computing the square root, such as for distance comparisons or vector norms.

Source

fn abs_squared(self) -> Self::Output

Returns the squared absolute value of self.

For real numbers, this computes x^2. For complex numbers z = a + b*i, this computes a^2 + b^2.

§Behavior
  • In debug builds (#[cfg(debug_assertions)]): This method calls try_abs_squared() and unwraps the result. It will panic if try_abs_squared returns an error.
  • In release builds (#[cfg(not(debug_assertions))]): This method directly computes the squared value for performance, bypassing the validations.
§Panics

This method will panic in debug builds if try_abs_squared() would return an Err.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Abs for f32

Source§

impl Abs for f64

Source§

impl<T> Abs for Complex<T>
where Self: RawComplexTrait<RawReal = T>, T: NativeReal + RealScalar,

Implementors§