Speeding up matrix exponentials
Show older comments
Hey all,
I am trying to speed up the calculation of matrix exponentials but the process is not fast enough and I cannot think of a more efficient way (without using parfor). The code is:
a = diag(ones(1,999),1) + diag(-ones(1,999),-1);
b = expm(a);
c = [];
counter = 1; %keep track of progress of for loop%
for i = 1:10^4-1
factor = exp(1j*(i+1)) - exp(1j*i);
c = [c,b^factor];
counter
counter = counter+1;
end
This process takes too long for 10^4 iterations so I was hoping there was a faster method, maybe some kind of vectorization or something?
Thanks
7 Comments
Rik
on 4 Apr 2023
Since you don't do anything with the result, you can simply do the last and skip the rest.
I also suspect a lot of time is being spent printing the counter.
Can you confirm you need a matrix exponentiation instead of an element-wise exponent?
per isakson
on 4 Apr 2023
bil
on 4 Apr 2023
Is it necessary to display or print the counter (which can be done with the loop index btw) ?
Pre-allocation is difficult with this one, as it requires too much memory (~74.5 GB of RAM). And that brings us to the fact that you will probably not have enough memory to store the final output as well.
Although, you can pre-allocate a cell array to store the data, even then it's still a question of memory.
As for speed, the bottle neck seems to calculating b^fact (not the loop).
I suggest you to find an effecient method to calculate matrix power. I was unable to find something from a cursory glance on FEX.
a = diag(ones(1,999),1) + diag(-ones(1,999),-1);
b = expm(a);
c = cell(10,1);
%counter = 1; %keep track of progress of for loop%
tic
for k = 1:10-1
fact = exp(1j*(k+1)) - exp(1j*k);
c{k} = b^fact;
end
toc
~12 seconds for 10 iterations
bil
on 4 Apr 2023
Mike Croucher
on 4 Apr 2023
Edited: Mike Croucher
on 4 Apr 2023
Can you talk about your application any more? Some questions I have include
- Would single precision be good enough for you?
- There is no pagempower function. But if there were would it have helped you? If so, a bit more info about your application would be really useful.
- Why can't you use parfor?
- What hardware do you have access to? HPC cluster? GPU?
bil
on 4 Apr 2023
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!