# Extracting functions from a PDE that may or may not be subscripted

Using the Mathematica code

pde = -Subscript[\[Sigma],
f] (1 + Subscript[\[Chi],
f]) (D[h[x, z], z] D[Subscript[A, SuperPlus[v]][x, y, z], x, y] +
D[Subscript[A, SuperPlus[v]][x, y, z], x, z] +
D[b[x, y, z], z] D[h[x, z], z] - D[b[x, y, z], y] +
D[Subscript[B, x][x, y, z], z] D[h[x, z], z] -
D[Subscript[B, x][x, y, z], y] + D[Subscript[B, y][x, y, z], x] -
D[Subscript[B, z][x, y, z], x] D[h[x, z], z]) == 0


I obtain the following equation:

$$-\sigma _f \left(\chi _f+1\right) \frac{\partial \mathit{h}}{\partial \mathit{z}} \frac{\partial ^2{A}_{v^+}}{\partial \mathit{x}\, \partial \mathit{y}}-\sigma _f \left(\chi _f+1\right) \frac{\partial ^2{A}_{v^+}}{\partial \mathit{x}\, \partial \mathit{z}}-\sigma _f \left(\chi _f+1\right) \frac{\partial \mathit{b}}{\partial \mathit{z}} \frac{\partial \mathit{h}}{\partial \mathit{z}}+\sigma _f \left(\chi _f+1\right) \frac{\partial \mathit{b}}{\partial \mathit{y}}-\sigma _f \left(\chi _f+1\right) \frac{\partial \mathit{h}}{\partial \mathit{z}} \frac{\partial {B}_x}{\partial \mathit{z}}+\sigma _f \left(\chi _f+1\right) \frac{\partial {B}_x}{\partial \mathit{y}}-\sigma _f \left(\chi _f+1\right) \frac{\partial {B}_y}{\partial \mathit{x}}+\sigma _f \left(\chi _f+1\right) \frac{\partial \mathit{h}}{\partial \mathit{z}} \frac{\partial {B}_z}{\partial \mathit{x}}=0$$

I have written a function which can extract terms involving the partial derivatives of some specified function. This is given by

TermsContainingDerivatives[eqn_, func_] := Total[Cases[Terms[eqn], a_ /; ! FreeQ[a, Derivative[___][func][__]] -> a]][;


where Terms is given by (perhaps not the most elegant - please do point out if there is a more elegant way of defining it):

Terms[eqn_] := (ExpandAll@First@SubtractSides@eqn)[[#]] & /@ Range[Length[ExpandAll@First@SubtractSides@eqn]]


As expected, the output of TermsContainingDerivatives[pde,b] is

$$-\sigma _f \chi _f \frac{\partial \mathit{b}}{\partial \mathit{z}} \frac{\partial \mathit{h}}{\partial \mathit{z}}-\sigma _f \frac{\partial \mathit{b}}{\partial \mathit{z}} \frac{\partial \mathit{h}}{\partial \mathit{z}}+\sigma _f \chi _f \frac{\partial \mathit{b}}{\partial \mathit{y}}+\sigma _f \frac{\partial \mathit{b}}{\partial \mathit{y}}$$

When I apply the same to TermsContainingDerivatives[pde,A], it gives 0. However, I would like it to pick up the terms subscripted $$v^+$$.

If I try to modify the function to become

TermsContainingDerivatives[eqn_, func_] :=
Total[Cases[Terms[eqn], a_ /; ! FreeQ[a, Derivative[___][Subscript[func, __]][__]] -> a]];


the result of TermsContainingDerivatives[pde,A] then becomes $$-\sigma _f \chi _f \frac{\partial \mathit{h}}{\partial \mathit{z}} \frac{\partial ^2{A}_{v^+}}{\partial \mathit{x}\, \partial \mathit{y}}-\sigma _f \frac{\partial \mathit{h}}{\partial \mathit{z}} \frac{\partial ^2{A}_{v^+}}{\partial \mathit{x}\, \partial \mathit{y}}-\sigma _f \chi _f \frac{\partial ^2{A}_{v^+}}{\partial \mathit{x}\, \partial \mathit{z}}-\sigma _f \frac{\partial ^2{A}_{v^+}}{\partial \mathit{x}\, \partial \mathit{z}}$$ but then TermsContainingDerivatives[pde,b] gives zero.

My question is:

• How can I modify my function which gives the non-zero answers in both cases, so that terms are picked up regardless of whether they are subscripted or not?

Furthermore, as a more general question:

• When constructing a function, is it possible to specify that an input may be empty?
• Please show us the Mathematica code for pde. Mar 12, 2021 at 12:19
• I've added this to the start of the question. Mar 12, 2021 at 12:27
• What is the function Terms? Mar 12, 2021 at 12:54
• @DanielHuber Done, but there might be a more elegant way of defining it. Mar 12, 2021 at 13:03
• TermsContainingDerivatives[pde, b] gives not zero, but:(-Subscript[\[Sigma], f])*Derivative[0, 1][h][x, z]* Derivative[0, 0, 1][b][x, y, z] - Subscript[\[Sigma], f]*Subscript[\[Chi], f]* Maybe a new kernel will help. Derivative[0, 1][h][x, z]* Derivative[0, 0, 1][b][x, y, z] + Subscript[\[Sigma], f]*Derivative[0, 1, 0][b][x, y, z] + Subscript[\[Sigma], f]*Subscript[\[Chi], f]* Derivative[0, 1, 0][b][x, y, z] Maybe a new kernel will help. Mar 12, 2021 at 13:07

Here is a version that works for A and b:
TermsContainingDerivatives[eqn_, func_] :=

TermsContainingDerivatives[pde, b]