Question

I'm trying to use Numexpr to make a fast Vector Norm function to compare with Numpy's. I try the following:

import numexpr as ne
import numpy as np

def L2_Norm(vector_in):
    vector1 = ne.evaluate("abs(vector_in)")
    vector2 = ne.evaluate("vector1**2")
    vector3 = ne.evaluate("sum(vector2)")
    vector_out = ne.evaluate("sqrt(vector3)")
    return vector_out`

ve = np.arange(10)
L2_Norm(ve)

and I get this:

File "C:\Folder1\Folder2\src\test.py", line 11, in L2_Norm
  vector3 = ne.evaluate("sum(vector2)")<br>
File "C:\Python27\lib\site-packages\numexpr\necompiler.py", line 701, in evaluate
  a = global_dict[name]<br>
KeyError: 'a'

I basically followed the same steps on their User Guide (which seems to be the only reference around). The only clue i have is this:

umexpr's principal routine is this:
evaluate(ex, local_dict=None, global_dict=None, **kwargs)

where ex is a string forming an expression, like "2*a+3*b". The values for a and b will by default be taken from the calling function's frame (through the use of sys._getframe()). Alternatively, they can be specified using the local_dict or global_dict arguments, or passed as keyword arguments

... which I don't really understand - I assume the author kept it simple because the package is simple. What have I overlooked?

Was it helpful?

Solution

Turns out the "local_dict=None, global_dict=None" parameters aren't default afterall. You need to specifically add them into your numexpr.evaluate function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top