Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16023

Re: Skipping Elements in Sum

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Skipping Elements in Sum
Date 2013-10-07 12:23 +0000
Message-ID <l2u93h$ort$1@smc.vnet.net> (permalink)
References <diabl5$ihc$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


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@gmail.com> wrote:

> On Sunday, October 9, 2005 2:00:05 AM UTC-4, qcade...@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...
>
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Skipping Elements in Sum Bob Hanlon <hanlonr357@gmail.com> - 2013-10-07 12:23 +0000

csiph-web