Boost logo

Proto :

Subject: Re: [proto] restructuring expression
From: Karsten Ahnert (karsten.ahnert_at_[hidden])
Date: 2012-05-29 18:24:49


On 05/29/2012 10:33 PM, Bart Janssens wrote:
> On Tue, May 29, 2012 at 8:21 PM, Eric Niebler <eric_at_[hidden]> wrote:
>> On 5/29/2012 1:44 AM, Karsten Ahnert wrote:
>>> I have an arithmetic expression template where multiplication is
>>> commutative. Is there an easy way to order a chain of multiplications
>>> such that terminals with values (like proto::terminal< double >) appear
>>> at the beginning? For example that
>>>
>>> arg1 * arg1 * 1.5 * arg1
>>>
>>> will be transformed to
>>>
>>> 1.5 * arg1 * arg1 * arg1
>>>
>>> ?
>>>
>>> I can imagine some complicated algorithms swapping expressions and child
>>> expressions but I wonder if there is a simpler way.
>>
>> There is no clever built-in Proto algorithm for commutative
>> transformations like this, I'm afraid.
>
> Just thinking out loud, could something like this work and be made recursive:
>
> boost::proto::when
> <
> proto::multiplies< <grammar for anything except double>,
> proto::terminal<double> >,
> proto::functional::make_multiplies(proto::_right, proto::_left)
>>

This does not work since it does not propagate the double terminals up
in the tree. Something like ( arg1 * ( arg1 * ( arg1 * 1.0 ) ) ) is
transformed to ( arg1 * ( arg1 * ( 1.0 * arg1 ) ) ).


Proto list run by eric at boostpro.com