Search code examples
.netalgorithmalgebrasimplificationpostfix-notation

Algorithms/How-to for simple algebraic simplification


Possible Duplicate:
automatically calculate string mathematical expressing in C#

Could someone point me towards some resources which describe in detail how to simplify simple algebraic equations such as "5x+(2x*6x)+2" which should result in "12x^2+5x+2" the operators I need to handle are "+","-","*","/","^" and parenthesis. So far I have implemented a parser and evaluator for numeric terms which converts from in-fix to post-fix and then evaluates.

If someone could give me a basic run-down on how I could accomplish this it would be greatly appreciated.


Solution

  • if you already have a tree internally that represents the equation, you can recursively visit each node to figure out if there's a way to combine the terms of the two sub-nodes. Think about what you would do at each node, then think about boundary conditions (node is null, node has no children, etc.).

    Here is how I would approach it:

    1. visit each child in turn
    2. have a function that will find common powers for the same variable in the children and combine them a. consider multi-variable equations b. remember your output format vs. working copy format. You will need to transform the end result back into text.

    That's a start. I think