Back to: Main Page > Mathematical programs

Mathematical expression parser in C++


Download the source (zipped tar file)

Description
Example
Copying
Thanks

Last updated: September 11, 2008.


Description

The title is quite self-explanatory: with this module, the user can type a mathematical formula, which is then built into an object allowing to do computations with this formula.

This parser is intended to be simple to use both for the programmer and the user. It offers some formal calculus possibilities, such as differentiation.

The parser will try to read as much as possible in the user's mind. For example, an input like sinxcos(x)^2 will be interpreted as (sin(x))*(cos(x))^2

For the programmer, it is object-oriented. A constructor simply takes a string expression and variable names and returns an object which can be used to get values of the formula, or combined with other objects of the same type. The example below should make it clear.

As a counterpart, the code is not very clean...

This program I used, for example, to write a program for Drawing vector fields, differential equations, level curves...

There are two groups of files, one for dealing with real expressions and the other one for complex expressions(bearing the same names suffixed with _c).

I have had problems compiling the complex library with g++. I can't find two computers where it works the same way. It may be necessary to slighlty change the headers of the complex files.

Note that I began writing this at a time when the C++ standard library did not exist. I haven't had the energy to update everything, hence the use of old-fashioned C-style strings and arrays.


Example

See a sample file on how to use the code.

The command
g++ example.cpp mathexpr.cpp -lm
should leave you with a working sample executable.


Copying

This software is available under the MIT Licence:

Copyright (c) 1997-2000 Yann OLLIVIER

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Thanks

Thanks to Werner Jacobs, Valts Silaputnins and Olivier Wittenberg for bug correction.


Back to: Main Page > Mathematical programs

To leave a comment: contact (domain) yann-ollivier.org