Skip to main content

Crate himari

Crate himari 

Source
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.
FormulaItem
FormulaSet
One piece of formula source = a declaration block + one or more formulas (the “federation” model: later entries may reference the names of earlier ones).
FrontError
Unified front-end error. line == 0 means “no position” (e.g. a purely semantic error).
HashMap
A hash map implemented with quadratic probing and SIMD lookup.
MatPattern
UnitSpec
A unit (dimension + conversion factor to SI base units).

Enums§

Expr
Fx
Generate a block expression: named intermediates (one let tN per shared subtree) + the final expression.
MatrixKind
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.
PatternCell
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 for func_arity). Used by the “registry consistency” test: adding an operator means changing only this and func_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_DEPTH is 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 area inside vol = area * h is 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 f32 variant block: the same IR, with literals carrying the f32 suffix.
emit_block_libm
The no_std (libm) variant block: the same IR, with transcendental functions going through libm::*.
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. eval is 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 diff cannot 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): returns coeffs[k] = the coefficient of var^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) → a 1×n row 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) → the x satisfying expr = 0 (expr must be linear in x).
solve_poly
Polynomial root finding: roots(poly, x) → all roots satisfying poly = 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 name with by (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