Question: I'm having trouble adding this functionality to my calculator app. Here's what I had in mind:

I want to solve: `2x = 5 + 4x`.

To solve this is no problem if I would use an external token library to parse the equation, but since my entire program is built around a shunting yard parser (EXP4J), it complicates things. You might say that I could just use the token parser for specific cases, but when you consider that I have FUNCTIONS using EXP4J, it becomes really tangled.

So, while I could make it work for `2x = 5 + 4x` by simply alternating to a token parser for this specific case, calculations that use EXP4J, like:

`2x = 5*exp4jfunc(x + 5,x) + cos(x)` are completely beyond me.

Does anyone have any idea on how to get around this? I don't even know how my CASIO tackles this, as it freezes on solve-for-x equations like `x = 9^x`...

Answer: 
With the [symja library](https://github.com/axkr/symja_android_library) you can solve your problem like this:

```
package org.matheclipse.core.examples;

import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.SyntaxError;
import org.matheclipse.parser.client.math.MathException;

public class Solve2Example {

    public static void main(String[] args) {
        try {
            ExprEvaluator util = new ExprEvaluator();
            IExpr result = util.evaluate("Solve(2*x==5 + 4*x,x)");
            // print: {{x->-5/2}}
            System.out.println(result.toString());

            result = util.evaluate("Roots(2*x==5+4*x, x)");
            // print: x==-5/2
            System.out.println(result.toString());
        } catch (SyntaxError e) {
            // catch Symja parser errors here
            System.out.println(e.getMessage());
        } catch (MathException me) {
            // catch Symja math errors here
            System.out.println(me.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

```

You can look into the implementation of the [Roots() function](https://bitbucket.org/axelclk/symja_android_library/src/master/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/reflection/system/Roots.java) how the parsed AST is transformed to solve the equation.
I am not really familiar with exp4j... Given that the `Expression` class doesn't seem to provide much access to the parsed structure, a symbolic approach seems not really feasible with exp4j.

One thing you could try with exp4j instead:

* Split the equation at `=` into `left` and `right`
* Build a new equation that is the difference of both sides:

`String diff = "(" + left + ") - (" + right + ")"`
* Parse `diff` with exp4j
* Use a numerical method (e.g. the [Secant method](https://en.wikipedia.org/wiki/Secant_method)) with `setVariable()` and `evaluate()` to find the solution(s).

Shameless plug: If you are considering to use a different parser, take a look at [this one](https://github.com/stefanhaustein/expressionparser)