complex.h 2.0 KB

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