# why 10 times faster after 'vpa' the sym matrix

4 views (last 30 days)
Jiyuan Liu on 27 Jul 2018
Commented: Walter Roberson on 27 Jul 2018
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 on 27 Jul 2018
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 CommentsShow 1 older commentHide 1 older comment
Jiyuan Liu on 27 Jul 2018
the former superior.
Walter Roberson on 27 Jul 2018
SymMat = vpa(subs(SymMat));
would be my preference.