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#
Not exposed by the WASM / JavaScript bindings. Available as Context.reset_context in Python, UniffiSession.resetContext in Kotlin, UniffiSession.resetContext in Swift, api::Session::reset_context in Rust.
Replace 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#
Raw WebAssembly: Default context
parse(text: string): ExpressionCalling forms
Explicit context: Context.parse
parse(text: string): ExpressionRaw WebAssembly: Explicit context: Context.parse
parse(text: string): ExpressionSymiFacade method: SymiFacade.parse
parse(text: string): 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#
Not exposed by the WASM / JavaScript bindings. Available as Context.parse_latex in Python, UniffiSession.parseLatex in Kotlin, UniffiSession.parseLatex in Swift, api::serialization::Session::parse_latex in Rust.
Parse 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#
Not exposed by the WASM / JavaScript bindings. Available as Context.parse_latex_with_environment in Python, UniffiSession.parseLatexWithEnvironment in Kotlin, UniffiSession.parseLatexWithEnvironment in Swift, api::serialization::Session::parse_latex_with_environment in Rust.
Parse 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#
Raw WebAssembly: Default context
symbol(
name: string,
real?: boolean | null,
positive?: boolean | null,
negative?: boolean | null,
integer?: boolean | null,
finite?: boolean | null,
rational?: boolean | null,
natural?: boolean | null,
nonzero?: boolean | null,
): ExpressionCalling forms
Explicit context: Context.symbol
symbol(name: string): ExpressionRaw WebAssembly: Explicit context: Context.symbol
symbol(
name: string,
real?: boolean | null,
positive?: boolean | null,
negative?: boolean | null,
integer?: boolean | null,
finite?: boolean | null,
rational?: boolean | null,
natural?: boolean | null,
nonzero?: boolean | null,
): ExpressionSymiFacade method: SymiFacade.symbol
symbol(name: string): 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#
Raw WebAssembly: Default context
integer(value: bigint): ExpressionCalling forms
Explicit context: Context.integer
integer(value: bigint | number): ExpressionRaw WebAssembly: Explicit context: Context.integer
integer(value: bigint): ExpressionSymiFacade method: SymiFacade.integer
integer(value: bigint | number): ExpressionBuild an arbitrary-precision exact integer. Python bool values are rejected. JavaScript's
recommended facade accepts bigint and safe integral number values.
rational#
Raw WebAssembly: Default context
rational(numerator: string, denominator: string): ExpressionCalling forms
Explicit context: Context.rational
rational(numerator: bigint | number, denominator: bigint | number): ExpressionRaw WebAssembly: Explicit context: Context.rational
rational(numerator: string, denominator: string): ExpressionSymiFacade method: SymiFacade.rational
rational(numerator: bigint | number, denominator: bigint | number): 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#
Not exposed by the WASM / JavaScript bindings. Available as Context.integer_from_string in Python, UniffiSession.integerFromString in Kotlin, UniffiSession.integerFromString in Swift, api::Session::integer_from_string in Rust.
Build 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.
const x = symi.symbol("x");
console.log(symi.parse("x^2 + 2*x + 1").toString());
console.log(x.power(2).add(x.multiply(2)).add(1).toString());
console.log(symi.rational(2n, 3n).add(symi.integer(1n)).toString());Additional API#
integerFromString#
Raw WebAssembly: Default context
integerFromString(text: string): ExpressionBuild an arbitrary-precision integer literal from its decimal string.
Calling forms
Raw WebAssembly: Explicit context: Context.integerFromString
integerFromString(text: string): ExpressionparseLatex#
Raw WebAssembly: Default context
parseLatex(latex: string): 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.
parseLatexWithEnvironment#
Raw WebAssembly: Default context
parseLatexWithEnvironment(
latex: string,
serialized_definitions: any[],
): 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.
Calling forms
Raw WebAssembly: Explicit context: Context.parseLatexWithEnvironment
parseLatexWithEnvironment(
latex: string,
serialized_definitions: any[],
): anyresetContext#
Explicit context
resetContext(): voidReplace 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).