Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Multiple sum with iterators that cannot equal

  • To: mathgroup at smc.vnet.net
  • Subject: [mg19396] Re: [mg19365] Multiple sum with iterators that cannot equal
  • From: BobHanlon at aol.com
  • Date: Mon, 23 Aug 1999 13:57:12 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Eric,

There are a couple of ways that you can do this.

g1[f_, n_Integer?Positive] := 
  Sum[f[i, j], {j, n}, {i, n}] - Sum[f[i, i], {i, n}]

g2[f_, n_Integer?Positive] := 
  Sum[(1 - KroneckerDelta[i, j ])*f[i, j], {j, n}, {i, n}]

g3[f_, n_Integer?Positive] := 
  Sum[f[i, j], {j, n}, {i, j - 1}] + Sum[f[i, j], {j, n}, {i, j + 1, n}]

g4[f_, n_Integer?Positive] := Sum[If[i == j, 0, f[i, j]], {j, n}, {i, n}]

g1[h, 3]

h[1, 2] + h[1, 3] + h[2, 1] + h[2, 3] + h[3, 1] + h[3, 2]

n = 10;
g1[f, n] == g2[f, n] == g3[f, n] == g4[f, n]

True

n = 75;
Timing[#[f, n]][[1]] & /@ {g1, g2, g3, g4} // ColumnForm

0.48333333333266637*Second
0.683333333333394*Second
0.4000000000005457*Second
0.38333333333321207*Second

The use of the condition is as fast as any. Consequently, define a 
conditional summation.

ClearAll[conditionalSum]

SetAttributes[conditionalSum, HoldFirst];

conditionalSum::usage = 
    "conditionalSum[condition, expression, {i, imin, imax}, {j, jmin, jmax}] \
sums the terms for which the condition is True.";

conditionalSum[cond_:True, expr_, {i_, imin_Integer:1, imax_Integer}, 
    {j_, jmin_Integer:1, jmax_Integer}] := 
  Sum[If[cond, expr, 0], {i, imin, imax}, {j, jmin, jmax}]

If no condition is specified, conditionalSum defaults to the normal Sum

n = 5;
conditionalSum[f[i, j], {j, n}, {i, n}] == Sum[f[i, j], {j, n}, {i, n}]

True

The specific case that you are interested in, i.e., i != j:

conditionalSum[i != j, f[i, j], {j, 3}, {i, 3}]

f[1, 2] + f[1, 3] + f[2, 1] + f[2, 3] + f[3, 1] + f[3, 2]

More complex conditions

n = 11;
conditionalSum[EvenQ[i] && OddQ[j], f[i, j], {j, n}, {i, 0, n}] == 
  Sum[f[i, j], {j, 1, n, 2}, {i, 0, n, 2}]

True

conditionalSum[(i + j) > 3, f[i, j], {i, 5}, {j, 5}] == 
  Sum[f[i, j], {i, 5}, {j, Max[1, 4 - i], 5}]

True


Bob Hanlon

In a message dated 8/21/99 4:36:00 AM, rspahr at worldnet.att.net writes:

>Using  multiple summmation signs with condition 'i "not
>equal" j', evaluation stops immediately as 'i' starts as
>1 as does 'j'.  My formula requires that the 'i's' and
>'j's' together:
>
>   Sum[Sum[f,{j,1,n}],{i,1,n}] ignore cases where i = j.
>
>   Or as copied from my notebook:
>
>\!\(\(\(\[Sum]\+\(i = 1\)\%n\[Sum]\+\(j = 1\)\%n\)\+\(i
>!= j\)\) \(w\_i\) 
>    w\_j\)
>
>How do I solve this without some cumbersome loops.
>


  • Prev by Date: RE: Magnitude of Vector
  • Next by Date: Please help me! (Warnings and Messages...)
  • Previous by thread: Multiple sum with iterators that cannot equal
  • Next by thread: Re: Multiple sum with iterators that cannot equal