• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     pybind11/complex.h: Complex number support
3 
4     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6     All rights reserved. Use of this source code is governed by a
7     BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "pybind11.h"
13 #include <complex>
14 
15 /// glibc defines I as a macro which breaks things, e.g., boost template names
16 #ifdef I
17 #  undef I
18 #endif
19 
20 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
21 
22 template <typename T> struct format_descriptor<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
23     static constexpr const char c = format_descriptor<T>::c;
24     static constexpr const char value[3] = { 'Z', c, '\0' };
25     static std::string format() { return std::string(value); }
26 };
27 
28 #ifndef PYBIND11_CPP17
29 
30 template <typename T> constexpr const char format_descriptor<
31     std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>>::value[3];
32 
33 #endif
34 
35 PYBIND11_NAMESPACE_BEGIN(detail)
36 
37 template <typename T> struct is_fmt_numeric<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
38     static constexpr bool value = true;
39     static constexpr int index = is_fmt_numeric<T>::index + 3;
40 };
41 
42 template <typename T> class type_caster<std::complex<T>> {
43 public:
44     bool load(handle src, bool convert) {
45         if (!src)
46             return false;
47         if (!convert && !PyComplex_Check(src.ptr()))
48             return false;
49         Py_complex result = PyComplex_AsCComplex(src.ptr());
50         if (result.real == -1.0 && PyErr_Occurred()) {
51             PyErr_Clear();
52             return false;
53         }
54         value = std::complex<T>((T) result.real, (T) result.imag);
55         return true;
56     }
57 
58     static handle cast(const std::complex<T> &src, return_value_policy /* policy */, handle /* parent */) {
59         return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
60     }
61 
62     PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
63 };
64 PYBIND11_NAMESPACE_END(detail)
65 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
66