Re: Skipping Elements in Sum
- To: mathgroup at smc.vnet.net
- Subject: [mg131801] Re: Skipping Elements in Sum
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Mon, 7 Oct 2013 08:25:21 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <diabl5$ihc$1@smc.vnet.net>
To keep the same structure as Sum when there are no exclusions and to handle exclusions with infinite sums Clear[mySum] mySum[expr_, {n_, min_, max_, step_: 1, excl : _List : {}}] := If[(max == Infinity) || (min == -Infinity), Sum[expr, {n, min, max, step}] - Sum[expr, {n, excl}], Sum[expr, {n, Complement[Range[min, max, step], excl]}]] mySum[f[n], {n, 1, 10}] f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8] + f[9] + f[10] mySum[f[n], {n, 2, 10, 2}] f[2] + f[4] + f[6] + f[8] + f[10] mySum[x^n/n!, {n, 0, Infinity, 2}] Cosh[x] mySum[x^n/(-n)!, {n, -Infinity, 0, 2}] Cosh[1/x] If exclusions are given, then step must be included and exclusions must be a list mySum[f[n], {n, 1, 10, 1, {5}}] f[1] + f[2] + f[3] + f[4] + f[6] + f[7] + f[8] + f[9] + f[10] mySum[f[n], {n, 1, 10, 2, {3, 5}}] f[1] + f[7] + f[9] mySum[x^n/n!, {n, 0, Infinity, 2, {1, 3}}] -x - x^3/6 + Cosh[x] mySum[x^n/(-n)!, {n, -Infinity, 0, 2, {-1, -3}}] -(1/(6*x^3)) - 1/x + Cosh[1/x] Bob Hanlon On Sun, Oct 6, 2013 at 3:46 AM, <scottcnoble at gmail.com> wrote: > On Sunday, October 9, 2005 2:00:05 AM UTC-4, qcade... at gmail.com wrote: > > Does anyone know how to skip elements using the mathematica sum? e.g. > > take the sum of all i, where i not equal to j. > > mySum[expr_,min_,max_,excl_] := Block[ > {newlist,i,n}, > > newlist=Complement[Table[i,{i,min,max}],excl]; > Sum[ expr[newlist[[i]]], > {i,1,Length[newlist]}] > ] > where "expr" is a function representing the argument of the sum, "min" is > the minimum value of the index, "max" is the maximum value of the index, > and "excl" is the list of indices to exclude from the sum. > > You can do this with Product[] as well... > > >