# Differentiate w.r.t. squared variable

Is it possible to differentiate symbolically w.r.t. some squared quantity in Mathematica? Evaluating something like

D[x^4 + 2 x^2 + 6, x^2]


gives the error message

General::ivar: x^2 is not a valid variable.


Is there some way around this without defining a new variable $y = x^2$?

• What would be the derivative of $x^3$ wrt $x^2$? – anderstood Jan 18 '17 at 16:36
• @anderstood $\frac{3}{2} x$. – Casimir Jan 18 '17 at 16:40
• So, just D[x^4 + 2 x^2 + 6, x] / D[x^2,x]? – swish Jan 18 '17 at 16:47
• @swish I guess this should be an (if not the) answer. – anderstood Jan 18 '17 at 17:16
• By the chain rule, using implicit differentiation, Df/Dx^2 = (Df/Dx) / (Dx^2/Dx), so this method would also work in general. – JKreft Jan 18 '17 at 18:17

From @swish's comment (but using Dt):

myD[f_,x_]:=FullSimplify[Dt[f]/Dt[x]]

myD[x^3, x^2]
(* (3 x)/2 *)

myD[x^4 + 2 x^2 + 6, x^2]
(* 2 (1 + x^2) *)


If you need to specify constants, I would recommend the following:

myD[f_, x_, cons : OptionsPattern[Dt]] :=
FullSimplify[Dt[f, cons]/Dt[x, cons] ]

myD[1/Sqrt[x^2 y^4 + m^4], y^3, Constants -> {x, m}]
(* -((2 x^2 y)/(3 (m^4 + x^2 y^4)^(3/2))) *)

• Thanks for the inspiration. Since I will apply myD to terms containing multiple independent variables for which Dt gives lots of extra terms containing differentials of variables that should really be treated as constant, I ended up going with the implementation myD[f_,x_^n_]:=FullSimplify[D[f,x]/D[x^n,x]]. For instance, for f[y] = 1/Sqrt[x^2 y^4 + m^4], myDt, i.e. your implementation gives myDt[f[y],y^3] = (-2 m^3 Dt[m] - x y^3 (y Dt[x] + 2 x Dt[y]))/(3 y^2 (m^4 + x^2 y^4)^( 3/2) Dt[y]) whereas myD yields myD[f[y],y^3] = -((2 x^2 y)/(3 (m^4 + x^2 y^4)^(3/2))). – Casimir Jan 19 '17 at 13:49

Temporarily replace x^2 by y and then revert the change:

D[x^4 + 2 x^2 + 6 /. x -> Sqrt[y], y] /. y -> x^2
(* 2 + 2 x^2 *)


For the general case, furthermore use Simplify to replace Sqrt[x^2] in the final result by x:

Simplify[D[x^3 /. x -> Sqrt[y], y] /. y -> x^2, Assumptions -> x > 0]
(* (3 x)/2 *)