Thouis (Ray) Jones | 12 Jun 2012 16:12
Picon
Gravatar

having trouble with fused types and ndarray of arbitrary dimensions

I would like to write a function using ndarrarys of fused types.  The
only restriction I would like on the input arrays is that they be
broadcastable.

My first attempt was:
    ctypedef fused fused1:
        np.int32_t
        np.int64_t

    ctypedef fused fused2:
        np.int32_t
        np.int64_t
        np.float64_t
        np.float32_t

    def labeled_reduce(np.ndarray[fused1] labels, np.ndarray[fused2]
values, ops)
       print "called with", labels.dtype, values.dtype, ops

But that requires that labels and values be 1D.

I ended up writing it like this:
    def _labeled_reduce(np.ndarray labels, np.ndarray values, fused1
labelexample, fused2 valueexample, ops=0):
       print "called with", labels.dtype, values.dtype, ops

    def labeled_reduce(np.ndarray labels, np.ndarray values, ops):
       typemap = {np.float32 : 'float32_t',
                  np.float64 : 'float64_t',
                  np.int32   : 'int32_t',
(Continue reading)

Thouis (Ray) Jones | 14 Jun 2012 23:17
Picon
Gravatar

Re: having trouble with fused types and ndarray of arbitrary dimensions

Answering my own question.  Found it here:
https://groups.google.com/d/topic/cython-users/OfAUydo_o0U/discussion

On Tue, Jun 12, 2012 at 4:12 PM, Thouis (Ray) Jones <thouis <at> gmail.com> wrote:
> I would like to write a function using ndarrarys of fused types.  The
> only restriction I would like on the input arrays is that they be
> broadcastable.
>
> My first attempt was:
>    ctypedef fused fused1:
>        np.int32_t
>        np.int64_t
>
>    ctypedef fused fused2:
>        np.int32_t
>        np.int64_t
>        np.float64_t
>        np.float32_t
>
>    def labeled_reduce(np.ndarray[fused1] labels, np.ndarray[fused2]
> values, ops)
>       print "called with", labels.dtype, values.dtype, ops
>
> But that requires that labels and values be 1D.
>
> I ended up writing it like this:
>    def _labeled_reduce(np.ndarray labels, np.ndarray values, fused1
> labelexample, fused2 valueexample, ops=0):
>       print "called with", labels.dtype, values.dtype, ops
>
(Continue reading)


Gmane