3.22 How to obtains list of all derivatives in expression?

I had a need to find all derivatives of form  diff(y( anything), anything) in an ode so to check that \(y\) argument is not different among them.

For an example, given

\[ a \left (\frac {d^{2}}{d x^{2}}y \left (x \right )\right ) \left (\frac {d^{3}}{d x^{3}}y \left (x \right )\right )-\sqrt {1+\left (\frac {d^{2}}{d x^{2}}y \left (x \right )\right )^{2}}+\frac {1}{\sin \left (\frac {d^{5}}{d x^{5}}y \left (x \right )\right )} = \frac {d}{d z}y \left (z \right )+{\mathrm e}^{y \left (x \right )+\frac {d}{d x}y \left (x \right )}+\frac {d}{d x}r \left (x \right ) \]

the result should be

\[ \left [\begin {array}{c} \frac {d^{5}}{d x^{5}}y \left (x \right ) \\ \frac {d^{4}}{d x^{4}}y \left (x \right ) \\ \frac {d^{3}}{d x^{3}}y \left (x \right ) \\ \frac {d^{2}}{d x^{2}}y \left (x \right ) \\ \frac {d}{d x}y \left (x \right ) \\ \frac {d}{d z}y \left (z \right ) \end {array}\right ] \]

One issue is how to check for diff and also check that the dependent variable is \(y\) so as not to pick other dependent variables such as \(z\) in this example. This was done by converting diff to D otherwise it will not work.

restart; 
expr:=a*diff(y(x),x$2)*diff(y(x),x$3)-sqrt(1+ diff(y(x),x$2)^2)+1/sin(diff(y(x),x$5))=diff(y(z),z)+exp(y(x)+diff(y(x),x))+diff(r(x),x); 
 
#this finds all derivatives 
list_of_diffs:=indets(expr,'satisfies'(s->op(0,s)='diff' and op([0,1],convert(s,D))=y)); 
 
#This finds all dependent variables 
list_of_diffs:=convert(list_of_diffs,list); 
list_of_indep_variables   := map(x->PDEtools:-Library:-GetIndepVars(x)[-1],list_of_diffs); 
 
#this converts it to set. If the ODE is valid, then the list_of_indep_variables should 
#have one entry $x$ in it and nothing else. 
list_of_indep_variables   := convert(ListTools:-Flatten(list_of_indep_variables),set); 
 
if nops(list_of_indep_variables)>1 then 
  error( cat("Only one independent variable expected in differential form, found ", 
                   convert(list_of_indep_variables,string)) ); 
fi; 
 
if list_of_indep_variables[1]<>x then 
   error( cat("Independent variable expected in differential form not same as independent variable in function given ", 
      convert(list_of_indep_variables,string)) ); 
fi;
 

Another option instead of doing all the above is to do this

expr:=a*diff(y(x),x$2)*diff(y(x),x$3)-sqrt(1+ diff(y(x),x$2)^2)+1/sin(diff(y(x),x$5))=diff(y(z),z)+exp(y(x)+diff(y(x),x))+diff(r(x),x); 
try 
   PDEtools:-Library:-GetDepVars([y(x)],expr); 
catch 
  error "functions with name [y] having different dependency: [[y(x), y(z)]]" 
end try;
 

The function  PDEtools:-Library:-GetDepVars([y(x)],expr) checks that only \(y(x)\) dependency shows up. It throws an error otherwise. So if an error is thrown, then this means \(y\) shows up with different independent variables.