# Simplifying the body of a pure function

After solving a differential equation I obtain a pure function which I wish to simplify.

I am able to simplify the corresponding expression

expr = Cos[x] Cot[g] / Sqrt[-Cos[a]^2 Cot[g]^2 + Sin[a]^2]


according to the relationship k == Sin[a] / Cos[g] using a replacement followed by simplification with assumptions:

expr /. a -> ArcSin[k Cos[g]] // FullSimplify[#, 0 < g < Pi/2] &
(* Cos[x] / Sqrt[-1 + k^2] *) (* hooray *)


However if I attempt the same thing with the pure function (which is the output of some DSolve command)

fun = Function[{x}, Cos[x] Cot[g] / Sqrt[-Cos[a]^2 Cot[g]^2 + Sin[a]^2]]


it doesn't work:

fun  /. a -> ArcSin[k Cos[g]] // FullSimplify[#, 0 < g < Pi/2] &
(* Function[{x}, Cos[x] Cot[g] / Sqrt[-Cos[ArcSin[k Cos[g]]]^2 Cot[g]^2 + Sin[ArcSin[k Cos[g]]]^2]] *)


How do I get the body of the pure function to simplify in the same way as it does with the expression? (i.e. desired output is Function[{x}, Cos[x] / Sqrt[-1 + k^2]] )

## 3 Answers

fun = Function[{x}, Cos[x] Cot[g]/Sqrt[-Cos[a]^2 Cot[g]^2 + Sin[a]^2]];

fun[] =
FullSimplify[fun[] /. a -> ArcSin[k Cos[g]], 0 < g < Pi/2];

fun simp = FullSimplify[#, 0 < g < Pi/2] &;
rule = a -> ArcSin[k Cos[g]];
(*Method 1*)
Function[x, #] &@simp[fun@x /. rule]
(*Method 2*)
Replace[fun /. rule, expr_ :> RuleCondition@simp@expr, {1}]
(*Method 3*)
fun /. f : Function :> Hold@f /. rule // simp // ReleaseHold
(*Method 4*)
simp[fun /. Function -> func /. rule] /. func -> Function


Try this:

fun /. {
Verbatim[Function][args_,expr_]:>
Function[args, Evaluate@FullSimplify[expr/. a -> ArcSin[k Cos[g]], 0 < g < Pi/2 ]]
}


Of course you can do this with any command you want to apply to the pure function.

• I think the body FullSimplify[...] needs // Evaluate; otherwise good. – lastresort Jul 6 '17 at 12:45