Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get mathematica to carry out a Sum when only part of it is defined?

I'm having a sum like this:

Sum[1 + x[i], {i, 1, n}]

Mathematica doesn't simplify it any more. What would I need to do so it translates it into:

n + Sum[x[i],{i,1,n}]
like image 678
bdecaf Avatar asked Nov 03 '11 09:11

bdecaf


People also ask

What is conditional expression in Mathematica?

is a symbolic construct that represents the expression expr when the condition cond is True.

What is flatten in Mathematica?

Details. Flatten "unravels" lists, effectively just deleting inner braces. Flatten[list,n] effectively flattens the top level in list n times.


3 Answers

Maybe this?

Distribute[Sum[1 + x[i], {i, 1, n}]]

which returns:

n + Sum[x[i], {i, 1, n}]
like image 131
Arnoud Buzing Avatar answered Nov 08 '22 09:11

Arnoud Buzing


AFAIK Sum simply won't give partial answers. But you can always split off the additive part manually, or semi-automatically. Taking your example,

In[1]:= sigma + (x[i] - X)^2 // Expand

Out[1]= sigma + X^2 - 2 X x[i] + x[i]^2

There's nothing we can do with the parts that contain x[i] without knowing anything about x[i], so we just split off the rest:

In[2]:= Plus @@ Cases[%, e_ /; FreeQ[e, x[i]]]

Out[2]= sigma + X^2

In[3]:= Sum[%, {i, 1, n}]

Out[3]= n (sigma + X^2)

Unrelated: It is a good idea never to use symbols starting with capital letters to avoid conflicts with builtins. N has a meaning already, and you shouldn't use it as a variable.

like image 42
Szabolcs Avatar answered Nov 08 '22 09:11

Szabolcs


A quick and dirty way would be to use Thread, so for example

Thread[Sum[Expand[sigma + (x[i] - X)^2], {i, 1, n}], Plus, 1]
like image 31
Heike Avatar answered Nov 08 '22 09:11

Heike