How Mathematical Content Is Validated in Invention Disclosures Using the Formula-Paradigm System
The formula-paradigm system validates mathematical content in invention disclosures through a restricted-evaluation pipeline that parses LaTeX equations, evaluates them against numeric examples, and reports ok/mismatch/skip status.
Invention disclosures in the handsomestWei/patent-disclosure-skill repository rely on a formula-paradigm system to ensure mathematical correctness. This system bridges the gap between human-readable LaTeX formulas and machine-verifiable numeric validation, giving inventors immediate feedback on whether their equations hold true under concrete examples.
The Five-Stage Validation Pipeline
The formula-paradigm validation follows a strict pipeline implemented across three core modules in tools/shared/.
1. Load Formula-Paradigm Configuration
The system first loads a merged configuration of rules, paradigms, and combos from references/formulas/paradigms.yaml. Case-specific overrides can extend or modify these definitions.
# tools/shared/formula_paradigms.py → load_paradigms()
from tools.shared.formula_paradigms import load_paradigms
paradigms = load_paradigms("my_custom_case/")
# Returns merged dict with 'rules', 'paradigms', 'combos' keys
This configuration declares which formula patterns are recognized (e.g., weighted_sum, stoichiometric_reaction) and what structural constraints apply.
2. Parse Each Equation from the Disclosure Plan
The check_plan() function in tools/shared/check_formula_plan.py extracts equations from the disclosure plan and prepares them for evaluation.
# tools/shared/check_formula_plan.py → check_plan()
from tools.shared.check_formula_plan import check_plan
plan = {
"paradigm_ids": ["weighted_sum"],
"equations": [
{"tag": 1, "paradigm_id": "weighted_sum", "latex": r"s = \alpha x + \beta y", "role": "score"},
],
"numeric_example": {
"given": {"alpha": 0.5, "x": 0.8, "beta": 0.5, "y": 0.6},
"result": {"s": 0.7},
},
}
Each equation carries a LaTeX representation and references a declared paradigm.
3. Perform Restricted Evaluation of the Right-Hand Side
The heart of mathematical content validation is eval_equation() in tools/shared/formula_eval.py. This function performs safe, restricted evaluation through these steps:
- LaTeX normalization: Converts
\frac,\cdot,\times, Greek symbols, and other LaTeX constructs to pure Python arithmetic via_latex_ops_to_python - Symbol substitution: Replaces symbolic names with numeric values from the
givenexample (given_map) - Safety filtering: Rejects expressions containing disallowed constructs via
_should_skipand_ast_allowed - Implicit multiplication insertion: Automatically inserts
*where needed (e.g.,2x → 2 * x)
# tools/shared/formula_eval.py → eval_equation()
from tools.shared.formula_eval import eval_equation
result = eval_equation(
r"s = \alpha x + \beta y", # LaTeX equation
{"alpha": 0.5, "x": 0.8, "beta": 0.5, "y": 0.6}, # given_map
{"s": 0.7}, # expected result map
)
print(result)
# {'status': 'ok', 'lhs': 's', 'got': 0.7, 'expected': 0.7}
4. Compare Computed vs. Disclosed Results Using Tolerance
The system determines match status through close_enough(), which applies:
- Relative tolerance:
1e-6 - Absolute tolerance:
1e-9
| Status | Trigger Condition |
|---|---|
| ok | Computed value matches disclosed result within tolerance |
| mismatch | Values differ beyond tolerance |
| skip | Expression cannot be safely evaluated (unsupported operators, missing symbols, etc.) |
5. Aggregate Validation Outcomes
check_plan() returns a structured result that downstream tools render to the inventor:
validation = check_plan(plan, eval_numeric=True)
print(validation)
# {'ok': True, 'errors': [], 'warnings': []}
The ok boolean indicates overall validity, while errors and warnings provide detailed per-equation diagnostics.
Core Validation Rules Enforced by the System
The formula-paradigm system imposes strict constraints to maintain safety and predictability.
Allowed Operators and Constructs
Only these operations are permitted in _ALLOWED_AST:
- Arithmetic:
+,-,*,/ - Functions:
min,max - Simple fractions via
\frac
Skip Patterns That Trigger Automatic Bypass
The _SKIP_PATTERNS regex and _should_skip function reject:
- Summations:
\sum - Products:
\prod - Case statements:
\begin{cases} - Absolute-value bars:
|...|
# Example: summation causes skip status
result = eval_equation(
r"\sigma = \frac{1}{W} \sum_{k=1}^{W} s_{(k)}",
{"W": 3},
{"sigma": 1},
)
print(result)
# {'status': 'skip', 'reason': '含无法受限代算的结构(\\sum)'}
Symbol Binding Requirements
All symbols on the right-hand side must exist in given_map. Missing symbols trigger a skip with explanatory reason.
Result Mapping and Key Normalization
The left-hand side identifier is normalized via _norm_key() before matching against the result map, handling LaTeX formatting variations.
Complete Validation Examples
Valid Weighted-Sum Equation
from tools.shared.formula_eval import eval_equation
result = eval_equation(
r"s = \alpha x + \beta y",
{"alpha": 0.5, "x": 0.8, "beta": 0.5, "y": 0.6},
{"s": 0.7},
)
print(result) # {'status': 'ok', 'lhs': 's', 'got': 0.7, 'expected': 0.7}
Detected Mismatch with Diagnostic Details
from tools.shared.check_formula_plan import check_plan
plan = {
"paradigm_ids": ["weighted_sum"],
"equations": [
{"tag": 1, "paradigm_id": "weighted_sum", "latex": r"s = \alpha x + \beta y", "role": "score"},
],
"numeric_example": {
"given": {"alpha": 0.5, "x": 0.8, "beta": 0.5, "y": 0.6},
"result": {"s": 0.1}, # INCORRECT: should be 0.7
},
}
validation = check_plan(plan, eval_numeric=True)
print(validation["ok"]) # False
print(validation["errors"])
# [{'status': 'mismatch', 'reason': '代算 0.7 与 result 0.1 不符(rel=1e-06)', ...}]
Full Plan Validation with Numeric Evaluation Enabled
from tools.shared.check_formula_plan import check_plan
plan = {
"paradigm_ids": ["weighted_sum"],
"equations": [
{"tag": 1, "paradigm_id": "weighted_sum", "latex": r"s = \alpha x + \beta y", "role": "score"},
],
"numeric_example": {
"given": {"alpha": 0.5, "x": 0.8, "beta": 0.5, "y": 0.6},
"result": {"s": 0.7},
},
}
validation = check_plan(plan, eval_numeric=True)
print(validation) # {'ok': True, 'errors': [], 'warnings': []}
Source Code Reference Map
| File | Purpose | Key Functions |
|---|---|---|
tools/shared/formula_paradigms.py |
Configuration loading | load_paradigms() |
tools/shared/formula_eval.py |
Core evaluation engine | eval_equation(), _latex_ops_to_python(), _should_skip(), _ast_allowed(), close_enough() |
tools/shared/check_formula_plan.py |
Plan-level orchestration | check_plan() |
references/formulas/paradigms.yaml |
Declarative paradigm definitions | YAML rules, paradigms, combos |
tests/shared/test_formula_eval.py |
Unit tests for validation logic | Test cases for ok/mismatch/skip |
Summary
- Configuration-driven: The formula-paradigm system loads reusable mathematical patterns from
paradigms.yamland supports case-specific overrides. - Safety-first evaluation:
eval_equation()informula_eval.pyconverts LaTeX to Python through restricted AST evaluation, rejecting dangerous constructs automatically. - Clear status reporting: Every equation receives one of three statuses—ok, mismatch, or skip—with human-readable explanations.
- Tolerance-aware matching: Numeric comparison uses
1e-6relative and1e-9absolute tolerance to handle floating-point variation. - Integration-ready:
check_plan()aggregates results into a standard format consumable by skill UIs and documentation generators.
Frequently Asked Questions
What causes a formula to receive "skip" status instead of "mismatch"?
A skip status indicates the expression cannot be safely evaluated, not that it failed validation. Triggers include unsupported LaTeX constructs like \sum, \prod, or \begin{cases}, disallowed AST nodes beyond + - * / min max, or missing symbols in the given_map. This distinction prevents false negatives while maintaining security.
How does the system handle implicit multiplication in LaTeX formulas?
The _insert_implicit_mul() function in formula_eval.py automatically inserts multiplication operators between adjacent terms, converting patterns like 2x or \alpha\beta into 2*x and alpha*beta before Python evaluation. This bridges the gap between mathematical notation and executable code.
Can custom formula paradigms be added to the system?
Yes. The load_paradigms() function accepts an optional directory path for case-specific overrides. Place a paradigms.yaml with your custom rules, paradigms, or combos definitions in this directory; they merge with the base configuration from references/formulas/paradigms.yaml.
What tolerance values does the validation use for numeric comparison?
The close_enough() function applies relative tolerance of 1e-6 and absolute tolerance of 1e-9. This matches common scientific computing standards and reliably catches meaningful discrepancies while accommodating minor floating-point representation differences.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →