Expand description
himari core: dimensioned IR / symbolic differentiation / matrix and linear solving / CSE / Rust emitter.
LaTeX, matex and paper glyphs are each lowered by a front end into the same IR; this crate does not guess dialects, it only does algebra, checking, evaluation and code generation.
Re-exports§
pub use i18n::Error;pub use i18n::ReservedReason;
Modules§
- i18n
- Localization for user-facing messages.
- runtime
- Runtime numeric values and checked linear algebra.
- wgsl
Structs§
- C
- Complex number (real part + imaginary part).
- Decl
- Formula symbol declaration. The dimension must be given explicitly — this is where this scheme pins down LaTeX’s semantic ambiguity.
- Dim
- The 7 base quantities: kg, m, s, A, K, mol, cd.
- Formula
Item - Formula
Set - One piece of formula source = a declaration block + one or more formulas (the “federation” model: later entries may reference the names of earlier ones).
- Front
Error - Unified front-end error.
line == 0means “no position” (e.g. a purely semantic error). - HashMap
- A hash map implemented with quadratic probing and SIMD lookup.
- MatPattern
- Unit
Spec - A unit (dimension + conversion factor to SI base units).
Enums§
- Expr
- Fx
- Generate a block expression: named intermediates (one
let tNper shared subtree) + the final expression. - Matrix
Kind - Builtin matrix shapes that can be generated. A pattern matrix must first be recognized as one of these; if it cannot be recognized, report an error and list the supported shapes — rejection is a feature.
- Pattern
Cell - One cell of a pattern matrix: a concrete element, or an ellipsis (its direction is inferred from the neighbours when writing back).
- Value
- Evaluation result: scalar or matrix. Matrix elements are
C, so a real matrix and a complex matrix are the same variant (the imaginary part of a real number is always 0).
Constants§
- FUNC_
NAMES - The function name in the registry closest to
name(edit distance ≤ 2). The single list of builtin functions (the truth source forfunc_arity). Used by the “registry consistency” test: adding an operator means changing only this andfunc_arity; a lane that forgets to register it gets caught by the test. - MAX_
DEPTH - Maximum nesting depth the IR allows.
- MAX_
PARSE_ NESTING - Maximum bracket nesting accepted by the front-ends before parsing.
- MAX_
STEPS - Upper bound on the explicit loop steps for sums/integrals (a teaching lane, not a numerics library: do not let it become a DoS).
Functions§
- add
- check_
calls - Arity checking of all function calls in an expression (called once at the end of the front-end parse).
- check_
calls_ with_ shapes - Validate calls with declaration ranks available, allowing runtime solve(A,b) without weakening the existing symbolic solve checks.
- check_
decl_ names - Reserved-word collision check for declaration names (
decl { exp : ... }would shadow the function name). - check_
depth - Depth guard: reject outright once
MAX_DEPTHis exceeded (to avoid downstream recursive stack overflow). - check_
set_ depth - Guard over the whole formula set (called once at the end of the front-end parse): depth + function arity.
- closest_
func - contains_
nan_ expr - Whether the expression contains
diff’s “not symbolically differentiable” marker (Num(NaN)). - contains_
nonfinite_ expr - Whether the expression contains any non-finite floating-point constant (inf / NaN).
- convert
- Unit-aware conversion:
convert(1.0, "nF", "F") == 1e-9. - depth_
of - Expression depth (a leaf = 1). Used as a guard before persisting/before emitting.
- deriv
- diff
- dim_
check - dim_
solve - Decide “can the target dimension be formed by multiplying/dividing powers of these variables?”, and if so give one set of exponents.
- dimensionless_
groups - Dimensionless groups (Buckingham Π theorem).
- dims_of
- dims_
of_ set - Federated dimension table: decls as the base, then the names of earlier formulas merged in
with the dimension of their expressions —
so referencing
areainsidevol = area * his legal, and dimensions propagate. - div
- emit_
block - emit_
block_ complex - Emission of a complex block (equivalent to
emit_block, but values are(f64, f64)). - emit_
block_ complex_ with - Complex block emission (with the information “which names are already complex”).
- emit_
block_ f32 - The
f32variant block: the same IR, with literals carrying thef32suffix. - emit_
block_ libm - The
no_std(libm) variant block: the same IR, with transcendental functions going throughlibm::*. - emit_
matrix_ ctor - Rust source of the generated matrix constructor (parameterized by shape: n is the runtime size).
The signature is derived from the pattern’s free variables (
k: f64, ..., n: usize), without hard-coded parameter names. - eval
- eval_
value - Structured evaluation: both scalars (real/complex) and matrices can be computed.
evalis its “must be a real scalar” convenience wrapper. - expand_
call - Unified unary/binary operator expansion: matrix operators + linear solving.
- expand_
deep - Deep expansion: replace the expandable operators (det/trace/adjoint/inv/solve/roots/ dot/norm) anywhere in the tree with their expanded ordinary IR.
- expand_
dot - Expand
dot(a, b)intoΣaᵢbᵢ(both operands must be concrete and have the same number of elements). - expand_
matrix_ call - Expand the matrix operators (det / trace / adjoint / inv) on concrete literals into ordinary IR.
- federate_
inline - Federation expansion: starting from the second entry, substitute earlier named formulas into the references (no function calls, each entry self-contained). The cost is duplicated computation (the caller’s CSE only helps within a single entry); the macro inlining lane uses a let chain and pays no such cost.
- flatten_
matrix_ value - Fold a statically expandable matrix expression into a concrete matrix IR (element level).
- fmt_num
- format_
matex_ rows - Canonical matrix layout: each column’s width = the widest content in that column + at least 1
space;
every row ends with
;(except the last row);...is aligned with the content in its column. - format_
set - Canonical text of the whole formula set (declarations on multiple lines + formulas one by one + matrix alignment).
- func_
arity - Function registry: name → required arity.
- grad
- horner_
in - Horner form:
c₀ + c₁x + c₂x² + …→(…((cₙx + cₙ₋₁)x + cₙ₋₂)x … + c₀). - indexed
- integral
- is_
matrix_ value - Whether the expression is statically expandable into a concrete matrix (decides the return type of the generated function).
- is_
structurally_ zero - Structural zero test: expressions that are algebraically identically zero and provable as such by “combining like terms”.
- key
- laplacian
- latex_
var - Variable name → LaTeX form.
- math_
fract - Fractional part, available in both std and no_std builds.
- mul
- neg
- nondiffable
- Names of functions/operators
diffcannot symbolically differentiate (for a named error, instead of just dropping a NaN and leaving the caller to guess). - num
- numeric_
roots - Accepts polynomials only (in
var): returnscoeffs[k]= the coefficient ofvar^k. - params_
in_ decl_ order - Parameter order = declaration order (not lexicographic): the generated function signature must be readable by a human in formula order.
- parse_
unit - Parse unit strings such as
A/m^2,kg*m/s^2,V/A*s^(-1/2). - parse_
unit_ spec - Parse a unit string, yielding both the dimension and the conversion factor
(
nF→ (C/V, 1e-9)). - pi_text
- Textual form of a dimensionless group:
rho^1·v^1·L^1·mu^-1. - poly_
coeffs - poly_
coeffs_ vec - Coefficient vector of a polynomial (low degree to high degree):
poly_coeffs_vec(poly, x)→ a1×nrow vector. - pow
- recognize_
pattern - scan_
source - Source text scan: reject invisible and control characters.
- shape_
check - shape_
of - Static shape: (rows, cols) of a concrete matrix literal. Pattern matrices/symbols return None (dynamic shape; the first version does not infer shapes); a concrete matrix with inconsistent shape (jagged rows) is a hard error.
- simplify
- solve_
linear - Linear solving:
solve(expr, x)→ thexsatisfyingexpr = 0(exprmust be linear in x). - solve_
poly - Polynomial root finding:
roots(poly, x)→ all roots satisfyingpoly = 0(column vector). - solve_
system - Linear system solving:
solve([eq1, eq2, …], [x1, x2, …]), each equation written as “expression = 0”. - sub
- subst
- Symbolic substitution: replace every occurrence of
namewithby(inline expansion of a federation reference). - sum
- to_
latex - to_
matex - Canonical matlab-side write-back: parenthesized by operator precedence, matrices aligned by
column.
This is the single source of truth for the formatter (
fmt) — humans may use any amount of whitespace, the machine output is constant. - to_
mathml - IR → MathML Core (presentation). Pure string generation, zero dependencies — this is the first building block of the rendering assets: the single source of truth distributed to VS Code hover, hikari components and the docs site.
- transpose
- uses_
complex - Whether the expression contains the imaginary unit
j(deciding whether to take the complex emission lane). - var
- vars_of