I am looking for an algorithm that I can use to evaluate mathematical expressions. I've seen a few questions on SO that are simmilar but the answers are C#/Delphi or python specific. I need to write the algorithm in C :)
The problem I am trying to solve is given a user input like
3*(2*x + 1)/x
I can evaluate the expression for any value of x.
What algorithms are available to do this? If you would like to suggest a library that already does this, then I would prefer a C library
Thank you
You can use Shunting-yard algorithm, it work's great and enables you to parse functions etc. at ease. It doesn't actually compute it, but it converts an expression to ONP, which can be evaluated very easily.
You should take a look at TinyExpr. It does exactly what you're looking for, is self-contained in a single ANSI C source file, and is permissibly licensed (zlib license).
Here's what the code would look like to solve your example problem of
3*(2*x + 1)/x
:Note that you can "compile" once and then evaluate many times with different values of
x
. For some expressions, it's only a few percent slower than native C code.The algorithm you need here is the Shunting Yard Algorithm.
This allows you to convert an in-fix expression into Reverse Polish notation, which is quite simple to evaluate programatically.
The Shunting Yard Algorithm is quite involved, but my experiences is that you can code it up just as its written and it all works - you don't have to go to the trouble of analysing it.
I asked Google for "recursive descent expression parser" (I don't blame you for not knowing what to look for) and found Parsing Expressions by Recursive Descent which provides an introduction to some useful parsing techniques.
Also, the Wikipedia article on Recursive descent parser includes a fairly complete example in C.
An alternative to implementing your own parser and expression evaluator would be to link against a library that provides one for you to use. An interesting choice would be an easily embedded scripting language such as Lua.
It is straightforward to set up a Lua interpreter instance, and pass it expressions to be evaluated, getting back a function to call that evaluates the expression. You can even let the user have variables...
Update: LE, A simple expression evaluator using Lua
Here is a sketchy implementation of a simple expression evaluator based on a Lua interpreter. I compiled this and tried it for a few cases, but it certainly should not be trusted in production code without some attention to error handling and so forth. All the usual caveats apply here.
I compiled and tested this on Windows using Lua 5.1.4 from Lua for Windows. On other platforms, you'll have to find Lua from your usual source, or from www.lua.org.
Public interface to LE
Here is the file
le.h
:Sample code using LE
Here is the file t-le.c, demonstrating a simple use of this library. It takes its single command-line argument, loads it as an expression, and evaluates it with the global variable x changing from 0.0 to 1.0 in 11 steps:
Here is some output from t-le:
Implementation of LE
Here is
le.c
, implementing the Lua Expression evaluator:Remarks
The above sample consists of 189 lines of code total, including a spattering of comments, blank lines, and the demonstration. Not bad for a quick function evaluator that knows how to evaluate reasonably arbitrary expressions of one variable, and has rich library of standard math functions at its beck and call.
You have a Turing-complete language underneath it all, and it would be an easy extension to allow the user to define complete functions as well as to evaluate simple expressions.