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

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 | (1) | (2) | double | MIT | ~ 10.000.000 | ||||||||

muparserSSE | float | max. 10 | MIT | ~ 20.000.000 | |||||||||

muparserX | double | BSD New | ~ 1.600.000 |

- Supported data types: double, integer, complex, boolean, string, array
- Extensible with custom operators (binary, infix or postfix)
- Extensible with custom functions with an arbitrary number of function arguments
- Support for an unlimited number of variables and constants
- No limit on expression complexity
- Reads binary, hexadecimal, complex, integer and string values from expressions and can be extended to read user defined values as well.
- Supports a large variety of predefined operators, functions and constants.
- Written in standard compliant C++ code with no external dependencies.

By default the parser supports the following mathematical constants:

- The eulerian number with:
`e = 2.718281828459045235360287`

- Pi with
`pi = 3.141592653589793238462643`

- The imaginary unit with:
`i = sqrt(-1)`

- Standard operators:
`+, -, *, /, ^`

- Assignment operators:
`=, +=, -=, *=, /=`

- Logical operators:
`and, or, xor, ==, !=, >, <, <=, >=`

- Bit manipulation:
`&, |, <<, >>`

- String concatenation:
`//`

- if then else conditionals with lazy evaluation:
`?:`

- Unit postfixes (nano, micro, milli, kilo, giga, mega):
`n, mu, m, k, G, M`

- Transpose operator:
`'`

- Sign operator and type conversions:
`-, (float), (int)`

- Multidimensional index operator:
`a[1,2]`

- Ternary if-then-else operator:
`(a<b) ? c:d`

- In-place array creation:
`{1,2,3}`

- Standard functions
`abs, sin, cos, tan, sinh, cosh, tanh, ln, log, log10, exp, sqrt`

- Unlimited number of arguments:
`min, max, sum`

- String functions:
`str2dbl, strlen, toupper`

- Complex functions:
`real, imag, conj, arg, norm`

- Array functions:
`sizeof, eye, ones, zeros`

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: