Commit 2c3c04b2 authored by Robert Bradshaw's avatar Robert Bradshaw

Allow conversion of std::complex to/from Python objects.

parent 2bac445c
......@@ -3231,6 +3231,7 @@ builtin_cpp_conversions = {
"std::unordered_set": 1,
"std::map": 2,
"std::unordered_map": 2,
"std::complex": 1,
}
class CppClassType(CType):
......
......@@ -227,3 +227,35 @@ cdef object {{cname}}(const map[X,Y]& s):
o[X_to_py(key_value.first)] = Y_to_py(key_value.second)
cython.operator.preincrement(iter)
return o
#################### complex.from_py ####################
{{template_type_declarations}}
cdef extern from *:
cdef cppclass std_complex "std::complex" [T]:
std_complex()
std_complex(T, T) except +
@cname("{{cname}}")
cdef std_complex[X] {{cname}}(object o) except *:
cdef double complex z = o
return std_complex[X](<X>z.real, <X>z.imag)
#################### complex.to_py ####################
{{template_type_declarations}}
cdef extern from *:
cdef cppclass std_complex "std::complex" [T]:
X real()
X imag()
@cname("{{cname}}")
cdef object {{cname}}(const std_complex[X]& z):
cdef double complex tmp
tmp.real = <double>z.real()
tmp.imag = <double>z.imag()
return tmp
# tag: cpp
from libcpp.complex cimport complex as complex_class
def double_complex(complex_class[double] a):
"""
>>> double_complex(1 + 2j)
(1+2j)
>>> double_complex(1.5 + 2.5j)
(1.5+2.5j)
"""
return a
def double_int(complex_class[int] a):
"""
>>> double_int(1 + 2j)
(1+2j)
>>> double_int(1.5 + 2.5j)
(1+2j)
"""
return a
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment