Question: using select on expression with single diff(y(x),x)

I have an expression and I want to select the part of the expression which has diff(y(x),x) in the expression.

Using select(has,expr,diff(y(x),x) works, except when the expression happend to be exactly diff(y(x),x) in this case select returns diff()

I understand why this happens. But I can not avoid this problem by say first checking if the expression has more than one operand, because nops(diff(y(x),x) is 2 and not one. Also 1+diff(y(x),x) has two operands. And I can not check if the expression is of type `+` or `*` before, because other types can have more than one operand also.

So now what I do is the folliwng: first check if the expression has diff(y(x),x). If so, convert the expression to D and now check if nops is more than one, and if so, only now call select.

This is becuase nops(D(y)(x)) is one, while nops(diff(y(x),x) is two.

Is there a better way to do this, in order to avoid calling select and getting diff() ? I suppose I could also just check if the expression is exactly diff(y(x),x) before even calling select or has and avoid all this?

Worksheet attached


 

interface(version)

`Standard Worksheet Interface, Maple 2022.1, Windows 10, May 26 2022 Build ID 1619613`

#the wrong way
expr:=diff(y(x),x):
if has(expr,diff(y(x),x)) then
   part_with_diff:=select(has,expr,diff(y(x),x));
fi;

diff()

#add extra check,  example 1
expr:=diff(y(x),x):
if has(expr,diff(y(x),x)) then
    if nops(convert(expr,D))>1 then
       part_with_diff:=select(has,expr,diff(y(x),x));
    else
       print("expression is itself diff(y(x),x))");
    fi;
fi;

"expression is itself diff(y(x),x))"

#add extra check, example 2
expr:=1+3*diff(y(x),x):
if has(expr,diff(y(x),x)) then
    if nops(convert(expr,D))>1 then
       part_with_diff:=select(has,expr,diff(y(x),x));
       print("part_with_diff=",part_with_diff);
    else
       print("expression is diff(y(x),x))");
    fi;
fi;

"part_with_diff=", 3*(diff(y(x), x))


 

Download best_way_to_check.mw

Please Wait...