5.91 Example 1

Detect \(\sqrt {x y}\) in expression.

restart; 
expr:= sin(x)*sqrt(x*y); 
if patmatch(expr,a::anything*(b::anything*x*y)^(c::anything),'la') then 
    assign(la); 
    if c =1/2 or c=-1/2 then 
       print("found sqrt(x*y)"); 
    else 
       print("did not find sqrt(x*y)"); 
    fi; 
 fi;
 

But if the expression was \(\sin (x)\sqrt {x y}+3\) then the above would fail, because there are a term after \(\sqrt {x y}\), so the pattern has to change to

restart; 
expr:= sin(x)*sqrt(x*y)+3; 
if patmatch(expr,a::anything*(b::anything*x*y)^(c::anything)+d::anything,'la') then 
    assign(la); 
    if c =1/2 or c=-1/2 then 
       print("found sqrt(x*y)"); 
    else 
       print("did not find sqrt(x*y)"); 
    fi; 
 fi;