Derivatives & Integrals

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

The tutorial consists of more than 200 live examples from 50 sections given separately for JAVA, C# and C++. Each of the examples can be copied and run on your own environment. In addition, mXparser provides an extensive collection of over 500 built-in math functions, expressions and symbols. Familiarize yourself with the scope and the syntax. Live testing is the best way to learn. Good luck! 🙂

Tutorial Math Collection API spec Download

Below is the code for JAVA, C# (the code for C# is almost identical) and C++. To copy the code, double-click inside the frame.

Case 1: General derivative – at given point

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("cos(1) - der(sin(x), x, 1)");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("cos(1) - der(sin(x), x, 1)");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 1");
ExpressionPtr e = new_Expression("cos(x) - der(sin(x), x)", x);
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: cos(x) - der(sin(x), x) = 8.588628E-10

Case 3: Left / right derivative – at given point

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e1 = new_Expression("der-(abs(x), x, 0)");
ExpressionPtr e2 = new_Expression("der+(abs(x), x, 0)");

mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate());
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 0");
ExpressionPtr e1 = new_Expression("der-(abs(x), x)", x);
ExpressionPtr e2 = new_Expression("der+(abs(x), x)", x);
     
mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate());
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Derivative from Taylor series approximation of sin(x)*/
ExpressionPtr 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());

ExpressionPtr 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());

ExpressionPtr 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());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 1");

/* Derivative from Taylor series approximation of sin(x)*/
ExpressionPtr 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());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2 * int( sqrt(1-x^2), x, -1, 1 )");
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2 * int( sqrt(1-x^2), x, -1, 1 )");
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 2 * int( sqrt(1-x^2), x, -1, 1 ) = 3.1415920928388927
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser -Version 6.1.0

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency (Java, Kotlin, Scala, Groovy, …)

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

Maven – Gradle

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

CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)

include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY
https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG
v.6.1.0
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
target_link_libraries(YourExecutable
MathParserOrgMxParser)

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.6.1.0 Sagitara: .NET bin onlyDownload latest release – v.6.1.0 Sagitara: JAVA bin onlyDownload latest release – v.6.1.0 Sagitara: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

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

My other creative spaces

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE