# How to make functional rules not depend on an argument?

Consider the following:

rule = f[x] -> Sin[x]


if I now do, say:

f[y] /. rule


this does not work, because the rule wants the argument to specifically be x. Of course, I could do:

f[x] /. rule /. x -> y


but I really would be able to "clean" the rule of the input. As an example, If we use NDSolve to solve an ODE, we obtain a rule of the form:

{f -> InterpolatingFunction["stuff"]}


this is nice insofar that I can apply this rule to f, where the argument can be anything I wish. So, the question is, If I ALREADY have a rule as above, is there a nice way to "remove" the "[x]" part of all the functions, in the sense described above?

• Why not use rule = f -> Sin? Jan 31, 2019 at 20:33
• The reason is that the rule I have is obtained by solving an algebraic equation for a variable f(x). The output of Solve gives you a rule, which then has the explicit argument input, unfortunately. That is why I specified how to change the rule in the desired way. Except if we can force Solve to behave like NDSolve, insofar that it "forgets" about the argument (both for the LHS and RHS of the rule)? Feb 1, 2019 at 14:52

While John Doty's answer works pretty well, if you want to obtain a pure function as rhs of your rule like in your InterpolatingFunction example, you can do something like

rule = f[x] -> Sin[x];
newrule = f -> Block[
{x},
Function @@ List[{x}, f[x] /. rule]
]
(*f -> Function[{x}, Sin[x]]*)

• Quite interesting. I am new to Mathematica and I have never encountered the use of @@ before. What is the use of it? Feb 1, 2019 at 14:56
• f @@ expr (spaces are optional) is a shortcut for Apply[f, expr], which changes the head of expr to f. Consult Apply in the Documentation Center for details. Feb 1, 2019 at 15:38

It's just like defining a function. Make x the name of a pattern, not a literal symbol:

rule = f[x_] -> Sin[x]
f[y] /. rule
(* Sin[y] *)

• If you do this, you should generally use RuleDelayed (:>) instead of Rule (->). Feb 1, 2019 at 9:52
• I see! It really was a simple answer. Thank you very much! Feb 1, 2019 at 14:49