forked from xtensor-stack/xtensor-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
147 lines (119 loc) · 4.63 KB
/
main.cpp
File metadata and controls
147 lines (119 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor/xarray.hpp"
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"
#include "xtensor-python/pyvectorize.hpp"
namespace py = pybind11;
using complex_t = std::complex<double>;
// Examples
double example1(xt::pyarray<double>& m)
{
return m(0);
}
xt::pyarray<double> example2(xt::pyarray<double>& m)
{
return m + 2;
}
// Readme Examples
double readme_example1(xt::pyarray<double>& m)
{
auto sines = xt::sin(m);
return std::accumulate(sines.cbegin(), sines.cend(), 0.0);
}
double readme_example2(double i, double j)
{
return std::sin(i) - std::cos(j);
}
auto complex_overload(const xt::pyarray<std::complex<double>>& a)
{
return a;
}
auto no_complex_overload(const xt::pyarray<double>& a)
{
return a;
}
auto complex_overload_reg(const std::complex<double>& a)
{
return a;
}
auto no_complex_overload_reg(const double& a)
{
return a;
}
// Vectorize Examples
int add(int i, int j)
{
return i + j;
}
template <class T> std::string typestring() { return "Unknown"; }
template <> std::string typestring<uint8_t>() { return "uint8"; }
template <> std::string typestring<int8_t>() { return "int8"; }
template <> std::string typestring<uint16_t>() { return "uint16"; }
template <> std::string typestring<int16_t>() { return "int16"; }
template <> std::string typestring<uint32_t>() { return "uint32"; }
template <> std::string typestring<int32_t>() { return "int32"; }
template <> std::string typestring<uint64_t>() { return "uint64"; }
template <> std::string typestring<int64_t>() { return "int64"; }
template <class T>
inline std::string int_overload(xt::pyarray<T>& m)
{
return typestring<T>();
}
void dump_numpy_constant()
{
std::cout << "NPY_BOOL = " << NPY_BOOL << std::endl;
std::cout << "NPY_BYTE = " << NPY_BYTE << std::endl;
std::cout << "NPY_UBYTE = " << NPY_UBYTE << std::endl;
std::cout << "NPY_INT8 = " << NPY_INT8 << std::endl;
std::cout << "NPY_UINT8 = " << NPY_UINT8 << std::endl;
std::cout << "NPY_SHORT = " << NPY_SHORT << std::endl;
std::cout << "NPY_USHORT = " << NPY_USHORT << std::endl;
std::cout << "NPY_INT16 = " << NPY_INT16 << std::endl;
std::cout << "NPY_UINT16 = " << NPY_UINT16 << std::endl;
std::cout << "NPY_INT = " << NPY_INT << std::endl;
std::cout << "NPY_UINT = " << NPY_UINT << std::endl;
std::cout << "NPY_INT32 = " << NPY_INT32 << std::endl;
std::cout << "NPY_UINT32 = " << NPY_UINT32 << std::endl;
std::cout << "NPY_LONG = " << NPY_LONG << std::endl;
std::cout << "NPY_ULONG = " << NPY_ULONG << std::endl;
std::cout << "NPY_LONGLONG = " << NPY_LONGLONG << std::endl;
std::cout << "NPY_ULONGLONG = " << NPY_ULONGLONG << std::endl;
std::cout << "NPY_INT64 = " << NPY_INT64 << std::endl;
std::cout << "NPY_UINT64 = " << NPY_UINT64 << std::endl;
}
PYBIND11_PLUGIN(xtensor_python_test)
{
xt::import_numpy();
py::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.def("example1", example1);
m.def("example2", example2);
m.def("complex_overload", no_complex_overload);
m.def("complex_overload", complex_overload);
m.def("complex_overload_reg", no_complex_overload_reg);
m.def("complex_overload_reg", complex_overload_reg);
m.def("readme_example1", readme_example1);
m.def("readme_example2", xt::pyvectorize(readme_example2));
m.def("vectorize_example1", xt::pyvectorize(add));
m.def("rect_to_polar", xt::pyvectorize([](complex_t x) { return std::abs(x); }));
m.def("compare_shapes", [](const xt::pyarray<double>& a, const xt::pyarray<double>& b) {
return a.shape() == b.shape();
});
m.def("int_overload", int_overload<uint8_t>);
m.def("int_overload", int_overload<int8_t>);
m.def("int_overload", int_overload<uint16_t>);
m.def("int_overload", int_overload<int16_t>);
m.def("int_overload", int_overload<uint32_t>);
m.def("int_overload", int_overload<int32_t>);
m.def("int_overload", int_overload<uint64_t>);
m.def("int_overload", int_overload<int64_t>);
m.def("dump_numpy_constant", dump_numpy_constant);
return m.ptr();
}