# 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? – Carl Woll Jan 31 '19 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)? – Patrick.B Feb 1 '19 at 14:52

## 2 Answers

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? – Patrick.B Feb 1 '19 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. – Anton.Sakovich Feb 1 '19 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 (->). – Sjoerd Smit Feb 1 '19 at 9:52
• I see! It really was a simple answer. Thank you very much! – Patrick.B Feb 1 '19 at 14:49