title: Symbolic Differentiation Algorithm
body: <p>So I'm trying to create an algorithm to perform symbolic differentiation to get the exact derivatives of a given function. I've chosen the Reverse Polish Notation to store the expression because it saves me the space and time to build up an actual Expression Tree.
  I'll illustrate my problem with a concrete example, and I'll play down my algorithm by hand; so If you expect a simple question or have no clue about the answer please stop read and go away ( I don't need mindless downvotes on this question ).</p>
  
  <pre><code>Example:
   Find the derivative of: F=x^3+2*x^2+x
   1) Convert F to RPN form results in: x3^2x2^*+x+
   2) I'll store RPN in stack as follows:
      ---
      |+|
      ---
      |x|
      ---
      |+|
      ---
      |*|
      ---
      |^|
      ---
      |2|
      ---
      |x|
      ---
      |2|
      ---
      |^|
      ---
      |3|
      ---
      |x|
      ---
  
  This is basically the post-order traversal of the expression tree for the F expression. For now I'm focusing on kind of simple derivation rules.
  
   2) Pop stack (Should be an operator): [+] Rule: op0[+]op1 := d[op0]+d[op1]         -&gt; d[op0]d[op1]+
   3) Get op0 (POP):                     [x] Rule:         x :=          d[x]         -&gt; 1
   4)         APPLY:                     [1]
   5) Get op1 (POP):                     [+] Rule: op0[+]op1 := d[op0]+d[op1]         -&gt; d[op0]d[op1]+
   6) Get op0 (POP):                     [*] Rule: op0[*]op1 := d[op0]*op1+op0*d[op1] -&gt; d[op0]op1*op0d[op1]*+
   7) Get op0 (POP):                     [^] Rule: op1[^]op0 := op1*op0^op1-1         -&gt; op1op0op11-^*
   8) Get op1 (POP):                     [2]
   9) Get op0 (POP):                     [x]
  10)         APPLY:                     [1|2|x|2|1|-|^|*]
  11) Get op1 (POP):                     [2]
  12)         APPLY:                     // How can I apply the multiplication operator now?
  </code></pre>
  
  <p>So if my thought is right, I have the following stack: [2|x|2|1|-|^|<em>] for the first operand and [2] for the second operand. Now I have to perform [2|x|2|1|-|^|</em>] [<em>] [2], which requires me to have this function recursive. Because now I have to perform derivation for [2|x|2|1|-|^|</em>] also. And in this case I also have to store the results of each operator nodes in different stacks and them merge them in the correct order.</p>
  
  <p>Anyone could recommend a more effective way of doing this?</p>