alkahest
Python API

Plotting, printing and parsing

function
latex(expr) → str

Return a LaTeX string for the given Expr.

A Series (from series()) renders its expansion with the O(·) term, and a DerivedResult renders its .value.

The result is suitable for $...$ in a LaTeX document or IPython.display.Math(alkahest.latex(expr)) in a Jupyter notebook.

Example:

Python
>>> import alkahest
>>> p = alkahest.ExprPool()
>>> x = p.symbol("x")
>>> alkahest.latex(alkahest.sin(x)**2 + alkahest.cos(x)**2)
'\\sin\\!\\left(x\\right)^2 + \\cos\\!\\left(x\\right)^2'
function
plot(
    expr,
    var,
    range_: tuple[float, float]=(-5.0, 5.0),
    *,
    n: int=300,
    label: str | None=None,
    title: str | None=None,
    backend: str | None=None,
    **kw: Any
)

Plot a single-variable symbolic expression.

Parameters
exprExpr — symbolic expression in var.
varExpr — the free variable.
range_(lo, hi) — plotting interval (default: (-5, 5)).
nnumber of sample points (default: 300).
labellegend label.
titleaxis title.
backend"matplotlib" (default) or "plotly".
**kwforwarded to the backend's line-plot call.
Returns

Axes (matplotlib) or Figure (plotly).

function
plot3d(
    expr,
    var_x,
    var_y,
    x_range: tuple[float, float]=(-5.0, 5.0),
    y_range: tuple[float, float]=(-5.0, 5.0),
    *,
    n: int=80,
    title: str | None=None,
    backend: str | None=None,
    **kw: Any
)

Plot a two-variable symbolic expression as a 3-D surface.

Parameters
exprExpr — symbolic expression in var_x and var_y.
var_x, var_yExpr — the two free variables.
x_range, y_range(lo, hi) — plotting intervals.
ngrid resolution per axis (default: 80).
titlefigure title.
backend"matplotlib" (default) or "plotly".
**kwforwarded to the backend surface call.
Returns

Axes3D (matplotlib) or Figure (plotly).

function
plot_dag(expr, *, title: str | None=None, **kw: Any)

Visualise the expression DAG.

Tries the graphviz Python package first (renders a nice PNG/SVG via system Graphviz). Falls back to returning the raw DOT string so the caller can pipe it to dot manually.

Parameters
exprExpr — the expression to visualise.
Returns

graphviz.Source — if the graphviz package is installed.

str — raw DOT string otherwise.

function
plot_implicit(
    expr,
    var_x,
    var_y,
    x_range: tuple[float, float]=(-5.0, 5.0),
    y_range: tuple[float, float]=(-5.0, 5.0),
    *,
    n: int=200,
    title: str | None=None,
    backend: str | None=None,
    **kw: Any
)

Plot the zero-set of a two-variable expression via matplotlib contour.

Evaluates expr on an n×n grid and draws the zero contour. Only supports the "matplotlib" backend (Plotly contour lines are available as a "plotly" fallback using Contour).

function
plot_parametric(
    expr_x,
    expr_y,
    param,
    range_: tuple[float, float]=(0.0, 6.283185307179586),
    *,
    n: int=500,
    label: str | None=None,
    title: str | None=None,
    backend: str | None=None,
    **kw: Any
)

Plot a parametric curve (x(t), y(t)).

Parameters
expr_x, expr_yExpr — x- and y-components in param.
paramExpr — the parameter variable.
range_(lo, hi) — parameter interval (default: (0, 2π)).
nnumber of sample points.
function
plot_roots(
    unipoly,
    var,
    *,
    title: str | None=None,
    backend: str | None=None,
    **kw: Any
)

Mark real roots of a UniPoly on the x-axis (rug plot).

Uses real_roots() to isolate roots, then places a vertical line at the midpoint of each isolating interval.

Parameters
unipolyUniPoly — polynomial whose real roots are to be marked.
varExpr — the variable (used for axis labelling only).
function
plot_series(
    series_result,
    original_expr,
    var,
    range_: tuple[float, float]=(-3.0, 3.0),
    *,
    n: int=300,
    title: str | None=None,
    backend: str | None=None,
    **kw: Any
)

Overlay a Taylor/Laurent series truncation against the original function.

Parameters
series_resultSeries or DerivedResult — output of series().
original_exprExpr — the exact expression (plotted as a reference).
varExpr — the variable.
range_(lo, hi) — plotting interval.
function
plot_svg(
    expr,
    var,
    range_: tuple[float, float]=(-5.0, 5.0),
    *,
    width: int=640,
    height: int=400,
    n: int=300
) → str

Return a standalone SVG string for the expression (no Python dep needed).

This calls the Rust render_svg function directly and requires no matplotlib / plotly installation. The result can be:

  • Written to a .svg file.
  • Embedded in HTML as <img src="data:image/svg+xml;base64,...">.
  • Displayed in Jupyter: IPython.display.SVG(alkahest.plot_svg(...)).
function
unicode_str(expr) → str

Return a Unicode pretty-printed string for the given Expr.

Uses Unicode superscripts, Greek letters, fraction characters, and root symbols for a human-readable representation without LaTeX markup.

Example:

Python
>>> import alkahest
>>> p = alkahest.ExprPool()
>>> x = p.symbol("x")
>>> alkahest.unicode_str(x**2 + p.integer(1))
'x² + 1'