# [Rcpp-devel] use of auxiliary functions

Davor Cubranic cubranic at stat.ubc.ca
Sun Aug 8 03:21:14 CEST 2010

```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
```