| 1 | use crate::{ |
| 2 | AssertFloatEq, AssertFloatEqAll, DebugUlpsDiff, FloatEq, FloatEqAll, FloatEqDebugUlpsDiff, |
| 3 | FloatEqUlpsTol, UlpsTol, |
| 4 | }; |
| 5 | use num_complex::Complex; |
| 6 | |
| 7 | /// The absolute difference between two floating point [`num::Complex<T>`] instances |
| 8 | /// in ULPs. |
| 9 | /// |
| 10 | /// The `T` in [`num::Complex<T>`] is constrained by `Clone` and `PartialEq`, so this |
| 11 | /// implements those too. |
| 12 | /// |
| 13 | /// [`num::Complex<T>`]: https://docs.rs/num/0.3.0/num/struct.Complex.html |
| 14 | #[allow(clippy::derive_partial_eq_without_eq)] // Most likely this is going to use floats, and we don't want to derive Eq for those |
| 15 | #[derive(Clone, Debug, PartialEq)] |
| 16 | pub struct ComplexUlps<T> { |
| 17 | /// Real portion of the complex number in ULPs. |
| 18 | pub re: T, |
| 19 | /// Imaginary portion of the complex number in ULPs. |
| 20 | pub im: T, |
| 21 | } |
| 22 | |
| 23 | impl<T> ComplexUlps<T> { |
| 24 | /// Create a new ComplexUlps |
| 25 | pub fn new(re: T, im: T) -> Self { |
| 26 | ComplexUlps { re, im } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<T: FloatEqUlpsTol> FloatEqUlpsTol for Complex<T> |
| 31 | where |
| 32 | UlpsTol<T>: Sized, |
| 33 | { |
| 34 | type UlpsTol = ComplexUlps<UlpsTol<T>>; |
| 35 | } |
| 36 | |
| 37 | impl<T: FloatEqDebugUlpsDiff> FloatEqDebugUlpsDiff for Complex<T> { |
| 38 | type DebugUlpsDiff = ComplexUlps<DebugUlpsDiff<T>>; |
| 39 | } |
| 40 | |
| 41 | /// [`ComplexUlps<T>`] type matching [`num::Complex32`]. |
| 42 | /// |
| 43 | /// [`num::Complex32`]: https://docs.rs/num-complex/0.3/num_complex/type.Complex32.html |
| 44 | pub type ComplexUlps32 = UlpsTol<Complex<f32>>; |
| 45 | |
| 46 | /// [`ComplexUlps<T>`] type matching [`num::Complex64`]. |
| 47 | /// |
| 48 | /// [`num::Complex64`]: https://docs.rs/num-complex/0.3/num_complex/type.Complex64.html |
| 49 | pub type ComplexUlps64 = UlpsTol<Complex<f64>>; |
| 50 | |
| 51 | impl<T: FloatEq> FloatEq for Complex<T> |
| 52 | where |
| 53 | T::Tol: Sized, |
| 54 | UlpsTol<T::Tol>: Sized, |
| 55 | { |
| 56 | type Tol = Complex<T::Tol>; |
| 57 | |
| 58 | #[inline] |
| 59 | fn eq_abs(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 60 | self.re.eq_abs(&other.re, &tol.re) && self.im.eq_abs(&other.im, &tol.im) |
| 61 | } |
| 62 | |
| 63 | #[inline] |
| 64 | fn eq_rmax(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 65 | self.re.eq_rmax(&other.re, &tol.re) && self.im.eq_rmax(&other.im, &tol.im) |
| 66 | } |
| 67 | |
| 68 | #[inline] |
| 69 | fn eq_rmin(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 70 | self.re.eq_rmin(&other.re, &tol.re) && self.im.eq_rmin(&other.im, &tol.im) |
| 71 | } |
| 72 | |
| 73 | #[inline] |
| 74 | fn eq_r1st(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 75 | self.re.eq_r1st(&other.re, &tol.re) && self.im.eq_r1st(&other.im, &tol.im) |
| 76 | } |
| 77 | |
| 78 | #[inline] |
| 79 | fn eq_r2nd(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 80 | self.re.eq_r2nd(&other.re, &tol.re) && self.im.eq_r2nd(&other.im, &tol.im) |
| 81 | } |
| 82 | |
| 83 | #[inline] |
| 84 | fn eq_ulps(&self, other: &Self, tol: &UlpsTol<Self::Tol>) -> bool { |
| 85 | self.re.eq_ulps(&other.re, &tol.re) && self.im.eq_ulps(&other.im, &tol.im) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl<T: FloatEqAll> FloatEqAll for Complex<T> { |
| 90 | type AllTol = T::AllTol; |
| 91 | |
| 92 | #[inline] |
| 93 | fn eq_abs_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 94 | self.re.eq_abs_all(&other.re, tol) && self.im.eq_abs_all(&other.im, tol) |
| 95 | } |
| 96 | |
| 97 | #[inline] |
| 98 | fn eq_rmax_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 99 | self.re.eq_rmax_all(&other.re, tol) && self.im.eq_rmax_all(&other.im, tol) |
| 100 | } |
| 101 | |
| 102 | #[inline] |
| 103 | fn eq_rmin_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 104 | self.re.eq_rmin_all(&other.re, tol) && self.im.eq_rmin_all(&other.im, tol) |
| 105 | } |
| 106 | |
| 107 | #[inline] |
| 108 | fn eq_r1st_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 109 | self.re.eq_r1st_all(&other.re, tol) && self.im.eq_r1st_all(&other.im, tol) |
| 110 | } |
| 111 | |
| 112 | #[inline] |
| 113 | fn eq_r2nd_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 114 | self.re.eq_r2nd_all(&other.re, tol) && self.im.eq_r2nd_all(&other.im, tol) |
| 115 | } |
| 116 | |
| 117 | #[inline] |
| 118 | fn eq_ulps_all(&self, other: &Self, tol: &UlpsTol<Self::AllTol>) -> bool { |
| 119 | self.re.eq_ulps_all(&other.re, tol) && self.im.eq_ulps_all(&other.im, tol) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl<T> AssertFloatEq for Complex<T> |
| 124 | where |
| 125 | T: AssertFloatEq, |
| 126 | T::Tol: Sized, |
| 127 | T::DebugTol: Sized, |
| 128 | UlpsTol<T::Tol>: Sized, |
| 129 | UlpsTol<T::DebugTol>: Sized, |
| 130 | { |
| 131 | type DebugAbsDiff = Complex<T::DebugAbsDiff>; |
| 132 | type DebugTol = Complex<T::DebugTol>; |
| 133 | |
| 134 | #[inline] |
| 135 | fn debug_abs_diff(&self, other: &Self) -> Self::DebugAbsDiff { |
| 136 | Self::DebugAbsDiff { |
| 137 | re: self.re.debug_abs_diff(&other.re), |
| 138 | im: self.im.debug_abs_diff(&other.im), |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[inline] |
| 143 | fn debug_ulps_diff(&self, other: &Self) -> DebugUlpsDiff<Self::DebugAbsDiff> { |
| 144 | DebugUlpsDiff::<Self::DebugAbsDiff> { |
| 145 | re: self.re.debug_ulps_diff(&other.re), |
| 146 | im: self.im.debug_ulps_diff(&other.im), |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[inline] |
| 151 | fn debug_abs_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 152 | Self::DebugTol { |
| 153 | re: self.re.debug_abs_tol(&other.re, &tol.re), |
| 154 | im: self.im.debug_abs_tol(&other.im, &tol.im), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[inline] |
| 159 | fn debug_rmax_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 160 | Self::DebugTol { |
| 161 | re: self.re.debug_rmax_tol(&other.re, &tol.re), |
| 162 | im: self.im.debug_rmax_tol(&other.im, &tol.im), |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #[inline] |
| 167 | fn debug_rmin_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 168 | Self::DebugTol { |
| 169 | re: self.re.debug_rmin_tol(&other.re, &tol.re), |
| 170 | im: self.im.debug_rmin_tol(&other.im, &tol.im), |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | #[inline] |
| 175 | fn debug_r1st_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 176 | Self::DebugTol { |
| 177 | re: self.re.debug_r1st_tol(&other.re, &tol.re), |
| 178 | im: self.im.debug_r1st_tol(&other.im, &tol.im), |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[inline] |
| 183 | fn debug_r2nd_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 184 | Self::DebugTol { |
| 185 | re: self.re.debug_r2nd_tol(&other.re, &tol.re), |
| 186 | im: self.im.debug_r2nd_tol(&other.im, &tol.im), |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #[inline] |
| 191 | fn debug_ulps_tol(&self, other: &Self, tol: &UlpsTol<Self::Tol>) -> UlpsTol<Self::DebugTol> |
| 192 | where |
| 193 | UlpsTol<Self::DebugTol>: Sized, |
| 194 | { |
| 195 | UlpsTol::<Self::DebugTol> { |
| 196 | re: self.re.debug_ulps_tol(&other.re, &tol.re), |
| 197 | im: self.im.debug_ulps_tol(&other.im, &tol.im), |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl<T> AssertFloatEqAll for Complex<T> |
| 203 | where |
| 204 | T: AssertFloatEqAll, |
| 205 | T::AllDebugTol: Sized, |
| 206 | UlpsTol<T::AllDebugTol>: Sized, |
| 207 | { |
| 208 | type AllDebugTol = Complex<T::AllDebugTol>; |
| 209 | |
| 210 | #[inline] |
| 211 | fn debug_abs_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 212 | Self::AllDebugTol { |
| 213 | re: self.re.debug_abs_all_tol(&other.re, tol), |
| 214 | im: self.im.debug_abs_all_tol(&other.im, tol), |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #[inline] |
| 219 | fn debug_rmax_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 220 | Self::AllDebugTol { |
| 221 | re: self.re.debug_rmax_all_tol(&other.re, tol), |
| 222 | im: self.im.debug_rmax_all_tol(&other.im, tol), |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #[inline] |
| 227 | fn debug_rmin_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 228 | Self::AllDebugTol { |
| 229 | re: self.re.debug_rmin_all_tol(&other.re, tol), |
| 230 | im: self.im.debug_rmin_all_tol(&other.im, tol), |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | #[inline] |
| 235 | fn debug_r1st_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 236 | Self::AllDebugTol { |
| 237 | re: self.re.debug_r1st_all_tol(&other.re, tol), |
| 238 | im: self.im.debug_r1st_all_tol(&other.im, tol), |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #[inline] |
| 243 | fn debug_r2nd_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 244 | Self::AllDebugTol { |
| 245 | re: self.re.debug_r2nd_all_tol(&other.re, tol), |
| 246 | im: self.im.debug_r2nd_all_tol(&other.im, tol), |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | #[inline] |
| 251 | fn debug_ulps_all_tol( |
| 252 | &self, |
| 253 | other: &Self, |
| 254 | tol: &UlpsTol<Self::AllTol>, |
| 255 | ) -> UlpsTol<Self::AllDebugTol> |
| 256 | where |
| 257 | UlpsTol<Self::AllDebugTol>: Sized, |
| 258 | { |
| 259 | UlpsTol::<Self::AllDebugTol> { |
| 260 | re: self.re.debug_ulps_all_tol(&other.re, tol), |
| 261 | im: self.im.debug_ulps_all_tol(&other.im, tol), |
| 262 | } |
| 263 | } |
| 264 | } |