I'm currently working on a modified version of the Shunting Yard Algorithm that would work with variables, but I cant figure out how to get it to work. For example, I would want the algorithm to re-write 2 * (2x + 5) - 5 to 4x + 5. Any ideas / links to already implemented algorithms that does this already?
Asked
Active
Viewed 1,581 times
4
-
That's too complex if you don't want to use a binary tree. – gab06 Aug 15 '14 at 02:43
-
If I use a binary tree instead, how would I go to implement a solution? – Alston Lin Aug 15 '14 at 02:53
1 Answers
4
- Take the expression:
2 * (2x + 5) - 5
- Add the * symbol to make it more understandable for the computer:
2 * (2*x + 5) - 5
- Parse it using the Shunting Yard Algorithm, it becomes:
2 2 x * 5 + * 5 -
(Each character could be seen as an element of an array). - With the parsed expression, create the binary tree:
-
/ \
* 5
/ \
2 +
/ \
* 5
/ \
2 x
5. Define and apply algebraic rules to the tree. For example, a rule to be able to 'multiply' the 2
node with the 2 * x + 5
subtree.

gab06
- 578
- 6
- 23
-
-
Thanks, I see what you mean. Would this approach work for functions as well? – Alston Lin Aug 15 '14 at 04:56
-
With functions you mean trigonometric functions? I'm developing an online CAS with this approach and at the moment it can solve algebraic equations, limits, derivatives and a few integrals. – gab06 Aug 15 '14 at 05:01
-
-
Yeah, with this model you can cover all of those. Trig functions, log, ln, etc. have just one child node (argument). – gab06 Aug 17 '14 at 01:35
-
1@gab06 Thanks. ;) - didn't want to seem chatty in the other question. – Funk Forty Niner Sep 02 '14 at 18:45