Neal Becker wrote: > 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 >> NPY_CONTIGUOUS. >> * 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...
I'm sorry, I still think we're talking past each other. What do you mean by "native data type"? If you just want to get an ndarray without specifying a type, use PyArray_FROM_O(). That's what it's for. You don't need to know the data type beforehand. -- Robert Kern "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 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion