concision_utils/traits/
complex.rs1#![cfg(feature = "complex")]
6use num::Num;
7use num::complex::Complex;
8
9pub trait AsComplex<T> {
10 type Complex<A>;
11
12 fn as_complex(&self, real: bool) -> Self::Complex<T>;
13
14 fn as_re(&self) -> Self::Complex<T> {
15 self.as_complex(true)
16 }
17
18 fn as_im(&self) -> Self::Complex<T> {
19 self.as_complex(false)
20 }
21}
22pub trait IntoComplex<T> {
24 type Complex<A>;
25
26 fn into_complex(self, real: bool) -> Self::Complex<T>
27 where
28 Self: Sized;
29
30 fn into_re(self) -> Self::Complex<T>
31 where
32 Self: Sized,
33 {
34 self.into_complex(true)
35 }
36
37 fn into_im(self) -> Self::Complex<T>
38 where
39 Self: Sized,
40 {
41 self.into_complex(false)
42 }
43}
44
45impl<T> AsComplex<T> for T
50where
51 T: Clone + Num,
52{
53 type Complex<A> = Complex<A>;
54
55 fn as_complex(&self, real: bool) -> Complex<T> {
56 match real {
57 true => Complex::new(self.clone(), Self::zero()),
58 false => Complex::new(Self::zero(), self.clone()),
59 }
60 }
61}
62
63impl<T> IntoComplex<T> for T
64where
65 T: Num,
66{
67 type Complex<A> = Complex<A>;
68
69 fn into_complex(self, real: bool) -> Self::Complex<T>
70 where
71 Self: Sized,
72 {
73 match real {
74 true => Complex::new(self, T::zero()),
75 false => Complex::new(T::zero(), self),
76 }
77 }
78}