30
$\begingroup$

I'm relatively inexperienced with mathematica, so I apologize if this is a trivial question. I want to take a double sum over a function $f(i,j)$ of two indices, of the form $$ \sum_{i = -\infty}^\infty \sum_{j = 0\atop j\not = i}^m f(i,j). $$ That is, in the inner sum I want to sum over only those indices $j$ in my range of summation that satisfy the assumption $j \not = i$. How can I input such a sum to Mathematica?

$\endgroup$
2
  • 7
    $\begingroup$ Use Boole[] or KroneckerDelta[]. See this as well. $\endgroup$ Commented May 26, 2013 at 9:14
  • 2
    $\begingroup$ Change function inside sum to: Sign[Abs[i - j]] f[i,j], not very elegant but it works. $\endgroup$ Commented May 26, 2013 at 9:24

2 Answers 2

28
$\begingroup$

You can use Boole as follows:

Sum[f[i, j] Boole[i != j], {j, 0, m}, {i, -Infinity, Infinity}] 

Here's an example where f[i, j] = Sin[i] Cos[j]

Sum[Sin[i] Cos[j] Boole[i != j], {j, 0, 3}, {i, 0, 3}] 

Which gives

Sin[1] + Cos[2] Sin[1] + Cos[3] Sin[1] + Sin[2] + Cos[1] Sin[2] + Cos[3] Sin[2] + Sin[3] + Cos[1] Sin[3] + Cos[2] Sin[3] 
$\endgroup$
3
  • $\begingroup$ @NickStrehlke, glad I could help. $\endgroup$ Commented May 27, 2013 at 5:21
  • 2
    $\begingroup$ What if f[i,j] diverges when i=j? Imagine f[i,j]=M[i,j]^(-a), where M[i,j] is a matrix with M[i,j]=0 if i=j, and a is a positive integer number. @RunnyKine $\endgroup$ Commented May 5, 2015 at 16:15
  • $\begingroup$ @RunnyKine Do you know how to do double summation with conditions. mathematica.stackexchange.com/questions/250873/… $\endgroup$ Commented Jul 12, 2021 at 5:30
26
$\begingroup$

You can use If to put the condition in the argument of the sum. It would be something like

Sum[If[i != j, f[i, j], 0], {i, -Infinity, Infinity}, {j, 0, m}] 

where If[i != j, f[i,j], 0] tells Mathematica to use $f(i,j)$ in the sum if $i\neq{j}$ or 0 if $i=j$.

$\endgroup$
3
  • $\begingroup$ Thank you! This answer was helpful too. $\endgroup$ Commented May 27, 2013 at 3:13
  • 14
    $\begingroup$ I found that your If solution works better than the Boole by RunnyKine, because it allows f[i,j] to be undefined at i == j. $\endgroup$ Commented Sep 19, 2013 at 1:57
  • $\begingroup$ Sum[If[n==0, 1/n^2, 0], {n,0, Infinity}] still gives an error at $n=0$ $\endgroup$ Commented Mar 23 at 14:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.