Is there a faster complex exponent?
Show older comments
Is there any way to more quickly evaluate complex exponentials, i.e:

where Q is a real array? Quick numerical tests show that complex input noticeably slows down MATLAB's exp function.
Several thoughts:
- Some libraries, such as Julia Base, provide a cis/cisoid function that directly evaluates the Euler expansion.
- The GNU C library has sincos function that simultaneously evaluate sine and cosine more quickly than separate calls.
- The Fixed Point Designer has a cordicexp function that seems to be identical to cis, but I don't have this toolbox. No idea how this performs compared to the standard exp function.
11 Comments
Walter Roberson
on 15 Dec 2023
Edited: Walter Roberson
on 15 Dec 2023
Hmmm, the timing tests are pretty irregular here in MATLAB online.
On my own system, the timings are closer to 0.0003 / 0.0004
format long g
doit();
function doit
N = 100000;
Q = randn(1,N);
start = tic; z1 = exp(1i*Q); t1 = toc(start);
start = tic; z2 = cos(Q) + i*sin(Q); t2 = toc(start);
t1
t2
end
Daniel Dolan
on 15 Dec 2023
Walter Roberson
on 15 Dec 2023
On my system, testing with N = 1e8, the times are about 0.31 / 0.49 which is about 1.58 times slower for the seperate operations.
Bruno Luong
on 15 Dec 2023
Edited: Bruno Luong
on 15 Dec 2023
"However, the former uses separate calls to cosine and sine,"
How do you know cosine and sine are called separately?
Daniel Dolan
on 15 Dec 2023
@Daniel Dolan " but my tests suggest that the composite sum takes about twice as long sine or cosine by itself"
Sorry, the test of this code on my PC ad online PC does NOT clearly support your observation
doit
function doit
N = 1000000;
Q = pi*rand(1,N);
tic; z = exp(1i*Q); toc % Elapsed time is 0.003994 seconds.
tic; z = exp(complex(0,Q)); toc % Elapsed time is 0.007032 seconds.
tic
c = cos(Q);
s = sqrt(1-c.*c);
%ss = 2*mod(Q,2*pi)>pi-1;
z = complex(c,s);
toc % Elapsed time is 0.006615 seconds.
tic
c = cos(Q);
s = sin(Q);;
z = complex(c,s);
toc % Elapsed time is 0.007155 seconds.
tic
c = cos(Q);
s = zeros(size(c));
zr = complex(c,s); % wrong result
toc % Elapsed time is 0.005765 seconds. NOT faster than exp(1i*Q)
end
cos by itself is faster than anything else, as is to be expected.
But the exp() form is faster than the other three forms
format long g
doit
function doit
N = 1000000;
Q = pi*rand(1,N);
tic; z = exp(1i*Q); t1 = toc; %warmup
tic; z = exp(1i*Q); t1 = toc
tic; z = exp(complex(0,Q)); t2 = toc
tic
c = cos(Q);
s = sqrt(1-c.*c);
%ss = 2*mod(Q,2*pi)>pi-1;
z = complex(c,s);
t3 = toc
tic
c = cos(Q);
s = sin(Q);;
z = complex(c,s);
t4 = toc
tic
c = cos(Q);
s = zeros(size(c));
zr = complex(c,s); % wrong result
t5 = toc
t2./t1
t3./t1
t4./t1
t5./t1
end
Bruno Luong
on 15 Dec 2023
Edited: Bruno Luong
on 15 Dec 2023
"cos by itself is faster than anything else, as is to be expected."
Not on my PC (timing in comment next to toc in my code.
My feeling is exp(1i*Q) is pretty much optimized, so asking to find some way to accelerate it has little chance to be possible acheived.
Walter Roberson
on 15 Dec 2023
But... on my system, I get quite different values
t1 = 0.002637121
t2 = 0.005705978
t3 = 0.008435955
t4 = 0.006200915
t5 = 0.005191424
ans = 2.16371489969554
ans = 3.19892602576825
ans = 2.35139570766757
ans = 1.968595297675
FWIW, Simulink offers a sincos and cos + jsin functions (Trigonometric Function), with options for how those functions are computed (Algorithm - Approximation Method). Don't know if the "under the hood" Simulink implementation would offer any performance benefits if brought into Matlab proper.
Bruno Luong
on 15 Dec 2023
But again I'm not convice MATLAB is NOT already do specific acceleration for exp(1i*Q). It is faster than cos alone on my PC and Walter PC as well
Accepted Answer
More Answers (1)
Let's compare two ways e.g.:
Q = linspace(-10, 10, 1e6);
tic;
CQ1 = exp(1i*Q);
T1 =toc
tic;
CQ2 = cos(Q)+1i*sin(Q);
T2 =toc
fprintf('Calc time of exp(1i*Q): %f; cos(Q)+i*sin(Q): %f; \n', [T1, T2])
1 Comment
Daniel Dolan
on 15 Dec 2023
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!