Skip to content

Commit 9d9ee9d

Browse files
committed
Add AssociatedReal::real() and ::imag()
1 parent 75f9726 commit 9d9ee9d

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ pub fn into_scalar<T: Scalar>(f: f64) -> T {
8787
pub trait AssociatedReal: Sized {
8888
type Real: RealScalar;
8989
fn inject(Self::Real) -> Self;
90+
/// Returns the real part of `self`.
91+
fn real(self) -> Self::Real;
92+
/// Returns the imaginary part of `self`.
93+
fn imag(self) -> Self::Real;
9094
fn add_real(self, Self::Real) -> Self;
9195
fn sub_real(self, Self::Real) -> Self;
9296
fn mul_real(self, Self::Real) -> Self;
@@ -141,6 +145,8 @@ macro_rules! impl_traits {
141145
impl AssociatedReal for $real {
142146
type Real = $real;
143147
fn inject(r: Self::Real) -> Self { r }
148+
fn real(self) -> Self::Real { self }
149+
fn imag(self) -> Self::Real { 0. }
144150
fn add_real(self, r: Self::Real) -> Self { self + r }
145151
fn sub_real(self, r: Self::Real) -> Self { self - r }
146152
fn mul_real(self, r: Self::Real) -> Self { self * r }
@@ -150,6 +156,8 @@ impl AssociatedReal for $real {
150156
impl AssociatedReal for $complex {
151157
type Real = $real;
152158
fn inject(r: Self::Real) -> Self { Self::new(r, 0.0) }
159+
fn real(self) -> Self::Real { self.re }
160+
fn imag(self) -> Self::Real { self.im }
153161
fn add_real(self, r: Self::Real) -> Self { self + r }
154162
fn sub_real(self, r: Self::Real) -> Self { self - r }
155163
fn mul_real(self, r: Self::Real) -> Self { self * r }

0 commit comments

Comments
 (0)