<div dir="ltr"><div><br></div>You can always write your own gufunc with signature '(),(),()->(a, a)', and write a Python wrapper that always call it with an `out=` parameter of shape (..., 3, 3), something along the lines of:<div>

<br></div><div><font face="courier new, monospace">def my_wrapper(a, b, c, out=None):</font></div><div><font face="courier new, monospace">    if out is None:</font></div><div><font face="courier new, monospace">        out = np.empty(np.broadcast(a,b,c).shape + (3, 3))</font></div>

<div><font face="courier new, monospace">    if out.shape[-2:] != (3, 3):</font></div><div><font face="courier new, monospace">        raise ValueError("Wrong shape for 'out'")</font></div><div><font face="courier new, monospace">    return my_gufunc(a, b, c, out=out)</font></div>

<div><font face="courier new, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">Writing your own gufunc is a little challenging, but not that hard with all the examples now available in the numpy.linalg code base, and it is a terribly powerful tool.</font></div>
<div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Jaime</font></div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Aug 22, 2014 at 4:40 PM, James Crist <span dir="ltr"><<a href="mailto:crist042@umn.edu" target="_blank">crist042@umn.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>I suspected as much. This is actually part of my work on numerical evaluation in SymPy. In its current state compilation to C and autowrapping *works*, but I think it could definitely be more versatile/efficient. Since numpy seemed to have solved the broadcasting/datatype issues internally I hoped I could reuse this. <br>

<br>I'll look into nditer (as this is for a library, it's better to do it *right* than do it now), but right now it's looking like I may need to implement this functionality myself...<br><br></div>Thanks,<br>
<br>
</div>-Jim<br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 21, 2014 at 4:50 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>On Thu, Aug 21, 2014 at 2:34 AM, James Crist <<a href="mailto:crist042@umn.edu" target="_blank">crist042@umn.edu</a>> wrote:<br>


> All,<br>
><br>
> I have a C function func that takes in scalar arguments, and an array of<br>
> fixed dimension that is modified in place to provide the output. The<br>
> prototype is something like:<br>
><br>
> `void func(double a, double b, double c, double *arr);`<br>
><br>
> I've wrapped this in Cython and called it from python with no problem. What<br>
> I'd like to do now though is get it set up to broadcast over the input<br>
> arguments and return a 3 dimensional array of the results. By this I mean<br>
><br>
> a = array([1, 2, 3])<br>
> b = array([2.0, 3.0, 4.0])<br>
> c = array([3, 4, 5])<br>
><br>
> func(a, b, c) -> a 3d array containing the results of func for (a, b, c) =<br>
> (1, 2.0, 3), (2, 3.0, 4), (3, 4.0, 5)<br>
><br>
> I'm not sure if this would qualify as a ufunc, as the result of one function<br>
> call isn't a scalar but an array, but the effect I'm looking for is similar.<br>
> Ideally it would handle datatype conversions (in the above `a` and `c`<br>
> aren't double, but `func` takes in double). It would also be awesome to<br>
> allow an argument to be a scalar and not an array, and have it be broadcast<br>
> as if it were.<br>
><br>
> I'm just wondering what the best way for me to hook my code up to the<br>
> internals of numpy and get this kind of behavior in an efficient way. I've<br>
> read the "writing your own ufunc" part of the docs, but am unsure if what<br>
> I'm looking for qualifies. Note that I can change the inner workings of<br>
> `func` if this is required to achieve this behavior.<br>
<br>
</div></div>I don't think it's currently possible to write a ufunc that maps<br>
scalars to fixed-length arrays.<br>
<br>
There's probably some not-too-terrible way to do this using the C<br>
nditer interface, but IIRC the docs aren't for that aren't very<br>
helpful.<br>
<br>
The simplest approach is just to do a bit of work at the Python level<br>
using the standard numpy API to do error-checking and set up your<br>
arrays in the way you want, and then pass them to the C<br>
implementation. This is not the best way to get a Right solution that<br>
will handle all the funky corner cases that ufuncs handle, but it's by<br>
far the fastest way to get something that's good enough for whatever<br>
you need Right Now.<br>
<span><font color="#888888"><br>
-n<br>
<br>
--<br>
Nathaniel J. Smith<br>
Postdoctoral researcher - Informatics - University of Edinburgh<br>
<a href="http://vorpus.org" target="_blank">http://vorpus.org</a><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</font></span></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
</div>