[Rcpp-devel] use of auxiliary functions
baptiste auguie
baptiste.auguie at googlemail.com
Sun Aug 8 04:03:44 CEST 2010
Thanks Davor, Douglas and Dirk –– all your comments have been very
helpful and I used them to improve my code (along with getting a
better understanding of C++). The only thing I haven't tried yet is
avoid duplicating the objects in memory using const (seems tricky from
what I read on the net).
All the best,
baptiste
On 8 August 2010 03:21, Davor Cubranic <cubranic at stat.ubc.ca> wrote:
> On August 6, 2010 02:44:00 pm Douglas Bates wrote:
>> On Fri, Aug 6, 2010 at 3:14 PM, baptiste auguie
>>
>> > arma::mat euler(double phi, double theta, double psi)
>> > {
>> > arma::mat Rot(3,3);
>> > Rot(0,0) = cos(psi)*cos(phi) - cos(theta)*sin(phi)*sin(psi);
>> > Rot(0,1) = cos(psi)*sin(phi) + cos(theta)*cos(phi)*sin(psi);
>> > Rot(0,2) = sin(psi)*sin(theta);
>> >
>> > Rot(1,0) = -sin(psi)*cos(phi) - cos(theta)*sin(phi)*cos(psi);
>> > Rot(1,1) = -sin(psi)*sin(phi) + cos(theta)*cos(phi)*cos(psi);
>> > Rot(1,2) = cos(psi)*sin(theta);
>> >
>> > Rot(2,0) = sin(phi)*sin(theta);
>> > Rot(2,1) = -cos(phi)*sin(theta);
>> > Rot(2,2) = cos(theta);
>> > return (Rot);
>> > }
>>
>> It is probably not a big deal now but you are recalculating the same
>> trigonometric functions several times in there. Why not calculate
>> the cos and sin of the each of the angles just once and then form
>> the elements of the rotation matrix as linear combinations? (Once
>> upon a time we used to keep track of every floating point
>> calculation and try to work out ways to minimize that number.)
>
> True, but I wonder whether in this case any modern compiler will catch
> the common intermediate calculations and optimize them for you so that
> they're only calculated once.
>
> Easy enough to check with gcc and its '-s' argument, which will give you
> the assembly output. :-)
>
> Davor
>
--
____________________
Dr. Baptiste Auguié
Departamento de Química Física,
Universidade de Vigo,
Campus Universitario, 36310, Vigo, Spain
tel: +34 9868 18617
http://webs.uvigo.es/coloides
____________________
More information about the Rcpp-devel
mailing list