
// EvalExprs.java
// Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A small example of how to use mXparser
   https://mathparser.org/

   More info at:
   https://mathparser.org/mxparser-hello-world/mxparser-hello-world-java/
   and
   https://mathparser.org/mxparser-tutorial/

   Usage (Windows):
     > compile.bat EvalExprs.java
     > run.bat EvalExprs

   Usage (Linux):
     > ./compile.sh EvalExprs.java
     > ./run.sh EvalExprs

*/



import org.mariuszgromada.math.mxparser.*;
import org.mariuszgromada.math.mxparser.parsertokens.*;

public class EvalExprs
{
  // private static final String SQROOT = "\u221A";

  public static void main(String[] args)
  {
    License.iConfirmNonCommercialUse("Andrew");  
            // required by mXparser

    Expression e = new Expression("2+1");
    System.out.println("Res: " + e.getExpressionString() + 
                                " = " + e.calculate());

    e = new Expression("sqrt(2)");
    System.out.println("Res: " + e.getExpressionString() + 
                                " = " + e.calculate());

    Argument x = new Argument("x");
    Argument y = new Argument("y = x+1", x);
    for (int i=0; i < 5; i++) {
      x.setArgumentValue(i);
      System.out.println("Res: " + y.getArgumentName() + 
                               " = " + y.getArgumentValue());
    }

    e = new Expression("x+1", x);
    for (int i=0; i < 5; i++) {
      x.setArgumentValue(i);
      System.out.println("x = " + x.getArgumentValue() + ", " + 
                                   e.getExpressionString() + 
                                   " = " + e.calculate());
    }
  }

} // end of EvalExprs class

