# How to factorise a variable out of an indefinite sum?

(I should start by saying I'm a total beginner with Mathematica, so if the answer needs code I'll need it spelled out quite clearly, the learning curve for this is proving steep!)

Basically what I am trying to so is manipulate a power series for an expression involving the function alpha(r) inside an indefinite sum, so that I can pick out coefficients from it (series solving an ODE, for context). The following code is used to produce alpha and then the sum of squares of its first derivative.

α[r_] := Sum[Subscript[α, i, j]*r^i, {i, 0, 5}]

η[r_] :=
Sum[D[α[r], r]^2, {j, 1, N}] /. Sum[x_, y_] :> (Sum[#, y] & /@ Expand @ x)


Without /. Sum[x_, y_] :> (Sum[#, y] & /@ Expand@x), the output stays all collected into one sum over j even when Expand is applied, shown below:

With that code, the sum splits up into a series of sums, each only one term long:

However, the variable r is still inside the sum in each case, meaning that when I apply Coefficient[,r,2] (etc) to the output, it just spits out all of the output (i.e. doesn't do anything) rather than just identifying the terms of the correct power of r. What I really need is some code that pulls the 'r' factor out of each summation term, so that Coefficient will pick out the required terms.

• It is not quite clear what you are looking for but I guess this is related How do I expand a sum? Aug 24, 2014 at 21:15
• I'll make it clearer hopefully: if you do (e.g.) Coefficient[,r,2] on the 2nd of the above outputs, it returns the whole expression, not just the r^2 bits. If the 'r's were factored out of the sums, it would work. That's what I need. Aug 24, 2014 at 21:29
• That code is a start, but it doesn't seem to work on my example for some reason. :/ Aug 24, 2014 at 21:44

Here's my approach using replacement rule /. Sum[r^p_. a__, b_] :> r^p Sum[Times[a], b]. I have a hunch that there's a built in function that could accomplish this but I can't seem to find it. I've also made some modifications to your code:

1) Changing the first function to β to avoid clashing with the subscripted symbol α.

2) Changing the upper limit of the sum in η from N to n, since N is a system function.

3) Streamlining the η function using @Artes answer in the linked Q&A in the question's comment.

Clear[α, β, η, r]
β[r_] := Sum[Subscript[α, i, j]*r^i, {i, 0, 5}]
η[r_] := Distribute@Sum[D[β[r], r]^2 // Expand, {j, 1, n}] /.
Sum[r^p_. a__, b_] :> r^p Sum[Times[a], b]

η[r]


CoefficientList[η[r], r] //
Grid[Transpose@{Defer[r^#] & /@ Range[0, Length@# - 1], #}, Frame -> All] &


• That's excellent, thank you so much, I'll try it now! re your changing of the function name: do you think that's a significant problem? I don't think I'e run to any issues yet with it, but the equations I'm doing are quite complicated so I suppose it's possible. Aug 24, 2014 at 21:55
• I'm not too sure. I don't think the first problem is that significant (your function works in this case even if the function and the symbols are both alpha), but the second problem might be. Also, you should wait a while (like 48 hours) before accepting an answer in order to attract more (and better) answers to your questions. Aug 24, 2014 at 22:03
• Oh, that tick means accept? I am a noob on here too as you can see :) Still, you solved my problem and reading your code has actually taught me about how to make my own, I can see where you get it from now I look at it, so I think you deserve it anyway! Aug 24, 2014 at 22:07
• Yes :) Thank you for accepting, though I'm sure there are more elegant ways to achieve what you want (that you might want to adopt instead of my method), hence my suggestion to wait a while to accept an answer. Thanks again. Aug 24, 2014 at 22:08
• I have a question in retrospect, seismatica: why is the Times function where it is? Why not just /. Sum[r^p_. a__, b_] :> r^p Sum[a, b]? I'm still trying to figure out this code precisely! Aug 26, 2014 at 7:47