eqdisco.expr.Expression#

class eqdisco.expr.Expression(nodes, feature_names=None)[source]#

Bases: object

A symbolic expression stored as a prefix sequence of Node.

Parameters:
nodessequence of Node

Prefix sequence; it must describe a single complete tree.

feature_namessequence of str, optional

Names used by the printers (variables are always referenced by index).

children(i)[source]#

Indices of the children of node i.

property depth#

Length of the longest root–leaf path (a leaf has depth 0).

depths()[source]#

Depth of every node (distance from the root).

evaluate(X, theta=None)[source]#

Evaluate \(f(\mathbf{x}_k;\boldsymbol\theta)\) for every row of X.

Parameters:
Xarray_like of shape (N, n)
thetaarray_like, optional

Parameter values to use instead of the stored ones.

Returns:
ndarray of shape (N,)
classmethod from_string(text, pset=None, feature_names=None, params=True)[source]#

Parse an infix string such as "2*x0 + sin(x1)".

Function names of the registry (sqrt, exp, aq, …) and ^ for powers are understood.

classmethod from_sympy(expr, pset=None, feature_names=None, params=True)[source]#

Build an expression from a SymPy expression.

Symbols must be feature names (or x0, x1, ...). Numbers become parameters when params is true, except integer exponents, which are expressed with square, cube, sqrt, inv or fixed constants.

interval(lower, upper)[source]#

Interval evaluation: bounds of \(f\) over boxes.

Parameters:
lower, upperarray_like of shape (n,) or (B, n)

Lower and upper corners of one or B boxes.

Returns:
lo, hindarray of shape (B,)

Guaranteed (possibly loose) bounds of the expression on each box.

jacobian(X, theta=None)[source]#

Output and Jacobian \(\partial f/\partial\boldsymbol\theta\) (forward mode).

Returns:
valuendarray of shape (N,)
Jndarray of shape (N, p)
key()[source]#

Structural key (parameters ignored) used for deduplication.

latex(feature_names=None, digits=4)[source]#

LaTeX string (via SymPy) with digits significant digits.

static linear_combination(terms, weights, intercept=None)[source]#

Build \(w_0 + \sum_i w_i F_i\) as one expression (weights are parameters).

param_positions()[source]#

Node indices of the parameters (non-fixed constants), prefix order.

parents()[source]#

Parent index of every node (-1 for the root).

replace(i, new)[source]#

A copy with the subtree at i replaced by new.

property size#

Number of nodes \(|f|\).

subtree(i)[source]#

The subtree rooted at node i as a new expression.

property theta#

Vector of parameter values \(\boldsymbol\theta\) in prefix order.

to_list()[source]#

JSON-friendly list of nodes: ["f", name], ["v", j], ["c", value, fixed].

to_string(feature_names=None, digits=4)[source]#

Infix string with the minimum of parentheses; negative terms are printed with minus signs.

to_sympy(feature_names=None, symbolic_params=False, digits=None)[source]#

Convert to a SymPy expression.

Protected operators become their plain mathematical counterparts. With symbolic_params=True the parameters are symbols theta_0, ....

property variables#

Sorted indices of the variables that occur in the expression.

with_theta(theta)[source]#

A copy with the parameters replaced by theta.