# How to simplify a pure function?

I am dealing with the vibration of a multi degree of freedom system with driving force, and I need to find the relation between the amplitude and the frequency of the driving force.

I hope to get an accurate solution, So I used DSolve to get the analytical solution, then I used /. to replace symbols with numeric values. However, I can't simplify the anwser.

Here is a simple example that can explain what I want to do:

How can I simplify

Function[{t}, t + 1 + 1]

to

Function[{t}, t + 2]

• Not that it's not a good question, but this smells like an XY problem. Have you tried DSolve[expr, y[t], t] instead of DSolve[expr, y, t] to get a non-pure function? If you're substituting the solution into an expression with derivatives of y (or other operators), then you're good, but otherwise it might be simpler to just get the image of t under y (i.e. y[t]) instead of the function itself. Not trying to dismiss your question, just trying to address the core of your issue as best as possible. Commented Oct 10, 2022 at 18:14
• If you use DSolve[expr, y[x], t] instead of DSolve[expr, y, t], you can use the resource function SolutionRulesToFunctions to obtain a pure function as follows: DSolve[expr, y[t], t]//Simplify//SolutionRulesToFunctions. Commented Oct 10, 2022 at 21:06

Similar, but different, to @Alan's answer:

f = Function[{t}, t + 1 + 1];
MapAt[Evaluate, f, {2}]


Another one:

f = Function[{t}, t + 1 + 1];
ReplacePart[f, 2 -> f[[2]]]

• As written, these leave f unchanged.
– Alan
Commented Oct 12, 2022 at 0:31
f = Function[{t}, t + 1 + 1]
f[[2]] = f[[2]]

• As written, this changes f. Commented Oct 12, 2022 at 8:01
• @AntonAntonov My comment was meant to help the OP. Just in case.
– Alan
Commented Oct 13, 2022 at 14:58
• Same here. Thanks! Commented Oct 14, 2022 at 0:18

We can also use Map (/@). Also, if t is polluted, we need Block etc.:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t}, Evaluate /@ f]


Alternatively:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t}, Function @@ List @@ f]


Or directly use Block to disable Function:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t, Function}, f]


Just for fun, an unnecessarily complicated but working solution:

f = Function[{t}, t + 1 + 1];
t = 111;
Block[{t}, Replace[f, a_ :> RuleCondition@a, {1}]]

• Rule for constructing the Block: Verbatim[Function][v_List, body_] :> Block[v, < your favorite fix >]. If Function[t,...], then give a list of rules covering the cases, I guess, including Function[Null,...] (+1) Commented Oct 10, 2022 at 16:15

Using DSolve[expr, y[t], t] instead DSolve[expr, y, t]:

solrules = First @@ DSolve[{y''[x] - x y[x] == 0, y[0] == 1, y[9] == 1}, y[x], x]


Using Simplify and the resource function SolutionRulesToFunctions:

solrules //Simplify //SolutionRulesToFunctions


Also similar to other answers (in-place modification):

f=Function[{t}, t + 1 + 1]

f[[2]]//=Evaluate;f

(* Function[{t}, 2 + t] *)


Alternatively:

f//=MapAt[Evaluate,2]


There is also Query

Query[{2->Evaluate}]@f


but 'under the hood' this appears to be identical to the answer given by Anton Antonov

Query[{2->Evaluate}]//Normal

(* MapAt[Evaluate, 2] *)


Using FullSimplifyFunction (by E. Chan-López, Carlos Francisco Arias Méndez & Ulrich Neumann):

FullSimplifyFunction = ResourceFunction["FullSimplifyFunction"];

FullSimplifyFunction@Function[{t}, t + 1 + 1]


Function[{t}, 2 + t]

• Nice find - haven't seen this before :)
– eldo
Commented Jul 7 at 22:17
• Thanks, @eldo! This function arose from a question from Ulrich Neumann in another post. Commented Jul 7 at 22:20