Built-in tokens (key words) manipulation

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

The tutorial consists of more than 200 live examples from 50 sections. 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, the code for C# is almost identical.

Case 1: Removing built-in tokens

import org.mariuszgromada.math.mxparser.*;
...
/*
 * Removing built-in tokens methods:
 *
 * mXparser.removeBuiltinTokens(String... tokens)
 * mXparser.unremoveBuiltinTokens(String... tokens)
 * mXparser.unremoveAllBuiltinTokens()
 * mXparser.getBuiltinTokensToRemove()
 */
Expression e;
/*
 * Example 1: Before tokens removal
 */
e = new Expression("sin(pi)+cos(pi)");
e.checkSyntax();
mXparser.consolePrintln("*** Example 1: Before tokens removal");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 2: Removing "sin", "cos" tokens
 */
mXparser.removeBuiltinTokens("sin", "cos");
e = new Expression("sin(pi)+cos(pi)");
e.checkSyntax();
mXparser.consolePrintln("*** Example 2: Removing \"sin\", \"cos\" tokens");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 3: Printing removed built-in tokens
 */
mXparser.consolePrint("*** Example 4: Printing removed built-in tokens : ");
for (String t : mXparser.getBuiltinTokensToRemove() )
    mXparser.consolePrint(t + ", ");
mXparser.consolePrintln();
mXparser.consolePrintln();
/*
 * Example 4: Un-removing built-in token
 */
mXparser.unremoveBuiltinTokens("cos");
e.checkSyntax();
mXparser.consolePrintln("*** Example 4: Un-removing built-in token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 5: Un-removing all built-in token
 */
mXparser.unremoveAllBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 5: Un-removing all built-in token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
[mXparser-v.5.0.0] *** Example 1: Before tokens removal
[mXparser-v.5.0.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] no errors.

[mXparser-v.5.0.0] *** Example 2: Removing "sin", "cos" tokens
[mXparser-v.5.0.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] (sin, 0) invalid <TOKEN>.
[sin(pi)+cos(pi)] (cos, 5) invalid <TOKEN>.
[sin(pi)+cos(pi)] errors were found.

[mXparser-v.5.0.0] *** Example 4: Printing removed built-in tokens : sin, cos, 
[mXparser-v.5.0.0] 
[mXparser-v.5.0.0] *** Example 4: Un-removing built-in token
[mXparser-v.5.0.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] (sin, 0) invalid <TOKEN>.
[sin(pi)+cos(pi)] errors were found.

[mXparser-v.5.0.0] *** Example 5: Un-removing all built-in token
[mXparser-v.5.0.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] no errors.

[mXparser-v.5.0.0] 

Case 2: Modifying built-in tokens

import org.mariuszgromada.math.mxparser.*;
...
/*
 * Modifying built-in tokens methods:
 *
 * mXparser.modifyBuiltinToken(String currentToken, String newToken)
 * mXparser.modifyBuiltinToken(String currentToken, String newToken, String newTokenDescription)
 * mXparser.unmodifyBuiltinTokens(String... currentOrNewTokens)
 * mXparser.unmodifyAllBuiltinTokens()
 * mXparser.getBuiltinTokensToModify()
 */
Expression e;
/*
 * Example 1: "sine" and "cosine" are not defined
 */
e = new Expression("sine(pi)+cosine(pi)");
e.checkSyntax();
mXparser.consolePrintln("*** Example 1: \"sine\" and \"cosine\" are not defined");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 2: Changing "sin" to "sine"
 */
mXparser.modifyBuiltinToken("sin", "sine");
e.checkSyntax();
mXparser.consolePrintln("*** Example 2: Changing \"sin\" to \"sine\"");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 3: Changing "cos" to "cosine" + setting new description
 */
mXparser.modifyBuiltinToken("cos", "cosine", "My new cosine token");
e.checkSyntax();
mXparser.consolePrintln("*** Example 3: Changing \"cos\" to \"cosine\" + setting new description");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
mXparser.consolePrintHelp("My new cosine");
mXparser.consolePrintln();
/*
 * Example 4: Printing modified built-in tokens
 */
mXparser.consolePrintln("*** Example 4: Printing modified built-in tokens: ");
String[][] modifiedTokens = mXparser.getBuiltinTokensToModify();
mXparser.consolePrintln();
for (int i = 0; i < modifiedTokens.length; i++) {
    String currentToken = modifiedTokens[i][0];
    String newToken = modifiedTokens[i][1];
    String newTokenDescription = modifiedTokens[i][2];
    mXparser.consolePrintln(currentToken + " -> " + newToken + ", " + newTokenDescription);
}
mXparser.consolePrintln();
/*
 * Example 5: Un-modifying "sin" token
 */
e = new Expression("sine(pi)+cosine(pi)");
mXparser.unmodifyBuiltinTokens("sin");
e.checkSyntax();
mXparser.consolePrintln("*** Example 5: Un-modifying \"sin\" token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 6: Un-modifying all modified token
 */
mXparser.unmodifyAllBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 6: Un-modifying all modified token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
[mXparser-v.5.0.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (sin, 0) <FUNCTION> expecting 1 argument.
[sine(pi)+cosine(pi)] (cos, 8) <FUNCTION> expecting 1 argument.
[sine(pi)+cosine(pi)] (in, 10) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.5.0.0] *** Example 2: Changing "sin" to "sine"
[mXparser-v.5.0.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (cos, 5) <FUNCTION> expecting 1 argument.
[sine(pi)+cosine(pi)] (in, 7) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.5.0.0] *** Example 3: Changing "cos" to "cosine" + setting new description
[mXparser-v.5.0.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] no errors.

[mXparser-v.5.0.0] Help content: 

    #  key word            type                    syntax                                        since description
    -  --------            ----                    ------                                        ----- -----------
   73. cosine              <Unary Function>        cosine(x)                                     1.0   My new cosine token


[mXparser-v.5.0.0] *** Example 4: Printing modified built-in tokens: 
[mXparser-v.5.0.0] 
[mXparser-v.5.0.0] sin -> sine, null
[mXparser-v.5.0.0] cos -> cosine, My new cosine token
[mXparser-v.5.0.0] 
[mXparser-v.5.0.0] *** Example 5: Un-modifying "sin" token
[mXparser-v.5.0.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (sin, 0) <FUNCTION> expecting 1 argument.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.5.0.0] *** Example 6: Un-modifying all modified token
[mXparser-v.5.0.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (sin, 0) <FUNCTION> expecting 1 argument.
[sine(pi)+cosine(pi)] (cos, 8) <FUNCTION> expecting 1 argument.
[sine(pi)+cosine(pi)] (in, 10) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.5.0.0] 

Case 3: Overriding built-in tokens

import org.mariuszgromada.math.mxparser.*;
...
/*
 * Overriding built-in tokens methods:
 *
 * mXparser.setToOverrideBuiltinTokens();
 * mXparser.setNotToOverrideBuiltinTokens();
 * mXparser.checkIfsetToOverrideBuiltinTokens();
 */
Expression e;
/*
 * Example 1: user defined function and user defined constant with name conflict
 */
Function sin = new Function("sin(x) = 2*x");
Constant pi = new Constant("pi = 1");
e = new Expression("sin(pi)", sin, pi);
e.checkSyntax();
mXparser.consolePrintln("*** Example 1: user defined function and user defined constant with name conflict");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 2: Setting option to override built-in tokens
 */
mXparser.setToOverrideBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 2: Setting option to override built-in tokens");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
mXparser.consolePrintln();
mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
/*
 * Example 3: Checking override built-in tokens option
 */
mXparser.consolePrintln();
mXparser.consolePrintln("*** Example 3: Checking override built-in tokens option");
mXparser.consolePrintln(" Option = " + mXparser.checkIfsetToOverrideBuiltinTokens());
/*
 * Example 4: Setting option not to override built-in tokens
 */
mXparser.consolePrintln();
e = new Expression("sin(pi)", sin, pi);
mXparser.setNotToOverrideBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 4: Setting option not to override built-in tokens");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
[mXparser-v.5.0.0] *** Example 1: user defined function and user defined constant with name conflict
[mXparser-v.5.0.0] 

[sin(pi)] checking ...
[sin(pi)] (pi) Duplicated <KEYWORD>.
[sin(pi)] (sin) Duplicated <KEYWORD>.
[sin(pi)] errors were found.

[mXparser-v.5.0.0] *** Example 2: Setting option to override built-in tokens
[mXparser-v.5.0.0] 

[sin(pi)] checking ...
[sin(pi)] (sin, 0) checking user defined function ...
[sin(pi)] -> [sin] = [ 2*x] checking ...
[sin(pi)] -> [sin] = [ 2*x] no errors.
[sin(pi)] no errors.

[mXparser-v.5.0.0] 
[mXparser-v.5.0.0] sin(pi) = 2.0
[mXparser-v.5.0.0] 
[mXparser-v.5.0.0] *** Example 3: Checking override built-in tokens option
[mXparser-v.5.0.0]  Option = true
[mXparser-v.5.0.0] 
[mXparser-v.5.0.0] *** Example 4: Setting option not to override built-in tokens
[mXparser-v.5.0.0] 

[sin(pi)] checking ...
[sin(pi)] (pi) Duplicated <KEYWORD>.
[sin(pi)] (sin) Duplicated <KEYWORD>.
[sin(pi)] errors were found.

[mXparser-v.5.0.0] 
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.2.0

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency

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

Maven – Gradle

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

Maven – Gradle (Kotlin)

implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.2.0")

GitHub

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

OTHER DOWNLOAD OPTIONS

Download latest release – v.5.2.0 Orion: .NET bin onlyDownload latest release – v.5.2.0 Orion: JAVA bin onlyDownload latest release – v.5.2.0 Orion: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

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

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