-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathArrayHelpers.h
169 lines (137 loc) · 4 KB
/
ArrayHelpers.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include "NewOrder.h"
#include <pybind11/numpy.h>
#include <array>
namespace no {
template<size_t D>
using Index_t = std::array<py::ssize_t, D>;
// this should be faster than using initializer_list as the compiler can optimise better?
template<typename T, size_t D=1>
T& at(py::array_t<T>& a, const Index_t<D>& index)
{
assert(a.ndim() == D);
T* buf = (T*)a.request().ptr;
size_t offset = index[0];
for (size_t i = 1; i < D; ++i)
{
offset = offset * a.strides()[i-1]/sizeof(T) + index[i];
}
return buf[offset];
}
// no impl for D=0
template<typename T>
T& at(py::array_t<T>& a, const Index_t<0>& index);
template<typename T, size_t D=1>
const T& at(const py::array_t<T>& a, const Index_t<D>& index)
{
assert(a.ndim() == D);
T* buf = (T*)a.request().ptr;
size_t offset = index[0];
for (size_t i = 1; i < D; ++i)
{
offset = offset * a.strides()[i-1]/sizeof(T) + index[i];
}
return buf[offset];
}
// no impl for D=0
template<typename T>
const T& at(py::array_t<T>& a, const Index_t<0>& index);
template<typename T>
T* begin(py::array_t<T>& a)
{
assert(a.itemsize() == sizeof(T));
return (T*)a.request().ptr;
}
template<typename T>
const T* cbegin(const py::array_t<T>& a)
{
assert(a.itemsize() == sizeof(T));
return (const T*)a.request().ptr;
}
template<typename T>
T* end(py::array_t<T>& a)
{
assert(a.itemsize() == sizeof(T));
return (T*)a.request().ptr + a.size();
}
template<typename T>
const T* cend(const py::array_t<T>& a)
{
assert(a.itemsize() == sizeof(T));
return (const T*)a.request().ptr + a.size();
}
// Create a 1d array, initialising with a function
// e.g. "ones" is make_array<double>(n, [](){ return 1.0; })
template<typename T, size_t D=1>
py::array_t<T> make_array(const Index_t<D>& n, const std::function<T()>& f)
{
py::array_t<T> a(n);
std::generate(begin(a), end(a), f);
return a;
}
template<typename T>
py::array_t<T>& fill(py::array_t<T>& a, T val)
{
std::fill(begin(a), end(a), val);
return a;
}
// "nullary"/scalar unary op implemented as make_array
template<typename R, typename A>
py::array_t<R> unary_op(const py::array_t<A>& arg, const std::function<R(A)>& f)
{
py::array_t<R> result(std::vector<py::ssize_t>(arg.shape(), arg.shape() + arg.ndim()));
const A* p = (const A*)arg.request().ptr;
R* r = (R*)result.request().ptr;
for (py::ssize_t i = 0; i < arg.size(); ++i)
{
r[i] = f(p[i]);
}
return result;
}
template<typename R, typename A0, typename A1>
py::array_t<R> binary_op(A0 arg0, const py::array_t<A1>& arg1, const std::function<R(A0, A1)>& f)
{
py::array_t<R> result(std::vector<py::ssize_t>(arg1.shape(), arg1.shape() + arg1.ndim()));
const A1* p = (const A1*)arg1.request().ptr;
R* r = (R*)result.request().ptr;
for (py::ssize_t i = 0; i < arg1.size(); ++i)
{
r[i] = f(arg0, p[i]);
}
return result;
}
template<typename R, typename A0, typename A1>
py::array_t<R> binary_op(const py::array_t<A0>& arg0, A1 arg1, const std::function<R(A0, A1)>& f)
{
py::array_t<R> result(std::vector<py::ssize_t>{arg0.shape(), arg0.shape() + arg0.ndim()});
const A0* p = (const A0*)arg0.request().ptr;
R* r = (R*)result.request().ptr;
for (py::ssize_t i = 0; i < arg0.size(); ++i)
{
r[i] = f(p[i], arg1);
}
return result;
}
template<typename R, typename A0, typename A1>
py::array_t<R> binary_op(const py::array_t<A0>& arg0, const py::array_t<A1>& arg1, const std::function<R(A0, A1)>& f)
{
if (arg0.ndim() != arg1.ndim())
{
throw std::runtime_error("binary_op: argments must have same dimension");
}
for (py::ssize_t i = 0; i < arg0.ndim(); ++i)
{
if (arg0.shape()[i] != arg1.shape()[i])
throw std::runtime_error("binary_op: argments must have same size");
}
py::array_t<R> result(std::vector<py::ssize_t>(arg0.shape(), arg0.shape() + arg0.ndim()));
const A0* p0 = (const A0*)arg0.request().ptr;
const A1* p1 = (const A1*)arg1.request().ptr;
R* r = (R*)result.request().ptr;
for (py::ssize_t i = 0; i < arg0.size(); ++i)
{
r[i] = f(p0[i], p1[i]);
}
return result;
}
}