Skip to content

Commit 6d62f0f

Browse files
authored
Merge pull request #30 from JohanMabille/complex
Complex
2 parents 3285e44 + 7a39162 commit 6d62f0f

File tree

10 files changed

+57
-3
lines changed

10 files changed

+57
-3
lines changed

benchmark/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ endif()
7878
configure_file(benchmark_pyarray.py benchmark_pyarray.py COPYONLY)
7979
configure_file(benchmark_pytensor.py benchmark_pytensor.py COPYONLY)
8080
configure_file(benchmark_pybind_array.py benchmark_pybind_array.py COPYONLY)
81+
configure_file(benchmark_pyvectorize.py benchmark_pyvectorize.py COPYONLY)
82+
configure_file(benchmark_pybind_vectorize.py benchmark_pybind_vectorize.py COPYONLY)
8183

8284
add_custom_target(xbenchmark DEPENDS ${XTENSOR_PYTHON_BENCHMARK_TARGET})
8385

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from benchmark_xtensor_python import pybind_rect_to_polar
2+
import numpy as np
3+
4+
from timeit import timeit
5+
w = np.ones(100000, dtype=complex)
6+
print (timeit('pybind_rect_to_polar(w[::2])', 'from __main__ import w, pybind_rect_to_polar', number=1000))

benchmark/benchmark_pyvectorize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from benchmark_xtensor_python import rect_to_polar
2+
import numpy as np
3+
4+
from timeit import timeit
5+
w = np.ones(100000, dtype=complex)
6+
print (timeit('rect_to_polar(w[::2])', 'from __main__ import w, rect_to_polar', number=1000))

benchmark/main.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#include "xtensor/xarray.hpp"
77
#include "xtensor-python/pyarray.hpp"
88
#include "xtensor-python/pytensor.hpp"
9+
#include "xtensor-python/pyvectorize.hpp"
910

10-
#include <complex>
11+
using complex_t = std::complex<double>;
1112

1213
namespace py = pybind11;
1314

@@ -47,5 +48,18 @@ PYBIND11_PLUGIN(benchmark_xtensor_python)
4748
}
4849
);
4950

51+
m.def("rect_to_polar", [](xt::pyarray<complex_t> const& a) {
52+
return py::make_tuple(xt::pyvectorize([](complex_t x) { return std::abs(x); })(a),
53+
xt::pyvectorize([](complex_t x) { return std::arg(x); })(a));
54+
});
55+
56+
m.def("pybind_rect_to_polar", [](py::array a) {
57+
if (py::isinstance<py::array_t<complex_t>>(a))
58+
return py::make_tuple(py::vectorize([](complex_t x) { return std::abs(x); })(a),
59+
py::vectorize([](complex_t x) { return std::arg(x); })(a));
60+
else
61+
throw py::type_error("rect_to_polar unhandled type");
62+
});
63+
5064
return m.ptr();
5165
}

include/xtensor-python/pybuffer_adaptor.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ namespace xt
280280

281281
inline self_type operator+(difference_type n) const { return self_type(p_current + n); }
282282
inline self_type operator-(difference_type n) const { return self_type(p_current - n); }
283+
inline self_type operator-(const self_type& rhs) const
284+
{
285+
self_type tmp(*this);
286+
tmp -= (p_current - rhs.p_current);
287+
return tmp;
288+
}
283289

284290
pointer get_pointer() const { return p_current; }
285291

include/xtensor-python/pycontainer.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cmath>
1515
#include "pybind11/pybind11.h"
1616
#include "pybind11/common.h"
17+
#include "pybind11/complex.h"
1718
// Because of layout, else xiterator and xtensor_forward are sufficient
1819
#include "xtensor/xcontainer.hpp"
1920

@@ -188,6 +189,13 @@ namespace xt
188189
std::is_same<T, double>::value ? 1 : std::is_same<T, long double>::value ? 2 : 0));
189190
};
190191

192+
template <class T>
193+
struct is_fmt_numeric<std::complex<T>>
194+
{
195+
static constexpr bool value = true;
196+
static constexpr int index = is_fmt_numeric<T>::index + 3;
197+
};
198+
191199
template <class T>
192200
struct numpy_traits
193201
{

include/xtensor-python/pyvectorize.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace xt
2626
{
2727
}
2828

29-
pybind11::object operator()(const pyarray<Args>&... args)
29+
inline pyarray<R> operator()(const pyarray<Args>&... args)
3030
{
3131
pyarray<R> res = m_vectorizer(args...);
3232
return res;

test/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <numeric>
88

99
namespace py = pybind11;
10+
using complex_t = std::complex<double>;
1011

1112
// Examples
1213

@@ -58,5 +59,10 @@ PYBIND11_PLUGIN(xtensor_python_test)
5859

5960
m.def("vectorize_example1", xt::pyvectorize(add), "");
6061

62+
m.def("rect_to_polar", [](xt::pyarray<complex_t> const& a) {
63+
return py::make_tuple(xt::pyvectorize([](complex_t x) { return std::abs(x); })(a),
64+
xt::pyvectorize([](complex_t x) { return std::arg(x); })(a));
65+
});
66+
6167
return m.ptr();
6268
}

test/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def build_extensions(self):
107107
description='An example project using xtensor-python',
108108
long_description='',
109109
ext_modules=ext_modules,
110-
install_requires=['pybind11==2.0.1'],
110+
install_requires=['pybind11>=2.0.1'],
111111
cmdclass={'build_ext': BuildExt},
112112
zip_safe=False,
113113
)

test/test_pyarray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ def test_readme_example2(self):
5050
[-1.499227, 0.136731, 1.646979, 1.643002, 0.128456],
5151
[-1.084323, -0.583843, 0.45342 , 1.073811, 0.706945]], 1e-5)
5252

53+
def test_rect_to_polar(self):
54+
print("test6")
55+
x = np.ones(10, dtype=complex)
56+
z = xt.rect_to_polar(x[::2]);
57+
np.testing.assert_allclose(z, (np.ones(5, dtype=float), np.zeros(5, dtype=float)), 1e-5)
58+

0 commit comments

Comments
 (0)