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?
It is a bug as @jason commented:
https://github.com/pydata/numexpr/issues/442#issuecomment-1754515334
https://github.com/pandas-dev/pandas/issues/54449
https://github.com/pandas-dev/pandas/issues/54542
use ast instead
import ast
ast.literal_eval("0.+1.e+12j")
numexpr uses eval() which is a big security concern.
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)
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