@@ -3,8 +3,10 @@ use super::super::pyobject::{
33} ;
44use super :: super :: vm:: VirtualMachine ;
55use super :: objfloat;
6+ use super :: objint;
67use super :: objtype;
78use num_complex:: Complex64 ;
9+ use num_traits:: ToPrimitive ;
810
911pub fn init ( context : & PyContext ) {
1012 let complex_type = & context. complex_type ;
@@ -13,7 +15,10 @@ pub fn init(context: &PyContext) {
1315 "Create a complex number from a real part and an optional imaginary part.\n \n \
1416 This is equivalent to (real + imag*1j) where imag defaults to 0.";
1517
18+ context. set_attr ( & complex_type, "__abs__" , context. new_rustfunc ( complex_abs) ) ;
1619 context. set_attr ( & complex_type, "__add__" , context. new_rustfunc ( complex_add) ) ;
20+ context. set_attr ( & complex_type, "__eq__" , context. new_rustfunc ( complex_eq) ) ;
21+ context. set_attr ( & complex_type, "__neg__" , context. new_rustfunc ( complex_neg) ) ;
1722 context. set_attr ( & complex_type, "__new__" , context. new_rustfunc ( complex_new) ) ;
1823 context. set_attr (
1924 & complex_type,
@@ -70,6 +75,13 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7075 ) )
7176}
7277
78+ fn complex_abs ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
79+ arg_check ! ( vm, args, required = [ ( zelf, Some ( vm. ctx. complex_type( ) ) ) ] ) ;
80+
81+ let Complex64 { re, im } = get_value ( zelf) ;
82+ Ok ( vm. ctx . new_float ( re. hypot ( im) ) )
83+ }
84+
7385fn complex_add ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
7486 arg_check ! (
7587 vm,
@@ -92,6 +104,36 @@ fn complex_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
92104 Ok ( vm. ctx . new_complex ( v1. conj ( ) ) )
93105}
94106
107+ fn complex_eq ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
108+ arg_check ! (
109+ vm,
110+ args,
111+ required = [ ( zelf, Some ( vm. ctx. complex_type( ) ) ) , ( other, None ) ]
112+ ) ;
113+
114+ let z = get_value ( zelf) ;
115+
116+ let result = if objtype:: isinstance ( other, & vm. ctx . complex_type ( ) ) {
117+ z == get_value ( other)
118+ } else if objtype:: isinstance ( other, & vm. ctx . int_type ( ) ) {
119+ match objint:: get_value ( other) . to_f64 ( ) {
120+ Some ( f) => z. im == 0.0f64 && z. re == f,
121+ None => false ,
122+ }
123+ } else if objtype:: isinstance ( other, & vm. ctx . float_type ( ) ) {
124+ z. im == 0.0 && z. re == objfloat:: get_value ( other)
125+ } else {
126+ return Ok ( vm. ctx . not_implemented ( ) ) ;
127+ } ;
128+
129+ Ok ( vm. ctx . new_bool ( result) )
130+ }
131+
132+ fn complex_neg ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
133+ arg_check ! ( vm, args, required = [ ( zelf, Some ( vm. ctx. complex_type( ) ) ) ] ) ;
134+ Ok ( vm. ctx . new_complex ( -get_value ( zelf) ) )
135+ }
136+
95137fn complex_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
96138 arg_check ! ( vm, args, required = [ ( obj, Some ( vm. ctx. complex_type( ) ) ) ] ) ;
97139 let v = get_value ( obj) ;
0 commit comments