Construction and parsing
Build expressions, matrices, geometry values, and callable functions from exact host values or parsed text. Every value belongs to the context that created it.
Context lifecycle#
new#
context() (Python) / new context() (JS)
Create a fresh context with an empty symbol table and expression store.
reset_context#
Explicit context
reset_context() -> NoneReplace the context's entire symbol table and expression store with fresh
empty ones. Warning: existing Expression/Matrix/geometry objects from
this context become stale and must not be used afterwards — their symbol
identifiers no longer resolve. Deliberately not available at module level:
resetting the shared default context would strand every live object, and the
weak expression store already keeps memory bounded by live objects (see
the default context).
Parsing and building#
parse#
Default context
parse(text: str) -> ExpressionParse Symi textual syntax into an expression. Accepts ^ and ** for
powers; knows the builtin function names (sin, log, sqrt, …) and the
constants pi, e. Raises on syntax errors and on meta-command input.
Parsing is always explicit: strings are not converted by expression-like
parameters.
parse_latex#
Default context
parse_latex(latex: str) -> AnyParse a LaTeX string in Symi's canonical input dialect — the inverse of
to_latex and the parser the Symi frontend uses.
Returns whichever top-level object the input denotes: an ordinary expression, a
\begin{matrix}…\end{matrix}, or a geometry figure ((x, y) point, segment,
polygon, or circle), wrapped in the matching exported class exactly as
parse_serialized_object does. Coverage spans
arithmetic, powers, fractions, roots, the elementary and many special functions,
comparisons, integrals (including the bare-d differential), Leibniz
derivatives (\frac{d}{dx} — differentiated eagerly), limits, summations,
products, determinants, and actuarial annuities. The resolution is
context-aware in a way parse is not: a bare i is the imaginary unit and a
bare e is Euler's number unless the letter is a bound index (a summation /
product / limit index shadows the constant), and \mathrm{d} — or a bare d
that closes an integral — is the differential operator while a bare d
elsewhere is an ordinary variable. Raises on syntax errors.
MathQuill-style one-character scripts may be bare or braced. For example,
\int_a^2 xdx, \sum_{i=1}^a i, \prod_{i=1}^a i, \sin^a(x), and
\log_a(x) are equivalent to their braced forms. General script expressions
remain braced, such as \log_{a+1}(x). Binder variables shadow definitions in
their bodies, while free names in bounds remain available for environment
expansion.
parse_latex_with_environment#
Default context
parse_latex_with_environment(
latex: str,
serialized_definitions: list[str],
) -> AnyCalling forms
Explicit context: Context.parse_latex_with_environment
parse_latex_with_environment(
latex: str,
serialized_definitions: list[str],
) -> AnyParse LaTeX after building an ordered definition environment. Each serialized
definition is a string with four fields separated by ASCII Unit Separator
(U+001F): kind, name, parameters, and body_latex. kind is object
or function; object parameters are empty, while function parameters are
separated by ASCII Record Separator (U+001E). For example,
object\x1fa\x1f\x1f5 followed by function\x1ff\x1fx\x1fx^2 makes
f(a) parse to 25. Definitions are parsed in list order using the environment
accumulated so far. The delimiters are rejected inside fields. Also available
as a module-level function.
symbol#
Default context
symbol(
name: str,
real: bool = False,
positive: bool = False,
negative: bool = False,
integer: bool = False,
finite: bool = False,
rational: bool = False,
natural: bool = False,
nonzero: bool = False,
) -> ExpressionCalling forms
Explicit context: Context.symbol
symbol(
name: str,
real: bool = False,
positive: bool = False,
negative: bool = False,
integer: bool = False,
finite: bool = False,
rational: bool = False,
natural: bool = False,
nonzero: bool = False,
) -> ExpressionIntern a symbol and (optionally) attach assumptions used by the assumption
engine and the simplifier. Domain flags refine in the order natural
integer rational real; positive/negative also set nonzero, and
nonzero may be requested independently.
Notes: assumptions are per-context, per-name, and sticky: re-interning the
same name keeps previously set assumptions, and a refinement applies
retroactively to every existing expression containing the symbol. Refinement
is monotonic — strengthening an unknown property is allowed; contradicting an
earlier declaration (e.g. positive=True after negative=True, or widening
the domain) raises an error naming the clash. Use
clear_assumptions to start a symbol
over.
integer#
Default context
integer(value: Any) -> ExpressionBuild an arbitrary-precision exact integer. Python bool values are rejected. JavaScript's
recommended facade accepts bigint and safe integral number values.
rational#
Default context
rational(numerator: Any, denominator: Any) -> ExpressionCalling forms
Explicit context: Context.rational
rational(numerator: Any, denominator: Any) -> ExpressionConstructs an exact reduced rational from arbitrary-size
integers. A zero denominator is an argument error. Python also accepts
fractions.Fraction wherever an expression-like input is accepted.
integer_from_string#
Default context
integer_from_string(text: str) -> ExpressionCalling forms
Explicit context: Context.integer_from_string
integer_from_string(text: str) -> ExpressionBuild an arbitrary-precision integer literal from its decimal string.
Named unknown functions are built with undefined_function, whose handle,
calls, and structural derivatives are documented together in
Undefined functions.
Example#
Build the same expression from a literal, a parse, and exact numeric constructors.
import symi
x = symi.symbol("x", positive=True)
print(symi.parse("x^2 + 2*x + 1"))
print(x**2 + 2 * x + 1)
print(symi.rational(2, 3) + symi.integer(1))
print(symi.parse_latex(r"\frac{1}{2} + x"))