muparser - fast math parser library

About the the muparser library

Many applications require the parsing of mathematical expressions. The main objective muparser is to provide a fast, easy and secure way of doing this. muparser is an extensible high performance math expression parser library written in C++. It works by transforming a mathematical expression into bytecode and precalculating constant parts of the expression. For best performance muparser allows parallelized evaluation of expressions with OpenMP. parallelized evaluation of expressions with OpenMP.

The library was designed with portability in mind and should compile on every standard compliant C++ compiler. The build system is based on CMake. The code runs on both 32 bit and 64 bit architechtures and has been tested using Visual Studio 2019, CLANG and GCC. Code samples are provided in order to help you understand its usage. The library is open source and distributed under the BSD - Clause 2 "Simplified" Licence . The source code archive of the last stable version is available for download here:

Download Latest muparser release

The muparser library is in "production" state. Code changes are kept to a minimum with little active development. The main focus is put on bug fixing and keeping the archive compatible with the current generation of C++ compilers.

Acknowledgements

This library is open source. Over the course of time many people have contributed in one way or another to the project. The complete list of contributors can be viewed at GitHub. Special thanks to Julien Schueller and Francis Giraldeau for setting up the CMake build system as well as Travis-CI and Appveyor integration. There would not be a CMake based build system without them.

Example code

Using muparser is pretty straightforward. The following example defines a parser variable named "a" and adds a user defined functions named "MyFunc". When using the parser, make sure that you don't forget to catch possible of type Parser::exception_type.

#include <iostream>
#include "muParser.h"

double MySqr(double a_fVal) {  return a_fVal*a_fVal; }

int main(int argc, char *argv[])
{
	try
	{
		double var_a = 1;
		mu::Parser p;
		p.DefineVar("a", &var_a); 
		p.DefineFun("MySqr", MySqr); 
		p.SetExpr("MySqr(a)*_pi+min(10,a)");

		for (std::size_t a=0; a<100; ++a)
		{
			var_a = a;  // Change value of variable a
			std::cout << p.Eval() << std::endl;
		}
	}
	catch (mu::Parser::exception_type &e)
	{
		std::cout << e.GetMsg() << std::endl;
	}
	
	return 0;
}

Release Notes

Rev 2.3.5: 13.12.2024

  • Fix for #125 (bug in muParserDLL.cpp / mupDefineInfixOprt)
  • Bitwise "and" and "or" used the same precedence. (Now bitwise and priority is higher, like in C++)
  • fix for #127 minimum required cmake version set to 3.15
  • fix for #132 example1 fails to builds on Windows with mingw gcc
  • fix for #147 Build failed with MSVC/C++20

Rev 2.3.4: 06.11.2022

  • cmake is using OpenMP target and setting _UNICODE preprocessor definition
  • fix for issue #117 (sprintf deprecation warning)

Rev 2.3.3: 22.01.2022

  • Added a new option "-DENABLE_WIDE_CHAR" to CMake for building muparser with wide character support
  • Disabled compiler warning 26812 (Prefer 'enum class' over 'enum') I consider this a bogus warning. Use of plain old enums has not been deprecated and only MSVC is complaining.
  • Disabled compiler warning 4251 (... needs to have dll-interface to be used by clients of class ...) When the build system was changed to CMake Linux and Windows builds were unified. Each dynamic library contains the class interface as well as the C-interface. Before the linux shared library was using the class interface and the windows dll was using the C-interface. Only the C-Interface is safe to use when you intent to bring an executable to another linux distribution or windows version! This is up to the client software. I cannot change this because on linux the shared library was always using the class interface. Usually this is not a problem since distributions compile all applications from scratch. If you use the class interface you can not take for granted that your software will run with a muparser version compiled for another operating system or linux distribution! You must either use the C-Interface if you want this or use a static library build of muparser!
  • fix for https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24167
  • fix for https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24355
  • fixed a couple of issues for building the C-Interface (muParserDLL.cpp/.h) with wide character support.

Rev 2.3.0 and 2.3.1:

Those releases were shortlived and replaced within days due to several issues. Version 2.3.2 is the successor to version 2.2.6.

Rev 2.2.6: