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

Keith Goodman kwgoodman@gmail....
Wed Dec 29 13:53:35 CST 2010


On Wed, Dec 29, 2010 at 11:43 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
> Hi,
>
>>> That might be because I'm not understanding you very well, but I was
>>> thinking that:
>>>
>>> cdef dtype descr = PyArray_DescrFromType(NPY_FLOAT64)
>>>
>>> would give you the float64 dtype that I thought you wanted?  I'm
>>> shooting from the hip here, in between nieces competing for the
>>> computer and my attention.
>>
>> I think I need a function. One that does this:
>>
>>>> n = 10.0
>>>> hasattr(n, 'ndim')
>>   False
>>>> m = np.float64(n)
>>>> hasattr(m, 'ndim')
>>   True
>
> Now the nieces have gone, I see that I did completely misunderstand.
> I think you want the C-API calls to be able to create a 0-dim ndarray
> object from a python float.
>
> There was a thread on C-API array creation on the cython list a little
> while ago:
>
> http://www.mail-archive.com/cython-dev@codespeak.net/msg07703.html
>
> Code in scipy here:
>
> https://github.com/scipy/scipy-svn/blob/master/scipy/io/matlab/mio5_utils.pyx
>
> See around line 36 there, and 432, and the header file I copied from
> Dag Sverre:
>
> https://github.com/scipy/scipy-svn/blob/master/scipy/io/matlab/numpy_rephrasing.h
>
> As you can see, it's a little horrible, in that you have to take care
> to get the references right to the dtype and to the data.  I actually
> did not investigate in detail whether this lower-level array creation
> was speeding my code up much.
>
> I hope that's more useful...

Wow! That's a mouthful of code. Yes, very handy to have an example to
work from. Thank you.


More information about the NumPy-Discussion mailing list