<div dir="ltr"><div><div><div>I didn't notice identity before.  Seems like frompyfunc always sets it to None.  If it were zero maybe it would work as desired here.<br><br></div>In the writing your own ufunc doc, I was wondering if the pointer to data could be used to get a constant at runtime.  If not, what could that be used for?<br><pre><span class="gmail-k">static</span> <span class="gmail-kt">void</span> <span class="gmail-nf">double_logit</span><span class="gmail-p">(</span><span class="gmail-kt">char</span> <span class="gmail-o">**</span><span class="gmail-n">args</span><span class="gmail-p">,</span> <span class="gmail-n">npy_intp</span> <span class="gmail-o">*</span><span class="gmail-n">dimensions</span><span class="gmail-p">,</span>
                            <span class="gmail-n">npy_intp</span><span class="gmail-o">*</span> <span class="gmail-n">steps</span><span class="gmail-p">,</span> <span class="gmail-kt">void</span><span class="gmail-o">*</span> <span class="gmail-n">data</span><span class="gmail-p">)</span></pre></div>Why would the numerical accuracy be any different?  The subtraction and square operations look identical and I thought np.sum just calls np.add.reduce, so the reduction step uses the same code and would therefore have the same accuracy.<br><br></div>Thanks<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 4, 2016 at 1:56 PM, Sebastian Berg <span dir="ltr"><<a href="mailto:sebastian@sipsolutions.net" target="_blank">sebastian@sipsolutions.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Fr, 2016-11-04 at 13:11 -0400, Matthew Harrigan wrote:<br>
> I was reading this and got thinking about if a ufunc could compute<br>
> the sum of squared differences in a single pass without a temporary<br>
> array.  The python code below demonstrates a possible approach.<br>
><br>
> import numpy as np<br>
> x = np.arange(10)<br>
> c = 1.0<br>
> def add_square_diff(x1, x2):<br>
>     return x1 + (x2-c)**2<br>
> ufunc = np.frompyfunc(add_square_diff, 2, 1)<br>
> print(ufunc.reduce(x) - x[0] + (x[0]-c)**2)<br>
> print(np.sum(np.square(x-c)))<br>
><br>
> I have (at least) 4 questions:<br>
> 1. Is it possible to pass run time constants to a ufunc written in C<br>
> for use in its inner loop, and if so how?<br>
<br>
</span>I don't think its anticipated, since a ufunc could in most cases use a<br>
third argument, but a 3 arg ufunc can't be reduced. Not sure if there<br>
might be some trickery possible.<br>
<span class=""><br>
> 2. Is it possible to pass an initial value to reduce to avoid the<br>
> clean up required for the first element?<br>
<br>
</span>This is the identity normally. But the identity can only be 0, 1 or -1<br>
right now I think. The identity is what the output array gets<br>
initialized with (which effectively makes it the first value passed<br>
into the inner loop).<br>
<span class=""><br>
> 3. Does that ufunc work, or are there special cases which cause it to<br>
> fall apart?<br>
> 4. Would a very specialized ufunc such as this be considered for<br>
> incorporating in numpy since it would help reduce time and memory of<br>
> functions already in numpy?<br>
><br>
<br>
</span>Might be mixing up things, however, IIRC the single pass approach has a<br>
bad numerical accuracy, so that I doubt that it is a good default<br>
algorithm.<br>
<br>
- Sebastian<br>
<br>
<br>
> Thank you,<br>
> Matt<br>
> ______________________________<wbr>_________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
> <a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
<br></blockquote></div><br></div>