Playing with expression tokens

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: Printing expression tokens

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");

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

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

// JAVA
// ...
import org.mariuszgromada.math.mxparser.*;
import org.mariuszgromada.math.mxparser.parsertokens.*;
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();
}
// C#
// ...
using org.mariuszgromada.math.mxparser;
using org.mariuszgromada.math.mxparser.parsertokens;
using System;
using System.Collections.Generic;

/* 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;
foreach (Token t in tokens)
    if (t.tokenLevel > mxlev) mxlev = t.tokenLevel;

/* Printing tokens */
mXparser.consolePrintln(e.getExpressionString());
mXparser.consolePrintln("--------------------------");
for (int level = 0; level <= mxlev; level++) {
    foreach (Token t in 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.2.1] 41+2^3-sin(10+cos(20-1))+30
[mXparser-v.5.2.1] --------------------------
[mXparser-v.5.2.1] 1+2^3-                 +30
[mXparser-v.5.2.1]       sin                 
[mXparser-v.5.2.1]          (10+         )   
[mXparser-v.5.2.1]              cos          
[mXparser-v.5.2.1]                 (20-1)   

Case 3: Playing with invalid tokens

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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.2.1]  --------------------
[mXparser-v.5.2.1] | Expression tokens: |
[mXparser-v.5.2.1]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.2.1] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.5.2.1]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.2.1] |           0 |          pi |          pi |           1 |           9 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           1 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           2 |        var1 |             |          -1 |          -1 |           0 |         NaN |    argument |
[mXparser-v.5.2.1] |           3 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           4 |        fun1 |             |          -1 |          -1 |           0 |         NaN |    function |
[mXparser-v.5.2.1] |           5 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.5.2.1] |           6 |        var1 |             |          -1 |          -1 |           1 |         NaN |    argument |
[mXparser-v.5.2.1] |           7 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.5.2.1] |           8 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           9 |        fun2 |             |          -1 |          -1 |           0 |         NaN |    function |
[mXparser-v.5.2.1] |          10 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.5.2.1] |          11 |        var2 |             |          -1 |          -1 |           1 |         NaN |    argument |
[mXparser-v.5.2.1] |          12 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.5.2.1] |          13 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |          14 |           1 |             |           1 |           0 |           0 |         1.0 |             |
[mXparser-v.5.2.1] |          15 |           * |             |           3 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |          16 |          pi |             |           1 |           9 |           0 |         NaN |             |
[mXparser-v.5.2.1]  ---------------------------------------------------------------------------------------------------------------
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