For example,I want to match a multiple partial derivative of a function $f^{(1,0,1)}[x,y,z]$ which is the same as$\frac{\partial^2{f}}{\partial{x}\partial{z}}$.The only information given is the list of the independent variables such as {x,y,z} in this example. So I try to construct a pattern something like u_^[(n__)][independ] to match an arbitrary partial differential of an unknown function. However, it turns out to be wrong. For a simple case that has only one independent variable like $f^{(3)}[x]$ I can construct a pattern like D[u_[independ],{independ,n_}] to correctly match the partial derivative.But i don't know how to proceed to the multivariable cases. Are there some good soulutions?


2 Answers 2


Tutorial: Introduction to Patterns

"...the structure the Wolfram Language uses in pattern matching is the full form of expressions printed by FullForm."


D[foo[x, y, z], {x, 2}, {y, 3}]

enter image description here

See the underlying expression using FullForm:

FullForm @  %
Derivative[2, 3, 0][foo][x, y, z]

So the pattern you need is Derivative[__][_][__]:

list = {g[x], D[foo[x, y, z], {x, 2}, {y, 3}], x + 4, w''[x]};

Cases[Derivative[__][_][__]] @ list

enter image description here


Define some test cases, including 1 that we would like to leave unchanged

tests = Flatten[{f[u], f'[x], D[f[x, y], {{x, y}, 2}]}];

Examining the FullForm of these gives us a clue as to what we are trying to match

FullForm[tests] // OutputForm
(* List[f[u], Derivative[1][f][x], Derivative[2, 0][f][x, y], Derivative[1, 1][f][x, y], 
Derivative[1, 1][f][x, y], Derivative[0, 2][f][x, y]] *)

Define a pattern to match this (and a rule, transforming the result)

pattern = Derivative[ns__][g_][xs__] -> g[xs, ns];

Demonstrate that it matches where desired

tests /. pattern
(* {f[u], f[x, 1], f[x, y, 2, 0], f[x, y, 1, 1], f[x, y, 1, 1], f[x, y, 0, 2]} *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.