I am using the Python numexpr module to evaluate user inputs (numeric or formula). Numbers can be complex, and this works as long as I'm avoiding scientific notation:
>>> import numexpr as ne
>>> ne.evaluate("1000000000000j")
array(0.+1.e+12j)
>>> ne.evaluate("0.+1.e+12j")
ValueError: Expression 0.+1.e+12j has forbidden control characters.
I tried to evaluate complex numbers in scientific notation with some variations and was expecting numexpr to process these numbers. However, I always ended up with the ValueError described above.
Is this a bug or am I doing something wrong?
The errors you see with numexpr
when trying to evaluate the expression "0.+1.e+12j"
are due to the fact that numexpr
parses complex numbers differently than standard Python. It does not accept "0.+1.e+12j"
as valid because it prefers expressions where operations between numerical and logical units are explicitly defined.
However, if you reformify the expression to "1.e+12 * 1j"
, numexpr
handles it correctly because this option explicitly separates the scalar and imaginary units by the multiplication operation, which numexpr
can be handled effectively:
>>> ne.evaluate("1.e+12 * 1j")
array(0.+1.e+12j)