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:

  1. parse_complex_function(expr_str) - Parse a string to a sympy expression

  2. get_callable(expr_str) - Parse and return a callable function

  3. validate_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

z^2

z**2

2z

2*z

z(z+1)

z*(z+1)

(z+1)(z-1)

(z+1)*(z-1)

|z|

Abs(z)

z*

conjugate(z)

i or j

I (imaginary unit)

e^z

exp(z)

ln(z)

log(z)

arcsin(z)

asin(z)

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