Possible Duplicate:
How do I expand a sum?

How do I expand an equation in the given form:

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

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$

I know that I've asked this question, but this answer works only if the expression is being edited at the very moment. How to expand it and to distribute sum when I have such an expression saved into variable:

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

Distribue[Eq] and Expand[Eq] return just unchanged Eq.


f[n_] := Sum[-2 x[i] ( - a x[i] - b + y[i]), {i, n}]
eq = f[n] == 0;
eq /. Sum[x__, y_] :> Distribute@Sum[Expand@x, y]
-> Sum[2*b*x[i], {i, n}] + Sum[2*a*x[i]^2, {i, n}] + Sum[-2*x[i]*y[i], {i, n}] == 0
  • $\begingroup$ That is just something I wanted to avoid. In my calculations Eq is a result of previous derivations, and to make my code flexible I cannot define it that way f[n_]:=... I need to work with my eq expression $\endgroup$ – Misery Jan 7 '13 at 12:59
  • $\begingroup$ @Misery Use the last statement only $\endgroup$ – Dr. belisarius Jan 7 '13 at 13:07
  • $\begingroup$ Thanks, now it works as expected :] I am quite a noob in Mathematica, therefore I find some answers hard to use. $\endgroup$ – Misery Jan 7 '13 at 14:06
  • 2
    $\begingroup$ @Misery I found your comment "That is just something I wanted to avoid" somewhat stubborn for a "noob" $\endgroup$ – Dr. belisarius Jan 7 '13 at 14:16
  • 1
    $\begingroup$ Well... I am quite experienced programmer in SCILAB and C++ and I must admit, that sometimes I get really irritated if I cannot even understand a code snippet :p Sorry! Didn't mean to "sound" that way. $\endgroup$ – Misery Jan 7 '13 at 14:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.