# How to define a derivative but have it work with the existing D[] function?

I have a set of functions f_n[] that are functions of several variables c_m.

I'd like to give MMa a simple rule like df_n/dc_m = f_(n+m) but have it still use ordinary derivative rules on functions of f_n.

For example, if I ask for the derivative of (f_1 - f_0)^2 wrt c_2, I'd like MMa to apply the power rule (or other calc rules) on its own, but then apply the rule I supplied and return 2(f_1-f_0)(f_3-f_2). If I ask for the derivative of sin(f_123) wrt c_14, I'd like it to return cos(f_123)*f_137. And so on...

I posted my failed code attempts in my first versions of this Question, but those just seemed to confuse commenters as to WHAT I was trying to do. How can I get MMa to do what I want here?

• I don't think this is how the Derivative head is typically used...generally Derivative[n][f] represents the nth derivative of f, and Derivative[n1, n2, n3][f] represents the n1th derivative in the first argument, the n2th in the second, etc. Note that D is not short for Derivative, it's a different thing. Oct 7, 2021 at 5:21
• also, you mention having f_n[c_1,c_2,c_3]...is this actually f[n][c1, c2, c3]? I'm confused by f having a single argument in the code so far. Oct 7, 2021 at 5:24
• @thorimur Okay, I completely rewrote the question to clarify what I'm trying to do. Oct 7, 2021 at 13:35

EDIT: Rather than overload Derivative in the manner of your original question, der is used instead.

Clear["Global*"]

\$Version

(* "12.3.1 for Mac OS X x86 (64-bit) (June 19, 2021)" *)

der[expr_, c[m_]] := Module[{x},
D[expr /. f[n_] :> f[n][x], x] /.
{f[n_][x] :> f[n], f[n_]'[x] :> f[n + m]}]

der[f[n], c[m]]

(* f[m + n] *)

der[(f[1] - f[0])^2, c[2]]

(* 2 (-f[0] + f[1]) (-f[2] + f[3]) *)

der[Sin[f[123]], c[14]]

(* Cos[f[123]] f[137] *)

• Okay, wow, that sure did work. Could you maybe give me some idea WHY that worked? That last thing you did there inside the brackets was either pure sorcery or some MMa functionality I've never encountered before. Oct 7, 2021 at 23:51
• To differentiate a function it needs to be an explicit function of the variable by which it is being differentiated. The expression is artificially made a function of x, differentiated with respect to x, then the artificial dependence on x is removed. That last step includes f[n_]'[x] :> f[n + m] which implements your original Derivative[f[n_], c[m_]] :> f[n + m]` Oct 8, 2021 at 0:05