detect sqrt in expression?



As Ray says, "sqrt" is just the external form; internally, it is x^(1/2).
 To "detect the op sqrt" in an expression, you'll need to specify more
clearly what you mean by "the op sqrt".  Do you mean all cases where Maxima
uses sqrt as part of the external form?  For example, Maxima displays
x^(-1/2) as 1/sqrt(x) -- does that "contain the operator sqrt" for your
purposes?  On the other hand, it displays sqrt(x)^a as x^(a/2) -- does that
contain the operator sqrt for your purposes?  How about sqrt(sqrt(x)) ==
x^(1/4) or sqrt(x^(1/3)) == x^(1/6)?  For your purposes, do you want to
count one sqrt or two in sqrt(x*y) == sqrt(x)*sqrt(y) (with
assume(x>0,y>0))?

The simplest answer to your question is something like this:

has_sqrt(ex):= if mapatom(ex) then false else if op(ex)='sqrt then true
else any_of(maplist(has_sqrt,ex))$
any_of(list) := member(true,list)$

It turns out this is horrifically inefficient (because of the way op works
for inflag:false), but that may not matter.  If it does matter, then
something like this:

has_sqrt(ex):= block([inflag:true], if mapatom(ex) then false else if
op(ex)="^" then is(part(ex,2)=1/2) else any_of(maplist(has_sqrt,ex))$
any_of(list) := member(true,list))$

Of course the exact logic of is(part(ex,2)=1/2) will need to be adjusted
based on your definition.

           -s


On Wed, Nov 16, 2011 at 18:16, Raymond Toy <toy.raymond at gmail.com> wrote:

>
>
> On Wed, Nov 16, 2011 at 2:54 PM, Edwin Woollett <woollett at charter.net>wrote:
>
>> I need a way to detect the op sqrt in an expression.
>>
>> So far, neither ?isinop or listofops seems to work:
>> ------------------------------**---------------
>> Maxima 5.25.1 http://maxima.sourceforge.net
>> using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
>>
>> (%i1) display2d:false$
>>
>> (%i2) ?isinop(a*bessel_j(0,x),**bessel_j);
>>
>> (%o2) bessel_j(0,x)
>>
>> (%i3) ?isinop(a*sqrt(x),sqrt);
>>
>> (%o3) false
>>
>
> If you want to know what's happening, try the following:
>
>  (%i2) sqrt(x);
>
> (%o2) sqrt(x)
> (%i3) :lisp $%o2
>
> ((MEXPT SIMP) $X ((RAT SIMP) 1 2))
>
> Thus, sqrt is converted internally to, essentially, x^(1/2).
>
> (%i3) ?isinop(a*sqrt(x),?mexpt);
>
> (%o3) sqrt(x)
>
> This doesn't check that the exponent is 1/2, though.
>
>>
>> (%i4) ?isinop(sqrt(x^2),sqrt);
>>
>> (%o4) false
>>
> This fails because sqrt(x^2) has already been simplified to abs(x) by the
> time isinop sees it.
>
> Ray
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
>