Viyan

Viyan AI

Moving Math Recognition Beyond String Matching

The PA-CDM metric shifts evaluation from flat character matching to position-aware tree structures, allowing builders to diagnose structural failures in mathematical models.

Existing metrics for handwritten mathematical expression recognition treat equations as linear strings. This approach fails to capture the spatial hierarchy inherent in math, where the position of a symbol determines its meaning. PA-CDM introduces position-forest encoding to move evaluation from simple string comparisons to hierarchical tree structures.

The Failure of Flat Metrics

Traditional evaluation metrics treat mathematical expressions as flat lists of symbols, which ignores the spatial relationships required to understand an equation. In a standard expression, the semantic value of a symbol depends on its role in the tree, such as whether a character acts as a base or an exponent. Render-based Character Detection Matching aligns individual glyphs to the ground truth, but it remains position-blind regarding the structural relationship between those glyphs. Relying on string-level metrics, such as Edit Distance on LaTeX tokens, ignores the layout entirely, resulting in scores that equate a missing decimal point with a missing exponent.

Metric Position Awareness Sensitivity to Structure Error Alignment
Exact-Match Rate No No Low
String-Similarity No Low Low
Render-based CDM No Medium Medium
PA-CDM Yes High High

From Pixels to Trees

To build a tree from handwritten inputs, the system maps raw coordinate data into logical relationships. The algorithm converts spatial coordinates into a tree topology where parent nodes represent base symbols and child nodes represent spatial relations like subscripts or superscripts. This transforms raw ink into a structured forest where each node explicitly encodes the spatial relationship to its parent. Because this encoding preserves the hierarchy, the metric can distinguish between a symbol that is misplaced and a symbol that is fundamentally misidentified.

Separating Character and Structural Errors

PA-CDM calculates divergence using a tree-edit distance, which allows for the decomposition of error by isolating node identity from node position. When comparing the predicted tree against the ground truth, the algorithm performs a two-stage match. First, it verifies the symbol identity at a specific location. If the symbol is incorrect, the error is flagged as a recognition failure. If the symbol matches but the spatial coordinates do not satisfy the required relationship, the metric flags a structural deviation. By decoupling the symbol identification from the spatial edge validation, the metric provides a precise audit of where the model’s internal representation of the math diverges from the visual ground truth.

A Worked Example of Sensitivity

Consider an expression like 2^x where the handwritten input misplaces the x, placing it on the same baseline as the 2. A flat metric sees this as a minor character shift. PA-CDM identifies this as a failure of structural edge validation, isolating the error in the edge between the base and the exponent rather than treating the exponent as a character substitution. Because the tree-edit distance measures the cost of these structural operations separately from symbol substitutions, the developer sees that the model is performing correctly on character identification but failing to interpret spatial hierarchies. This separation prevents the model from being penalized for illegible symbols when the underlying issue is a failure to map the coordinate geometry into the correct parent-child relationship.

Limitations and Unknowns

PA-CDM relies on the accuracy of the initial glyph detection. If the coordinate mapping of a single character is shifted, the entire tree topology can be disrupted. A single faulty detection of a fraction bar, for instance, can force all subsequent symbols into the denominator node of the tree, causing a cascade effect where the tree-edit distance penalty accumulates significantly for every misclassified downstream child node.

Regarding computational cost, PA-CDM carries a higher overhead than simple string-matching baselines. While string edit distance typically operates at O(n²) complexity, tree-edit distance calculations are more computationally expensive, often requiring O(n³) or higher depending on the tree structure and the specific mapping algorithm employed. Furthermore, the reliance on a static tree structure assumes a single ground-truth hierarchy for every expression. Ambiguous expressions, where multiple valid tree structures exist, remain a primary challenge, and future iterations must determine how to handle these multiple-root cases without introducing arbitrary tie-breaking rules.