[Numpy-discussion] NumPy C-API equivalent of np.float64()

Keith Goodman kwgoodman@gmail....
Wed Dec 29 11:44:58 CST 2010


On Wed, Dec 29, 2010 at 9:37 AM, Robert Bradshaw
<robertwb@math.washington.edu> wrote:
> On Wed, Dec 29, 2010 at 9:05 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
>> On Tue, Dec 28, 2010 at 11:22 PM, Robert Bradshaw
>> <robertwb@math.washington.edu> wrote:
>>> On Tue, Dec 28, 2010 at 8:10 PM, John Salvatier
>>> <jsalvati@u.washington.edu> wrote:
>>>> Wouldn't that be a cast? You do casts in Cython with <double>(expression)
>>>> and that should be the equivalent of float64 I think.
>>>
>>> Or even <numpy.float64_t >(expression) if you've cimported numpy
>>> (though as mentioned this is the same as double on every platform I
>>> know of). Even easier is just to use the expression in a the right
>>> context and it will convert it for you.
>>
>> That will give me a float object but it will not have dtype, shape,
>> ndim, etc methods.
>>
>>>> m = np.mean([1,2,3])
>>>> m
>>   2.0
>>>> m.dtype
>>   dtype('float64')
>>>> m.ndim
>>   0
>>
>> using <np.float64_t> gives:
>>
>> AttributeError: 'float' object has no attribute 'dtype'
>
> Well, in this case I doubt your'e going to be able to do much better
> than np.float64(expr), as the bulk or the time is probably spent in
> object allocation (and you're really asking for an object here). If
> you knew the right C calls, you might be able to get a 2x speedup.

A factor of 2 would be great! A tenth of a micro second is a lot of
overhead for small input arrays.

I'm guessing it is one of these functions but I don't understand the
signatures (nor ref counting):

PyObject* PyArray_Scalar(void* data, PyArray_Descr* dtype, PyObject* itemsize)

    Return an array scalar object of the given enumerated typenum and
itemsize by copying from memory pointed to by data . If swap is
nonzero then this function will byteswap the data if appropriate to
the data-type because array scalars are always in correct machine-byte
order.

PyObject* PyArray_ToScalar(void* data, PyArrayObject* arr)

    Return an array scalar object of the type and itemsize indicated
by the array object arr copied from the memory pointed to by data and
swapping if the data in arr is not in machine byte-order.


More information about the NumPy-Discussion mailing list