Fast numerical expression evaluator for NumPy that accelerates array operations through optimized implementations and multi-threading
—
Core functionality for evaluating mathematical expressions on NumPy arrays with performance optimization and multi-threading support. This module provides the primary interface for NumExpr's fast expression evaluation capabilities.
Evaluates mathematical expressions on arrays, providing substantial performance improvements over NumPy operations through optimized compilation and execution.
def evaluate(ex, local_dict=None, global_dict=None, out=None, order='K', casting='safe', **kwargs):
"""
Evaluate a mathematical expression on arrays.
This is the primary function for NumExpr evaluation, parsing and executing
mathematical expressions on NumPy arrays with automatic optimization and
multi-threading.
Parameters:
- ex (str): Mathematical expression string to evaluate
- local_dict (dict, optional): Dictionary mapping variable names to arrays/values
- global_dict (dict, optional): Global namespace for variable lookup
- out (ndarray, optional): Pre-allocated output array for result storage
- order (str): Memory layout order - 'K' (keep), 'A' (any), 'C' (C-order), 'F' (Fortran)
- casting (str): Casting safety level - 'no', 'equiv', 'safe', 'same_kind', 'unsafe'
- **kwargs: Additional keyword arguments for variable substitution
Returns:
ndarray: Result array from expression evaluation
Raises:
ValueError: If expression is invalid or variables are incompatible
TypeError: If array types cannot be safely cast
"""Usage Examples:
import numpy as np
import numexpr as ne
# Basic arithmetic with implicit variable lookup
a = np.arange(1000000)
b = np.arange(1000000)
result = ne.evaluate("a + b * 2")
# Complex mathematical expressions
result = ne.evaluate("sin(a) * exp(-b/1000) + sqrt(a*b)")
# With explicit variable dictionary
data = {'x': np.random.random(10000), 'y': np.random.random(10000)}
result = ne.evaluate("x**2 + y**2 < 1", local_dict=data)
# Using pre-allocated output array
out = np.empty(len(a))
ne.evaluate("a * 3.14159", out=out)
# With casting control
result = ne.evaluate("a + 1.5", casting='safe') # Ensures safe type conversionRe-evaluates the most recently compiled expression with new variable values, providing optimal performance for repeated evaluation patterns.
def re_evaluate(local_dict=None, **kwargs):
"""
Re-evaluate the previous expression with new variable values.
This function reuses the compiled representation of the last expression
evaluated with evaluate(), providing optimal performance when the same
expression needs to be computed with different data.
Parameters:
- local_dict (dict, optional): New local variable bindings
- **kwargs: New variable values as keyword arguments
Returns:
ndarray: Result of re-evaluation with new variables
Raises:
RuntimeError: If no previous expression exists to re-evaluate
ValueError: If new variables are incompatible with compiled expression
"""Usage Examples:
# Initial evaluation compiles the expression
a1 = np.arange(1000)
b1 = np.arange(1000)
result1 = ne.evaluate("a * b + 100")
# Re-evaluate with new data - much faster since expression is pre-compiled
a2 = np.arange(1000, 2000)
b2 = np.arange(2000, 3000)
result2 = ne.re_evaluate(local_dict={'a': a2, 'b': b2})
# Using keyword arguments
result3 = ne.re_evaluate(a=a2*2, b=b2*3)Arithmetic: +, -, *, /, %, **
Comparison: ==, !=, <, <=, >, >=
Logical: & (and), | (or), ~ (not)
Trigonometric: sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh, arcsinh, arccosh, arctanh
Exponential/Logarithmic: exp, expm1, log, log1p, log10
Power and Roots: sqrt, pow (same as **)
Rounding: ceil, floor
Other: abs/absolute, arctan2(y, x), fmod(x, y)
Complex Numbers: real, imag, complex(real, imag), conjugate
This allows natural variable usage without explicit dictionary passing in most cases.
Install with Tessl CLI
npx tessl i tessl/pypi-numexpr