# Save a Derivative

I have defined the derivative of a function

Derivative[1][f][x_] := Sin[x]

How can I save this definition in a file? Derivatives are not included in FullDefinition[f], so Save["file.m",f] won't work. The best I can do now is

WriteLine["file.m","Derivative[1][f][x_] := Sin[x]"]

Any ideas how to do this more properly?

• This is weird, because if I say g[x_]:=Log[x]+2 for example, I can get the DownValues[g] and it gives {HoldPattern[g[x_]] :> Log[x] + 2}. But if I define Derivative[1][f][x_] := Sin[x] - what happens then?? because DownValues[f'] is empty. I guess this boils down to finding how to get DownValues for indexed functions and non-head forms. – flinty Oct 3 at 15:54
• Try the following g[3][x_] := Tan[x] and do FullDefinition[g]. That should work. Therefore I think in your case it's associating the definition with Derivative and not f. If you do FullDefinition[Derivative] you should see f'[x_] := Sin[x] appearing at the end. This means you have to save the definition of Derivative. You'll need to Unprotect the Derivative when you reload it, so it gets overwritten with your new definitions. – flinty Oct 3 at 15:58
• @flinty I don't think Derivative is protected. You can just do Save[..., {f, Derivative}] and it should work. – Sjoerd Smit Oct 3 at 16:11

This works for me:

ClearAttributes[Derivative, {ReadProtected}]
Derivative[1][f][x_] := Sin[x]
Save["file.m", {f, Derivative}]


# Edit

Included the line that clears the ReadProtected attribute, in case it's necessary. It seems like this might depend on version or something, because for me Derivative never has that attribute in a new kernel.

• Doesn't work for me. The output file just contains the attributes of Derivative. I am using Kernel 11.2 – Brett van de Sande Oct 3 at 18:25
• On further investigation, you solution works if the ReadProtected is removed: ClearAttributes[Derivative, {ReadProtected}] It also adds the associated documentation string, but I can live with that. – Brett van de Sande Oct 3 at 18:36
• Oh, that's strange. I did that once, but not in this kernel session. Looks like if you use ClearAttributes[Derivative, {ReadProtected}], the attributes remain that way even between sessions. – Sjoerd Smit Oct 3 at 21:09

I did not fully understand the intention, it looks like you are trying to assign a function to an operator. If you use the name df the following works:

df[x_] := Sin[x]
(* save *)
Export["/tmp/test.wdx", df[x]]
(* clear *)
Clear@df