why 10 times faster after 'vpa' the sym matrix

5 次查看(过去 30 天)
Hi lovely Matlab experts,
Could you please let me know the rationale behind the speed-up stated below? Is it a random case or not? Hope there's something I can keep in mind to have faster run.
symMat=subs(sym,{},{});
%symMat is a complex matrix whose elements are too long to show in command window
symMat
%*elapsed time ~20s*, digits=32 as default
vpa(symMat,32)
% *elapsed time ~5s*
More surprisingly is the following 'for' or 'parfor' loop will be 10 times faster after being 'vpa'ed as shown below, and I thought there's 'vpa' in the loop so escaping another vpa outside loop is good:
tic
for i=1:1000
digits(4)
vpa(subs(symMat(:,1),,)));
digits(32)
vpa(subs(symMat(:,2),,)));
end
toc %%*the above elapse ~1200s*
VpasymMat=vpa(symMat,32)
tic
for i=1:1000
digits(4)
vpa(subs(VpasymMat(:,1),,)));
digits(32)
vpa(subs(VpasymMat(:,2),,)));
end
toc %%*the above elapse 60-100s*
thanks,
best
J

采纳的回答

Walter Roberson
Walter Roberson 2018-7-27
Why shouldn't it be faster? You are doing different operations. When you vpa() symMat ahead of time, then the vpa() pre-calculates most things that can be calculated as numeric, using fixed-point calculations. This can permit it to consolidate a number of values.
For example, you might have
m = x * (sin(sym(pi)/17) + int(tan(y),y,0,sym(pi)/4))^(1453/611)
This involves the indefinitely-precise value of sin(pi/17) and an integral that will probably produce a transcendental number, and involves taking the sum of those two to the 1453 power and taking the 611'th root of that -- more indefinitely precise numbers, all of which have to be held as indefinitely precise because the code does not know what you are going to do with m later. Repeat this a number of times and it is going to add up in time.
If, on the other hand, you vpa() this ahead of time, then it is going to reduce to
m = 0.22127813173338713927096201193632*x
which is comparatively fast to execute many times.
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by