[Math] Solve a function with sum in Mathematica

mathematica

I want to solve an equation with sum in Mathematica, e.g.,

$$\sum _i \frac{a_i}{x}=1$$

But when I input this:

Solve[Sum[Subscript[a, i]/x, i] == 1, x]

Mathematica will tell me:

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

If I remove the $x$ from the equation like this:

$$\frac{\sum _i a_i}{x}=1$$

And:

Solve[Sum[Subscript[a, i], i]/x == 1, x]

Then Mathematica can actually solve it. But this seems a bit troublesome and strange to me.
So am I using it incorrectly? What should I input in this case, thanks!

Best Answer

Mathematica does not have the (user facing) ability to do manipulations of unevaluated sums or integrals. However, simple cases are easily programmed by hand.

I assume that the indefinite sum that you're really interested in is more complicated than the example you provided. Here's some quick code for taking common factors out of sums and expanding sums. You can write similar code to reverse these operations.

factorSum[expr_] := expr /. Sum[x_, i_] :> Module[
    {factx = Factor[x], common, 
     ii = If[Head[i] === List, First[i], i]},
    common = If[Head[factx] === Times, Select[factx, FreeQ[#, ii]&], 1];
    common Sum[factx/common, i]]
expandSum[expr_] := expr /. Sum[x_, i_] :> Module[
    {expandx = Expand[x], ii = If[Head[i] === List, First[i], i]},
    If[Head[expandx] === Plus, 
     Total@Table[Sum[term, i], {term, List @@ expandx}], Sum[x, i]]]

And here it is in action (you can replace the indefinite i with a more definite {i, n1, n2} and it still works):

In[3]:= factorSum[Sum[a[i]*x*y*b^i + y*c[i], i]]
        expandSum[%]
        factorSum[%]

Out[3]= y*Sum[b^i*x*a[i] + c[i], i]

Out[4]= y*(Sum[b^i*x*a[i], i] + Sum[c[i], i])

Out[5]= y*(x*Sum[b^i*a[i], i] + Sum[c[i], i])

Note that it is a little slow, because every time a new Sum is produced, Mathematica tries to see if it can evaluate it. If you were serious about implementing formal manipulations of sums and integrals, you'd have to define new symbolicSum-type objects or turn off the built in behaviour while your code is running.


Anyway, here's your original problem:

In[6]:= Solve[factorSum[Sum[Subscript[a, i]/x, i]] == 1, x]
Out[6]= {{x -> Sum[Subscript[a, i], i]}}
Related Question