why 10 times faster after 'vpa' the sym matrix

조회 수: 3 (최근 30일)
Jiyuan Liu
Jiyuan Liu 2018년 7월 27일
댓글: Walter Roberson 2018년 7월 27일
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
Jiyuan Liu
Jiyuan Liu 2018년 7월 27일
the former superior.
Walter Roberson
Walter Roberson 2018년 7월 27일
SymMat = vpa(subs(SymMat));
would be my preference.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by