I wonder if anyone could help me to improve the following code. I used the Do loop, which is not recommended by MMA. What I need is the final value of 'fB' after looping over arrB
. I think maybe I should use Fold
, but I cannot think of any good way. Please note that both arrA
and arrB
are lists containing 6
real numbers. s
is also a real number. The code is as follows.
fB=0;
pre=s^(-2);
Do[
argu=arrB[[i]]*s^2;
Which[
argu<.1, fB=fB+arrA[[i]]*arrB[[i]]*(1-.5*argu),
argu>20, fB=fB+arrA[[i]]*pre,
argu>=.1 && argu<=20,fB=fB+arrA[[i]]*(1-Exp[-argu])*pre
]
,{i,6}];
fB;
I'd also like to know is Piecewise
faster than Which
in general? Thanks.
arrA
andarrB
ands
in your question. $\endgroup$ – xzczd Jul 9 '19 at 7:25