1
$\begingroup$

How can I get Mathematica to collect terms from an indexed sum? My application is somewhat convoluted, but this minimum code captures the idea:

s[i_, n_] := 
 Sum[l[i, j]*e[c[j]], {j, 1, n}] + Sum[m[i, j]*e[c[j]], {j, 1, n}] + 
  Sum[l[i, j]*e[z[j]], {j, 1, n}] + Sum[m[i, j]*e[z[j]], {j, 1, n}]

I'm trying to use Collect to report $\sum _ {j = 1}^n e (c (j)) [l (i, j) + m (i, j)] + \sum _ {j = 1}^n e (z (j)) [l (i, j) + m (i, j)]$

How can I achieve that?

$\endgroup$
2
$\begingroup$
Clear["Global`*"]

s[i_, n_] :=
 Sum[l[i, j]*e[c[j]], {j, 1, n}] +
  Sum[m[i, j]*e[c[j]], {j, 1, n}] +
  Sum[l[i, j]*e[z[j]], {j, 1, n}] +
  Sum[m[i, j]*e[z[j]], {j, 1, n}]

The simplified form is

s2[i_, n_] :=
 Evaluate@(Sum[l[i, j]*e[c[j]], {j, 1, n}] +
     Sum[m[i, j]*e[c[j]], {j, 1, n}] +
     Sum[l[i, j]*e[z[j]], {j, 1, n}] +
     Sum[m[i, j]*e[z[j]], {j, 1, n}] //.
    Sum[x_*y_, {v_, vmin_, vmax_}] +
      Sum[z_*y_, {v_, vmin_, vmax_}] :>
     Sum[y*(x + z), {v, vmin, vmax}])

Which simplifies to a single sum.

s2[i, n]

(* Sum[(e[c[j]] + e[z[j]])*(l[i, j] + m[i, j]), {j, 1, n}] *)

Checking

k = 8;

And @@ Flatten[Table[s[i, n] == s2[i, n] // Simplify, {i, k}, {n, k}]]

(* True *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.