NumPy C-API equivalent of np.float64()
I'm looking for the C-API equivalent of the np.float64 function, something that I could use inline in a Cython function. I don't know how to write the function. Anyone have one sitting around? I'd like to use it, if it is faster than np.float64 (np.int32, np.float32, ...) in the Bottleneck package when the output is a scalar, for example bn.median(arr, axis=None).
Wouldn't that be a cast? You do casts in Cython with <double>(expression) and that should be the equivalent of float64 I think. On Tue, Dec 28, 2010 at 3:32 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
I'm looking for the C-API equivalent of the np.float64 function, something that I could use inline in a Cython function.
I don't know how to write the function. Anyone have one sitting around? I'd like to use it, if it is faster than np.float64 (np.int32, np.float32, ...) in the Bottleneck package when the output is a scalar, for example bn.median(arr, axis=None). _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
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. - Robert
On Tue, Dec 28, 2010 at 3:32 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
I'm looking for the C-API equivalent of the np.float64 function, something that I could use inline in a Cython function.
I don't know how to write the function. Anyone have one sitting around? I'd like to use it, if it is faster than np.float64 (np.int32, np.float32, ...) in the Bottleneck package when the output is a scalar, for example bn.median(arr, axis=None). _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
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.
m = np.mean([1,2,3]) m 2.0 m.dtype
That will give me a float object but it will not have dtype, shape, ndim, etc methods. dtype('float64')
m.ndim 0
using <np.float64_t> gives: AttributeError: 'float' object has no attribute 'dtype'
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. - Robert
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.
Hi, On Wed, Dec 29, 2010 at 5:37 PM, 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'
Forgive me if I haven't understood your question, but can you use PyArray_DescrFromType with e.g NPY_FLOAT64 ? Best, Matthew
On Wed, Dec 29, 2010 at 9:48 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
Hi,
On Wed, Dec 29, 2010 at 5:37 PM, 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'
Forgive me if I haven't understood your question, but can you use PyArray_DescrFromType with e.g NPY_FLOAT64 ?
I'm pretty hopeless here. I don't know how to put all that together in a function.
Forgive me if I haven't understood your question, but can you use PyArray_DescrFromType with e.g NPY_FLOAT64 ?
I'm pretty hopeless here. I don't know how to put all that together in a function.
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. See you, Matthew
On Wed, Dec 29, 2010 at 10:13 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
Forgive me if I haven't understood your question, but can you use PyArray_DescrFromType with e.g NPY_FLOAT64 ?
I'm pretty hopeless here. I don't know how to put all that together in a function.
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
np.float64 is fast, just hoping someone had a C-API inline version of np.float64() that is faster.
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.py... 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_rephras... 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... Matthew
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.py...
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_rephras...
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.
Keith Goodman wrote:
np.float64 is fast, just hoping someone had a C-API inline version of np.float64() that is faster.
You're looking for PyArrayScalar_New and _ASSIGN. See https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/arraysca... Undocumented (bad), but AFAIK public.
On Wed, Dec 29, 2010 at 11:54 AM, Pauli Virtanen <pav@iki.fi> wrote:
Keith Goodman wrote:
np.float64 is fast, just hoping someone had a C-API inline version of np.float64() that is faster.
You're looking for PyArrayScalar_New and _ASSIGN. See https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/arraysca...
Undocumented (bad), but AFAIK public.
Those look nice. I'm stuck since I can't cimport them. I'll have to read up on how to tell cython about those functions.
participants (5)
-
John Salvatier
-
Keith Goodman
-
Matthew Brett
-
Pauli Virtanen
-
Robert Bradshaw