solver
Find variable values that drive an expression to a root or extremum (min/max). Supports single and multiple unknowns with various search algorithms.
Instructions
Find the value(s) of one or more variables that drive an expression to a root or extremum.
solver takes the SAME expression language as calculate — every operator,
function, literal form, and (crucially) multi-line programs with name = expr
assignments — but instead of evaluating the expression it SEARCHES for the value
of the unknown(s) that drive the expression to the chosen objective:
"find-root" (default): find where the expression equals zero. Write an equation
f = gas the expressionf - gand find its root."find-minimum" / "find-maximum": find where the expression reaches its smallest / largest value within the bracket(s).
There are two input forms for the unknowns:
SINGLE:
variable+lower+upper— one unknown searched over the bracket[lower, upper](lowermust be belowupper). This is the default golden-section engine.MULTIPLE:
variables— a dict mapping each unknown name to its[lower, upper]bracket, e.g.{"x": [0, 5], "y": [-4, 2]}. This needs the Nelder-Mead engine (algorithm="nelder-mead"), which searches all the unknowns jointly. Give exactly one of the two forms. Each unknown must OCCUR in the expression and must NOT be assigned by it; every OTHER name is a constant the program sets via an assignment line (e.g."r = 0.05\np = 1000\np * (1 + r)**n - 2000"solving fornwithr,pfixed). A name that is neither an unknown nor assigned is an error.
objective (optional) names what to search for — "find-root", "find-minimum", or
"find-maximum"; omitted, it defaults to "find-root". (The older spellings solve,
minimise/maximise and their min/max and American forms are accepted too.)
algorithm (optional) names the search engine — "golden-section-search" (the
default, single-variable), "brent-parabolic" (single-variable too, parabolic
interpolation with a golden-section fallback — usually faster on smooth extrema),
or "nelder-mead" (multivariate, a bounds-clamped downhill simplex). The two
single-variable engines solve only the SINGLE form; the variables form requires
"nelder-mead". (golden, brent, simplex and a few other spellings are accepted
too.)
mode and min_fixed_point_precision behave exactly as in calculate: the
search runs in that numeric type, and the found value is reported in it. See
calculate and help for the shared grammar, modes, and precision rules.
The search is bounded by a hard 2-second time limit. If it has not converged by then it stops and reports the best value reached so far (a find-root that has not got close enough to zero in that time is reported as no-solution, naming the limit).
Returns a dict: solutions is a list of {variable, solution, solution_hex_dump},
one per unknown in input order — each solution rendered and marked "(approximate)"
(the search locates it to a tolerance, never exactly), with its bit-backed hex. For
the SINGLE form the scalar variable / solution / solution_hex_dump are also
set (the one unknown); for the MULTIPLE form those scalars are null and solutions
carries every value. value is the EXPRESSION evaluated at that solution, annotated
with its own precision verdict (near zero for find-root, the extremum otherwise),
and value_hex_dump its hex. mode is the resolved numeric type; exact and
precision describe value exactly as in calculate. objective echoes the
resolved objective, algorithm names the engine used, and iterations is how many
search steps it took. On any failure — a bad mode/precision, a malformed expression,
an invalid request (no/both input forms, empty bracket, unknown not in the
expression, golden-section asked for multiple unknowns, unknown objective or
algorithm), or no solution in the bracket — every data field is null and error
carries the message (a no-solution error reports the closest |expr| it reached); on
success error is null.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | ||
| variable | No | ||
| lower | No | ||
| upper | No | ||
| variables | No | ||
| objective | No | ||
| algorithm | No | ||
| mode | No | fixed-point | |
| min_fixed_point_precision | No |