For some reason, I encounter a problem when doing the Sum and ParallelSum (or doing the Do or ParallelDo, e.g. such as the issue here), I figured that the potential cause of the problem can be resolved if I find a way to evaluate the following test toy model without calling the variable i twice at i == 3 || i== 5 as,
Sum[If[i == 3 ||i== 5, i, 0], {i, 0, 10}]
or
ParallelSum[If[i == 3 ||i== 5, i, 0], {i, 0, 10}]
but just combine the calling of i once as
Sum[If[i == (3 || 5), i, 0], {i, 0, 10}].
Obviously, the i == (3 || 5) does not work, which is unreadable in Mathematica.
My question: Is there any way to express i == 3 || i== 5 such that i is only called once? So it saves the time by the i once, instead of calling i twice and evaluate its value separately comparing to 3 and then comparing to 5.
p.s. The reason I asked this is because in general the i in my program is a very complicated function of multivariable, and evaluate such a function causes enormous times. For example, it could be something like
Sum[If[f[i,j,k] == 3 || f[i,j,k] == 5, i, 0], {i, 0, 2^10}, {j, 0, 2^10}, {k, 0, 2^10}]
where f[i,j,k] is a earlier defined complicated complex integrations/contour integrals, etc. If there is a way to express
Sum[If[f[i,j,k] == (3 || 5), i, 0], {i, 0, 2^10}, {j, 0, 2^10}, {k, 0, 2^10}]
it will be easier. Thanks in advance for the answers from experts!
(Note) One resolution, of course, is to define a variable g as the g=function f[i,j,k], so that
Sum[If[(g == 3 || g == 5)/.g-> f[i,j,k], i, 0], {i, 0, 2^10}, {j, 0, 2^10}, {k, 0, 2^10}]
this indeed works. However, it does not work and potentially gives wrong answer when we consider the ParallelSum,
ParallelSum[If[(g == 3 || g == 5)/.g-> f[i,j,k], i, 0], {i, 0, 2^10}, {j, 0, 2^10}, {k, 0, 2^10}]
Or write the ParallelDo version of the above to do the sum. The trouble seems that the g variable is parallelly used simultaneously, so it causes the confusion.
MatchQ[i, 3 | 5]will work for you. – Carl Woll Sep 05 '17 at 22:17