why does the function replacement not work here?
b[1, x_] := x^2;
a'[x] /. a -> Function[x, b[1, x]]
gives $b^{(0,1)}(1,x)$ without evaluating further.
I would like the output to be $2x$
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communitywhy does the function replacement not work here?
b[1, x_] := x^2;
a'[x] /. a -> Function[x, b[1, x]]
gives $b^{(0,1)}(1,x)$ without evaluating further.
I would like the output to be $2x$
Mathematica can't (won't) calculate the partial derivative of the function b
, if it does not know what it does for general values of all inputs.
If the other input is not meant to be a variable (but say a parameter in the function), the following definition works:
b[1][x_] := x^2;
a'[x] /. a -> Function[x, b[1][x]]
b[1, x_] := x^2;
a'[x] /. a -> Function[x, Evaluate @ b[1, x]]
2 x
Attributes[Function]
{HoldAll, Protected}
That is, "all arguments to Function
are to be maintained in an unevaluated form".
Wrapping body
in Function[arg, body]
with Evaluate
causes body
"to be evaluated even if it appears as the argument of a function whose attributes specify that it should be held unevaluated."
Use Trace
to see why we get $b^{(0,1)}(1,x)$ when we don't force evaluation of b[1, x]
:
a'[x] /. a -> Function[x, b[1, x]] // Trace // Column