pub trait ComplexScalar:
ComplexScalarMutateParts
+ Conjugate
+ Arg<Output = Self::RealType>
+ for<'a> Mul<&'a Self::RealType, Output = Self>
+ for<'a> MulAssign<&'a Self::RealType>
+ for<'a> Pow<&'a Self::RealType, Error = PowComplexBaseRealExponentErrors<Self::RawComplex>> {
// Required method
fn into_parts(self) -> (Self::RealType, Self::RealType);
// Provided methods
fn scale(self, c: &Self::RealType) -> Self { ... }
fn scale_mut(&mut self, c: &Self::RealType) { ... }
}Expand description
§Trait for complex scalar numbers
ComplexScalar is a specialized trait for complex number types that extends the
core FpScalar functionality with complex-specific operations. It provides a
unified interface for working with complex numbers across different validation
policies and underlying representations.
§Design Philosophy
This trait bridges the gap between raw complex number operations and the validated
complex number types in num-valid. It ensures that complex-specific
operations like conjugation, argument calculation, and real-number scaling are
available in a type-safe, validated context.
§Core Capabilities
The trait provides several key features:
- Component manipulation: Through
ComplexScalarMutateParts, allowing safe modification of real and imaginary parts - Conjugation: Via the
functions::Conjugatetrait for computing complex conjugates - Argument calculation: Through
functions::Argfor computing the phase angle - Real scaling: Multiplication and assignment with real numbers
- Power operations: Raising complex numbers to real exponents
- Convenience methods: Like
scaleandscale_mutfor efficient real-number scaling
§Type Relationships
Types implementing ComplexScalar must also implement FpScalar with
Kind = scalar_kind::Complex, establishing them as complex number types within
the num-valid type system.
§Usage Examples
§Basic Complex Operations
use num_valid::{ComplexNative64StrictFinite, RealNative64StrictFinite, ComplexScalar};
use num::Complex;
use try_create::TryNew;
let z = ComplexNative64StrictFinite::try_new(Complex::new(3.0, 4.0)).unwrap();
let factor = RealNative64StrictFinite::try_new(2.5).unwrap();
// Scale by a real number
let scaled = z.scale(&factor);
// Result: (7.5, 10.0)§In-place Operations
use num_valid::{ComplexNative64StrictFinite, RealNative64StrictFinite, ComplexScalar};
use num::Complex;
use try_create::TryNew;
let mut z = ComplexNative64StrictFinite::try_new(Complex::new(1.0, 2.0)).unwrap();
let factor = RealNative64StrictFinite::try_new(3.0).unwrap();
z.scale_mut(&factor);
// z is now (3.0, 6.0)§See Also
FpScalar: The base trait for all floating-point scalarsRealScalar: The equivalent trait for real numbersComplexScalarMutateParts: For component-wise operationsfunctions: Module containing mathematical functions for complex numbers
Required Methods§
Sourcefn into_parts(self) -> (Self::RealType, Self::RealType)
fn into_parts(self) -> (Self::RealType, Self::RealType)
Consumes the complex number and returns its real and imaginary parts as a tuple.
This method moves ownership of the complex number and extracts its two components, returning them as separate real scalar values. This is useful when you need to work with the components independently and no longer need the original complex value.
§Returns
A tuple (real, imaginary) where:
realis the real part of the complex numberimaginaryis the imaginary part of the complex number
§Examples
use num_valid::{ComplexNative64StrictFinite, ComplexScalar};
use num::Complex;
use try_create::TryNew;
let z = ComplexNative64StrictFinite::try_new(Complex::new(3.0, 4.0)).unwrap();
// Consume z and extract its parts
let (real, imag) = z.into_parts();
assert_eq!(*real.as_ref(), 3.0);
assert_eq!(*imag.as_ref(), 4.0);
// z is no longer available here (moved)§See Also
ComplexScalarGetParts::real_part: Get the real part without consumingComplexScalarGetParts::imag_part: Get the imaginary part without consuming
Provided Methods§
Sourcefn scale(self, c: &Self::RealType) -> Self
fn scale(self, c: &Self::RealType) -> Self
Scale the complex number self by the real coefficient c.
This is equivalent to complex multiplication by a real number, scaling both the real and imaginary parts by the same factor.
§Examples
use num_valid::{ComplexNative64StrictFinite, RealNative64StrictFinite, ComplexScalar};
use num::Complex;
use try_create::TryNew;
let z = ComplexNative64StrictFinite::try_new(Complex::new(3.0, 4.0)).unwrap();
let factor = RealNative64StrictFinite::try_new(2.0).unwrap();
let scaled = z.scale(&factor);
// Result: (6.0, 8.0)Sourcefn scale_mut(&mut self, c: &Self::RealType)
fn scale_mut(&mut self, c: &Self::RealType)
Scale (in-place) the complex number self by the real coefficient c.
This modifies the complex number in place, scaling both components by the real factor.
§Examples
use num_valid::{ComplexNative64StrictFinite, RealNative64StrictFinite, ComplexScalar};
use num::Complex;
use try_create::TryNew;
let mut z = ComplexNative64StrictFinite::try_new(Complex::new(3.0, 4.0)).unwrap();
let factor = RealNative64StrictFinite::try_new(2.0).unwrap();
z.scale_mut(&factor);
// z is now (6.0, 8.0)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 ComplexScalar for Complex<f64>
Implement the ComplexScalar trait for the Complex<f64> type.
impl ComplexScalar for Complex<f64>
Implement the ComplexScalar trait for the Complex<f64> type.