[Numpy-discussion] inplace operations
Travis Oliphant
oliphant at ee.byu.edu
Fri Jan 26 18:34:07 EST 2007
BBands wrote:
>If I have a NumPy array like so:
>
>[[1, 12],
> [2, 13],
> [3, 14],
> [4, 15],
> [5, 16],
> [6, 15],
> [7, 14]]
>
>How can I do an inplace diff, ending up with this?
>
>[[1, 0],
> [2, 1],
> [3, 1],
> [4, 1],
> [5, 1],
> [6, -1],
> [7, -1]]
>
>
Probably can be done (but it's a bit tricky because you have to be
careful not to write over the result before you need it to calculate the
difference).
Let 'a' be the array you've given
Try:
b = a[:,1]
b[:0:-1] -= b[5::-1]
b[0] = 0
>Also, can I covert to natural logs in place?
>
>[[1, 2.4849],
> [2, 2.5649],
> [3, 2.6391],
> [4, 2.7081],
> [5, 2.7726],
> [6, 2.7081],
> [7, 2.5649]]
>
>
Definitely *can't* be done because you are changing from integers to
double-precision.
If the original array is double precision than,
numpy.log(a[:,1],a[:,1])
should do it (Makes use of the output argument that all ufuncs accept).
-Travis
More information about the NumPy-Discussion
mailing list