[cig-commits] r13037 - cs/cigma/trunk/pysrc

luis at geodynamics.org luis at geodynamics.org
Wed Oct 15 02:07:24 PDT 2008


Author: luis
Date: 2008-10-15 02:07:23 -0700 (Wed, 15 Oct 2008)
New Revision: 13037

Added:
   cs/cigma/trunk/pysrc/numpy_util.cpp
   cs/cigma/trunk/pysrc/numpy_util.h
Log:
Utility functions for handling python numpy arrays from C++

Added: cs/cigma/trunk/pysrc/numpy_util.cpp
===================================================================
--- cs/cigma/trunk/pysrc/numpy_util.cpp	                        (rev 0)
+++ cs/cigma/trunk/pysrc/numpy_util.cpp	2008-10-15 09:07:23 UTC (rev 13037)
@@ -0,0 +1,410 @@
+#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
+#define NO_IMPORT_ARRAY
+#include "numpy_util.h"
+
+using namespace boost::python;
+
+namespace numpy_util {
+
+
+// 
+// specializations for use by makeArray
+//
+
+template <> PyArray_TYPES getEnum<unsigned char>(void)              { return PyArray_UBYTE; }
+template <> PyArray_TYPES getEnum<signed char>(void)                { return PyArray_BYTE; }
+template <> PyArray_TYPES getEnum<short>(void)                      { return PyArray_SHORT; }
+template <> PyArray_TYPES getEnum<unsigned short>(void)             { return PyArray_USHORT; }
+template <> PyArray_TYPES getEnum<unsigned int>(void)               { return PyArray_UINT; }
+template <> PyArray_TYPES getEnum<int>(void)                        { return PyArray_INT; }
+template <> PyArray_TYPES getEnum<long>(void)                       { return PyArray_LONG; }
+template <> PyArray_TYPES getEnum<unsigned long>(void)              { return PyArray_ULONG; }
+template <> PyArray_TYPES getEnum<long long>(void)                  { return PyArray_LONGLONG; }
+template <> PyArray_TYPES getEnum<unsigned long long>(void)         { return PyArray_ULONGLONG; }
+template <> PyArray_TYPES getEnum<float>(void)                      { return PyArray_FLOAT; }
+template <> PyArray_TYPES getEnum<double>(void)                     { return PyArray_DOUBLE; }
+template <> PyArray_TYPES getEnum<long double>(void)                { return PyArray_LONGDOUBLE; }
+template <> PyArray_TYPES getEnum<std::complex<float> >(void)       { return PyArray_CFLOAT; }
+template <> PyArray_TYPES getEnum<std::complex<double> >(void)      { return PyArray_CDOUBLE; }
+template <> PyArray_TYPES getEnum<std::complex<long double> >(void) { return PyArray_CDOUBLE; }
+
+
+typedef KindStringMap::value_type KindStringMapEntry;
+KindStringMapEntry kindStringMapEntries[] = {
+    KindStringMapEntry(PyArray_UBYTE,   "PyArray_UBYTE"),
+    KindStringMapEntry(PyArray_BYTE,    "PyArray_BYTE" ),
+    KindStringMapEntry(PyArray_SHORT,   "PyArray_SHORT"),
+    KindStringMapEntry(PyArray_INT,     "PyArray_INT"  ),
+    KindStringMapEntry(PyArray_LONG,    "PyArray_LONG" ),
+    KindStringMapEntry(PyArray_FLOAT,   "PyArray_FLOAT"),
+    KindStringMapEntry(PyArray_DOUBLE,  "PyArray_DOUBLE"),
+    KindStringMapEntry(PyArray_CFLOAT,  "PyArray_CFLOAT"),
+    KindStringMapEntry(PyArray_CDOUBLE, "PyArray_CDOUBLE"),
+    KindStringMapEntry(PyArray_OBJECT,  "PyArray_OBJECT"),
+    KindStringMapEntry(PyArray_NTYPES,  "PyArray_NTYPES"),
+    KindStringMapEntry(PyArray_NOTYPE,  "PyArray_NOTYPE")
+};
+
+
+typedef KindCharMap::value_type KindCharMapEntry;
+KindCharMapEntry kindCharMapEntries[] = {
+    KindCharMapEntry(PyArray_UBYTE,   'B'),
+    KindCharMapEntry(PyArray_BYTE,    'b'),
+    KindCharMapEntry(PyArray_SHORT,   'h'),
+    KindCharMapEntry(PyArray_INT,     'i'),
+    KindCharMapEntry(PyArray_LONG,    'l'),
+    KindCharMapEntry(PyArray_FLOAT,   'f'),
+    KindCharMapEntry(PyArray_DOUBLE,  'd'),
+    KindCharMapEntry(PyArray_CFLOAT,  'F'),
+    KindCharMapEntry(PyArray_CDOUBLE, 'D'),
+    KindCharMapEntry(PyArray_OBJECT,  'O')
+};
+
+
+typedef KindTypeMap::value_type KindTypeMapEntry;
+KindTypeMapEntry kindTypeMapEntries[] = {
+    KindTypeMapEntry('B', PyArray_UBYTE),
+    KindTypeMapEntry('b', PyArray_BYTE),
+    KindTypeMapEntry('h', PyArray_SHORT),
+    KindTypeMapEntry('i', PyArray_INT),
+    KindTypeMapEntry('l', PyArray_LONG),
+    KindTypeMapEntry('f', PyArray_FLOAT),
+    KindTypeMapEntry('d', PyArray_DOUBLE),
+    KindTypeMapEntry('F', PyArray_CFLOAT),
+    KindTypeMapEntry('D', PyArray_CDOUBLE),
+    KindTypeMapEntry('O', PyArray_OBJECT)
+};
+
+
+int numStringEntries = sizeof(kindStringMapEntries) / sizeof(KindStringMapEntry);
+int numCharEntries   = sizeof(kindCharMapEntries)   / sizeof(KindCharMapEntry);
+int numTypeEntries   = sizeof(kindTypeMapEntries)   / sizeof(KindTypeMapEntry);
+
+
+using namespace boost::python;
+
+static KindStringMap kindstrings(kindStringMapEntries, kindStringMapEntries + numStringEntries);
+static KindCharMap   kindchars  (kindCharMapEntries,   kindCharMapEntries   + numCharEntries);
+static KindTypeMap   kindtypes  (kindTypeMapEntries,   kindTypeMapEntries   + numTypeEntries);
+
+
+// Create a numeric array referencing Python sequence object
+numeric::array makeArray(object x)
+{
+    if (!PySequence_Check(x.ptr()))
+    {
+        PyErr_SetString(PyExc_ValueError, "expected a sequence");
+        throw_error_already_set();
+    }
+    object obj(handle<>(PyArray_ContiguousFromObject(x.ptr(), PyArray_NOTYPE, 0, 0)));
+    check_PyArrayElementType(obj);
+    return extract<numeric::array>(obj);
+}
+
+// Create a one-dimensional numeric array of length n and type t
+numeric::array makeArray(intp n, PyArray_TYPES t=PyArray_DOUBLE)
+{
+    object obj(handle<>(PyArray_FromDims(1, &n, t)));
+    return extract<numeric::array>(obj);
+}
+
+
+// Create a numeric array with dimensions dimens and type t
+numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t=PyArray_DOUBLE)
+{
+    object obj(handle<>(PyArray_FromDims(dimens.size(), &dimens[0], t)));
+    return extract<numeric::array>(obj);
+}
+
+
+numeric::array makeArray(const numeric::array& arr)
+{
+    // Returns a reference of arr by calling numeric::array copy constructor.
+    // The copy constructor increases arr's reference count.
+    return numeric::array(arr);
+}
+
+
+PyArray_TYPES type(numeric::array arr)
+{
+    return PyArray_TYPES(PyArray_TYPE(arr.ptr()));
+}
+
+void check_type(numeric::array arr, PyArray_TYPES expected_type)
+{
+    PyArray_TYPES actual_type = type(arr);
+    if (actual_type != expected_type)
+    {
+        std::ostringstream stream;
+        stream << "expected numeric type " << kindstrings[expected_type]
+               << ", found numeric type " << kindstrings[actual_type]
+               << std::ends;
+        PyErr_SetString(PyExc_TypeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+
+// Return the number of dimensions
+int rank(numeric::array arr)
+{
+    //std::cout << "inside rank" << std::endl;
+    if (!PyArray_Check(arr.ptr()))
+    {
+        PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+        throw_error_already_set();
+    }
+    return PyArray_NDIM(arr.ptr());
+}
+
+void check_rank(numeric::array arr, int expected_rank)
+{
+    int actual_rank = rank(arr);
+    if (actual_rank != expected_rank)
+    {
+        std::ostringstream stream;
+        stream << "expected rank " << expected_rank
+               << ", found rank " << actual_rank
+               << std::ends;
+        PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+
+intp size(numeric::array arr)
+{
+    if (!PyArray_Check(arr.ptr()))
+    {
+        PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+        throw_error_already_set();
+    }
+    return PyArray_Size(arr.ptr());
+}
+
+void check_size(numeric::array arr, intp expected_size)
+{
+    intp actual_size = size(arr);
+    if (actual_size != expected_size)
+    {
+        std::ostringstream stream;
+        stream << "expected size " << expected_size
+               << ", found size " << actual_size
+               << std::ends;
+        PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+
+std::vector<intp> shape(numeric::array arr)
+{
+    std::vector<intp> out_dims;
+    if (!PyArray_Check(arr.ptr()))
+    {
+        PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+        throw_error_already_set();
+    }
+    int* dims_ptr = PyArray_DIMS(arr.ptr());
+    int the_rank = rank(arr);
+    for (int i = 0; i < the_rank; i++)
+    {
+        out_dims.push_back(*(dims_ptr + i));
+    }
+    return out_dims;
+}
+
+
+intp get_dim(boost::python::numeric::array arr, int dimnum)
+{
+    assert(dimnum >= 0);
+    int the_rank = rank(arr);
+    if (the_rank < dimnum)
+    {
+        std::ostringstream stream;
+        stream << "Error: asked for length of dimension " << dimnum
+               << " but rank of array is " << the_rank
+               << std::ends;
+        PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    std::vector<intp> actual_dims = shape(arr);
+    return actual_dims[dimnum];
+}
+
+void check_shape(boost::python::numeric::array arr, std::vector<intp> expected_dims)
+{
+    std::vector<intp> actual_dims = shape(arr);
+    if (actual_dims != expected_dims)
+    {
+        std::ostringstream stream;
+        stream << "expected dimensions " << vector_str(expected_dims)
+               << ", found dimensions " << vector_str(actual_dims)
+               << std::ends;
+        PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize)
+{
+    std::vector<intp> actual_dims = shape(arr);
+    if (actual_dims[dimnum] != dimsize)
+    {
+        std::ostringstream stream;
+        stream << "Error: expected dimension number "
+               << dimnum << " to be length " << dimsize
+               << ", but found length " << actual_dims[dimnum]
+               << std::ends;
+        PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+
+bool iscontiguous(numeric::array arr)
+{
+    // return arr.iscontiguous();
+    return PyArray_ISCONTIGUOUS(arr.ptr());
+}
+
+
+void check_contiguous(numeric::array arr)
+{
+    if (!iscontiguous(arr))
+    {
+        PyErr_SetString(PyExc_RuntimeError, "expected a contiguous array");
+        throw_error_already_set();
+    }
+    return;
+}
+
+void* data(numeric::array arr)
+{
+    if (!PyArray_Check(arr.ptr()))
+    {
+        PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+        throw_error_already_set();
+    }
+    return PyArray_DATA(arr.ptr());
+}
+
+
+// Copy data into the array
+void copy_data(boost::python::numeric::array arr, char* new_data)
+{
+    char* arr_data = (char *) data(arr);
+    intp nbytes = PyArray_NBYTES(arr.ptr());
+    for (intp i = 0; i < nbytes; i++)
+    {
+        arr_data[i] = new_data[i];
+    }
+    return;
+}
+
+// Return a clone of this array
+numeric::array clone(numeric::array arr)
+{
+    object obj(handle<>(PyArray_NewCopy((PyArrayObject*)arr.ptr(), PyArray_CORDER)));
+    return makeArray(obj);
+}
+
+// Return a clone of this array with a new type
+numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t)
+{
+    return (numeric::array) arr.astype(type2char(t));
+}
+
+
+int refcount(numeric::array arr)
+{
+    return REFCOUNT(arr.ptr());
+}
+
+
+std::vector<intp> strides(numeric::array arr)
+{
+    std::vector<intp> out_strides;
+    if (!PyArray_Check(arr.ptr()))
+    {
+        PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+        throw_error_already_set();
+    }
+    intp* strides_ptr = PyArray_STRIDES(arr.ptr());
+    intp the_rank = rank(arr);
+    for (intp i = 0; i < the_rank; i++)
+    {
+        out_strides.push_back(*(strides_ptr + i));
+    }
+    return out_strides;
+}
+
+
+void check_PyArrayElementType(object newo)
+{
+    PyArray_TYPES theType = PyArray_TYPES(PyArray_TYPE(newo.ptr()));
+    if (theType == PyArray_OBJECT)
+    {
+        std::ostringstream stream;
+        stream << "array elements have been cast to PyArray_OBJECT, "
+               << "numhandle can only accept arrays with numerical elements"
+               << std::ends;
+        PyErr_SetString(PyExc_TypeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+std::string type2string(PyArray_TYPES t_type)
+{
+    return kindstrings[t_type];
+}
+
+char type2char(PyArray_TYPES t_type)
+{
+    return kindchars[t_type];
+}
+
+PyArray_TYPES char2type(char e_type)
+{
+    return kindtypes[e_type];
+}
+
+
+template <class T>
+inline std::string vector_str(const std::vector<T>& vec)
+{
+    std::ostringstream stream;
+    stream << "(" << vec[0];
+    for (std::size_t i = 1; i < vec.size(); i++)
+    {
+        stream << ", " << vec[i];
+    }
+    stream << ")";
+    return stream.str();
+}
+
+inline void check_size_match(std::vector<intp> dims, intp n)
+{
+    intp total = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<intp>());
+    if (total != n)
+    {
+        std::ostringstream stream;
+        stream << "expected array size " << n
+               << ", dimensions give array size " << total
+               << std::ends;
+        PyErr_SetString(PyExc_TypeError, stream.str().c_str());
+        throw_error_already_set();
+    }
+    return;
+}
+
+
+
+} // namespace numpy_util
+

Added: cs/cigma/trunk/pysrc/numpy_util.h
===================================================================
--- cs/cigma/trunk/pysrc/numpy_util.h	                        (rev 0)
+++ cs/cigma/trunk/pysrc/numpy_util.h	2008-10-15 09:07:23 UTC (rev 13037)
@@ -0,0 +1,315 @@
+#ifndef __NUMPY_UTIL_H__
+#define __NUMPY_UTIL_H__
+
+#include <boost/python.hpp>
+#include <numpy/noprefix.h>
+#include <sstream>
+#include <vector>
+#include <numeric>
+#include <map>
+#include <complex>
+
+namespace numpy_util {
+
+
+//!
+/**
+ * A free function that extracts a PyArrayObject from any sequential PyObject.
+ * @param x a sequential PyObject wrapped in a Boost.Python 'object'.
+ * @return a PyArrayObject wrapped in a Boost.Python numeric array.
+ */
+boost::python::numeric::array makeArray(boost::python::object x);
+
+/**
+ * Creates a 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.
+ */
+boost::python::numeric::array makeArray(intp n, PyArray_TYPES t);
+
+
+/**
+ * Creates an 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 integers specifying 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.
+ */
+boost::python::numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t);
+
+
+/**
+ * Function template returns PyArray_Type for C++ type.
+ * See numpy_util.cpp for specializations.
+ * @param T C++ type
+ * @return numpy type enum
+ */
+template <typename T>
+PyArray_TYPES getEnum(void)
+{
+    PyErr_SetString(PyExc_ValueError, "no mapping available for this type");
+    boost::python::throw_error_already_set();
+    return PyArray_VOID;
+}
+
+
+/**
+ * Function template creates a one-dimensional numpy array of length n
+ * containing a copy of data at data*. See numpy_util.cpp::getEnum<T>() for a
+ * list of specializations.
+ * @param T   C type of data
+ * @param T*  data pointer to start of data
+ * @param n   an integer indicating the size of the array.
+ * @return a numpy array of size n with elements initialized to data.
+ */
+template <typename T>
+boost::python::numeric::array makeArray(T* data, intp n=0)
+{
+    boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>())));
+    void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
+    memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n);
+    return boost::python::extract<boost::python::numeric::array>(obj);
+}
+
+
+/**
+ * Function template creates an n-dimensional numpy array with
+ * dimensions dims containing a copy of values starting at data.
+ * See numpy_util.cpp::getEnum<T>() for a list of specializations.
+ * @param T   C type of data
+ * @param T*  data pointer to start of data
+ * @param n   an integer indicating the size of the array.
+ * @return a numpy array of size n with elements initialized to data.
+ */
+template <typename T>
+boost::python::numeric::array makeArray(T* data, std::vector<intp> dims)
+{
+    intp total = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<intp>());
+    boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(), &dims[0], getEnum<T>())));
+    void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
+    memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
+    return boost::python::extract<boost::python::numeric::array>(obj);
+}
+
+
+/**
+ * Creates a numpy array from a numpy array, referencing the data.
+ * @param arr a Boost.Python numeric array.
+ * @return a numeric array referencing the input array.
+ */
+boost::python::numeric::array makeArray(const boost::python::numeric::array& arr);
+
+
+/**
+ * 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 elements.
+ */
+PyArray_TYPES type(boost::python::numeric::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.
+ */
+void check_type(boost::python::numeric::array arr, PyArray_TYPES expected_types);
+
+
+/**
+ * A free function that retrieves the number of dimensions of a numpy array.
+ * @param arr a Boost.Python numeric array.
+ * @return an integer that indicates the rank of an array.
+ */
+int rank(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the actual rank is not equal to the expected rank.
+ * @param arr a Boost.Python numeric array.
+ * @param expected_rank expected rank of the numeric array.
+ */
+void check_rank(boost::python::numeric::array arr, int expected_rank);
+
+
+/**
+ * A free function that returns the total size of the array.
+ * @param arr a Boost.Python numeric array.
+ * @return an integer that indicates the total size of the array.
+ */
+intp size(boost::python::numeric::array arr);
+
+
+/**
+ * Throw an exception if the actual total size of the array is not equal
+ * to the expected size.
+ * @param arr a Boost.Python numeric array.
+ * @param expected_size the expected size of the array
+ */
+void check_size(boost::python::numeric::array arr, intp expected_size);
+
+
+/**
+ * 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<intp> shape(boost::python::numeric::array arr);
+
+
+/**
+ * Returns the size of a specific dimension.
+ * @param arr a Boost.Python numeric array.
+ * @param dimnum an integer that identifies the dimension to retrieve.
+ * @return the size of the requested dimension.
+ */
+intp get_dim(boost::python::numeric::array arr, int dimnum);
+
+
+/**
+ * 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 dimensions.
+ */
+void check_shape(boost::python::numeric::array arr, std::vector<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(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the array is not contiguous.
+ * @param arr a Boost.Python numeric array.
+ */
+void check_contiguous(boost::python::numeric::array arr);
+
+
+/**
+ * Returns a pointer to the data in the array.
+ * @param arr a Boost.Python numeric array.
+ * @return a char pointer pointing to the first element of the array.
+ */
+void* data(boost::python::numeric::array arr);
+
+
+/**
+ * Copies data into the array.
+ * @param arr a Boost.Python numeric array.
+ * @param new_data a char pointer referencing the new data.
+ */
+void copy_data(boost::python::numeric::array arr, char* new_data);
+
+
+/**
+ * Returns a clone of this array.
+ * @param arr a Boost.Python numeric array.
+ * @return a replicate of the Boost.Python numeric array.
+ */
+boost::python::numeric::array clone(boost::python::numeric::array arr);
+
+
+/**
+ * Returns a clone of this array with a new type.
+ * @return arr a Boost.Python numeric array.
+ * @param t PyArray_TYPES of the output array.
+ * @return a replicate of 'arr' with type set to 't'.
+ */
+boost::python::numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t);
+
+
+/**
+ * Returns the reference count of the array
+ * @param arr a Boost.Python numeric array.
+ * @return the reference count of the array.
+ */
+int refcount(boost::python::numeric::array arr);
+
+
+/**
+ * Returns the strides array in a vector of integers.
+ * @param arr a Boost.Python numeric array.
+ * @return the strides of the array.
+ */
+std::vector<intp> strides(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the element of a numpy array is type cast
+ * to PyArray_OBJECT.
+ * @param newo a Boost.Python object.
+ */
+void check_PyArrayElementType(boost::python::object newo);
+
+
+/**
+ * Mapping from a PyArray_TYPE to its corresponding name in string.
+ */
+typedef std::map<PyArray_TYPES, std::string> KindStringMap;
+
+
+/**
+ * Mapping from a PyArray_TYPE to its corresponding typeID in char.
+ */
+typedef std::map<PyArray_TYPES, char> KindCharMap;
+
+
+/**
+ * Mapping from a typeID to its corresponding PyArray_TYPE.
+ */
+typedef std::map<char, PyArray_TYPES> KindTypeMap;
+
+
+/**
+ * Converts a PyArray_TYPE to its name in string.
+ * @param t_type a PyArray_TYPES
+ * @return the corresponding name as a string.
+ */
+std::string type2string(PyArray_TYPES t_type);
+
+
+/**
+ * Converts a PyArray_TYPE to its single character typecode.
+ * @param t_type a PyArray_TYPES.
+ * @return the corresponding typecode as a char.
+ */
+char type2char(PyArray_TYPES t_type);
+
+
+/**
+ * Converts a single character typecode to its PyArray_TYPES.
+ * @param e_type a PyArray_TYPES typecode in char.
+ * @return its corresponding PyArray_TYPES.
+ */
+PyArray_TYPES char2type(char e_type);
+
+
+/**
+ * Constructs a string which contains a list of elements extracted
+ * from the input vector.
+ * @param vec a vector of any type.
+ * @return a string that lists the elements from the input vector.
+ */
+template <class T>
+inline std::string vector_str(const std::vector<T>& vec);
+
+
+/**
+ * Throws an exception if the total size computed from a vector of integers
+ * does not match with the expected size.
+ * @param dims an integer vector of dimensions.
+ * @param n an expected size.
+ */
+inline void check_size_match(std::vector<intp> dims, intp n);
+
+
+} // namespace numpy_util
+
+#endif



More information about the cig-commits mailing list