How to get the math to perform the sum when only part of it is determined?

I have this amount:

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

Mathematica no longer simplifies it. What I need to do is translate it into:

n + Sum[x[i],{i,1,n}]
+5
source share
5 answers

Maybe this?

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

which returns:

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

AFAIK Sumsimply will not give partial answers. But you can always separate 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

We can do nothing with parts that contain x[i]without knowing anything about x[i], so we just separate 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)

: , , . N , .

+5

A quick and dirty way is to use Thread, for example,

Thread[Sum[Expand[sigma + (x[i] - X)^2], {i, 1, n}], Plus, 1]
+5
source

A simpler way:

Total[Sum[#, {i, 1, n}] & /@ {sigma, x[i]}]

enter image description here

If your expression is longer, this should give you an answer without the need for manual separation of terms

expr = sigma + (x[i] + i)^2 + Cos[Sin[i - x[i]]];
Total[Sum[#, {i, 1, n}] & /@ Level[expr, {1}]]

enter image description here

+2
source

This can also be made easy to understand with the help of rules:

sumofsumsrule = Sum[a_+b_,{i_,c_,d_}] :> Sum[a,{i,c,d}]+Sum[b,{i,c,d}];
expandsummandrule = Sum[a_,{i_,c_,d_}] :> Sum[Expand[a],{i,c,d}];
MyRules = {sumofsumsrule, expandsummandrule};

Now, if you mess around, you can use this (here are a few examples):

error = Sum[sigma+(x[i]-X)^2,{i,1,n}]

error /. sumofsumsrule

% /. expandsummandrule

error //. MyRules
+1
source

All Articles