Advertisement

muparserx - Math Parser Library

A C++ Library for Parsing Expressions with Strings, Complex Numbers, Vectors, Matrices and more.


Introducing the muparserx math parser library

The evaluation of a mathematical expression is a standard task required in many applications. It can be solved by either using a standard math expression parser such as muparser or by embedding a scripting language such as Lua. There are however some limitations: Although muparser is pretty fast it will only work with scalar values and although Lua is very flexible it does neither support binary operators for arrays nor complex numbers. So if you need a math expression parser with support for arrays, matrices and strings muparserx may be able to help you. It was originally based on the original muparser engine but has since evolved into a standalone project with a completely new parsing engine.

Parser Data types Precision User defined operators User defined functions Localization Licence Performance (Expr. per second)
complex scalar string vector Binary Postfix Infix Strings as parameters Arbitrary number of parameters
muparser somewhat(1) ok somewhat(2) fail double ok ok ok ok ok ok MIT ~ 10.000.000
muparserSSE fail ok fail fail float ok ok ok fail max. 10 ok MIT ~ 20.000.000
muparserX ok ok ok ok double ok ok ok ok ok fail BSD New ~ 1.600.000
Table 1: Feature comparison with other derivatives of muparser. (* Average performance calculated using this set of expressions; (1) muparser comes with an implementation for complex numbers but this is rather limited and more of a hack; (2) muparser can define strings but only as constants.)

Overview

Features

By default the parser supports the following mathematical constants:

Binary and ternary operators

Postfix Operators

Infix Operators

Special Operators

Predefined Functions

Sample expressions

The next table shows samples of expressions that can be evaluated using muparserx:

Expression Result Explanation
"hello"=="world" false Comparing strings
"hello "//"world" "hello world" String concatanation operator
sin(a+8i) ... Support for a variety of predefined functions working with complex numbers.
va[3]+vb[5] ... Support for array variables
va[3]=9 ... Assignment operator in combination with indexed access to a vector
toupper("hello"//"world") "HELLOWORLD" Transforming a concatenated string to uppercase.
#010010 18 Interpreting binary values
0x1eff 7935 Interpreting hex values
#10>0x1eff false Comparing binary and hex values
1+2-3*4/5^6 2.99923 The standard operators
a = ((a<b) ? 10 : -10) 10 or -10 depending on a and b Ternary operator for if-then-else conditionals

You might also like: