Java Equation Evaluation

101 views Asked by At

I have string equation like

x +(23 + 78 ) = (32112 / 432)

and inputting this equation I need to get the value of 'x' programmatically, also equation might be different in different times, but it will only have one variable, and I need this variable value computed. Is there any Java library available for the above requirement? if exist please provide one sample also.

1

There are 1 answers

0
axelclk On

In Symja you can write:

import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;

public class SolveExample {

  public static void main(String[] args) {

    ExprEvaluator util = new ExprEvaluator();
    IExpr result = util.eval("Solve(x +(23 + 78 ) == (32112 / 432),x)");
    // print: {{x->-80/3}}
    System.out.println(result.toString());

    ISymbol x = F.Dummy('x');
    result = util.eval(F.Solve(F.Equal(F.Plus(x, F.ZZ(23L), F.ZZ(78L)), F.QQ(32112L, 432L)), x));
    // print: {{x->-80/3}}
    System.out.println(result.toString());
  }
}