User defined functions

MathParser.org - mXparser - logo - Tutorial

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

JAVA code

Case 1: Fast function definition (performance of creation)

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f", "x^2", "x");
Expression e = new Expression("f(2)", f);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res 2: f(5) = " + f.calculate(5));
Res 1: f(2) = 4.0
Res 2: f(5) = 25.0

Case 2: Handy function constructor, but slower proces of function creation (performance of creation slower, but calculation the same)

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f(x) = x^2");
Expression e = new Expression("f(2)", f);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res 2: f(5) = " + f.calculate(5));
Res 1: f(2) = 4.0
Res 2: f(5) = 25.0

Case 3: Function with more parameters

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f(a, b, c) = a+b+c");
Expression e = new Expression("f(1,2,3)", f);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res 2: f(1,2,3) = " + f.calculate(1,2,3));
Res 1: f(1,2,3) = 6.0
Res 2: f(1,2,3) = 6.0

Case 4: Function in function

import org.mariuszgromada.math.mxparser.*;
...
Function g = new Function("g(x) = 2*x");
Function f = new Function("f(x) = g(x)^2", g);
		
mXparser.consolePrintln("Res 1: g(1) = " + g.calculate(1));
mXparser.consolePrintln("Res 2: f(1) = " + f.calculate(1));
Res 1: g(1) = 2.0
Res 2: f(1) = 4.0

Case 5: Implementing your own Function Extension

import org.mariuszgromada.math.mxparser.*;
...
...
/*
 * Implementing FunctionExtension interface
 */
public class Addition implements FunctionExtension {
   double x;
   double y;
   public Addition() {
      x = Double.NaN;
      y = Double.NaN;
   }
   public Addition(double x, double y) {
      this.x = x;
      this.y = y;
   }
   public int getParametersNumber() {
      return 2;
   }
   public void setParameterValue(int argumentIndex, double argumentValue) {
      if (argumentIndex == 0) x = argumentValue;
      if (argumentIndex == 1) y = argumentValue;
   }
   public double calculate(double... params) {
      return x+y;
   }
   public FunctionExtension clone() {
      return new Addition(x, y);
   }   
}
import org.mariuszgromada.math.mxparser.*;
...
...
/*
* Creating extended function
*/
Function f = new Function("f", new Addition());
mXparser.consolePrintln("f.calculate(1,2) = " + f.calculate(1,2) );
/*
* Using extended function in expression
*/
Expression e = new Expression("f(2,3)", f);
mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate() );
[mXparser-v.4.0.0] f.calculate(1,2) = 3.0
[mXparser-v.4.0.0] f(2,3) = 5.0

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

Enjoy! 🙂

 

Best regards,

Mariusz Gromada

Download latest release – v.4.0.0.2: bin + doc + src (.zip 10.7 MB)

Download latest release – v.4.0.0.2: bin only (.zip 3.04 MB) – separate binaries for java: 1.5, 1.6, 1.7, 1.8; .NET: 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, .NET Core, .NET Standard, .NET PCL, Xamarin.Android, Xamarin.iOS

Source code .zipSource code .tar.gz View on GitHubMathSpace.pl