Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numexpr.evaluate("a+b",out=a)

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.])
like image 729
David Avatar asked Mar 14 '13 21:03

David


1 Answers

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.

like image 72
mtadd Avatar answered Nov 20 '22 01:11

mtadd