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, i7-4500U@1.86GHz: In-built sin/cos: 1.5 sec | Look-up table: 1.8 sec | Taylor series: 1.7 sec