[Numpy-discussion] casting

Neal Becker ndbecker2@gmail....
Mon Jan 14 06:08:52 CST 2008

Robert Kern wrote:

> Neal Becker wrote:
>> numpy frequently refers to 'casting'.  I'm not sure if that term is ever
>> defined.  I believe it has the same meaning as in C.  In that case, it is
>> unfortunately used to mean 2 different things.  There are casts that do
>> not change the underlying bits (such as a pointer cast), and there are
>> casts that actually convert to different bits (such as float -> double).
>> I think numpy means the latter.  When an array where the underlying data
>> is one type, a cast to another type means actually reallocating and
>> converting the data.
> Yes, that is usually what people mean when they use _casting_ in the
> context of numpy. It is the more frequently performed operation of the
> two. The former can be accomplished with the .view(dtype) method of
> ndarrays.
>> It often occurs that I have an algorithm that can take any integral type,
>> because it is written with c++ templates.  In that case, I don't want to
>> use PyArray_FROMANY, because I don't want to unecessarily convert the
>> array
>> data.  Instead, I'd like to inquire what is the preferred type of the
>> data.
>> The solution I'm exploring is to use a function I
>> call 'preferred_array_type'.  This uses the __array_struct__ interface to
>> find the native data type.  I chose to use this interface, because then
>> it will work with both numpy arrays and other array-like types.
>> Any thoughts on all of this?
> I'm not sure what you mean by "preferred type of the data". Do you mean
> the dtype of the array as it comes in? There are several functions and
> function macros in the numpy C API which take differing amounts of
> information. For example,
>   * PyArray_FROM_O(PyObject*onj) just takes an object.
>   * PyArray_FROM_OF(PyObject* obj, int req) takes an object and flags like
>   * PyArray_FROM_OT(PyObject* obj, int typenum) takes an object and a type
>     number.
>   * PyArray_FROM_OTF(PyObject* obj, int typenum, int req) takes an object,
>   type,
>     and flags.

Let me try again to explain.  I don't want to convert to some type first -
that would be a waste.  I need to find out what is the native data type of
the input array first.  Also, I'd like to allow that the input is not a
PyArray, but could be something conforming to __array_struct__ interface. 
So, I need to find the native data type _first_, _then_ call the
appropriate PyArray_FROM...

Further, I don't believe this requirement is unique.  I would think it would
be needed for any time when a user wants to create a function that can
accept a numpy array, and would like to avoid unnecessary data conversion. 
This is particularly true when the underlying function is using c++
templates to allow the data type to be a template parameter (and so can
operate on any - or a range - of data types).

More information about the Numpy-discussion mailing list