# How do I expand a sum?

I have a problem with Mathematica's symbolic manipulations. As an example, consider the following expression:

$$\sum _{i=1}^n -2 x_i \left(-a x_i-b+y_i\right)=0$$

How do I get Mathematica to expand it into this form:

$$-\sum _{i=1}^n x_i y_i +a \sum _{i=1}^n x_i^2+b \sum _{i=1}^n x_i=0$$

What I mean is: what functions do I apply to get the product to expand and the summation operator to distribute?

-
You are requesting two changes: first to Expand the products in the summation and second to Distribute the action of Sum over the addition. Consulting the help pages for Expand and Distribute will answer your question. – whuber Dec 26 '12 at 18:15

Distribute @ Sum[-2 Subscript[x, i] (-a Subscript[x, i] - b + Subscript[y, i]) // Expand,

 thanks a lot :] – Misery Dec 26 '12 at 18:38 How can one make the 2a and 2b factors come out in front of the sums? (and in general any variables that do not depend on the summation index)? – Andrei May 9 at 13:26 One can play with pattern matching, e.g. let the resulting expression be expr, then expr /. {Sum[Times[c_, d__], List[i_, n_]] :> c Sum[Times[d], List[i, n]]} does what you want. In more general cases one can work with ReplaceRepeated (//.) and (or) restrict pattern like e.g. c_?NumericQ. – Artes May 9 at 14:09