Trick to Check Equality of Expression Containing Subscripts Using a Basic LaTeX Expression Evaluator
A silly bug turned genius hack.
Want to get notified about new posts? Join the mailing list.
Today I found a bug in a LaTeX expression evaluator that, by pure dumb luck, implemented a genius hack for comparing expressions involving subscripts.
The bug is that evaluator treats subscripts as multiplications. If you give it "a_{n}"
it parses a variable "a_"
times a variable "n"
.
(This is less a bug and more just that the evaluator was not built to handle subscripts, but I’ll call it a bug for simplicity since it could easily behave unexpextedly yet without errors if you don’t know about this limitation.)
Anyway, this bug initially seems silly – but as it turns out:
- if all you want to know is whether two LaTeX expressions are mathematically equivalent, like
"a_{n+1}"
and"a_{1+n}"
, - and you're testing equivalence by plugging in a bunch of random numbers for the variables and checking whether both expressions evaluate to the same numbers,
- then there is no chance of a false negative, and the chance of a false positive is negligibly miniscule (which is good enough for my use case).
Even better, the fact that the subscript is parsed into the preceding variable symbol means you’re able to disambiguate "a_{n}"
and "n_{a}"
despite multiplication being associative.
So, what all this means is that
- you can reliably compare expressions containing subscripts,
- WITHOUT actually having to do any reasoning about subscripts in the evaluator.
What an elegant way to sidestep all the complexity in what otherwise would have been a real headache to deal with!
Want to get notified about new posts? Join the mailing list.