API Reference
This page contains the full API documentation for complex-expr-parser.
Module Functions
- complex_expr_parser.parse_complex_function(expr_str)[source]
Parse a complex function string into a sympy expression.
This is a convenience function that creates a parser and parses the given expression in one call.
- Parameters:
expr_str (str) – Human-friendly function string.
- Returns:
A sympy expression with
zas the variable.- Raises:
ValueError – If the expression cannot be parsed.
- Return type:
Expr
Example
>>> parse_complex_function("z^2 + 1") z**2 + 1 >>> parse_complex_function("sin(z)/z") sin(z)/z >>> parse_complex_function("e^z") exp(z)
- complex_expr_parser.get_callable(expr_str, use_numpy=True)[source]
Parse and return a callable function for the given expression.
This is a convenience function that parses an expression and converts it to a callable in one step.
- Parameters:
- Returns:
A callable function
f(z) -> complexthat evaluates the expression.- Return type:
Example
>>> f = get_callable("z^2 + 1") >>> f(1+1j) (1+2j) >>> import numpy as np >>> f(np.array([1, 1j, -1])) array([2.+0.j, 0.+0.j, 2.+0.j])
- complex_expr_parser.validate_expression(expr_str)[source]
Validate a complex function expression without fully evaluating it.
This function checks if an expression can be parsed and converted to a callable function. Useful for input validation in user interfaces.
- Parameters:
expr_str (str) – Human-friendly function string.
- Returns:
A tuple of
(is_valid, error_message). If valid,error_messageisNone.- Return type:
Example
>>> validate_expression("z^2 + 1") (True, None) >>> validate_expression("z +* 1") (False, "Could not parse expression: 'z +* 1'...") >>> validate_expression("") (False, "Expression cannot be empty")
ComplexFunctionParser Class
- class complex_expr_parser.ComplexFunctionParser[source]
Bases:
objectFault-tolerant parser for complex functions.
Accepts various human-friendly notations and converts to sympy expressions.
Supported notations:
Power notation:
z^2orz**2Implicit multiplication:
2z,z(z+1),(z+1)(z-1)Absolute value:
|z|->Abs(z)Conjugate:
z*->conjugate(z)(note:z**is power)Unicode:
pi-> pi,sqrt-> sqrt,oo-> infinity- Function aliases:
ln->logarcsin/arccos/arctan->asin/acos/atanRe/real->re,Im/imag->imphase/angle->argconj->conjugate
Imaginary unit:
iorj->IEuler’s number: standalone
e->E
Example
>>> parser = ComplexFunctionParser() >>> expr = parser.parse("2z^2 + 3z + 1") >>> print(expr) 2*z**2 + 3*z + 1 >>> f = parser.to_callable(expr) >>> f(1+1j) (5+8j)
- __init__()[source]
Initialize the parser with sympy transformations and namespace.
- Return type:
None
- parse(expr_str)[source]
Parse a human-friendly complex function string into a sympy expression.
This method attempts multiple parsing strategies to handle various input formats gracefully.
- Parameters:
expr_str (str) – String representation of the function.
- Returns:
A sympy expression with
zas the variable.- Raises:
ValueError – If parsing fails with all strategies.
- Return type:
Expr
Example
>>> parser = ComplexFunctionParser() >>> parser.parse("z^2 + 1") z**2 + 1 >>> parser.parse("sin(z)/z") sin(z)/z >>> parser.parse("|z|") Abs(z)
- to_callable(expr, use_numpy=True)[source]
Convert sympy expression to a callable function.
- Parameters:
expr (Expr) – A sympy expression.
use_numpy (bool) – If True, use numpy for vectorized evaluation (faster). If False or if special functions are needed, use mpmath.
- Returns:
A callable that takes complex numbers (or numpy arrays) and returns complex numbers.
- Return type:
Note
Special functions like
gammaandzetaautomatically use mpmath for evaluation regardless of theuse_numpysetting.Example
>>> parser = ComplexFunctionParser() >>> expr = parser.parse("z^2") >>> f = parser.to_callable(expr) >>> f(1+1j) 2j >>> import numpy as np >>> f(np.array([1, 1j, -1])) array([ 1.+0.j, -1.+0.j, 1.+0.j])
Module Constants
z
- complex_expr_parser.z: sympy.Symbol
The complex variable symbol used in parsed expressions.
Example:
from complex_expr_parser import z, parse_complex_function expr = parse_complex_function("z^2") # expr is equivalent to z**2
KNOWN_FUNCTIONS
- complex_expr_parser.KNOWN_FUNCTIONS: list[str]
List of recognized function names that the parser handles specially. These function names are protected from implicit multiplication (e.g.,
sin(z)won’t becomes*i*n*(z)).Includes:
Trigonometric:
sin,cos,tan,asin,acos,atanHyperbolic:
sinh,cosh,tanh,asinh,acosh,atanhExponential/Log:
exp,log,ln,sqrtComplex:
Abs,abs,conjugate,conj,re,im,argSpecial:
gamma,zeta,factorial,binomialAliases:
arcsin,arccos,arctan,Re,Im,real,imag,phase,angle