num_util.h
Go to the documentation of this file.
1 #ifndef NUM_UTIL_H__
2 #define NUM_UTIL_H__
3 
4 // Copyright 2006 Phil Austin (http://www.eos.ubc.ca/personal/paustin)
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //
10 // $Id: num_util.h,v 1.19 2009/08/04 23:34:01 pfkeb Exp $
11 //
12 
13 #define PY_ARRAY_UNIQUE_SYMBOL HippoPyArrayHandle
14 #define NO_IMPORT_ARRAY
15 
16 // for have numarray etc
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 //#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
22 #define NO_IMPORT_ARRAY
23 
24 
25 #include <boost/python.hpp>
26 
27 #ifdef HAVE_NUMPY
28 #define NPY_NO_PREFIX
29 #include <numpy/arrayobject.h>
30 typedef int intp;
31 #else
32 #ifdef HAVE_NUMERIC
33 #include <Numeric/arrayobject.h>
34 #else
35 #include <numarray/arrayobject.h
36 typedef int intp;
37 #endif
38 #endif
39 
40 #include <iostream>
41 #include <sstream>
42 #include <vector>
43 #include <numeric>
44 #include <map>
45 #include <complex>
46 
47 
48 
49 namespace num_util{
51 
56  boost::python::numeric::array makeNum(boost::python::object x);
57 
65  boost::python::numeric::array makeNum(intp n, PyArray_TYPES t);
66 
74  boost::python::numeric::array makeNum(std::vector<int> dimens,
75  PyArray_TYPES t);
76 
84 #ifdef HAVE_NUMPY
85 #ifdef _MSC_VER
86  template <typename T> PyArray_TYPES getEnum ();
87 #else
88  template<typename T> PyArray_TYPES getEnum(void)
89  {
90  PyErr_SetString(PyExc_ValueError, "no mapping available for this type");
91  boost::python::throw_error_already_set();
92  return PyArray_VOID;
93  }
94 #endif
95 #else
96  template <typename T> PyArray_TYPES getEnum ();
97 #endif
98 
107  template <typename T> boost::python::numeric::array makeNum(T* data, int n = 0){
108  boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>())));
109 #ifdef HAVE_NUMPY
110  void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
111  memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n); // copies the input data to
112 #else
113  char *arr_data = ((PyArrayObject*) obj.ptr())->data;
114  memcpy(arr_data, data, sizeof(T) * n); // copies the input data to
115  // PyArrayObject->data
116 #endif
117 
118  return boost::python::extract<boost::python::numeric::array>(obj);
119  }
120 
131  template <typename T> boost::python::numeric::array makeNum(T * data, std::vector<int> dims){
132  intp total = std::accumulate(dims.begin(),dims.end(),1,std::multiplies<intp>());
133  boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(),&dims[0], getEnum<T>())));
134 #ifdef HAVE_NUMPY
135  void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
136  memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
137 #else
138  char *arr_data = ((PyArrayObject*) obj.ptr())->data;
139  memcpy(arr_data, data, sizeof(T) * total); // copies the input data to
140  // PyArrayObject->data
141 #endif
142 
143  return boost::python::extract<boost::python::numeric::array>(obj);
144  }
145 
146  template <> boost::python::numeric::array makeNum<double> ( double * data,
147  std::vector < int> dims );
148 
154  boost::python::numeric::array makeNum(const
155  boost::python::numeric::array& arr);
156 
162  PyArray_TYPES type(boost::python::numeric::array arr);
163 
171  void check_type(boost::python::numeric::array arr,
172  PyArray_TYPES expected_type);
173 
179  int rank(boost::python::numeric::array arr);
180 
187  void check_rank(boost::python::numeric::array arr, int expected_rank);
188 
194  intp size(boost::python::numeric::array arr);
195 
203  void check_size(boost::python::numeric::array arr, intp expected_size);
204 
210  std::vector<intptr_t> shape(boost::python::numeric::array arr);
211 
218  intp get_dim(boost::python::numeric::array arr, int dimnum);
219 
227  void check_shape(boost::python::numeric::array arr,
228  std::vector<intp> expected_dims);
229 
238  void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize);
239 
245  bool iscontiguous(boost::python::numeric::array arr);
246 
252  void check_contiguous(boost::python::numeric::array arr);
253 
259  void* data(boost::python::numeric::array arr);
260 
267  void copy_data(boost::python::numeric::array arr, char* new_data);
268 
274  boost::python::numeric::array clone(boost::python::numeric::array arr);
275 
282  boost::python::numeric::array astype(boost::python::numeric::array arr,
283  PyArray_TYPES t);
284 
285 
286 /* *Returns the reference count of the array. */
287 /* *@param arr a Boost/Python numeric array. */
288 /* *@return the reference count of the array. */
289 
290  int refcount(boost::python::numeric::array arr);
291 
297  std::vector<intp> strides(boost::python::numeric::array arr);
298 
305  void check_PyArrayElementType(boost::python::object newo);
306 
310  typedef std::map<PyArray_TYPES, std::string> KindStringMap;
311 
315  typedef std::map<PyArray_TYPES, char> KindCharMap;
316 
320  typedef std::map<char, PyArray_TYPES> KindTypeMap;
321 
327  std::string type2string(PyArray_TYPES t_type);
328 
334  char type2char(PyArray_TYPES t_type);
335 
341  PyArray_TYPES char2type(char e_type);
342 
349  template <class T>
350  inline std::string vector_str(const std::vector<T>& vec);
351 
359  inline void check_size_match(std::vector<intp> dims, intp n);
360 
361 } // namespace num_util
362 
363 #endif
PyArray_TYPES getEnum()
Function template returns PyArray_Type for C++ type See num_util.cpp for specializations.
numeric::array makeNum(object x)
Definition: num_util.cpp:219
std::string vector_str(const std::vector< T > &vec)
Constructs a string which contains a list of elements extracted from the input vector.
Definition: num_util.cpp:480
void check_type(boost::python::numeric::array arr, PyArray_TYPES expected_type)
Throws an exception if the actual array type is not equal to the expected type.
Definition: num_util.cpp:257
int intp
Definition: NumArrayTuple.h:29
void * data(numeric::array arr)
Definition: num_util.cpp:389
void check_contiguous(numeric::array arr)
Definition: num_util.cpp:380
char type2char(PyArray_TYPES t_type)
Converts a PyArray_TYPE to its single character typecode.
Definition: num_util.cpp:471
void check_shape(boost::python::numeric::array arr, std::vector< intptr_t > expected_dims)
Definition: num_util.cpp:349
int refcount(numeric::array arr)
Definition: num_util.cpp:445
intp get_dim(boost::python::numeric::array arr, int dimnum)
Returns the size of a specific dimension.
Definition: num_util.cpp:335
PyArray_TYPES char2type(char e_type)
Coverts a single character typecode to its PyArray_TYPES.
Definition: num_util.cpp:475
std::string type2string(PyArray_TYPES t_type)
Converts a PyArray_TYPE to its name in string.
Definition: num_util.cpp:467
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
std::map< PyArray_TYPES, std::string > KindStringMap
Mapping from a PyArray_TYPE to its corresponding name in string.
Definition: num_util.h:310
void check_rank(boost::python::numeric::array arr, int expected_rank)
Throws an exception if the actual rank is not equal to the expected rank.
Definition: num_util.cpp:284
std::vector< intp > strides(numeric::array arr)
Definition: num_util.cpp:427
PyArray_TYPES type(numeric::array arr)
Definition: num_util.cpp:249
numarray arrayobject h typedef int intp
Definition: num_util.h:36
boost::python::numeric::array makeNum< double >(double *data, std::vector< int > dims)
Definition: num_util.cpp:127
numeric::array clone(numeric::array arr)
Definition: num_util.cpp:412
intp size(numeric::array arr)
Definition: num_util.cpp:296
int rank(numeric::array arr)
Definition: num_util.cpp:271
void copy_data(boost::python::numeric::array arr, char *new_data)
Copies data into the array.
Definition: num_util.cpp:402
void check_PyArrayElementType(object newo)
Definition: num_util.cpp:449
bool iscontiguous(numeric::array arr)
Definition: num_util.cpp:374
void check_size(boost::python::numeric::array arr, intp expected_size)
Throw an exception if the actual total size of the array is not equal to the expected size...
Definition: num_util.cpp:305
std::map< char, PyArray_TYPES > KindTypeMap
Mapping from a typeID to its corresponding PyArray_TYPE.
Definition: num_util.h:320
void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize)
Throws an exception if a specific dimension from a numpy array does not match the expected size...
Definition: num_util.cpp:361
void check_size_match(std::vector< intp > dims, intp n)
Throws an exception if the total size computed from a vector of integer does not match with the expec...
Definition: num_util.cpp:492
numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t)
Returns a clone of this array with a new type.
Definition: num_util.cpp:423
std::map< PyArray_TYPES, char > KindCharMap
Mapping from a PyArray_TYPE to its corresponding typeID in char.
Definition: num_util.h:315

Generated for HippoDraw Class Library by doxygen