factoring out complex expression



If you want to put it in the form

     (a^2+b^2)*(...) + ...

you can do something like this:

exp: a^2*gama+b^2*gama + beta*a*c*p + beta*b*d*p - c*b*gama*p +
a*d*gama*p - beta*c*b*r + beta*a*d*r - a*c*gama*r - b*d*gama*r +
a^2*G+b^2*G;

a2b2: a^2+b^2$
expd: divide(exp,a2b2)$

expd[1]*a2b2 + facsum(expd[2],r,p,[gama,beta]);

Is that better?

           -s

2011/12/19 V?t Mach-?i?ka <vit.machzizka at gmail.com>

> thank you very much for your prompt answer and sorry for not
> describing my problem sufficiently.
>
> i thinking about maxima as a tool to generate analytical derivatives
> into my optimization program. i have hundreds of similar expressions
> and i want this to be a part of general simplification method which
> will be called over each of the expressions.
>
> so i don't know in advance if there will be some (b^2+a^2)
> subexpression. but if there will be i would like to handle them as i
> described.
> the command "substpart(factor(piece),e1,[1,4]);" is based on knowledge
> the (b^2+a^2) subexpressions are at positions 1 and 4. i think i'm
> looking for some content based method for this - if there are some
> (b^2+a^2) subexpressions then factor them out.
>
> thank you very much
> vit mach-zizka
>
>
> 2011/12/19 Stavros Macrakis <macrakis at alum.mit.edu>:
> > Don't know if this counts as "elegant", but...
> >
> > (%i1) exp: a^2*gama+b^2*gama + beta*a*c*p + beta*b*d*p - c*b*gama*p +
> > a*d*gama*p - beta*c*b*r + beta*a*d*r - a*c*gama*r - b*d*gama*r +
> > a^2*G+b^2*G;
> > (%o1) b^2*G+a^2*G-b*d*gama*r-a*c*gama*r+a*beta*d*r-b*beta*c*r+a*d*gama*p
> >            -b*c*gama*p+b*beta*d*p+a*beta*c*p+b^2*gama+a^2*gama
> > (%i2) e1:facsum(exp,r,p,[gama,beta]);
> > (%o2) (b^2+a^2)*G+(beta*(a*d-b*c)-(b*d+a*c)*gama)*r
> >                  +((a*d-b*c)*gama+beta*(b*d+a*c))*p+(b^2+a^2)*gama
> > (%i3) e2:substpart(factor(piece),e1,[1,4]);
> > (%o3) (b^2+a^2)*(G+gama)+(beta*(a*d-b*c)-(b*d+a*c)*gama)*r
> >                         +((a*d-b*c)*gama+beta*(b*d+a*c))*p
> >
> >
> > 2011/12/19 V?t Mach-?i?ka <vit.machzizka at gmail.com>
> >>
> >> exp: a^2*gama+b^2*gama + beta*a*c*p + beta*b*d*p - c*b*gama*p +
> >> a*d*gama*p - beta*c*b*r + beta*a*d*r - a*c*gama*r - b*d*gama*r +
> >> a^2*G+b^2*G;
> >
> >
>