What is this? Why did you write it?

This is the site of SymExpr, a generic Ada package that allow you some manipulation of "symbolic expressions."  I initially wrote this piece of code for another program of mine, aimed to simplify the creation of a GANNT chart.  My idea was to allow the user to specify the beginning and the end of the activities by giving constraints like "WP 4 will begin two months before the end of both WP 3 and WP 2." After finishing writing the code, I discovered that with minimal effort I could have made it a stand-alone package, so I did and SymExpr was born.

I find this code useful and I use it in my programs.  I release it, so maybe it could be useful to someone else.  

What can I do with it?

The expressions handled by SymExpr have the usual form with the four operations (+, -, * and /), the two unary operators (+ and -), function calls, variables and parenthesis. The package is a generic package parameterized by the type of the "values" used in the expressions (called "scalars" in the code), so you can have expressions of floats, of integers, but also of dates (my case), complexes, and so on...

  • You can create new expressions by parsing strings

         X := Parse("foo+(bar-3)*max(bar, foo)")

  •   You can build expressions using constructors and operators, for example  

         X := Variable("x");
         Y := X*X - 2*X + 1;

  • You can replace variables with scalars or other expressions, for example, 

         -- Replace every occurrence of "x" in Y with 42
         U := Replace(Y, "x", 42); 

         -- Makes V equal to (X*X)*(X*X) - 2*(X*X) + 1
         V := Replace(Y, "x", X*X);

  • You can evaluate an expression without variables to a scalar. For example, to plot a graph of Y you could use

         for I in -10..10 loop
           Plot(I, Eval(Replace(Y, "x", I)));
         end loop;
  • You can also solve "triangular" systems of equations such as
x = max(t,u) * min(y,a)
y = a*b - u
t = b/u
u = b*b - a
b = max(a, 0)
a = 21

I admit that the constraint of being triangular is quite strong, but this type of systems come out are not so unusual (they come out, for example, when working with DAG).

For more details, check the Documentation page and the comments inside symbolic_expressions.ads.

What I *cannot* do with it?

This code is really simple and small (2 files and less than 50K)  and it is aimed to solve the problem of allowing a user entering expressions. It cannot do the symbolic manipulations that you can find in systems like Matemathica or Maple (e.g., derivations, symbolic integration). Maybe in a future... ?

Installation and use


SymExpr is released under the terms of the LGPL v3. For more information, see the official site.

Contact me

Go to the Support & Contact page