Playing with expression tokens

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

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical.

Case 1: Printing expression tokens

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
mXparser.consolePrintTokens(e.getCopyOfInitialTokens());
[mXparser-v.5.0.0]  --------------------
[mXparser-v.5.0.0] | Expression tokens: |
[mXparser-v.5.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.0.0] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.5.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.0.0] |           0 |           1 |       _num_ |           1 |           0 |           0 |         1.0 |             |
[mXparser-v.5.0.0] |           1 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           2 |           2 |       _num_ |           1 |           0 |           0 |         2.0 |             |
[mXparser-v.5.0.0] |           3 |           ^ |           ^ |           5 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           4 |           3 |       _num_ |           1 |           0 |           0 |         3.0 |             |
[mXparser-v.5.0.0] |           5 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           6 |         sin |         sin |           1 |           4 |           1 |         NaN |             |
[mXparser-v.5.0.0] |           7 |           ( |           ( |           1 |          20 |           2 |         NaN |             |
[mXparser-v.5.0.0] |           8 |          10 |       _num_ |           1 |           0 |           2 |        10.0 |             |
[mXparser-v.5.0.0] |           9 |           + |           + |           1 |           1 |           2 |         NaN |             |
[mXparser-v.5.0.0] |          10 |         cos |         cos |           2 |           4 |           3 |         NaN |             |
[mXparser-v.5.0.0] |          11 |           ( |           ( |           1 |          20 |           4 |         NaN |             |
[mXparser-v.5.0.0] |          12 |          20 |       _num_ |           1 |           0 |           4 |        20.0 |             |
[mXparser-v.5.0.0] |          13 |           - |           - |           2 |           1 |           4 |         NaN |             |
[mXparser-v.5.0.0] |          14 |           1 |       _num_ |           1 |           0 |           4 |         1.0 |             |
[mXparser-v.5.0.0] |          15 |           ) |           ) |           2 |          20 |           4 |         NaN |             |
[mXparser-v.5.0.0] |          16 |           ) |           ) |           2 |          20 |           2 |         NaN |             |
[mXparser-v.5.0.0] |          17 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |          18 |          30 |       _num_ |           1 |           0 |           0 |        30.0 |             |
[mXparser-v.5.0.0]  ---------------------------------------------------------------------------------------------------------------

Case 2: Using tokens to print expression in a fancy way

import org.mariuszgromada.math.mxparser.*;
import org.mariuszgromada.math.mxparser.parsertokens.Token;
import java.util.List;
...
/* Expression definition */
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
/* Getting list of tokens */
List<Token> tokens = e.getCopyOfInitialTokens();
/* Evaluating max level */
int mxlev = 0;
for (Token t : tokens)
    if (t.tokenLevel > mxlev) mxlev = t.tokenLevel;
/* Printing tokens */
mXparser.consolePrintln(e.getExpressionString());
mXparser.consolePrintln("--------------------------");
for (int level = 0; level <= mxlev; level++) {
    for (Token t : tokens) {
        String s;
        if (t.tokenLevel == level) s = t.tokenStr;
        else {
            s = "";
            for (int i = 0; i < t.tokenStr.length(); i++)
                s = s + " ";
        }
        mXparser.consolePrint(s);
    }
    mXparser.consolePrintln();
}
[mXparser-v.5.0.0] 1+2^3-sin(10+cos(20-1))+30
[mXparser-v.5.0.0] --------------------------
[mXparser-v.5.0.0] 1+2^3-                 +30
[mXparser-v.5.0.0]       sin                 
[mXparser-v.5.0.0]          (10+         )   
[mXparser-v.5.0.0]              cos          
[mXparser-v.5.0.0]                 (20-1)    

Case 3: Playing with invalid tokens

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("pi+var1-fun1(var1)+fun2(var2)-1pi");
mXparser.consolePrintTokens(e.getCopyOfInitialTokens());

Please check “Looks Like” field

[mXparser-v.5.0.0]  --------------------
[mXparser-v.5.0.0] | Expression tokens: |
[mXparser-v.5.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.0.0] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.5.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.0.0] |           0 |          pi |          pi |           1 |           9 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           1 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           2 |        var1 |             |          -1 |          -1 |           0 |         NaN |    argument |
[mXparser-v.5.0.0] |           3 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           4 |        fun1 |             |          -1 |          -1 |           0 |         NaN |    function |
[mXparser-v.5.0.0] |           5 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.5.0.0] |           6 |        var1 |             |          -1 |          -1 |           1 |         NaN |    argument |
[mXparser-v.5.0.0] |           7 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.5.0.0] |           8 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |           9 |        fun2 |             |          -1 |          -1 |           0 |         NaN |    function |
[mXparser-v.5.0.0] |          10 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.5.0.0] |          11 |        var2 |             |          -1 |          -1 |           1 |         NaN |    argument |
[mXparser-v.5.0.0] |          12 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.5.0.0] |          13 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |          14 |           1 |             |           1 |           0 |           0 |         1.0 |             |
[mXparser-v.5.0.0] |          15 |           * |             |           3 |           1 |           0 |         NaN |             |
[mXparser-v.5.0.0] |          16 |          pi |             |           1 |           9 |           0 |         NaN |             |
[mXparser-v.5.0.0]  ---------------------------------------------------------------------------------------------------------------
Nuget
Install-Package MathParser.org-mXparser -Version 5.0.2
Maven
<dependency>
<groupid>org.mariuszgromada.math</groupid>
<artifactid>MathParser.org-mXparser</artifactid>
<version>5.0.2</version>
</dependency>
Gradle
implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.0.2'
Gradle (Kotlin)
implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.0.2")
GitHub
git clone https://github.com/mariuszgromada/MathParser.org-mXparser
OTHER DOWNLOAD OPTIONS

Download latest release – v.5.0.2 Leonis: bin + docDownload latest release – v.5.0.2 Leonis: bin only, includes separate binaries for various .NET platforms and Java versions

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