On Tue, Feb 9, 2010 at 11:54 AM, Nikos Apostolakis <nikos.ap at gmail.com> wrote:
> Dear list,
>
> how can one collect terms of an expression wrt a variable? ?I am using
> the following
>
> ? (%i1) subst(y = m*x +b, (x-2)^2 + (y-3)^2 = 5), expand;
> ? ? ? ? ? ? ? ?2 ?2 ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ?2
> ? (%o1) ? ? ? m ?x ?+ x ?+ 2 b m x - 6 m x - 4 x + b ?- 6 b + 13 = 5
> ? (%i2) apply("+", makelist(coeff(%, x, n)*x^n, n, 0,2));
> ? ? ? ? ? ? ? ? ?2 ? ? ? 2 ? ? ? ? ? ? ? ? ? ? ? ? ?2
> ? (%o2) ? ? ? ?(m ?+ 1) x ?+ (2 b m - 6 m - 4) x + b ?- 6 b + 13 = 5
>
> but surely there must be an easier way! I just couldn't find it.
I'm sure there are other possibilities, but here are two simple options:
(%i1) subst(y = m*x +b, (x-2)^2 + (y-3)^2 = 5), expand;
(%o1) m^2*x^2+x^2+2*b*m*x-6*m*x-4*x+b^2-6*b+13=5
(%i2) rat(%o1, x);
(%o2)/R/ (m^2+1)*x^2+((2*b-6)*m-4)*x+b^2-6*b+13=5
(%i3) load(format)$
(%i4) format(%o1, %poly(x));
(%o4) (m^2+1)*x^2+(2*b*m-6*m-4)*x+b^2-6*b+13=5
HTH, Andrej