1
$\begingroup$

I have the following sums $$\sum _{j=0}^{n-1} \left(\sum _{c=1}^K \left(\sum _{b=1}^K m[b]^2\right)\right)$$ or $$\sum _{j=0}^{n-1} \left(\sum _{c=1}^K \text{$ n$}[c]\right)$$ where $m[b]$ and $n[c]$ are undefined vectors. As you can see, in the first case the two summations are just summed on 1, since the only index in $m$ is $b$ and similar for the second one. What I would like mathematica to do is to simplify such sums like $$Kn\sum_{b=1}^K m[b]^2$$ and $$n\sum_{c=1}^K n[c]$$ for the second one. How could I do it in full generality?

Code

I have some user defined functions which I'm using

a3 = Attributes[Sum];
Unprotect[Sum];
ClearAttributes[Sum, a3];
Attributes[Sum];
Sum[f_ g_, i_] := f Sum[g, i] /; FreeQ[f, i[[1]]]
SetAttributes[Sum, a3];

SumExpand[exp_] := 
  exp /. Sum[c_, {i_, a_, b_}] :> 
    Distribute[Sum[ExpandAll[c], {i, a, b}]];

NewSum[exp_, {i_, i0_, n_}] := SumExpand[Sum[exp, {i, i0, n}]];

And

c2[x_] := Sign[Coefficient[x, s]] (x - \[Omega])^2;

The calculation which gives the factors cited above, plus many others, is the following

cc[1] = NewSum[
   NewSum[
    NewSum[
     NewSum[
      c2[j \[Tau]A + l \[Tau]B + m[b] + n[c] + 2 s], {b, 1, K} ], {c, 
      1, K}], {j, 0, n - 1}], {l, 0, 2}] + 
  NewSum[
   NewSum[
    NewSum[
     NewSum[
      c2[j \[Tau]A + l \[Tau]B + m[a] + n[c] + s], {a, 1, Nf} ], {c, 
      1, K}], {j, 0, n - 1}], {l, 0, 2}] + 
  NewSum[
   NewSum[
    NewSum[
     NewSum[c2[j \[Tau]A + l \[Tau]B + m[a] + n[d]], {a, 1, Nf} ], {b,
       1, Nf}], {j, 0, n - 1}], {l, 0, 2}]

The factos: $s,\omega, Nf, K, n$ are constants and

\[Tau]A = (2 \[Omega])/(n + 1);
\[Tau]B = (n \[Omega])/(n + 1);

these are $\tau_A, \tau_B$.

$\endgroup$
4
  • $\begingroup$ @MarcoB Well the code is a little bit cumbersome, but I'm adding it now $\endgroup$ Jun 22, 2022 at 12:16
  • $\begingroup$ @MarcoB of course! $\endgroup$ Jun 22, 2022 at 12:20
  • 1
    $\begingroup$ Perfect, thank you! $\endgroup$
    – MarcoB
    Jun 22, 2022 at 12:20
  • $\begingroup$ A quick-and-dirty redefinition of Sum (which might be a little dangerous!) could involve adding the following two lines after your factoring definition: Sum[expr_, {i_, n0_, n1_}] := (n1 - n0 + 1) expr /; FreeQ[expr, i]; Sum[expr_, iter0___, {i_, n0_, n1_}, iter1___] := Sum[(n1 - n0 + 1) expr, iter0, iter1] /; FreeQ[{expr, iter1}, i]. (Note that later iterators (iter1) are actually the "inner" iterators, perhaps counterintuitively.) There are of course cases that this doesn't catch, but it seems to work for your expression...! $\endgroup$
    – thorimur
    Jun 22, 2022 at 22:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.