Boost :

Subject: Re: [boost] [Units & Spirit] libraries
From: John Phillips (phillips_at_[hidden])
Date: 2009-06-08 12:42:30

Daniel,

One general thing, first. At least on my news reader, this
conversation with Hartmut appears in the middle of a long thread on
something else entirely. You may want to start it as a separate thread
to increase participation.

Daniel J. Duffy wrote:
> Hartmut,
> I would like to generate well-formed units and create expressions of the followiong kinds:
>
> Examples of unit expressions:
> 1. 2m + 15m*32s/16s = 32m.
> 2. 3m*7s + 2m*10s/5m = 21m + 4s -> ERROR. Can't add meters to seconds.

So, you want this to happen at run time, or at compile time? At
compile time, I think you are already set with the Units library. At run
time, there are examples with the library that would help.

If parsing the input is your concern, then Spirit is a good choice.
(Spirit 2 is even better, by the way.)

Using the two together, you could write a program where a user enters
your example lines and gets correct responses.

>
> The second group looks more tricky.
>
> Examples of simplification:
> 1. (x-2)^2 + 4x^2 - 2^x + 1 = 5x^2 - 8x + 10
> 2. x^2 + 7x + 10 = (x + 2)(x + 5)
> 3. (a + b)(a - b) = a^2 - b^2
> 4. sin(x)^2 + cos(x)^2 = 1
> 5. 2sin(x)cos(x) = sin(2x)
>
> One tricky part is for example #5 where the code needs to 'know' trigonometry.

As far as I can tell here, you are looking for a program that does
symbolic algebra. If that is true, then I don't think boost has a
complete solution for you.

Writing the parser for the input with Spirit wouldn't be that bad,
but writing the processing engine that matches patterns and does
simplifications is not covered by any current Boost libraries. I don't
recall seeing anyone poll for interest on this, so I don't think there
is even a partial library in the sandbox.

Would you want this at run time, compile time, both? I haven't tried
to write it, but I think a pure compile time implementation would be
prohibitively slow. For a pure run time solution, you might consider