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.18 2007/07/02 18:17:15 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 #include <boost/python.hpp>
25 #ifdef HAVE_NUMPY
26 #include <numpy/noprefix.h>
27 #else
28 #ifdef HAVE_NUMERIC
29 #include <Numeric/arrayobject.h>
30 #else
31 #include <numarray/arrayobject.h>
32 typedef int intp;
33 #endif
34 #endif
35 
36 #include <iostream>
37 #include <sstream>
38 #include <vector>
39 #include <numeric>
40 #include <map>
41 #include <complex>
42 
43 
44 
45 namespace num_util{
47 
52  boost::python::numeric::array makeNum(boost::python::object x);
53 
61  boost::python::numeric::array makeNum(intp n, PyArray_TYPES t);
62 
70  boost::python::numeric::array makeNum(std::vector<int> dimens,
71  PyArray_TYPES t);
72 
80 #ifdef HAVE_NUMPY
81 #ifdef _MSC_VER
82  template <typename T> PyArray_TYPES getEnum ();
83 #else
84  template<typename T> PyArray_TYPES getEnum(void)
85  {
86  PyErr_SetString(PyExc_ValueError, "no mapping available for this type");
87  boost::python::throw_error_already_set();
88  return PyArray_VOID;
89  }
90 #endif
91 #else
92  template <typename T> PyArray_TYPES getEnum ();
93 #endif
94 
103  template <typename T> boost::python::numeric::array makeNum(T* data, int n = 0){
104  boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>())));
105 #ifdef HAVE_NUMPY
106  void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
107  memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n); // copies the input data to
108 #else
109  char *arr_data = ((PyArrayObject*) obj.ptr())->data;
110  memcpy(arr_data, data, sizeof(T) * n); // copies the input data to
111  // PyArrayObject->data
112 #endif
113 
114  return boost::python::extract<boost::python::numeric::array>(obj);
115  }
116 
127  template <typename T> boost::python::numeric::array makeNum(T * data, std::vector<int> dims){
128  intp total = std::accumulate(dims.begin(),dims.end(),1,std::multiplies<intp>());
129  boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(),&dims[0], getEnum<T>())));
130 #ifdef HAVE_NUMPY
131  void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
132  memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
133 #else
134  char *arr_data = ((PyArrayObject*) obj.ptr())->data;
135  memcpy(arr_data, data, sizeof(T) * total); // copies the input data to
136  // PyArrayObject->data
137 #endif
138 
139  return boost::python::extract<boost::python::numeric::array>(obj);
140  }
141 
142  template <> boost::python::numeric::array makeNum<double> ( double * data,
143  std::vector < int> dims );
144 
150  boost::python::numeric::array makeNum(const
151  boost::python::numeric::array& arr);
152 
158  PyArray_TYPES type(boost::python::numeric::array arr);
159 
167  void check_type(boost::python::numeric::array arr,
168  PyArray_TYPES expected_type);
169 
175  int rank(boost::python::numeric::array arr);
176 
183  void check_rank(boost::python::numeric::array arr, int expected_rank);
184 
190  intp size(boost::python::numeric::array arr);
191 
199  void check_size(boost::python::numeric::array arr, intp expected_size);
200 
206  std::vector<intptr_t> shape(boost::python::numeric::array arr);
207 
214  intp get_dim(boost::python::numeric::array arr, int dimnum);
215 
223  void check_shape(boost::python::numeric::array arr,
224  std::vector<intp> expected_dims);
225 
234  void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize);
235 
241  bool iscontiguous(boost::python::numeric::array arr);
242 
248  void check_contiguous(boost::python::numeric::array arr);
249 
255  void* data(boost::python::numeric::array arr);
256 
263  void copy_data(boost::python::numeric::array arr, char* new_data);
264 
270  boost::python::numeric::array clone(boost::python::numeric::array arr);
271 
278  boost::python::numeric::array astype(boost::python::numeric::array arr,
279  PyArray_TYPES t);
280 
281 
282 /* *Returns the reference count of the array. */
283 /* *@param arr a Boost/Python numeric array. */
284 /* *@return the reference count of the array. */
285 
286  int refcount(boost::python::numeric::array arr);
287 
293  std::vector<intp> strides(boost::python::numeric::array arr);
294 
301  void check_PyArrayElementType(boost::python::object newo);
302 
306  typedef std::map<PyArray_TYPES, std::string> KindStringMap;
307 
311  typedef std::map<PyArray_TYPES, char> KindCharMap;
312 
316  typedef std::map<char, PyArray_TYPES> KindTypeMap;
317 
323  std::string type2string(PyArray_TYPES t_type);
324 
330  char type2char(PyArray_TYPES t_type);
331 
337  PyArray_TYPES char2type(char e_type);
338 
345  template <class T>
346  inline std::string vector_str(const std::vector<T>& vec);
347 
355  inline void check_size_match(std::vector<intp> dims, intp n);
356 
357 } // namespace num_util
358 
359 #endif

Generated for HippoDraw Class Library by doxygen