Introduction
In many areas of computer science and machine learning, we frequently encounter mathematical expressions that are "naive"—they are mathematically correct but computationally expensive to evaluate. While humans can sometimes rely on algebraic manipulation to find a more efficient version (such as swapping a nested loop for a vectorized operation), this process becomes exponentially harder as the complexity of the expression grows.
Existing symbolic engines like Mathematica or Maple excel at simplifying terms, but they do not explicitly search for lower-complexity alternatives. Conversely, brute-force searching through every possible combination of mathematical rules is impossible because the search space explodes.
This paper explores how machine learning can automate the discovery of efficient mathematical identities. By combining attribute grammars with search strategies guided by $n$-gram models and Recursive Neural Networks (RNNs), the authors successfully found new, computationally cheaper versions of complex symbolic expressions that were previously unknown or beyond the reach of human derivation.
Core Methodology
The authors propose a framework built on three main pillars to navigate the massive space of mathematical rules:
1. Attribute Grammars
They define a set of mathematical "building blocks" (rules) such as matrix multiplication, transposition, and row/column sums. Each rule has an associated computational cost (its "attribute"). The system builds grammar trees that explore valid compositions of these expressions.
2. Numerical Representation
To quickly check if two different-looking formulas are actually mathematically equivalent, the framework evaluates them using random integer inputs modulo a large prime $p$. This "descriptor" avoids the precision errors of floating-point math while providing a unique, reliable signature for equivalent expressions.
3. Learned Search Strategies
Instead of searching randomly—which fails as expressions grow more complex—the authors use two types of models to predict which grammar rule to apply next:
- $n$-gram models: These models count how often specific sub-trees appeared in successful solutions for simpler problems, exploiting the fact that mathematical "style" or idioms are often local and predictable.
- Recursive Neural Networks (RNNs): The authors trained an RNN to map symbolic expressions into a continuous 30-dimensional vector space. The network learns to group equivalent expressions together in this space. A softmax layer then predicts the most promising next step in the search tree based on the current expression's "embedding."
Key Insights and Training Approach
To train these models, the authors generated a synthetic dataset of symbolic expressions by exhaustively enumerating valid grammar trees up to a certain degree ($k$). For a degree of $k=6$, they generated a dataset of 24,210 expressions grouped into 1,687 equivalence classes (clusters of formulas that produce the same result).
- Continuous Math Embeddings: One of the most striking findings is that an RNN can learn a meaningful continuous representation for symbolic logic. By pre-training the RNN to classify expressions into equivalence classes, the model develops an internal "understanding" of mathematical operations that generalizes to new, unseen formulas.
- Curriculum Learning: The models were trained using a curriculum. By starting with very simple expressions (low degree $k$) and slowly increasing complexity, the search strategy was able to "bootstrap" its way up to identities that are too complex to find from scratch.
Results and Discoveries
The framework successfully discovered identities that reduced $O(n^3)$ operations (like certain matrix-matrix products) to $O(n^2)$ or even $O(n)$ time. Notably, the authors found several identities for Restricted Boltzmann Machine (RBM) partition functions and symmetric polynomials that they believe were previously undiscovered.
As the degree of the expression ($k$) increased, the baseline random search strategy consistently failed. In contrast, the $n$-gram and RNN-guided searches continued to find solutions. For example, in the "RBM-2" family at $k=5$, the RNN found a solution in roughly 100 seconds, whereas a random search took over 400 seconds on average.
Limitations and Future Work
While groundbreaking, the current work is limited to homogeneous polynomials of a fixed degree $k$ and expressions that result in a scalar output. It does not yet handle more complex structures like recursion or trigonometric functions. Additionally, the system requires a rigid "scheduler" to decide when to stop building a tree; a more flexible scheduler could allow the discovery of even broader classes of identities.
Ultimately, while the paper proves the concept, integrating this into production compilers or optimization tools will require expanding the grammar to handle the diverse types of operations found in real-world code.
Summary updated