Is it safe in python numexpr to assign values to the same array you are operating on to avoid creating a temporary array?
From the description of memory usage on the project homepage it looks okay, but without diving into the source code, that is hardly a solid answer.
I tried the following which works fine, but I am hoping for confirmation from someone more familiar with this package:
import numpy as np
import numexpr as ne
a = np.ones(5)
b = a.copy()
ne.evaluate("a+b",out=a)
array([ 2., 2., 2., 2., 2.])
It works, because numexpr still uses temporary arrays internally, albeit in chunk sizes of 1024 elements (or 4096 if using VML). You can think of these chunks of the inputs as slices, though they are stored as appropriate C data types for speed and memory compactness during the evaluation. The results will be stored into the out parameter after the calculation of each chunk is performed, otherwise it must allocate an array of of the same size as the inputs.
Check out the section Why It Works for pseudocode of how numexpr evaluates vectorized arithmetic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With