1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use crate::{ffi, AsPyPointer, PyAny, Python};
use std::os::raw::c_double;

/// Represents a Python [`complex`](https://docs.python.org/3/library/functions.html#complex) object.
///
/// Note that `PyComplex` supports only basic operations. For advanced operations
/// consider using [num-complex](https://docs.rs/num-complex)'s [`Complex`] type instead.
/// This optional dependency can be activated with the `num-complex` feature flag.
///
/// [`Complex`]: https://docs.rs/num-complex/latest/num_complex/struct.Complex.html
#[repr(transparent)]
pub struct PyComplex(PyAny);

pyobject_native_type!(
    PyComplex,
    ffi::PyComplexObject,
    ffi::PyComplex_Type,
    #checkfunction=ffi::PyComplex_Check
);

impl PyComplex {
    /// Creates a new `PyComplex` from the given real and imaginary values.
    pub fn from_doubles(py: Python<'_>, real: c_double, imag: c_double) -> &PyComplex {
        unsafe {
            let ptr = ffi::PyComplex_FromDoubles(real, imag);
            py.from_owned_ptr(ptr)
        }
    }
    /// Returns the real part of the complex number.
    pub fn real(&self) -> c_double {
        unsafe { ffi::PyComplex_RealAsDouble(self.as_ptr()) }
    }
    /// Returns the imaginary part of the complex number.
    pub fn imag(&self) -> c_double {
        unsafe { ffi::PyComplex_ImagAsDouble(self.as_ptr()) }
    }
}

#[cfg(not(any(Py_LIMITED_API, PyPy)))]
mod not_limited_impls {
    use super::*;
    use std::ops::{Add, Div, Mul, Neg, Sub};

    impl PyComplex {
        /// Returns `|self|`.
        pub fn abs(&self) -> c_double {
            unsafe {
                let val = (*(self.as_ptr() as *mut ffi::PyComplexObject)).cval;
                ffi::_Py_c_abs(val)
            }
        }
        /// Returns `self` raised to the power of `other`.
        pub fn pow(&self, other: &PyComplex) -> &PyComplex {
            unsafe {
                self.py()
                    .from_owned_ptr(complex_operation(self, other, ffi::_Py_c_pow))
            }
        }
    }

    #[inline(always)]
    unsafe fn complex_operation(
        l: &PyComplex,
        r: &PyComplex,
        operation: unsafe extern "C" fn(ffi::Py_complex, ffi::Py_complex) -> ffi::Py_complex,
    ) -> *mut ffi::PyObject {
        let l_val = (*(l.as_ptr() as *mut ffi::PyComplexObject)).cval;
        let r_val = (*(r.as_ptr() as *mut ffi::PyComplexObject)).cval;
        ffi::PyComplex_FromCComplex(operation(l_val, r_val))
    }

    impl<'py> Add for &'py PyComplex {
        type Output = &'py PyComplex;
        fn add(self, other: &'py PyComplex) -> &'py PyComplex {
            unsafe {
                self.py()
                    .from_owned_ptr(complex_operation(self, other, ffi::_Py_c_sum))
            }
        }
    }

    impl<'py> Sub for &'py PyComplex {
        type Output = &'py PyComplex;
        fn sub(self, other: &'py PyComplex) -> &'py PyComplex {
            unsafe {
                self.py()
                    .from_owned_ptr(complex_operation(self, other, ffi::_Py_c_diff))
            }
        }
    }

    impl<'py> Mul for &'py PyComplex {
        type Output = &'py PyComplex;
        fn mul(self, other: &'py PyComplex) -> &'py PyComplex {
            unsafe {
                self.py()
                    .from_owned_ptr(complex_operation(self, other, ffi::_Py_c_prod))
            }
        }
    }

    impl<'py> Div for &'py PyComplex {
        type Output = &'py PyComplex;
        fn div(self, other: &'py PyComplex) -> &'py PyComplex {
            unsafe {
                self.py()
                    .from_owned_ptr(complex_operation(self, other, ffi::_Py_c_quot))
            }
        }
    }

    impl<'py> Neg for &'py PyComplex {
        type Output = &'py PyComplex;
        fn neg(self) -> &'py PyComplex {
            unsafe {
                let val = (*(self.as_ptr() as *mut ffi::PyComplexObject)).cval;
                self.py()
                    .from_owned_ptr(ffi::PyComplex_FromCComplex(ffi::_Py_c_neg(val)))
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::PyComplex;
        use crate::Python;
        use assert_approx_eq::assert_approx_eq;

        #[test]
        fn test_add() {
            Python::with_gil(|py| {
                let l = PyComplex::from_doubles(py, 3.0, 1.2);
                let r = PyComplex::from_doubles(py, 1.0, 2.6);
                let res = l + r;
                assert_approx_eq!(res.real(), 4.0);
                assert_approx_eq!(res.imag(), 3.8);
            });
        }

        #[test]
        fn test_sub() {
            Python::with_gil(|py| {
                let l = PyComplex::from_doubles(py, 3.0, 1.2);
                let r = PyComplex::from_doubles(py, 1.0, 2.6);
                let res = l - r;
                assert_approx_eq!(res.real(), 2.0);
                assert_approx_eq!(res.imag(), -1.4);
            });
        }

        #[test]
        fn test_mul() {
            Python::with_gil(|py| {
                let l = PyComplex::from_doubles(py, 3.0, 1.2);
                let r = PyComplex::from_doubles(py, 1.0, 2.6);
                let res = l * r;
                assert_approx_eq!(res.real(), -0.12);
                assert_approx_eq!(res.imag(), 9.0);
            });
        }

        #[test]
        fn test_div() {
            Python::with_gil(|py| {
                let l = PyComplex::from_doubles(py, 3.0, 1.2);
                let r = PyComplex::from_doubles(py, 1.0, 2.6);
                let res = l / r;
                assert_approx_eq!(res.real(), 0.788_659_793_814_432_9);
                assert_approx_eq!(res.imag(), -0.850_515_463_917_525_7);
            });
        }

        #[test]
        fn test_neg() {
            Python::with_gil(|py| {
                let val = PyComplex::from_doubles(py, 3.0, 1.2);
                let res = -val;
                assert_approx_eq!(res.real(), -3.0);
                assert_approx_eq!(res.imag(), -1.2);
            });
        }

        #[test]
        fn test_abs() {
            Python::with_gil(|py| {
                let val = PyComplex::from_doubles(py, 3.0, 1.2);
                assert_approx_eq!(val.abs(), 3.231_098_884_280_702_2);
            });
        }

        #[test]
        fn test_pow() {
            Python::with_gil(|py| {
                let l = PyComplex::from_doubles(py, 3.0, 1.2);
                let r = PyComplex::from_doubles(py, 1.2, 2.6);
                let val = l.pow(r);
                assert_approx_eq!(val.real(), -1.419_309_997_016_603_7);
                assert_approx_eq!(val.imag(), -0.541_297_466_033_544_6);
            });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::PyComplex;
    use crate::Python;
    use assert_approx_eq::assert_approx_eq;

    #[test]
    fn test_from_double() {
        use assert_approx_eq::assert_approx_eq;

        Python::with_gil(|py| {
            let complex = PyComplex::from_doubles(py, 3.0, 1.2);
            assert_approx_eq!(complex.real(), 3.0);
            assert_approx_eq!(complex.imag(), 1.2);
        });
    }
}