[Date Index]
[Thread Index]
[Author Index]
Re: Question about evaluation with Sum
*To*: mathgroup at smc.vnet.net
*Subject*: [mg19613] Re: Question about evaluation with Sum
*From*: "Allan Hayes" <hay at haystack.demon.co.uk>
*Date*: Sat, 4 Sep 1999 21:09:17 -0400
*References*: <7qqbom$4ge@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
CORNIL Jack Michel <jmcornil at club-internet.fr> wrote in message
news:7qqbom$4ge at smc.vnet.net...
> Hello,
>
>
> I have a big problem of evaluation with the function Sum.
>
> With the very simple following example,
>
> p=k
>
> Sum[p,{k, 1, n}]
>
> p k
>
> Sum[Evaluate[p], {k, 1, n}]
>
> 1
> - n (1+n)
> 2
>
> Sum[p,{k, 1, 5}]
>
> 15
>
> how can I explain my students that
>
> in the first case p is left unevaluate and it needs Evaluate,
>
> in the last case p is evaluate correctly ?
>
> I do not understand how the "Attributes" of Sum can explain this.
>
>
> Thank You in advance
>
>
> Jack Michel CORNIL
>
> Versailles FRANCE
>
Jack,
Interesting examples.
Here is my account of what is happening
I come back to your examples in part [C]
[A] EVALUATION OF
Sum[expr,{j, a,b}]
(see notes below)
Because Sum has the attribute Holdall, the order of evaluation need not
follow the standard head then elements pattern, and instead it preceeds as
follows:
(notice how the kind of iterator affects the evaluation and the special
treatment of the summation variable, j)
Evaluate a,b to get
Sum[expr,{j, a*,b*}]
--- If the iterator {j, a*,b*} does not give an explicit finite sequence of
values for j, then replace j and any occurences of j in expr with a local
variable K$r where r is a unique integer index
Sum[expr',{K$n, a*,b*}]
Evaluate expr' to get
Sum[expr'*,{K$n, a*,b*}]
If this can be summed then the sum is returned; if not thenthe original
input is returned.
--- If the iterator {j, a*,b*} gives an explicit sequence of values,
j1,j2,... for j, then evaluate expr to expr* and sum this over these values
of j.
NOTES:
(1) The evaluation can be altered using Evaluate, Hold etc.
For example, with
Sum[Evaluate[expr],{j, a,b}]
the first change is to
Sum[expr*,{j, a,b}]
(where expr* is the value of expr) - then we procede as above (to evaluate
a and b etc).
(2) More generally, the iterator can be of the form {i, a, b, d}, in which
case we start with evluating a, b and d.
[B] GENERAL EXAMPLES
1)
ClearAll["`*"]
p = j; a = A; b = B;
Sum[f[p, j], {j, a, b}]
Sum[f[p, j], {j, a, b}]
Evaluation steps
Evaluate the limits a, b, in the iterator {j, a, b}
Sum[f[p, j], {j, A, B}]
{j, A,B}, does not give an explicit finite list j1,j2, .... so introduce a
local variable K$n in place of the summation variable, j.
Sum[f[p,K$n],{K$r,a,b}]
Evaluate f[p,K$r]
Sum[f[i,K$r],{K$r,A,B}]
This cannot be summed so return the original input
2 )
Add another assignment
f[j, r_] := j r
Sum[f[p, j], {j, a, b}]
-(1/2)*(-1 + A - B)*(A + B)*j
Evaluation steps
Evaluate the limits a, b in the iterator {j, a, b}
Sum[f[p, j], {j, A, B}]
{j, A,B} does not give an explicit finite list j1,j2, .... so introduce a
local variable K$r in place of the summation variable, j.
Sum[f[p,K$r],{K$r,a,b}]
Evaluate f[p,K$r]
Sum[j K$r,{K$r,A,B}] (*note the new j introduced*)
Sum this assuming that B-A is non negative integer
-(1/2)(-1 + A - B)(A + B) j
3)
Clear["`*"]
a = A; b = A + 2; p = j;
Sum[f[p, j], {j, a, b}]
f[A, A] + f[1 + A, 1 + A] + f[2 + A, 2 + A]
Evaluation steps
Evaluate the limits a, b, in the iterator {j, a, b}
Sum[f[p, j], {j, A, B}]
{j, A,B} gives an explicit sequance of values for j, A,A+1,A+2; so do not
introduce a local variable K$r in place of the summation symbol, j.
Evaluate f[p,j] to get f[j,j]
Sum this over the sequence of values for j
f[A,A]+f[1+A,1+A]+f[2+A,2+A]
[C] YOUR EXAMPLES
Clear["`*"]
p = k;
Sum[p, {k, 1, n}]
k n
( not p k)
Evaluation steps
Sum[p,{K$r,1,n}]
Sum[k,{K$r,1,n}]
k n
2)
Sum[Evaluate[p], {k, 1, n}]
1/2*n*(1 + n)
Evaluation steps
Because of Evaluate the first change is to
Sum[k, {k, 1, n}]
Then we follow the general pattern
Sum[K$r, {K$r, 1, n}]
Which can be summed.
3)
Sum[p, {k, 1, 5}]
15
Evaluation steps
Because the iterator gives the explicit list 1,2,3,4,5, no local variable is
introduced for k and we simply sum k over the value 1,2,3,4,5
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
Prev by Date:
**Re: in center of a triangle**
Next by Date:
**Re: in center of a triangle**
Previous by thread:
**Re: Question about evaluation with Sum**
Next by thread:
**Re: Langford's Problem**
| |