13 #define PY_ARRAY_UNIQUE_SYMBOL HippoPyArrayHandle
14 #define NO_IMPORT_ARRAY
22 #define NO_IMPORT_ARRAY
25 #include <boost/python.hpp>
29 #include <numpy/arrayobject.h>
33 #include <Numeric/arrayobject.h>
35 #include <numarray/arrayobject.h
56 boost::python::numeric::array
makeNum(boost::python::object x);
65 boost::python::numeric::array
makeNum(
intp n, PyArray_TYPES t);
74 boost::python::numeric::array
makeNum(std::vector<int> dimens,
86 template <
typename T> PyArray_TYPES
getEnum ();
88 template<
typename T> PyArray_TYPES
getEnum(
void)
90 PyErr_SetString(PyExc_ValueError,
"no mapping available for this type");
91 boost::python::throw_error_already_set();
96 template <
typename T> PyArray_TYPES
getEnum ();
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>())));
110 void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
111 memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n);
113 char *arr_data = ((PyArrayObject*) obj.ptr())->data;
114 memcpy(arr_data, data,
sizeof(
T) * n);
118 return boost::python::extract<boost::python::numeric::array>(obj);
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>())));
135 void *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr());
136 memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
138 char *arr_data = ((PyArrayObject*) obj.ptr())->data;
139 memcpy(arr_data, data,
sizeof(
T) * total);
143 return boost::python::extract<boost::python::numeric::array>(obj);
147 std::vector < int> dims );
154 boost::python::numeric::array
makeNum(
const
155 boost::python::numeric::array& arr);
162 PyArray_TYPES
type(boost::python::numeric::array arr);
171 void check_type(boost::python::numeric::array arr,
172 PyArray_TYPES expected_type);
179 int rank(boost::python::numeric::array arr);
187 void check_rank(boost::python::numeric::array arr,
int expected_rank);
194 intp size(boost::python::numeric::array arr);
203 void check_size(boost::python::numeric::array arr,
intp expected_size);
210 std::vector<intptr_t>
shape(boost::python::numeric::array arr);
218 intp get_dim(boost::python::numeric::array arr,
int dimnum);
227 void check_shape(boost::python::numeric::array arr,
228 std::vector<intp> expected_dims);
238 void check_dim(boost::python::numeric::array arr,
int dimnum,
intp dimsize);
259 void*
data(boost::python::numeric::array arr);
267 void copy_data(boost::python::numeric::array arr,
char* new_data);
274 boost::python::numeric::array
clone(boost::python::numeric::array arr);
282 boost::python::numeric::array
astype(boost::python::numeric::array arr,
290 int refcount(boost::python::numeric::array arr);
297 std::vector<intp>
strides(boost::python::numeric::array arr);
PyArray_TYPES getEnum()
Function template returns PyArray_Type for C++ type See num_util.cpp for specializations.
numeric::array makeNum(object x)
std::string vector_str(const std::vector< T > &vec)
Constructs a string which contains a list of elements extracted from the input vector.
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.
void * data(numeric::array arr)
void check_contiguous(numeric::array arr)
char type2char(PyArray_TYPES t_type)
Converts a PyArray_TYPE to its single character typecode.
void check_shape(boost::python::numeric::array arr, std::vector< intptr_t > expected_dims)
int refcount(numeric::array arr)
intp get_dim(boost::python::numeric::array arr, int dimnum)
Returns the size of a specific dimension.
PyArray_TYPES char2type(char e_type)
Coverts a single character typecode to its PyArray_TYPES.
std::string type2string(PyArray_TYPES t_type)
Converts a PyArray_TYPE to its name in string.
std::vector< intptr_t > shape(numeric::array arr)
std::map< PyArray_TYPES, std::string > KindStringMap
Mapping from a PyArray_TYPE to its corresponding name in string.
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.
std::vector< intp > strides(numeric::array arr)
PyArray_TYPES type(numeric::array arr)
numarray arrayobject h typedef int intp
boost::python::numeric::array makeNum< double >(double *data, std::vector< int > dims)
numeric::array clone(numeric::array arr)
intp size(numeric::array arr)
int rank(numeric::array arr)
void copy_data(boost::python::numeric::array arr, char *new_data)
Copies data into the array.
void check_PyArrayElementType(object newo)
bool iscontiguous(numeric::array arr)
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...
std::map< char, PyArray_TYPES > KindTypeMap
Mapping from a typeID to its corresponding PyArray_TYPE.
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...
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...
numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t)
Returns a clone of this array with a new type.
std::map< PyArray_TYPES, char > KindCharMap
Mapping from a PyArray_TYPE to its corresponding typeID in char.