deep_causality_num/complex/complex_number/
arithmetic.rs1use crate::{Complex, RealField};
6use core::iter::{Product, Sum};
7use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
8
9impl<T: RealField> Sum for Complex<T> {
11 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
12 iter.fold(Self::new(T::zero(), T::zero()), |acc, x| acc + x)
13 }
14}
15
16impl<T: RealField> Product for Complex<T> {
18 fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
19 iter.fold(Self::new(T::one(), T::zero()), |acc, x| acc * x)
20 }
21}
22
23impl<T: RealField> Add for Complex<T> {
25 type Output = Self;
26 #[inline]
27 fn add(self, rhs: Self) -> Self {
28 Self::new(self.re + rhs.re, self.im + rhs.im)
29 }
30}
31
32impl<T: RealField> AddAssign for Complex<T> {
34 #[inline]
35 fn add_assign(&mut self, rhs: Self) {
36 self.re += rhs.re;
37 self.im += rhs.im;
38 }
39}
40
41impl<T: RealField> Add<T> for Complex<T> {
43 type Output = Self;
44 #[inline]
45 fn add(self, rhs: T) -> Self {
46 Self::new(self.re + rhs, self.im)
47 }
48}
49
50impl<T: RealField> AddAssign<T> for Complex<T> {
52 #[inline]
53 fn add_assign(&mut self, rhs: T) {
54 self.re += rhs;
55 }
56}
57
58impl<T: RealField> Sub for Complex<T> {
60 type Output = Self;
61 #[inline]
62 fn sub(self, rhs: Self) -> Self {
63 Self::new(self.re - rhs.re, self.im - rhs.im)
64 }
65}
66
67impl<T: RealField> SubAssign for Complex<T> {
69 #[inline]
70 fn sub_assign(&mut self, rhs: Self) {
71 self.re -= rhs.re;
72 self.im -= rhs.im;
73 }
74}
75
76impl<T: RealField> Sub<T> for Complex<T> {
78 type Output = Self;
79 #[inline]
80 fn sub(self, rhs: T) -> Self {
81 Self::new(self.re - rhs, self.im)
82 }
83}
84
85impl<T: RealField> SubAssign<T> for Complex<T> {
87 #[inline]
88 fn sub_assign(&mut self, rhs: T) {
89 self.re -= rhs;
90 }
91}
92
93impl<T: RealField> Mul for Complex<T> {
95 type Output = Self;
96 #[inline]
97 fn mul(self, rhs: Self) -> Self {
98 Self::new(
99 self.re * rhs.re - self.im * rhs.im,
100 self.re * rhs.im + self.im * rhs.re,
101 )
102 }
103}
104
105impl<T: RealField> MulAssign for Complex<T> {
107 #[inline]
108 fn mul_assign(&mut self, rhs: Self) {
109 let re = self.re * rhs.re - self.im * rhs.im;
110 let im = self.re * rhs.im + self.im * rhs.re;
111 self.re = re;
112 self.im = im;
113 }
114}
115
116impl<T: RealField> Mul<T> for Complex<T> {
118 type Output = Self;
119 #[inline]
120 fn mul(self, rhs: T) -> Self {
121 Self::new(self.re * rhs, self.im * rhs)
122 }
123}
124
125impl<T: RealField> MulAssign<T> for Complex<T> {
127 #[inline]
128 fn mul_assign(&mut self, rhs: T) {
129 self.re *= rhs;
130 self.im *= rhs;
131 }
132}
133
134impl<T: RealField> Div for Complex<T> {
136 type Output = Self;
137 #[allow(clippy::suspicious_arithmetic_impl)]
140 #[inline]
141 fn div(self, rhs: Self) -> Self {
142 self * rhs._inverse_impl()
143 }
144}
145
146impl<T: RealField> DivAssign for Complex<T> {
148 #[allow(clippy::suspicious_op_assign_impl)]
150 #[inline]
151 fn div_assign(&mut self, rhs: Self) {
152 *self *= rhs._inverse_impl();
153 }
154}
155
156impl<T: RealField> Div<T> for Complex<T> {
158 type Output = Self;
159 #[inline]
160 fn div(self, rhs: T) -> Self {
161 Self::new(self.re / rhs, self.im / rhs)
162 }
163}
164
165impl<T: RealField> DivAssign<T> for Complex<T> {
167 #[inline]
168 fn div_assign(&mut self, rhs: T) {
169 self.re /= rhs;
170 self.im /= rhs;
171 }
172}
173
174impl<T: RealField> Neg for Complex<T> {
176 type Output = Self;
177 #[inline]
178 fn neg(self) -> Self {
179 Self::new(-self.re, -self.im)
180 }
181}