Quickstart
Installation
Install from PyPI:
pip install complex-expr-parser
For full functionality, install with optional dependencies:
# NumPy support (recommended for vectorized evaluation)
pip install complex-expr-parser[numpy]
# mpmath support (for special functions like gamma, zeta)
pip install complex-expr-parser[mpmath]
# All optional dependencies
pip install complex-expr-parser[all]
Basic Usage
The package provides three main functions:
parse_complex_function(expr_str)- Parse a string to a sympy expressionget_callable(expr_str)- Parse and return a callable functionvalidate_expression(expr_str)- Check if an expression is valid
Parsing Expressions
from complex_expr_parser import parse_complex_function
# Parse to sympy expression
expr = parse_complex_function("z^2 + 2z + 1")
print(expr) # z**2 + 2*z + 1
print(expr.factor()) # (z + 1)**2
Getting Callable Functions
from complex_expr_parser import get_callable
# Get a callable function
f = get_callable("sin(z)/z")
# Evaluate at a complex point
result = f(1 + 1j)
print(result) # (0.6349639147847361+0.2988350886551747j)
Validating User Input
from complex_expr_parser import validate_expression
# Valid expression
is_valid, error = validate_expression("z^2 + 1")
print(is_valid) # True
print(error) # None
# Invalid expression
is_valid, error = validate_expression("z +* 1")
print(is_valid) # False
print(error) # Error message
Supported Notations
The parser accepts many human-friendly notations:
Input |
Interpreted As |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Working with NumPy Arrays
The callable functions work with NumPy arrays for efficient vectorized computation:
import numpy as np
from complex_expr_parser import get_callable
# Create a complex grid
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = X + 1j * Y
# Evaluate function over the entire grid
f = get_callable("z^2 - 1")
result = f(Z) # Returns 100x100 complex array