1+ use core:: cmp:: Ordering ;
12use core:: fmt;
23use core:: ops:: { Add , Div , Index , Mul , Sub } ;
34
@@ -7,6 +8,7 @@ use crate::{
78} ;
89
910/// A complex number
11+ #[ repr( C ) ]
1012#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
1113#[ cfg_attr(
1214 feature = "rkyv" ,
@@ -241,6 +243,42 @@ impl_single_op_variants_other!(
241243 "Divide a real number by a complex number"
242244) ;
243245
246+ impl PartialOrd for Complex {
247+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
248+ let self_mag_sq = self . real * self . real + self . imaginary * self . imaginary ;
249+ let other_mag_sq = other. real * other. real + other. imaginary * other. imaginary ;
250+
251+ self_mag_sq. partial_cmp ( & other_mag_sq)
252+ }
253+ }
254+
255+ impl PartialEq < f64 > for Complex {
256+ fn eq ( & self , other : & f64 ) -> bool {
257+ self . imaginary == 0.0 && self . real == * other
258+ }
259+ }
260+
261+ impl PartialOrd < f64 > for Complex {
262+ fn partial_cmp ( & self , other : & f64 ) -> Option < Ordering > {
263+ let self_mag_sq = self . real * self . real + self . imaginary * self . imaginary ;
264+ let other_mag_sq = other * other;
265+
266+ self_mag_sq. partial_cmp ( & other_mag_sq)
267+ }
268+ }
269+
270+ impl PartialEq < Complex > for f64 {
271+ fn eq ( & self , other : & Complex ) -> bool {
272+ other. eq ( self )
273+ }
274+ }
275+
276+ impl PartialOrd < Complex > for f64 {
277+ fn partial_cmp ( & self , other : & Complex ) -> Option < Ordering > {
278+ other. partial_cmp ( self ) . map ( Ordering :: reverse)
279+ }
280+ }
281+
244282impl Index < usize > for Complex {
245283 type Output = f64 ;
246284
0 commit comments