# Sum not recognized as a linear operator by Solve

When I try to solve an equation with a constant under the summation sign, Mathematica does not factor the constant out of the summation and fails to solve a simple equation.

How do I make Mathematica handle the problem that arises below as it handles the second case that follows it:

z[t_] = Sum[2 a + m[t], {t, 0, M}];
Solve[z[t] == 0, a]


Solve::nsmet: This system cannot be solved with the methods available to Solve.

Solve[Sum[2*a + m[t], {t, 0, M}] == 0, a]

z[t_] = a Sum[2, {t, 0, M}] + Sum[m[t], {t, 0, M}];
Solve[z[t] == 0, a]

{{a -> -(Sum[m[t], {t, 0, M}]/(2 (1 + M)))}}

• Maybe: z[t_] = Sum[#, {t, 0, M}] & /@ (2*a + m[t]); Solve[z[t] == 0, a]? May 11, 2019 at 19:44

One idea is to create a function that expands out sums:

expandSums[expr_] := expr /. s_Sum :> RuleCondition[expandSum[s]]

expandSum @ Sum[a_, i__] := factorSum[Expand[a], i]

factorSum[a_Plus, i__] := Thread[Unevaluated @ factorSum[a, i], Plus]
factorSum[a_Times, i__List] := With[{iter = Alternatives[i][[All, 1]]},
Times[
DeleteCases[a, _?(Not @* FreeQ[iter])],
Sum[DeleteCases[a, _?(FreeQ[iter])], i]
]
]
factorSum[a_, i__] := Sum[a, i]


Then you can apply this function to your Sum:

expanded = expandSums[Sum[2 a + m[t], {t, 0, M}]];
expanded //TeXForm


$$2 a(0) \sum _{t=0}^M 1+\sum _{t=0}^M m(t)$$

and then use Solve:

Solve[expanded == 0, a] //TeXForm


$$\left\{\left\{a(0)\to -\frac{\sum _{t=0}^M m(t)}{2 \sum _{t=0}^M 1}\right\}\right\}$$

• I do not understand this code yet, but this is robust. I could find p after the change: expanded = expandSums[ Sum[2 (p m[t] + 2 v[t] p p + p m[t])^2, {t, 0, M}]]; May 12, 2019 at 0:30
• Thanks for your kind response @CarlWoll. After simplification, the sum of "one's", ∑1 from 0 to M, in your response evaluates to equal (1+M). This simplification does not occur when expecting ∑∑m[t] to equal (1+M)* ∑m[t]. Any thoughts? May 16, 2019 at 23:32

Mathematica works this way. Notice that the summation with explicit numeric bounds:

Sum[2 + x[n], {n, 3}] == 6 + x + x + x


returns result True. The summation with no explicit numeric bounds:

Sum[2 + x[n], {n, k}]


returns the unevaluated result. There are good reasons why it works this way. However, there are always more than one way to get the results you want and you have used one of them yourself. Unless I am very mistaken, there is no easy way here working only within Sum[].

Another way is to treat summation as a linear operator with the help of Map[]. The following code attempt:

Map[Sum[#, {t, 0, N}] &, (2 a + m[t])]


does just that. The idea is that Map[] works on the top level of the sum 2 a + m[t] and distributes the Sum[] over the top level Plus[]. Of course, this is risky code because it will blindly do similar things if you apply it to 2 a * m[t] and, of course, Sum[] doesn't distribute over Times[].

An improved and safer version of it is this code:

(2 a+m[t]) /. x : __Plus :> Map[Sum[#, {t, 0, N}] &, x]


which only distributes over a sum of terms because of the explicit Rule[]. It probably does what you want.