title: Strategies for simplifying math expressions
body: <p>I have a well-formed tree that represents a mathematical expression.  For example, given the string: <code>"1+2-3*4/5"</code>, this gets parsed into:</p>
  
  <pre><code>subtract(add(1,2),divide(multiply(3,4),5))
  </code></pre>
  
  <p>Which is expressed as this tree:</p>
  
  <p><img src="https://i.stack.imgur.com/ieQvw.png" alt="&quot;1+2-3*4/5&quot;"></p>
  
  <p>What I'd like to be able to do is take this tree and reduce it as much as possible.  In the case above, this is pretty simple, because all of the numbers are constants.  However, things start to get trickier once I allow for unknowns (denoted with a <code>$</code> followed by an identifier):</p>
  
  <p><code>"3*$a/$a"</code> becomes <code>divide(multiply(3,$a), $a)</code></p>
  
  <p>This should simplify to <code>3</code>, since the <code>$a</code> terms should cancel each other out.  The question is, "how do I go about recognizing this in a generic manner?"  How do I recognize that <code>min(3, sin($x))</code> is always going to be <code>sin($x)</code>?  How do I recognize that <code>sqrt(pow($a, 2))</code> is <code>abs($a)</code>?  How do I recognize that <code>nthroot(pow(42, $a), $a)</code> (the a<sup>th</sup> root of 42 to the a<sup>th</sup> power) is <code>42</code>?</p>
  
  <p>I realize this question is pretty broad, but I've been beating my head against this for a while and haven't come up with anything satisfactory enough.</p>