Simplification of algebraic expressions is rather difficult, especially in comparison with the calculation of derivatives. Simplification must be done recursively. First, you simplify the most secret expressions. Do not try too much at a time. I would start with the simplest simplifications, for example:
0 + x -> x
0 * x -> 0
1 * x -> x
x ^ 0 -> 1
x ^ 1 -> x
Replace subtraction with addition and division by multiplication
x - y -> x + (-1)*x
x / y -> x ^ (-1)
It may not look so simple, but it will simplify your code. You can always undo this step at the end.
. , ( , )
(x * y) * z -> x * (y * z)
x * 2 -> 2 * x
2 * (x * 3) -> 2 * (3 * x)
(x ^ y) ^ z -> x^(y * z)
.
2 * (3 * x) -> 6 * x
2 + (3 + x) -> 5 + x
, .