       Re: simplification of sums

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: simplification of sums
• From: Leendert van Gastel <gastel at can.nl>
• Date: Sat, 02 Jul 1994 15:51:51 +0200

```> hi,
>
> i'm using Mathematica and would like to manipulate various symbolic sums. in particular,
> given that Xbar=Sum[X[i],{i,n}]/n, i would like to be able to simplify
>
> Sum[(X[i]-Xbar)^2,{i,n}] to Sum[X[i]^2,{i,n}] - n Xbar^2
>
> also, i would like to be able to distribute summation in general:
>
> Sum[a + b - c,{i,n}] -> Sum[a,{i,n}] + Sum[b,{i,n}] - Sum[c,{i,n}]
>
> and pull constant factors out of the sum
>
> Sum[2 a X[i],{i,n}] -> 2 a Sum[X[i],{i,n}]
>
> i've tried to write my own Rules for ReplaceAll, but without success. the package
> Algebra`SymbolicSum` does not seem to help.
>
> any suggestions out there?
>
> bob
>
> --
> Dr. Robert B. Nachbar | Merck Research Laboratories | 908/594-7795
> nachbar at merck.com     | RY50S-100                   | 908/594-4224 FAX
>                       | PO Box 2000                 |
>                       | Rahway, NJ 07065            |
>
>

In writing the rules for this simplification one has to be careful
how the evaluation of Mathematica works. In this case one has to prevent
evaluation of both the left hand side as the right hand side of the
rules before the actual application. It is best done with Literal and :>
as in the rule to expand the summand

rule1 = Literal[Sum[summand_, iterator_]] :>
Sum[Expand[summand], iterator]

Now for the splitting the summand we define similarly

rule2 = Literal[Sum[a_ + b_, iterator_]] :>
Sum[a, iterator] + Sum[b, iterator]

In:= Sum[a + b - c,{i,n}] //. rule2

Out= Sum[a, {i, n}] + Sum[b, {i, n}] + Sum[-c, {i, n}]

And for the constant factor (we have added FreeQ to check
the constancy)

rule3 = Literal[Sum[const_ summand_, iterator_]] :>
const Sum[summand, iterator] /; FreeQ[const, iterator[]]

In:= Sum[2 a X[i],{i,n}] //. rule3

Out= 2 a Sum[X[i], {i, n}]

In:= Sum[X[i] Y[i], {i, n}] //. rule3

Out= Sum[X[i] Y[i], {i, n}]

Now we get indeed:

In:= Sum[(X[i]-Xbar)^2,{i,n}] //. {rule1, rule2, rule3}

2                                               2
Out= Sum[Xbar , {i, n}] - 2 Xbar Sum[X[i], {i, n}] + Sum[X[i] , {i, n}]

To get the first term computed (knowing that Xbar does not depend on i),
we can use the SymbolicSum package

In:= <<Algebra`SymbolicSum`

In:= Sum[(X[i]-Xbar)^2,{i,n}] //. {rule1, rule2, rule3}

2                                      2
Out= n Xbar  - 2 Xbar Sum[X[i], {i, n}] + Sum[X[i] , {i, n}]

These methods are useful for handling similar functions like Integrate.

Leendert van Gastel
CAN Expertise Centre
gastel at can.nlIn:= Sum[X[i] Y[i], {i, n}] //. rule2

Out= Sum[X[i] Y[i], {i, n}]

```

• Prev by Date: Re: Reading Formatted Data
• Next by Date: Re: PowerPCs
• Previous by thread: Behavior of Opional command
• Next by thread: Why won't If[ ] evaluate?