1use super::*;
2
3pub fn to_polar<T>( x: Cartesian<T> ) -> Polar<T>
10 where T: MixedNum + MixedNumSigned + MixedSqrt + MixedAtan + MixedPowi + MixedAbs + MixedOps
11{
12 let c_polar = Polar::<T>{
13 mag: abs(x),
14 ang: x.im.mixed_atan2(x.re)
15 };
16 return c_polar;
17}
18
19pub fn abs<T>( a: Cartesian<T> ) -> T
36where T: MixedNum + MixedNumSigned + MixedSqrt + MixedPowi + MixedOps
37{
38 let r_sqr = a.re.mixed_powi(2) + a.im.mixed_powi(2);
39 return r_sqr.mixed_sqrt();
40}
41
42pub fn to_cartesian<T>( a: Polar<T> ) -> Cartesian<T>
59 where T: MixedNum + MixedNumSigned + MixedTrigonometry + MixedWrapPhase + MixedOps
60{
61 let theta = a.ang.mixed_wrap_phase();
62 let (imag_s, real_s) = theta.mixed_sincos();
63
64 let c_cartesian = Cartesian::<T>{
65 re: a.mag*real_s,
66 im: a.mag*imag_s
67 };
68 return c_cartesian;
69}
70
71pub fn add<T>( a: Cartesian<T>, b: Cartesian<T> ) -> Cartesian<T>
74 where T: MixedNum + MixedOps
75{
76 let c_cartesian = Cartesian::<T>{
77 re: a.re + b.re,
78 im: a.im + b.im
79 };
80 return c_cartesian;
81}
82
83pub fn sub<T>( a: Cartesian<T>, b: Cartesian<T> ) -> Cartesian<T>
87 where T: MixedNum + MixedNumSigned + core::ops::Sub<Output = T>
88{
89 let c_cartesian = Cartesian::<T>{
90 re: a.re - b.re,
91 im: a.im - b.im
92 };
93 return c_cartesian;
94}
95
96pub fn mul_polar<T>( a: Polar<T>, b: Polar<T> ) -> Polar<T>
99 where T: MixedNum + MixedNumSigned + MixedOps
100{
101 if a.mag==T::mixed_from_num(0) || b.mag==T::mixed_from_num(0)
102 {
103 let c = Polar::<T>{
104 mag: T::mixed_from_num(0),
105 ang: T::mixed_from_num(0)
106 };
107 return c;
108 }
109 else
110 {
111 let c = Polar::<T>{
112 mag: a.mag*b.mag,
113 ang: a.ang+b.ang
114 };
115 return c;
116 }
117}
118
119pub fn mul_cartesian<T>( ab: Cartesian<T>, bc: Cartesian<T> ) -> Cartesian<T>
122 where T: MixedNum + MixedNumSigned + MixedOps
123{
124 let a = ab.re;
125 let b = ab.im;
126 let c = bc.re;
127 let d = bc.im;
128
129 let re = (a*c) - (b*d);
130 let im = (a*d) + (b*c);
131 return Cartesian{re:re, im:im}
132}
133
134pub fn powi<T>( base: Cartesian<T>, power:usize ) -> Cartesian<T>
158 where T: fixed::traits::FixedSigned + MixedNum + MixedNumSigned + MixedAtan + MixedSin + MixedSqrt + MixedPowi
159{
160 let temp:T = base.re.mixed_powi(2) + base.im.mixed_powi(2);
162 let mag:T = temp.mixed_sqrt().mixed_powi(power as i32);
163
164 let phi:T = base.im.mixed_atan2(base.re) *<T>::from_num(power);
165
166 let (imag_s, real_s) = phi.mixed_sincos();
167
168 let real = mag*real_s;
169 let imag = mag*imag_s;
170
171 return Cartesian::new_from_cartesian( real, imag);
172}
173
174pub fn div_scalar_cartesian<T>( a: Cartesian<T>, b: T ) -> Cartesian<T>
178 where T: MixedReal + MixedNumSigned + MixedOps
179{
180 let mut c = a;
181
182 if b == T::mixed_from_num(0)
183 {
184 c.re = T::mixed_max_value();
185 c.im = T::mixed_max_value();
186 }
187 else
188 {
189 c.re = a.re / b;
190 c.im = a.im / b;
191 }
192 return c;
193}
194
195pub fn div_cartesian<T>( numerator: Cartesian<T>, denominator: Cartesian<T> ) -> Cartesian<T>
199 where T: MixedNum + MixedNumSigned + MixedOps + MixedPowi
200{
201 let a = numerator.re;
204 let b = numerator.im;
205 let c = denominator.re;
206 let d = denominator.re;
207
208 let re = (a*c+b*d)/(c.mixed_powi(2)+d.mixed_powi(2));
209 let im = (b*c-a*d)/(c.mixed_powi(2)+d.mixed_powi(2));
210 return Cartesian::new(re, im);
211}