Boost logo

Boost :

Subject: Re: [boost] [qvm] scalar_cast
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2012-11-05 16:38:17


On Sat, Nov 3, 2012 at 4:00 PM, Nikolay Mladenov
<nikolay.mladenov_at_[hidden]> wrote:
> I am trying qvm in a small project that uses complex numbers and have
> expressions of the kind :
> cv3 = c1*rv3_1 + c2*rv3_2
> (the v3's are vectors, the c's are complex and the r's are real).
>
> I wrote it as :
>
> cv3
> = scalar_cast<complex>(rv3_1)*c1
> + scalar_cast<complex>(rv3_2)*c2;
>
> but this seems to invoke 6 complex constructors (unnecessarily)
> I have not tested to see if the constructors will be optimized away,
> but it seems to me that if :
> v_traits<vector_scalar_cast_>::scalar_type == complex , bit
> v_traits<vector_scalar_cast_>::r/ir return real
> the expression will compile and no complex constructors will be invoked.

If I understand correctly, you mean that if a complex can be
implicitly constructed from a float, then r/ir can return float and
it'll all work. I don't think that it is a safe assumption to make in
general, that any scalar type would define a suitable implicit
constructor.

That said, it might be a good idea to specify in the documentation
that the return value from r/ir must be implicitly convertible to
::scalar_type, which would allow a partial specialization for your
complex type of the vector_scalar_cast_ and the other related
scalar_cast type templates. Are you willing to give this a try?

(As for scalar multiplication requiring the scalar on the right,
you're right it should work from the left too.)

Thanks,
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk