Mathematica Asked by Hao Wang on March 10, 2021
I want to compute a double sum like below with i != j. (When i =j, the denominator will be zero, which can not be assessed). Does anyone know how to do it? I want to skip the calculation when i=j.
Sum[1/(i-j),{i,1,10},{j,1,10}]
I found a appraoch like this:
pairs = Subsets[Range[10], {2}];
test[{i_, j_}] := 1/(i - j);
Total[test /@ datas]*2
which gives a result of -(4861/126), which is the anwser I need. But this method requires more time than the Sum funciton in Mathematica. (In addition, my function is more complex than the 1/i-j.
I would like to bid my sincere appreciate to any help.
m = SparseArray[{{i_, j_} /; j > i -> 1/(i - j)}, {10, 10}];
Total@Total@Normal[m]*2
Answered by cvgmt on March 10, 2021
Just because of its symmetry your sum is equal to zero. If you need sometjing else, where you need to exclude the case i=j under the sum, you may do as follows:
Sum[If[i != j, 1/(1 + (i - j)^2), 0], {i, 1, 3}, {j, 1, 3}]
(* 12/5 *)
Have fun!
Answered by Alexei Boulbitch on March 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP