Search code examples
matlabapproximationfunction-approximation

Fast approximation for sin/cos in MATLAB


I am trying to create a fast approximation of sin and cos in MATLAB, which is the current bottleneck in my program. Is there a faster method than the in-built routine?

Bottleneck: On each iteration, obtain angles from complex sparse matrix A (50,000 x 50,000) and col vectors b and c (50,000 x 1), then find

S=[sin(ang(diag(A)+b-c) cos(ang(diag(A)+b-c)];

All inputs to sin and cos are close to +pi/2 or -pi/2.

I have tried a look-up table (as suggested in Create sine lookup table in C++ ), and a simple Taylor series, but both are slower:

Lookup table:

appr=[round(1:.001:2,3);sin(1:.001:2);cos(1:.001:2)];
ang=round(angle(diag(A))+b-c);
loc=ang;
for cntr=1:length(ang)
    loc(cntr)=find(appr(1,:)==abs(ang(cntr)),1);
end
S=[appr(loc,2).*sign(ang) appr(loc,3)];

Taylor series (quadrant rotation necessary to bring ang=pi/2 close to zero)

ang=angle(diag(A))+b-c;
[ang,ind]=min(abs([ang+pi/2; ang-pi/2])); conv=[1 -1];
S=[(ang-0.1667*ang.^3).*conv(ind)), (1-0.5*ang.^2+0.041666*ang.^4).*conv(ind))];

Averages time on MATLAB 2016a, Windows 8.1, [email protected]: In-built sin/cos: 1.5 sec | Look-up table: 1.8 sec | Taylor series: 1.7 sec


Solution

  • Trying to optimize sin() and cos() (that are already optimized to the hilt) won't gain you much. In any case in your expression:

    S = [sin(angle(diag(A) + b - c)), cos(angle(diag(A) + b - c))];
    

    the other operations are going to take up time that is comparable to sin and cos. Did you run the profiler by splitting up the expression into its constituents? That should give you an idea of how much the relative cost of sin() and cos() is.

    For example:

    A = pi*rand(10^7,1);
    B = pi*rand(10^7,1);
    C = A + i*B; % i is sqrt(-1)
    
    tic;
    S = sin(A);
    toc;
    

    Elapsed time is 0.129710 seconds.

    tic;
    S = sin(angle(C) + B - C);
    toc;
    

    Elapsed time is 0.757102 seconds.

    The time is really dominated by the just the moving of matrices to and from memory.

    Depending on the exact nature of your code, the gpu toolbox may be an option.