[Numpy-discussion] performing operations in-place in numpy

Charles R Harris charlesr.harris@gmail....
Wed Jul 8 17:10:31 CDT 2009


On Wed, Jul 8, 2009 at 3:57 PM, Citi, Luca <lciti@essex.ac.uk> wrote:

> Hi Stefan,
> I am afraid I did not explain myself clear enough.
>
> Of course
> c = a + b + d
> leaves a, b, and d unchanged.
> The only array that is overwritten is (a+b) which is a temporary
> array that would be destroyed anyway.
> Normally the operation above is performed like this:
> 1) allocation of a temporary array f
> 2) execution of sum(a, b, out=f)
> 3) allocation of a temporary array g
> 4) execution of sum(f, d, out=g)
> 5) assignment of g to c
> 6) deallocation of f
> With my method it is performed like this:
> 1) allocation of a temporary array f
> 2) execution of sum(a, b, out=f)
> 3) execution of sum(f, d, out=f)
> 4) assignment of f to c
>
> When I write
> "The approach works in most cases (it passes
> most of the numpy tests) but makes the assumption that ufuncs can work
> even if one of the inputs is used as output."
> I mean that the core ufunc, the atomic operation working on the
> single array element, must be able to work even if one of the
> input is used as output.
> As far as I can tell, they already work like this.
> Can we expect someone implementing a new ufunc to keep that in mind?
>

Yes, the last thing the happens in a ufunc is the assignment to the output.
This should probably be a documented and required behaviour. The compiler
also has to assume that the pointer values can be aliased, so you should be
safe against optimizations.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090708/01b018e1/attachment.html 


More information about the NumPy-Discussion mailing list