num_util.h

Go to the documentation of this file.
00001 #ifndef NUM_UTIL_H__
00002 #define NUM_UTIL_H__
00003 
00004 // Copyright 2006  Phil Austin (http://www.eos.ubc.ca/personal/paustin)
00005 // Distributed under the Boost Software License, Version 1.0. (See
00006 // accompanying file LICENSE_1_0.txt or copy at
00007 // http://www.boost.org/LICENSE_1_0.txt)
00008 
00009 //
00010 // $Id: num__util_8h-source.html,v 1.19 2007/07/26 18:21:57 pfkeb Exp $
00011 //
00012 
00013 #define PY_ARRAY_UNIQUE_SYMBOL HippoPyArrayHandle
00014 #define NO_IMPORT_ARRAY
00015 
00016 // for have numarray etc
00017 #ifdef HAVE_CONFIG_H
00018 #include "config.h"
00019 #endif
00020 
00021 //#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
00022 #define NO_IMPORT_ARRAY
00023 
00024 #include <boost/python.hpp>
00025 #ifdef HAVE_NUMPY
00026 #include <numpy/noprefix.h>
00027 #else
00028 #ifdef HAVE_NUMERIC
00029 #include <Numeric/arrayobject.h>
00030 #else
00031 #include <numarray/arrayobject.h>
00032 typedef int intp;
00033 #endif
00034 #endif
00035 
00036 #include <iostream>
00037 #include <sstream>
00038 #include <vector>
00039 #include <numeric>
00040 #include <map>
00041 #include <complex>
00042 
00043 
00044 
00045 namespace num_util{
00047 
00052   boost::python::numeric::array makeNum(boost::python::object x);
00053 
00061   boost::python::numeric::array makeNum(intp n, PyArray_TYPES t);
00062 
00070   boost::python::numeric::array makeNum(std::vector<int> dimens, 
00071                                         PyArray_TYPES t);
00072                                       
00080 #ifdef HAVE_NUMPY
00081 #ifdef _MSC_VER
00082   template <typename T>  PyArray_TYPES getEnum ();
00083 #else
00084   template<typename T> PyArray_TYPES getEnum(void)
00085   {
00086     PyErr_SetString(PyExc_ValueError, "no mapping available for this type");
00087     boost::python::throw_error_already_set();
00088     return PyArray_VOID;
00089   }
00090 #endif
00091 #else
00092   template <typename T>  PyArray_TYPES getEnum ();
00093 #endif
00094 
00103   template <typename T> boost::python::numeric::array makeNum(T* data, int n = 0){
00104     boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>())));
00105 #ifdef HAVE_NUMPY
00106     void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
00107     memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n); // copies the input data to 
00108 #else
00109   char *arr_data = ((PyArrayObject*) obj.ptr())->data; 
00110   memcpy(arr_data, data, sizeof(T) * n); // copies the input data to 
00111                                            // PyArrayObject->data
00112 #endif
00113 
00114     return boost::python::extract<boost::python::numeric::array>(obj);
00115   }
00116 
00127   template <typename T> boost::python::numeric::array makeNum(T * data, std::vector<int> dims){
00128     intp total = std::accumulate(dims.begin(),dims.end(),1,std::multiplies<intp>());
00129     boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(),&dims[0], getEnum<T>())));
00130 #ifdef HAVE_NUMPY
00131     void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
00132     memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
00133 #else
00134   char *arr_data = ((PyArrayObject*) obj.ptr())->data; 
00135   memcpy(arr_data, data, sizeof(T) * total); // copies the input data to 
00136                                            // PyArrayObject->data
00137 #endif
00138 
00139     return boost::python::extract<boost::python::numeric::array>(obj);
00140   }
00141 
00142   template <>  boost::python::numeric::array makeNum<double> ( double * data,
00143                                                   std::vector < int>  dims );    
00144 
00150   boost::python::numeric::array makeNum(const 
00151                                         boost::python::numeric::array& arr);
00152 
00158   PyArray_TYPES type(boost::python::numeric::array arr);
00159 
00167   void check_type(boost::python::numeric::array arr, 
00168                   PyArray_TYPES expected_type);
00169 
00175   int rank(boost::python::numeric::array arr);
00176 
00183   void check_rank(boost::python::numeric::array arr, int expected_rank);
00184   
00190   intp size(boost::python::numeric::array arr);
00191   
00199   void check_size(boost::python::numeric::array arr, intp expected_size);
00200 
00206   std::vector<intptr_t> shape(boost::python::numeric::array arr);
00207 
00214   intp get_dim(boost::python::numeric::array arr, int dimnum);
00215 
00223   void check_shape(boost::python::numeric::array arr, 
00224                    std::vector<intp> expected_dims);
00225 
00234   void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize);
00235 
00241   bool iscontiguous(boost::python::numeric::array arr);
00242 
00248   void check_contiguous(boost::python::numeric::array arr);
00249 
00255   void* data(boost::python::numeric::array arr);
00256 
00263   void copy_data(boost::python::numeric::array arr, char* new_data);
00264   
00270   boost::python::numeric::array clone(boost::python::numeric::array arr);
00271   
00278   boost::python::numeric::array astype(boost::python::numeric::array arr, 
00279                                        PyArray_TYPES t);
00280 
00281 
00282 /*    *Returns the reference count of the array. */
00283 /*    *@param arr a Boost/Python numeric array. */
00284 /*    *@return the reference count of the array. */
00285 
00286   int refcount(boost::python::numeric::array arr);
00287 
00293   std::vector<intp> strides(boost::python::numeric::array arr);
00294 
00301   void check_PyArrayElementType(boost::python::object newo);
00302 
00306   typedef std::map<PyArray_TYPES, std::string> KindStringMap;
00307 
00311   typedef std::map<PyArray_TYPES, char> KindCharMap;
00312 
00316   typedef std::map<char, PyArray_TYPES> KindTypeMap;
00317 
00323   std::string type2string(PyArray_TYPES t_type);
00324 
00330   char type2char(PyArray_TYPES t_type);
00331   
00337   PyArray_TYPES char2type(char e_type);
00338 
00345   template <class T>
00346   inline std::string vector_str(const std::vector<T>& vec);
00347 
00355   inline void check_size_match(std::vector<intp> dims, intp n);
00356 
00357 } //  namespace num_util
00358 
00359 #endif

Generated for HippoDraw Class Library by doxygen