3

let's say, for example, I give this equation as an input:

4x + x - 20 + 10

And I want to get 5x - 10 as a result

3 Answers 3

1

You can use the Symja - Java computer algebra and symbolic math library.

import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;
import org.matheclipse.parser.client.SyntaxError;

public class Example {
  public static void main(String[] args) {
    try {
      ExprEvaluator util = new ExprEvaluator(false, (short) 100);

      // Convert an expression to the internal Java form:
      // Note: single character identifiers are case sensitive
      // (the "D()" function identifier must be written as upper case character)
      String javaForm = util.toJavaForm("4*x + x - 20 + 10");
      // prints: Plus(ZZ(-20L),x,Times(C4,x),C10)
      System.out.println("Out[1]: " + javaForm.toString());

      // Use the Java form to create an expression with F.* static methods:
      ISymbol x = F.Dummy("x");
      IAST function = F.Plus(F.ZZ(-20L), x, F.Times(F.C4, x), F.C10);
      IExpr result = util.eval(function);
      // print: -10+5*x
      System.out.println("Out[2]: " + result.toString());
    } catch (SyntaxError e) {
      // catch Symja parser errors here
      System.out.println(e.getMessage());
    } catch (RuntimeException rex) { 
      System.out.println(rex.getMessage());
    }
  }
}
0

DataMelt (http://jwork.org/dmelt) does this. Here is an example (I use Jython, but you can use Java too)

from jhplot.math import *  
from jhplot import *
j=Symbolic("jscl") # using jscl engine
print j.simplify("(x + 1)^(1/x)") == "(1+x)^(1/x)" 
print j.simplify("(x^2-1)/(x-1)") == "1+x"

This code returns "true" for all such simplifications

2
  • Please disclose any affiliation you have with this product. Aug 15, 2019 at 4:51
  • (I'm a web admin where many community/free programs are located. Not associated with this particular program in any other way). Aug 16, 2019 at 12:33
0

Maple does this well, but it is a commercial product.

For more alternatives check the List of computer algebra systems on Wikipedia, but that list does not show how something is implemented, or whether it has a Java API. You'd have to look this up yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.