2015-07-30 13:29:00 +00:00
|
|
|
/*
|
2015-10-15 16:13:33 +00:00
|
|
|
pybind11/complex.h: Complex number support
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
#include "pybind11.h"
|
2015-07-30 13:29:00 +00:00
|
|
|
#include <complex>
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_DECL_FMT(std::complex<float>, "Zf");
|
|
|
|
PYBIND11_DECL_FMT(std::complex<double>, "Zd");
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
template <typename T> class type_caster<std::complex<T>> {
|
|
|
|
public:
|
|
|
|
bool load(PyObject *src, bool) {
|
|
|
|
Py_complex result = PyComplex_AsCComplex(src);
|
|
|
|
if (result.real == -1.0 && PyErr_Occurred()) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = std::complex<T>((T) result.real, (T) result.imag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *cast(const std::complex<T> &src, return_value_policy /* policy */, PyObject * /* parent */) {
|
|
|
|
return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
|
|
|
|
}
|
|
|
|
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_TYPE_CASTER(std::complex<T>, "complex");
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
NAMESPACE_END(detail)
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_END(pybind11)
|