-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathnum_util.hpp
More file actions
262 lines (218 loc) · 8.62 KB
/
Copy pathnum_util.hpp
File metadata and controls
262 lines (218 loc) · 8.62 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifndef VPYTHON_PYTHON_NUM_UTIL_HPP
#define VPYTHON_PYTHON_NUM_UTIL_HPP
#define PY_ARRAY_UNIQUE_SYMBOL visual_PyArrayHandle
#ifndef IMPORT_ARRAY
#define NO_IMPORT_ARRAY
#endif
// num_util.h and num_util.cpp were obtained from:
// http://www.eos.ubc.ca/research/clouds/num_util.html on 2003-12-17 under the
// terms and conditions of the Boost Software License, version 1.0. num_util
// was written by Rhys Goldstein, Chris Seymour and Phil Austin.
// Questions or comments about num_util should be directed to Phil Austin at
// paustin@eos.ubs.ca.
// 2011/01/05 obtained additional components of latest num_util from:
// Copyright 2006 Phil Austin (http://www.eos.ubc.ca/personal/paustin)
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/*
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// Changes from the original num_util V1:
// - Moved into the visual namespace to prevent clashes with other projects
// using num_util. 2003-12-18
// - Changed header inclusion guards to follow the same conventions as the rest
// of Visual. 2003-12-18
// - Changed the definition of PY_ARRAY_UNIQUE_SYMBOL to prevent clashes with
// other projects using num_util. 2003-12-18
// - Changed header #includes to reduce compile times. 2003-12-19
// - Bring boost::python::numeric::array into the visual namespace for
// convienience. 2003-12-19
// - data(), shape(), rank(): Modified to ensure that no calls are made into the
// Python interpreter, espcially reference counting operations and PyArray_Check()
// For the reference counting operations, we can guarantee that we do not need
// them base on the fact that these functions are never called without owning
// at least one real boost::python::numeric::array. For the PyArray_Check(),
// we can guarantee that we *never* have an array on our hands that is not a
// genuine array. These functions needed to be changed to ensure that we do not
// call any functions in the interpreter or Numeric from within the rendering
// loop since they cause the interpreter to crash sporadically on multiprocessor
// machines. 2004-01-12
// Incorporated num_util release 2 to move to compatibility with numpy
// Eliminate references to numeric and numarray initiation, add numpy initiation
#include <boost/python/numeric.hpp>
#include <boost/python/extract.hpp>
#include <numpy/arrayobject.h>
//#include <iostream>
//#include <sstream>
#include <vector>
#include <numeric>
#include <map>
#include <complex>
namespace cvisual { namespace python {
using boost::python::numeric::array;
class double_array : public array {
public:
explicit double_array( const boost::python::handle<>& h ) : array(h) {}
//double_array( const array& a ) : array(a) {} //< TODO: callers are doing unnecessary copying; somewhat type unsafe
};
/**
*Creates an one-dimensional numpy array of length n and numpy type t.
* The elements of the array are initialized to zero.
*@param n an integer representing the length of the array.
*@param t elements' numpy type. Default is double.
*@return a numeric array of size n with elements initialized to zero.
*/
array makeNum(npy_intp n, NPY_TYPES t =NPY_DOUBLE);
/**
*Creates a n-dimensional numpy array with dimensions dimens and numpy
*type t. The elements of the array are initialized to zero.
*@param dimens a vector of interger specifies the dimensions of the array.
*@param t elements' numpy type. Default is double.
*@return a numeric array of shape dimens with elements initialized to zero.
*/
array makeNum(const std::vector<npy_intp>& dimens, NPY_TYPES t =NPY_DOUBLE);
/**
*Function template returns PyArray_Type for C++ type
*See num_util.cpp for specializations
*@param T C++ type
*@return numpy type enum
*/
/**
*A free function that retrieves the numpy type of a numpy array.
*@param arr a Boost/Python numeric array.
*@return the numpy type of the array's elements
*/
NPY_TYPES type(array arr);
/**
*Throws an exception if the actual array type is not equal to the expected
*type.
*@param arr a Boost/Python numeric array.
*@param expected_type an expected numpy type.
*@return -----
*/
void check_type(array arr,
NPY_TYPES expected_type);
/**
*Returns the dimensions in a vector.
*@param arr a Boost/Python numeric array.
*@return a vector with integer values that indicates the shape of the array.
*/
std::vector<npy_intp> shape(array arr);
/**
*Throws an exception if the actual dimensions of the array are not equal to
*the expected dimensions.
*@param arr a Boost/Python numeric array.
*@param expected_dims an integer vector of expected dimension.
*@return -----
*/
void check_shape(array arr,
std::vector<npy_intp> expected_dims);
/**
*Returns true if the array is contiguous.
*@param arr a Boost/Python numeric array.
*@return true if the array is contiguous, false otherwise.
*/
bool iscontiguous(array arr);
/**
*Throws an exception if the array is not contiguous.
*@param arr a Boost/Python numeric array.
*@return -----
*/
void check_contiguous(array arr);
/**
*Returns a pointer to the data in the array.
*@param arr a Boost/Python numeric array.
*@return a char pointer pointing at the first element of the array.
void* data(array arr);
*/
/**
*Returns a pointer to the data in the array.
*@param arr a Boost/Python numeric array.
*@return a char pointer pointing at the first element of the array.
*/
// USED
char* data(const array& arr);
/**
*Copies data into the array.
*@param arr a Boost/Python numeric array.
*@param new_data a char pointer referencing the new data.
*@return -----
*/
void copy_data(boost::python::numeric::array arr, char* new_data);
/**
*Returns a clone of this array with a new type.
*@param arr a Boost/Python numeric array.
*@param t NPY_TYPES of the output array.
*@return a replicate of 'arr' with type set to 't'.
*/
array astype(array arr,
NPY_TYPES t);
/**
*Mapping from a PyArray_TYPE to its corresponding name in string.
*/
typedef std::map<NPY_TYPES, std::string> KindStringMap;
/**
*Mapping from a PyArray_TYPE to its corresponding typeID in char.
*/
typedef std::map<NPY_TYPES, char> KindCharMap;
/**
*Mapping from a typeID to its corresponding PyArray_TYPE.
*/
typedef std::map<char, NPY_TYPES> KindTypeMap;
/**
*Converts a PyArray_TYPE to its name in string.
*@param t_type a NPY_TYPES.
*@return the corresponding name in string.
*/
std::string type2string(NPY_TYPES t_type);
/**
*Converts a PyArray_TYPE to its single character typecode.
*@param t_type a NPY_TYPES.
*@return the corresponding typecode in char.
*/
char type2char(NPY_TYPES t_type);
/**
*Coverts a single character typecode to its NPY_TYPES.
*@param e_type a NPY_TYPES typecode in char.
*@return its corresponding NPY_TYPES.
*/
NPY_TYPES char2type(char e_type);
#if PY_MAJOR_VERSION >= 3
int init_numpy();
#else
void init_numpy();
#endif
size_t typesize( NPY_TYPES t);
template <class T>
struct type_npy_traits {
};
template <>
struct type_npy_traits<float> {
static const int npy_type = NPY_FLOAT;
};
template <>
struct type_npy_traits<double> {
static const int npy_type = NPY_DOUBLE;
};
} } // visual
#endif