1
$\begingroup$

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].

$\endgroup$
3
  • $\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

1
$\begingroup$
deriv[f_[x_]] := 
 Inactivate[
  ToExpression@StringJoin[ToString[f], ToString[x]] = D[f[x], x], 
  Set | D]

Then:

deriv[kr[x]]

outputs

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

which displays as

kryx = D[kr[yx], yx]
$\endgroup$
2
$\begingroup$

Maybe this:

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

Then:

enter image description here

$\endgroup$

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.