0
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – flinty Oct 3 at 15:54
  • $\begingroup$ 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. $\endgroup$ – flinty Oct 3 at 15:58
  • $\begingroup$ @flinty I don't think Derivative is protected. You can just do Save[..., {f, Derivative}] and it should work. $\endgroup$ – Sjoerd Smit Oct 3 at 16:11
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Doesn't work for me. The output file just contains the attributes of Derivative. I am using Kernel 11.2 $\endgroup$ – Brett van de Sande Oct 3 at 18:25
  • $\begingroup$ 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. $\endgroup$ – Brett van de Sande Oct 3 at 18:36
  • 1
    $\begingroup$ 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. $\endgroup$ – Sjoerd Smit Oct 3 at 21:09
-1
$\begingroup$

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
(* load *)
df[x_] := Evaluate@Import["/tmp/test.wdx"]
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Your answer would be correct for saving the definition of the function itself. I am asking how to save a definition of a derivative of the function. $\endgroup$ – Brett van de Sande Oct 3 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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