[C++-sig] Re: Creating an array in C++

Jonathan Brandmeyer jbrandmeyer at earthlink.net
Thu Feb 26 00:07:48 CET 2004


On Wed, 2004-02-25 at 15:06, Philip Austin wrote:
> David Abrahams writes:
>  > 
>  > Excuse me?  Alpha?!
>  > 
>  > It may be missing a couple of features, but it's hardly alpha
>  > software.
> 
> Sorry, that was a careless choice of words.  We use numeric::array
> daily without a hitch, but like Marc-Alexis I'm not sure how to create
> an empty array of arbitray shape and type using the boost python
> array constructor.
> 
> > What python argument value do you expect to be passed to the python
> > object's __init__ function?
> 
> Our most common requirement is to be able to do something like
> 
> theArray=numarray.array(shape=(300,),type=numarray.UInt8)
> 
> from the C++ side, and then fill it with values returned
> from C or Fortran.
> 

num_util has that capability.  You would create it with this:

/** 
Creates a n-dimensional Numeric array with dimensions dimens and Numeric
type t. The elements of the array are initialized to zero.
@param dimens a vector of interger specifies the dimensions of the 
    array.
@param t elements' Numeric type. Default is double.
@return a numeric array of shape dimens with elements initialized to 
    zero.
*/
boost::python::numeric::array 
makeNum(std::vector<int> dimens, PyArray_TYPES t = PyArray_DOUBLE);


Num util also has some functions that let you directly manipulate the
underlying data array and functions that create and initialize an array
with preexisting data.  

One word of warning, though, you will have to ensure sure that any
arrays passed from Python to C++ through the Numeric interface meet your
requirements for shape, size, continuity, and data type.

HTH,
Jonathan Brandmeyer






More information about the Cplusplus-sig mailing list