You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cython...@googlegroups.com
I'm learning about the array syntax and fused types. The goal of the following code is to provide a function that sums a 1-d numpy array of type int, float or complex (numpy's np.int32, np.float64 and np.complex128, resp.). Is there a better or more idiomatic way to do this with cython 0.16?
cdef my_type _sum(my_type[:] a): cdef int n = a.shape[0] cdef unsigned i cdef my_type total
total = 0 for i in range(n): total += a[i]
return total
def sum(a): """a must be a 1-d numpy array of type int32, float64 or complex128."""
if a.dtype == _np.float64: result = _sum[cython.double](a) elif a.dtype == _np.int32: result = _sum[cython.int](a) elif a.dtype == _np.complex128: result = _sum[cython.complex](a) else: raise ValueError('data type not implemented')
return result -----
Warren
mark florisson
unread,
Apr 27, 2012, 7:27:46 AM4/27/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cython...@googlegroups.com
I think the topic of the thread could be improved :) Anyway, currently
with 0.16 it does not automatically dispatch buffers or numpy arrays.
You can try out this branch though:
https://github.com/markflorisson88/cython/tree/_fused_dispatch_rebased , which does it automatically for you.
Warren Weckesser
unread,
Apr 27, 2012, 3:17:10 PM4/27/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
OK, thanks. For 0.16, is the dispatch function in the example a reasonable approach? It works for me, and it is far better than writing three almost identical functions or using a separate templating technique, so I'm happy with it. I'm just trying to make sure that I'm not missing any additional techniques or tricks that could be used (while sticking to the released version).
Warren
mark florisson
unread,
Apr 27, 2012, 4:41:12 PM4/27/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cython...@googlegroups.com
Yeah, there's not really any better way to do it. The only other way
would be to pass in a scalar of the same type as the dtype, but that
will likely fail in the face of promotion (i.e. it won't know whether
you want the 'float' or 'double' C type given a Python float object).
Warren Weckesser
unread,
Apr 27, 2012, 5:02:20 PM4/27/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message