Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python numexpr: Evaluating complex numbers in scientific notation yields 'ValueError'

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?

like image 941
Chipmuenk Avatar asked Feb 03 '26 15:02

Chipmuenk


2 Answers

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.

like image 124
Talha Tayyab Avatar answered Feb 06 '26 04:02

Talha Tayyab


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)
like image 40
CodeByAidan Avatar answered Feb 06 '26 04:02

CodeByAidan