Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

Regards

share|improve this question

marked as duplicate by whuber, Artes, rm -rf Jan 7 '13 at 15:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer 1

up vote 2 down vote accepted
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
*)
share|improve this answer
    
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 –  Misery Jan 7 '13 at 12:59
    
@Misery Use the last statement only –  belisarius Jan 7 '13 at 13:07
    
Thanks, now it works as expected :] I am quite a noob in Mathematica, therefore I find some answers hard to use. –  Misery Jan 7 '13 at 14:06
2  
@Misery I found your comment "That is just something I wanted to avoid" somewhat stubborn for a "noob" –  belisarius Jan 7 '13 at 14:16
1  
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. –  Misery Jan 7 '13 at 14:19
add comment

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