# Any way to speed up calculation of a large complex exponential?

25 views (last 30 days)
Randy A Lemons on 25 Jun 2019
Commented: Randy A Lemons on 25 Jun 2019
I am dealing with the repeated calculation of the exponential of a complex array filled with numbers on the order of 1.04*10^8. This is for use in a FFT based laser propagation code that I have been tasked with implementing. The issue is that I often have to deal with arrays that are 2^12 by 2^12 elements and this means that each iteration of the exponential ends up taking around a second. In my attempts to troubleshoot why this particular computation was taking so long I tried using real numbers and saw that it was significantly faster.
I was wondering if anyone knew why computing the exponential of a complex number is so much slower than the exponential of a real number? In case you want a code to try out what I mean, this should work (i'm running on 2019a but it should be pretty universal).
A = 10^8+4*10^6*rand(2^12);
tic
for ii = 1:10
exp( 1i * A );
end
toc/10
tic
for ii = 1:10
exp( A );
end
toc/10
Ideally I would like to find a way to speed up this calculation but I am not hopeful. If anything an explanation for why it takes so much longer on comple data than real data would be interesting.
##### 1 CommentShowHide None
John D'Errico on 25 Jun 2019
Do you have any idea how large A is, and what the value of exp(A) would be? if you were to be able to compute it? It should have roughly 30,000,000 decimal digits. It will overflow, so the answer will always overflow to inf. But the exponential of i*A will be a complex number, with magnitude 1.
A = 10^8;
exp(A)
ans =
Inf
exp(i*A)
ans =
-0.36339 + 0.93164i
As such, it will actuallly require a computation for the imaginary argument, instead of just trapping out with an overflow to inf in the code.

Matt J on 25 Jun 2019
Edited: Matt J on 25 Jun 2019
One reason exp(A) is signficantly faster is that it is always Inf for the values of A you are feeding in. There is no computation involved. Another reason is that
exp(1i*A) = cos(A)+1i*sin(A)
which therefore takes twice as much memory and computation to evaluate. To speed things up, either use the GPU or consider changing A to type single instead of double.
##### 1 CommentShowHide None
Randy A Lemons on 25 Jun 2019
Well that really does nake quite a bit of sense. I never really took a look at the output of the calculation for the real numbers. I guess there is a catch for too large of numbers.
I knew that it would take twice the memory at the end of the day but was hoping there was a way to avoid the direct calculation using eulers formula. Or rather I was hoping there was something sneaky that could be done similar to an fft.
I guess if there was Mathworks probably would have implemented it.

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by