# Boost :

Subject: [boost] A question about Units & Spirit
From: John Phillips (phillips_at_[hidden])
Date: 2009-06-08 15:59:23

In the middle of the directX thread, Daniel Duffy asked a question
that seems more suited to its own thread. I'm starting that thread and
giving others a chance to join into the discussion by giving you the
content of the few messages that have passed by so far.

John

From Daniel ---------------------------------------------------

Hello,
This is my first mail, so I hope I have sent it to the right address :)

I am using the Units library for Dimensional Analysis. So far it is very
useful. Up till now I am using it in a hard-coded/compile time way as in
a test.cpp.

My inteterst is in using Units as part of a larger run-time system so I
need some way of making my code more flexible. Lambda library is an
option here but I would also like to create units and quanties from
string input as well as being able to parse these strings to units and
then store the units in a database.

Can I use Spirit (and Serialization) for these features? Maybe the

Daniel J. Duffy

From Hartmut ---------------------------------------------------

Sure Spirit is your friend for all parsing and output generation needs.
What did you have in mind?

Regards Hartmut

From Daniel ----------------------------------------------------

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.

The second group looks more tricky.

Examples of simplification:
1. (x-2)2 + 4x2 - 2^x + 1 = 5x2 - 8x + 10
2. x2 + 7x + 10 = (x + 2)(x + 5)
3. (a + b)(a - b) = a2 - b2
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.

best regards

Daniel

From John ----------------------------------------------------------

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 + 4x2 - 2^x + 1 = 5x2 -
8x + 10 2. x2 + 7x + 10 = (x + 2)(x + 5) 3. (a + b)(a - b) =
a2 - b2 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