# [Maxima] rearranging and collecting terms

Valery Pipin pip at iszf.irk.ru
Thu Aug 6 04:21:02 CDT 2009

```Hi,

> > I hope this is not too much a trivial question, I'm a newbie to Maxima.
> > I'd like to rearrange expressions like this
> >
> > (3*cos(d1)^3-3*cos(d2)^3)*x
> >
> > or this
> >
> > (3*a*b1-3*a*b2)*x
> >
> > into a simpler form with collected terms;
> >
> > 3*(cos(d1)^3-cos(d2)^3)*x
> >
> > 3*a*(b1-b2)*x
> >
> >
> > Is there a solution that works in BOTH cases ?
> >
> > Factor  or  facsum  work for the 2nd expression but lead to unwanted
> > trigonometric expansion in the 1st expression.
> >
The following answers the particular question
(%i1) display2d:false;
(%o1) false
(%i2) (3*cos(d1)^3-3*cos(d2)^3)*x;
(%o2) (3*cos(d1)^3-3*cos(d2)^3)*x
(%i3) facsum(subst([cos(d1)^3=b1,cos(d2)^3=b2],%));

(%o3) -3*(b2-b1)*x
(%i4) subst([b1=cos(d1)^3,b2=cos(d2)^3],%);

(%o4) -3*(cos(d2)^3-cos(d1)^3)*x

If you have a more general case say then I would do as follows

(%i5) (A*cos(d1)^3-A*cos(d2)^3)*x;

(%o5) x*(cos(d1)^3*A-cos(d2)^3*A)
(%i6) facsum(%);

(%o6) -(cos(d2)-cos(d1))*(cos(d2)^2+cos(d1)*cos(d2)+cos(d1)^2)*x*A
(%i7) collecttermsl(expand(%),[A,x]);
(%o7) (cos(d1)^3-cos(d2)^3)*x*A

all rhe best!
V

```