CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-numexpr

Fast numerical expression evaluator for NumPy that accelerates array operations through optimized implementations and multi-threading

Pending
Overview
Eval results
Files

expression-evaluation.mddocs/

Expression Evaluation

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.

Capabilities

Primary Expression Evaluation

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 conversion

Expression Re-evaluation

Re-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)

Expression Language Features

Supported Operations

Arithmetic: +, -, *, /, %, **

  • Full NumPy broadcasting support
  • Automatic type promotion following NumPy rules
  • Integer division follows Python 3 semantics

Comparison: ==, !=, <, <=, >, >=

  • Element-wise comparison returning boolean arrays
  • String comparison supported for string arrays

Logical: & (and), | (or), ~ (not)

  • Bitwise operations on integer types
  • Logical operations on boolean arrays

Mathematical Functions

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

Data Types

  • bool: Boolean values (True/False)
  • int: 32-bit signed integers
  • long: 64-bit signed integers
  • float: 32-bit floating point
  • double: 64-bit floating point (default for literals)
  • complex: Complex numbers
  • str: String types for comparison operations

Performance Characteristics

  • Optimal Array Size: Arrays too large for L1 cache (typically > 10KB)
  • Speed Improvements: 0.95x to 15x faster than pure NumPy
  • Memory Usage: Significantly reduced due to elimination of intermediate arrays
  • Threading: Automatic parallelization across available CPU cores
  • Cache Utilization: Optimized through intelligent data chunking

Variable Scope Resolution

  1. kwargs parameters (highest priority)
  2. local_dict dictionary
  3. Calling frame locals (automatic inspection)
  4. global_dict dictionary
  5. Calling frame globals (lowest priority)

This allows natural variable usage without explicit dictionary passing in most cases.

Install with Tessl CLI

npx tessl i tessl/pypi-numexpr

docs

compiled-expressions.md

expression-analysis.md

expression-evaluation.md

index.md

threading-performance.md

vml-integration.md

tile.json