8.32 collecting radicals (20.4.01)

8.32.1 Ken Monks
8.32.2 Allan Wittkopf (20.4.01)
8.32.3 David Harrington (20.4.01)
8.32.4 Robert Israel (20.4.01)
8.32.5 Helmut Kahovec (20.4.01)
8.32.6 Preben Alsholm (20.4.01)

8.32.1 Ken Monks

Is there a built in Maple command that collects the coefficients of like numeric radicals in an expression. For example, a command that would convert this:

2*a*sqrt(2)+2*b*sqrt(3)+c*sqrt(2)+7*d*sqrt(3)+5 
 
#to this: 
 
(2*a+c)*sqrt(2)+(2*b+7*d)*sqrt(3)+5
                                                                                  
                                                                                  
 

8.32.2 Allan Wittkopf (20.4.01)

You could use frontend and collect:

> xpr:=2*a*sqrt(2)+2*b*sqrt(3)+c*sqrt(2)+7*d*sqrt(3)+5; 
 
                          1/2        1/2      1/2        1/2 
              xpr := 2 a 2    + 2 b 3    + c 2    + 7 d 3    + 5 
 
> frontend(collect,[xpr,indets(xpr,`^`)],[{`+`,`*`,`set`},{}]); 
 
                                  1/2              1/2 
                     (2 b + 7 d) 3    + (2 a + c) 2    + 5
 

8.32.3 David Harrington (20.4.01)

Not very elegant but

subs(rt2=sqrt(2),rt3=sqrt(3),collect(subs(sqrt(2)=rt2,sqrt(3)=rt3,x),{rt2,rt3}));
 

will do it.

8.32.4 Robert Israel (20.4.01)

I don’t think so. But it’s not too hard to write one. More generally, here’s a command that collects with respect to all subexpressions of a given type.

> collectype:= proc(f::algebraic,T::type) local fp,x,X,S,q; 
   X:= indets(f,T); 
   S:= [seq(x = `tools/genglobal`(q),x=X)]; 
   fp:= subs(S,f); 
   subs(map(t -> rhs(t)=lhs(t),S),collect(fp,map(rhs,S),distributed)); 
   end;
 
In your case:
> collectype(2*a*sqrt(2)+2*b*sqrt(3)+c*sqrt(2)+7*d*sqrt(3)+5, radical); 
 
       (c + 2 a) sqrt(2) + (2 b + 7 d) sqrt(3) + 5
 

Or, for example:

> S:= a[1]*x*y+a[1]*x^3*y^3+a[1]*x*y^2+a[1]*x^3*y^2+a[2]*x^2*y 
    +a[2]*x^2*y^3+2*a[2]*x^2*y^2+a[3]*x^3*y 
    +a[3]*x*y^3+a[3]*x^3*y^2+a[3]*x*y^2; 
 
> collectype(S, indexed); 
 
      2    3  2          3  3           2      2  3      2  2 
  (x y  + x  y  + x y + x  y ) a[1] + (x  y + x  y  + 2 x  y ) a[2] 
               3    3  2      2    3 
         + (x y  + x  y  + x y  + x  y) a[3]
 

8.32.5 Helmut Kahovec (20.4.01)

Either of the two solutions shown below is a bit clumsy. You may either introduce dummy variables like sqrt2 and sqrt3

> e1:=2*a*sqrt(2)+2*b*sqrt(3)+c*sqrt(2)+7*d*sqrt(3)+5: 
 
> r:=sqrt(2)=sqrt2,sqrt(3)=sqrt3: 
> rinv:=sqrt2=sqrt(2),sqrt3=sqrt(3): 
 
> subs({rinv},collect(subs({r},e1),[sqrt2,sqrt3])); 
 
             (2 a + c) sqrt(2) + (2 b + 7 d) sqrt(3) + 5
 

or define new types like t1 and t2

> lprint(e1); 
2*a*2^(1/2)+2*b*3^(1/2)+c*2^(1/2)+7*d*3^(1/2)+5 
 
> t1:=identical(2)^identical(1/2); 
 
                                     identical(1/2) 
                   t1 := identical(2) 
 
> t2:=identical(3)^identical(1/2); 
 
                                     identical(1/2) 
                   t2 := identical(3) 
 
> factor(select(hastype,e1,t1)) 
>   +factor(select(hastype,e1,t2)) 
>   +remove(hastype,e1,{t1,t2}); 
             (2 a + c) sqrt(2) + (2 b + 7 d) sqrt(3) + 5
 

8.32.6 Preben Alsholm (20.4.01)

I think not. The best I could come up with was to use frontend with collect twice. One single use of frontend didn’t work as expected.

> u:=2*a*sqrt(2)+2*b*sqrt(3)+c*sqrt(2)+7*d*sqrt(3)+5: 
> u1:=frontend(collect,[u,sqrt(2)]); 
 
       u1 := (c + 2 a) sqrt(2) + 5 + 2 b sqrt(3) + 7 d sqrt(3) 
 
> frontend(collect,[u1,sqrt(3)]); 
 
             (2 b + 7 d) sqrt(3) + (c + 2 a) sqrt(2) + 5 
 
> frontend(collect,[u,[sqrt(2),sqrt(3)]]); #Doesn't work, why not? 
 
       2 a sqrt(2) + 2 b sqrt(3) + c sqrt(2) + 7 d sqrt(3) + 5