mixed_num/complex/
polar_impl.rs

1use super::*;
2
3impl <T: MixedNum> MixedNum for Polar<T>
4{
5}
6
7impl <T: MixedNum + MixedNumSigned> MixedComplex for Polar<T>
8{
9}
10
11impl<T> Polar<T>
12{
13    pub fn new(mag:T, ang:T) -> Self
14    {
15        return Polar{mag:mag, ang:ang};
16    }
17}
18
19impl <T: MixedNum + MixedNumSigned + MixedSqrt + MixedAtan + MixedPowi + MixedAbs + MixedOps > NewFromCartesian<T> for Polar<T>
20{
21    /// Type cast from real number T to Cartesian<T>.
22    fn new_from_cartesian( re:T, im:T ) -> Self
23    {
24        return ops::to_polar(Cartesian::new(re,im));
25    }
26}
27
28impl <T: MixedNum + MixedNumSigned + MixedWrapPhase + MixedTrigonometry> NewFromPolar<T> for Polar<T>
29{
30    fn new_from_polar( mag:T, ang:T ) -> Self
31    {
32        return Polar::new(mag, ang);
33    }
34}
35
36impl <T: MixedNum + MixedNumConversion<T2> + MixedZero, T2: MixedNum> MixedNumConversion<T2> for Polar<T>
37{
38    #[inline(always)]
39    fn mixed_from_num( number:T2 ) -> Self {
40        return Self{mag:T::mixed_from_num(number), ang:T::mixed_zero()};
41    }
42    #[inline(always)]
43    fn mixed_to_num( &self ) -> T2 {
44        return self.mag.mixed_to_num();
45    }
46}
47
48impl <T: MixedNum + MixedNumSigned + MixedWrapPhase + MixedOps + MixedTrigonometry> ToCartesian<T> for Polar<T>
49{
50    /// Cartesian<T> to Polar<T>.
51    #[inline(always)]
52    fn to_cartesian( &self ) -> Cartesian<T>
53    {
54        return ops::to_cartesian(*self);
55    }
56}
57
58impl <T: MixedNum + MixedNumSigned> ToPolar<T> for Polar<T>
59{
60    /// Cartesian<T> to Polar<T>.
61    #[inline(always)]
62    fn to_polar( &self ) -> Polar<T>
63    {
64        return *self;
65    }
66}
67
68impl <T: MixedNum + MixedZero> MixedZero for Polar<T>
69{
70    /// Return the zero value of type Self.
71    #[inline(always)]
72    fn mixed_zero() -> Self {
73        return Self{mag:T::mixed_zero(), ang:T::mixed_zero()};
74    }
75}
76
77impl <T: MixedNum + MixedZero + MixedOne> MixedOne for Polar<T>
78{
79    /// Return the zero value of type Self.
80    #[inline(always)]
81    fn mixed_one() -> Self {
82        return Self{mag:T::mixed_one(), ang:T::mixed_zero()};
83    }
84}
85
86impl <T: MixedNum + MixedZero> MixedComplexConversion<T>  for Polar<T>
87{
88    /// Type cast from real number T to Polar<T>.
89    fn mixed_to_complex( number:T ) -> Self {
90        return Self{mag:number, ang:T::mixed_zero()};
91    }
92}
93
94impl <T: MixedNum + MixedNumSigned> Mag<T> for Polar<T>
95{
96    /// Magnitude of the complex number.
97    #[inline(always)]
98    fn mag( &self ) -> T
99    {
100        return self.mag;
101    }
102    /// Magnitude of the complex number.
103    #[inline(always)]
104    fn abs( &self ) -> T
105    {
106        return self.mag;
107    }
108}
109
110impl <T: MixedNum + MixedNumSigned + MixedZero> MixedAbs for Polar<T>
111{
112    #[inline(always)]
113    fn mixed_abs( &self ) -> Self
114    {
115        return Self::new(self.mag, T::mixed_zero());
116    }
117}
118
119impl <T: MixedNum + MixedNumSigned> Arg<T> for Polar<T>
120{
121    /// Argument of the complex number.
122    #[inline(always)]
123    fn arg( &self ) -> T
124    {
125        return self.ang;
126    }
127
128    /// Angle of the complex number.
129    #[inline(always)]
130    fn ang( &self ) -> T
131    {
132        return self.ang;
133    }
134}
135
136impl <T: MixedNum + MixedNumSigned + MixedOps> core::ops::Mul<Polar<T>> for Polar<T> {
137    type Output = Self;
138    #[inline]
139    fn mul(self, rhs: Self) -> Self {
140        return ops::mul_polar(self, rhs);
141    }
142}
143
144impl <T: MixedNum + MixedNumSigned + MixedSqrt + MixedOps + MixedAbs + MixedPowi + MixedAtan + ToPolar<T>> core::ops::Mul<Cartesian<T>> for Polar<T> {
145    type Output = Self;
146    #[inline]
147    fn mul(self, rhs: Cartesian<T>) -> Self {
148        let rhs_pol = rhs.to_polar();
149        return ops::mul_polar(self, rhs_pol);
150    }
151}
152
153impl <T: MixedNum + MixedNumSigned + MixedOps> core::ops::Mul<T> for Polar<T> {
154    type Output = Self;
155    #[inline]
156    fn mul(self, rhs: T) -> Self {
157        return Polar::new(self.mag*rhs, self.ang);
158    }
159}
160
161impl <T: MixedReal + MixedNumSigned + MixedOps + MixedZero> core::ops::Div<T> for Polar<T> {
162    type Output = Self;
163    #[inline]
164    fn div(self, rhs: T) -> Self {
165        if rhs == T::mixed_zero() {
166            return Polar::new(T::mixed_max_value(), self.ang);
167        }
168        return Polar::new(self.mag/rhs, self.ang);
169    }
170}
171
172impl <T: MixedComplex + NewFromPolar<T2>, T2: MixedNum + MixedNumSigned> Conj<T> for Polar<T2>
173{
174    /// Complex Conjugate of T.
175    fn conj( &self ) -> T {
176        return T::new_from_polar(self.mag, -self.ang);
177    }
178}
179
180impl<T> core::fmt::Display for Polar<T>
181where
182    T: core::fmt::Display,
183{
184    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
185        write!(f, "{}∠{}", self.mag, self.ang)
186    }
187}