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. To copy the code, double-click inside the frame.

Case 1: Removing built-in tokens

// JAVA
// ...
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());
// C#
// ...
using org.mariuszgromada.math.mxparser;

/*
 * Removing built-in tokens methods:
 *
 * mXparser.removeBuiltinTokens(params String[] tokens)
 * mXparser.unremoveBuiltinTokens(params 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 : ");
foreach (String t in 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.2.1] *** Example 1: Before tokens removal
[mXparser-v.5.2.1] 

[sin(pi)+cos(pi)]: Starting syntax check...
[sin(pi)+cos(pi)]: No errors detected.
[mXparser-v.5.2.1] *** Example 2: Removing "sin", "cos" tokens
[mXparser-v.5.2.1] 

[sin(pi)+cos(pi)]: Starting syntax check...
[sin(pi)+cos(pi)]: Token 'sin', index 1: Invalid token.
[sin(pi)+cos(pi)]: Token 'cos', index 6: Invalid token.
[sin(pi)+cos(pi)]: Errors have been found.
[mXparser-v.5.2.1] *** Example 4: Printing removed built-in tokens : sin, cos, 
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] *** Example 4: Un-removing built-in token
[mXparser-v.5.2.1] 

[sin(pi)+cos(pi)]: Starting syntax check...
[sin(pi)+cos(pi)]: Token 'sin', index 1: Invalid token.
[sin(pi)+cos(pi)]: Errors have been found.
[mXparser-v.5.2.1] *** Example 5: Un-removing all built-in token
[mXparser-v.5.2.1] 

[sin(pi)+cos(pi)]: Starting syntax check...
[sin(pi)+cos(pi)]: No errors detected.

Case 2: Modifying built-in tokens

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

// To counter the possible ambiguity between
// implied multiplication and unknown tokens
mXparser.disableImpliedMultiplicationMode();

/*
 * 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());
// C#
// ...
using org.mariuszgromada.math.mxparser;

// To counter the possible ambiguity between
// implied multiplication and unknown tokens
mXparser.disableImpliedMultiplicationMode();

/*
 * Modifying built-in tokens methods:
 *
 * mXparser.modifyBuiltinToken(String currentToken, String newToken)
 * mXparser.modifyBuiltinToken(String currentToken, String newToken, String newTokenDescription)
 * mXparser.unmodifyBuiltinTokens(params 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.2.1] *** Example 1: "sine" and "cosine" are not defined
[mXparser-v.5.2.1] 

[sine(pi)+cosine(pi)]: Starting syntax check...
[sine(pi)+cosine(pi)]: Token 'sine', index 1: Invalid token.
[sine(pi)+cosine(pi)]: Token 'cosine', index 6: Invalid token.
[sine(pi)+cosine(pi)]: Errors have been found.
[mXparser-v.5.2.1] *** Example 2: Changing "sin" to "sine"
[mXparser-v.5.2.1] 

[sine(pi)+cosine(pi)]: Starting syntax check...
[sine(pi)+cosine(pi)]: Token 'cosine', index 6: Invalid token.
[sine(pi)+cosine(pi)]: Errors have been found.
[mXparser-v.5.2.1] *** Example 3: Changing "cos" to "cosine" + setting new description
[mXparser-v.5.2.1] 

[sine(pi)+cosine(pi)]: Starting syntax check...
[sine(pi)+cosine(pi)]: No errors detected.
[mXparser-v.5.2.1] Help content limited to query: 'My new cosine'
  #  Keyword             Type                    Syntax                                        Since Description
  -  --------            ----                    ------                                        ----- -----------
  1. cosine              <Unary function>        cosine(x)                                     1.0   My new cosine token
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] *** Example 4: Printing modified built-in tokens: 
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] sin -> sine, null
[mXparser-v.5.2.1] cos -> cosine, My new cosine token
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] *** Example 5: Un-modifying "sin" token
[mXparser-v.5.2.1] 

[sine(pi)+cosine(pi)]: Starting syntax check...
[sine(pi)+cosine(pi)]: Token 'sine', index 1: Invalid token.
[sine(pi)+cosine(pi)]: Errors have been found.
[mXparser-v.5.2.1] *** Example 6: Un-modifying all modified token
[mXparser-v.5.2.1] 

[sine(pi)+cosine(pi)]: Starting syntax check...
[sine(pi)+cosine(pi)]: Token 'sine', index 1: Invalid token.
[sine(pi)+cosine(pi)]: Token 'cosine', index 6: Invalid token.
[sine(pi)+cosine(pi)]: Errors have been found.

Case 3: Overriding built-in tokens

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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.2.1] *** Example 1: user defined function and user defined constant with name conflict
[mXparser-v.5.2.1] 

[sin(pi)]: Starting syntax check...
[sin(pi)]: Keyword 'pi': Duplicated keywords were found. Check user-defined items. Consider using option to override built-in tokens.
[sin(pi)]: Errors have been found.
[mXparser-v.5.2.1] *** Example 2: Setting option to override built-in tokens
[mXparser-v.5.2.1] 

[sin(pi)]: Starting syntax check...
[sin(pi)]: Token 'sin', index 1: Starting syntax check of the user-defined function.
[sin(pi)]: -> [sin] = [2*x] Starting syntax check...
[sin(pi)]: -> [sin] = [2*x] No errors detected.
[sin(pi)]: No errors detected.
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] sin(pi) = 2.0
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] *** Example 3: Checking override built-in tokens option
[mXparser-v.5.2.1]  Option = true
[mXparser-v.5.2.1] 
[mXparser-v.5.2.1] *** Example 4: Setting option not to override built-in tokens
[mXparser-v.5.2.1] 

[sin(pi)]: Starting syntax check...
[sin(pi)]: Keyword 'pi': Duplicated keywords were found. Check user-defined items. Consider using option to override built-in tokens.
[sin(pi)]: Errors have been found.
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 6.0.0

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency

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

Maven – Gradle

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

Maven – Gradle (Kotlin)

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

GitHub

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

OTHER DOWNLOAD OPTIONS

Download latest release – v.6.0.0 Picon: .NET bin onlyDownload latest release – v.6.0.0 Picon: JAVA bin onlyDownload latest release – v.6.0.0 Picon: 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