Calculus Functions Reference¶
Symbolic and numerical calculus operations.
Symbolic Differentiation¶
diff(expr, var)¶
Compute the symbolic derivative.
Arguments:
expr(ExprType): S-expression to differentiatevar(str): Variable to differentiate with respect to
Returns: Symbolic derivative expression
Example:
diff(['^', 'x', 3], 'x') # ['*', 3, ['^', 'x', 2]]
diff(['sin', 'x'], 'x') # ['cos', 'x']
diff(['exp', ['^', 'x', 2]], 'x') # Chain rule applied
gradient(expr, vars)¶
Compute the gradient (vector of partial derivatives).
Arguments:
expr(ExprType): S-expressionvars(List[str]): List of variables
Returns: List of symbolic partial derivatives
Example:
expr = ['+', ['^', 'x', 2], ['*', 'x', 'y']]
grad = gradient(expr, ['x', 'y'])
# grad[0] = df/dx
# grad[1] = df/dy
hessian(expr, vars)¶
Compute the Hessian matrix (second partial derivatives).
Arguments:
expr(ExprType): S-expressionvars(List[str]): List of variables
Returns: 2D list of symbolic second derivatives
Example:
jacobian(exprs, vars)¶
Compute the Jacobian matrix for a vector-valued function.
Arguments:
exprs(List[ExprType]): List of S-expressions [f1, f2, ...]vars(List[str]): List of variables
Returns: 2D list where J[i][j] = dfi/dxj
laplacian(expr, vars)¶
Compute the Laplacian (sum of second partial derivatives).
Arguments:
expr(ExprType): S-expressionvars(List[str]): List of variables
Returns: Symbolic Laplacian expression
Symbolic Integration¶
integrate(expr, var)¶
Compute the symbolic antiderivative.
Arguments:
expr(ExprType): S-expression to integratevar(str): Integration variable
Returns: Symbolic antiderivative, or ['int', expr, var] if no closed form
Example:
Simplification¶
simplify(expr)¶
Simplify an expression algebraically.
Arguments:
expr(ExprType): S-expression to simplify
Returns: Simplified expression
Example:
simplify(['+', 'x', 0]) # 'x'
simplify(['*', 'x', 1]) # 'x'
simplify(['^', 'x', 0]) # 1
simplify(['*', 'x', 'x']) # ['^', 'x', 2]
Numerical Evaluation¶
diff_at(expr, var, env)¶
Evaluate derivative at a point.
Arguments:
expr(ExprType): S-expressionvar(str): Variable to differentiateenv(Dict[str, Any]): Variable bindings
Returns: float
gradient_at(expr, vars, env)¶
Evaluate gradient at a point.
Arguments:
expr(ExprType): S-expressionvars(List[str]): List of variablesenv(Dict[str, Any]): Variable bindings
Returns: numpy.ndarray
hessian_at(expr, vars, env)¶
Evaluate Hessian at a point.
Arguments:
expr(ExprType): S-expressionvars(List[str]): List of variablesenv(Dict[str, Any]): Variable bindings
Returns: numpy.ndarray (2D)
Expression Evaluation¶
evaluate(expr, env, ops=None)¶
Evaluate a symbolic expression numerically.
Arguments:
expr(ExprType): S-expression to evaluateenv(Dict[str, Any]): Variable bindingsops(Dict[str, Callable]): Custom operators. Default: STANDARD_OPS
Returns: float
Example:
Standard Operators¶
The STANDARD_OPS dictionary defines built-in operators:
| Operator | Function |
|---|---|
+ |
Addition (variadic) |
- |
Subtraction or negation |
* |
Multiplication (variadic) |
/ |
Division |
^ |
Power |
sin, cos, tan |
Trigonometric |
arcsin, arccos, arctan |
Inverse trig |
sinh, cosh, tanh |
Hyperbolic |
exp |
Exponential |
log, ln |
Natural logarithm |
sqrt |
Square root |
abs |
Absolute value |
lgamma, gamma |
Gamma functions |
erf, erfc |
Error functions |
Special Forms¶
These have special evaluation semantics:
| Form | Meaning |
|---|---|
['sum', 'i', n, body] |
\(\sum_{i=1}^{n} \text{body}\) |
['prod', 'i', n, body] |
\(\prod_{i=1}^{n} \text{body}\) |
['@', 'x', 'i'] |
x[i] (1-based indexing) |
['len', 'x'] |
Length of x |
['total', 'x'] |
Sum of x |
['if', cond, then, else] |
Conditional |
Constants: 'e' and 'pi' evaluate to mathematical constants.