Let's say that I have


Is there a way to map $x$ to the first derivative of a function and $x^2$ to the second derivative of the same function? According to http://reference.wolfram.com/language/ref/Slot.html, I know that I can change

x /. x -> D[#, {y, 1}] &[a[y, z]]
x^2 /. x^2 -> D[#, {y, 2}] &[a[y, z]]

to yield the first and second derivatives of $a(y,z)$, respectively. I also know that I can use

x^2+x/. x^2 -> D[#, {y, 2}] &[a[y, z]] /. x -> D[#, {y, 1}] &[a[y, z]]

but if I have say, a polynomial of degree 70, manually telling Mathematica to do this is highly inefficient. Is there a method, such that, for $x^n$, I can tell Mathematica to map $x^n$ to the $n^{th}$ derivative of a function?


2 Answers 2

{x, x^2, x^2 + x} /. x^n_. :> Derivative[n, 0][a][y, z]
  • $\begingroup$ Sorry for short answer, was in hurry, if anything is unclear just ask. $\endgroup$
    – Kuba
    Commented Jun 2, 2016 at 18:18

Rule-replacement with x^n_. :> Derivative[n,0][a][y,z] (as done in Kuba's answer) has two drawbacks: if your polynomial has a constant term, then it will not be replaced by the zero-th derivative a[y,z], and if your polynomial is not expanded the result is incorrect. Namely, (1+x)(2+x) becomes (1+a'[y,z])(2+a'[y,z]) rather than 2a[y,z]+3a'[y,z]+a''[y,z] (here I use ' to denote derivative with respect to the first variable).

One option is to multiply by x and Expand:

{x, x^2, x^2 + x, (1 + x) (2 + x)} //
  (Expand[x #] /. x^n_. :> Derivative[n-1, 0][a][y, z] &)

(*{Derivative[1, 0][a][y, z],
   Derivative[2, 0][a][y, z],
   Derivative[1, 0][a][y, z] + Derivative[2, 0][a][y, z],
   2*a[y, z] + 3*Derivative[1, 0][a][y, z] + Derivative[2, 0][a][y, z]}*)

Another option which I find more natural is to get the CoefficientList, then rebuild the expression. But this does not thread nicely over lists of polynomials (here I used x^2+x).

Total@MapIndexed[#1 Derivative[#2[[1]] - 1, 0][a][y, z] &,
  CoefficientList[x + x^2, x]]

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.