# Computing exponentials and Trig functions

mr.janus at mac.com mr.janus at mac.com
Thu Dec 6 18:15:21 CET 2007

```Thanks for the quick response.

I have already done the modulo pi/2 trick to speed things up. I think the biggest part of my problem is that each time i call a trig function i create storage for the temporary calculations. I am currently modifying the code so there is a global 'scratch' matrix that i can just dump data into without having to ask malloc for more space and initialise each time. Hopefully this will speed things up nicely.

But i think MPFR could be the answer though. Still not as satisfying as doing it oneself.

Huw

On Thursday, December 06, 2007, at 05:00PM, "Fredrik Johansson" <fredrik.johansson at gmail.com> wrote:
>On Dec 6, 2007 5:28 PM,  <mr.janus at mac.com> wrote:
>> Hello,
>>
>> I'm trying to write a program in C using GMP as matlab is unable to handle the precision i need for solving my coupled equations accurately. However, one of the many problems i'm having is how to speed up the calculation of trig functions.
>>
>> I often have to solve complex exponentials but the way i have coded it is painfully slow. I split the exponential into the real and imaginary parts and solve separately. I then solve using sin, cos, sinh, and cosh identities.
>>
>> However the slowdown problem arises from my calculation of the trigonometric functions, which i do by Taylor expansion. Obviously, as I push the precision the Taylor expansion takes longer and longer to converge to the required precision. How does everyone else do this?
>>
>> Many thanks
>>
>> Huw
>
>There are tricks for accelerating the convergence of Taylor series of
>elementary functions. To begin with, if you want to calculate sin or
>cos of a big number you should first reduce the argument modulo pi/2.
>However, instead of going through the hard work of implementing the
>functions yourself, you could just use MPFR (http://www.mpfr.org/).
>
>Alternatively, if the precision you need is not too high, it might be
>faster to use Chebyshev approximations than Taylor series.
>
>Fredrik
>
>
```