4
$\begingroup$

For example, I have gotten the value for a certain function H[t,r]:

$H[t,r] \to -\frac{2r(K[t,r]+K^{(0,1)}[t,r]+2m+r+...)}{(2m-r)}$

I want to replace its value, using "rules" in another function that depends on the values ​​of H[t,r] and some first or second derivatives on t and r. For exemple:

$F=2m+H[t,r]+H^{(0,2)}[t,r]+H^{(1,1)}[t,r]+...$

When I just use /. ruleH Mathematica replaces only the values ​​of H[t,r] and does not change the derivatives. Is there a command that does this?

$\endgroup$
0

3 Answers 3

6
$\begingroup$

To obtain H -> Function[{t, r}, G[t, r]]form, use the following:

# /. HoldPattern[y_[x__] -> bodyfunc_] :> 
y -> Function[{x}, bodyfunc] & /@ {yourRule}

And then:

F /. H -> Function[{t, r}, G[t, r]]

Here, $G(t,r)$ is the expression on the right-hand side of your rule:

$$-\frac{2r(K[t,r]+K^{(0,1)}[t,r]+2m+r+...)}{(2m-r)} $$

An example:

D[x^3, {x, #}] & /@ Range[3]
(*{3 x^2, 6 x, 6}*)
# /. HoldPattern[y_[x__] -> bodyfunc_] :> 
y -> Function[{x}, bodyfunc] & /@ {y[x] -> x^3}
(* y -> Function[{x}, x^3]*)
{y'[x], y''[x], y''[x]} /. y -> Function[{x}, x^3]
(*{3 x^2, 6 x, 6}*)

Update: you can use the Resource Function SolutionRulesToFunctions.

enter image description here

$\endgroup$
3
$\begingroup$

The short form of your functions since you don't give the whole expression

F = 2*m + H[t, r] + D[H[t, r], r, r] + D[H[t, r], t, r]

set the rule to include derivatives:

ruleH1 = H -> Function[{t, r}, -((2*r*(K[t, r] + D[K[t, r], r] + 2*m + r))/(2*m - r))]

then just

F /. ruleH1 // FullSimplify

or if you like pure functions

ruleH2 = H -> (-((2*#2*(K[#1, #2] + D[K[#1, #2], #2] + 2*m + #2))/(2*m - #2)) &)

F /. ruleH2 // FullSimplify

Both give the same long expressions. Either way, to get your substitution to include derivatives, the substitution must be a function.

$\endgroup$
2
$\begingroup$

Rather than answering directly, let me try to you give some background info that will hopefully help you understand this yourself. Suppose you enter an expression such as f[t]+f'[t]. Mathematica actually interprets this input as the longer expression

Plus[f[t],Derivative[1][f][t]]

To see this, just type FullForm[f[t]+f'[t]] and it will show you the complete expression that is used internally. Mathematica does not show the FullForm by default since that would sometimes lead to awfully long expressions, but when you apply a rule to something and you cannot understand what is going on, you may have to look at the FullForm.

Examples:

  • If you enter f[t] + f'[t] /. (f[x_] :> x^2) then f[t] is a match but no part of Derivative[1][f][t] is a match, and you get t^2 + f'[t].

  • On the other hand, if you replace f[t] + f'[t] /. (f -> (#^2&)) then both occurrences of f match the left hand side of the rule, and you get t^2 + 2*t.

See here for more information.

$\endgroup$

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.