Sun Jan 13 21:23:55 CST 2008
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
* PyArray_FROM_OTF(PyObject* obj, int typenum, int req) takes an object, type,
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion