Derivatives & Integrals

Case 1: General derivative – at given point

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e = new Expression("cos(1) - der(sin(x), x, 1)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res: cos(1) - der(sin(x), x, 1) = 8.588628E-10


Case 2: General derivative – point given by argument value

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Argument x = new Argument("x = 1");
Expression e = new Expression("cos(x) - der(sin(x), x)", x);
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res: cos(x) - der(sin(x), x) = 8.588628E-10


Case 3: Left / right derivative – at given point

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e1 = new Expression("der-(abs(x), x, 0)");
Expression e2 = new Expression("der+(abs(x), x, 0)");

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());

[mXparser-v.5.2.1] Res 1: der-(abs(x), x, 0) = -1.0
[mXparser-v.5.2.1] Res 2: der+(abs(x), x, 0) = 1.0


Case 4: Left / right derivative – point given by argument value

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Argument x = new Argument("x = 0");
Expression e1 = new Expression("der-(abs(x), x)", x);
Expression e2 = new Expression("der+(abs(x), x)", x);

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());

[mXparser-v.5.2.1] Res 1: der-(abs(x), x) = -1.0
[mXparser-v.5.2.1] Res 2: der+(abs(x), x) = 1.0


Case 5: Derivative from more complex function – at given point

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...

/* Derivative from Taylor series approximation of sin(x)*/
Expression e1 = new Expression("cos(1) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x, 1)");
mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());

Expression e2 = new Expression("cos(2) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x, 2)");
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());

Expression e3 = new Expression("cos(3) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x, 3)");
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());

[mXparser-v.5.2.1] Res 1: cos(1) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x, 1) = 8.588628E-10
[mXparser-v.5.2.1] Res 2: cos(2) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x, 2) = -2.6459193E-9
[mXparser-v.5.2.1] Res 3: cos(3) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x, 3) = -1.5994834E-9


Case 6: Derivative from more complex function – point given by argument value

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Argument x = new Argument("x = 1");

/* Derivative from Taylor series approximation of sin(x)*/
Expression e = new Expression("cos(x) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x)", x);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());

x.setArgumentValue(2);
mXparser.consolePrintln("Res 2: " + e.getExpressionString() + " = " + e.calculate());

x.setArgumentValue(3);
mXparser.consolePrintln("Res 3: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res 1: cos(x) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x) = 8.588628E-10
[mXparser-v.5.2.1] Res 2: cos(x) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x) = -2.6459193E-9
[mXparser-v.5.2.1] Res 3: cos(x) - der( sum(k,0,10,(-1)^k*(x^(2*k+1))/(2*k+1)!), x) = -1.5994834E-9


Case 7: Integrals – calculating pi by integration sqrt(1-x^2)

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e = new Expression("2 * int( sqrt(1-x^2), x, -1, 1 )");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res: 2 * int( sqrt(1-x^2), x, -1, 1 ) = 3.1415920928388927

Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.2.1

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser --version 5.2.1

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser" Version="5.2.1"/>

Maven – Dependency

<dependency><groupid>org.mariuszgromada.math</groupid><artifactid>MathParser.org-mXparser</artifactid><version>5.2.1</version></dependency>

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.2.1'

implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.2.1")
git clone https://github.com/mariuszgromada/MathParser.org-mXparser