I've got a GREAT new program that will change the way we think about math in computing, taking in strings of algebraic functions and doing AMAZING things with them! The only problem, is that I am only able to parse specific algebra, otherwise the universe folds into itself, which is bad. Fortunately, I only need a few basic operations in this amazing new program's input, but I still need it expanded!
An answer must be able to simplify the following expressions
2+2should reduce to
(5+x)+6should reduce to
(x+2)^2should reduce to
(x-5)*(3+7*x)should reduce to
5*x+9*xshould reduce to
(2*x^2)*x^3should reduce to
Answers must be able to COMPLETELY remove parenthesis, which implies that all distribution must take place.
Answers should be able to handle all of the following operators and standard tokens:
+(The addition function)
-(The subtraction function)
*(The multiplication function)
((The left parenthesis, used to indicate a group)
)(The right parenthesis, used to indicate the end of the last started group)
x(The standard variable)
[0-9]+(literal nonnegative numbers)
Answers must be capable of at least squaring, using the notation expr^2, including (expr)^2 recursively, since (expr) is itself an expression ;)
A solution must be in a standard infix notation, none of the RPN nonsense!
No library functions such as Mathematica's
Simplifyto do this for you.
Solution should be a function that takes in a single argument and return the expanded version
As this is code-golf, the answer with the fewest (key)strokes wins, 1 week from OP.
There are no spaces in this world of math, of course! Only parenthesis.
So no division is required to save from factoring
Standard order of operations apply.
I'm aware that some of what I'm asking is simplification (e.g.
2+2=4) where other parts are actually the opposite, such as expanding
(x+1)^2 to be
x^2+2x+1. This is intentional. :)
-25 strokes for a solution that can do (expr)^n instead of just (expr)^2
-15 strokes for a solution able to evaluate juxtaposed multiplication, such as
-5 strokes for a solution able to handle multiple variables (A variable being exactly one lowercase alphabet character)
-5 strokes for a solution able to remove leading 0's (
007 to just
7 [Not today, Bond!] [Jeez now I feel like I'm writing Lisp])