I want to define a function deriv[f_[x_]] such that when I run deriv[f[x]], the output is an unevaluated expression fx=D[f[x],x].

The following does not work

deriv[f_[x_]] := ToExpression[ToString[f] <> ToString[x] <> "=" <> ToString[D[f[x], x]]]

because the output of deriv[f[x]] is just D[f[x],x], not fx=D[f[x],x].

  • $\begingroup$ = is an infix alias for Set, so when you force your string with ToExpression MMA will then evaluate that expression automatically as it does with all expressions that are not held or in Unevaluated. So really you're getting the output of Set and that's why it's only the RHS of your argument. Try wrapping the downvalue in Unevaluated. $\endgroup$
    – IPoiler
    Jul 29, 2015 at 15:27
  • 1
    $\begingroup$ Are you looking for something like deriv[f_[x_]] := HoldForm[Set[fx, D[f[x], x]]]? $\endgroup$ Jul 29, 2015 at 15:37
  • $\begingroup$ @hYPotenuser, Defer[] might actually make more sense here; on the other hand, if the input is g[z], I suppose the expected output is gz = … $\endgroup$ Jul 29, 2015 at 16:33

2 Answers 2

deriv[f_[x_]] := 
  ToExpression@StringJoin[ToString[f], ToString[x]] = D[f[x], x], 
  Set | D]




Inactive[Set][kryx, Inactive[D][kr[yx], yx]]

which displays as

kryx = D[kr[yx], yx]

Maybe this:

deriv[f_[x_]] := Inactive[D][f[x], x]


enter image description here


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.