replace function name with expression
I think the problem you try to solve is ill posed: if you want this to make sense you need to at least define some restrictions on what arguments the function f as it appears in the expression may have. What would you want the following to evaluate to?
MyFunction[f[x]-f[1,y]+f[z], f, Sin[x]]
The question becomes ill posed and hard to answer because you are intermixing expressions and functions in a way that makes things unnecessarily complicated. From your comments I understand that you can not aribtrarily change the calling syntax, but I would strongly suggest to discuss this with your collaborators, as a change will make all your lives a lot easier.
The following will do what I think you want (at least it succeeds for your example :-) with the restriction that the function f can only appear with one argument in expr and that that argument must be unique within expr (there is no checks whether that condition is fulfilled, though):
replaceFunctionWithExpression[expr_, f_, g_] := Module[{arg},
arg = Cases[
expr, (f[args___] |
Derivative[___][f][args___]) :> {args}, {0, Infinity},
Heads -> True][[1, 1]];
expr /. f -> (Function[g] /. arg -> #)
]
The whole task becomes somewhat involved because we need to find what the argument of the function f in expr is so we can find that within the expression g to generate the pure function with which to replace f. A bullet-proof version of this might use the HoldAll attribute for replaceFunctionWithExpression and implement some localization of at least the function argument, but I left that out. I would also like to emphazise avoiding this doesn't only make the code somewhat simpler but also demonstrates that Hold attributes are not necessary to get this to work for the derivatives example. With the HoldAll attribute you could get rid of the extra pattern for Derivatives, though, but that's only the part that searches for the arguments of f, the actual replacement works on the evaluated Derivative expressions just fine!
replace named function with function
You should note that with only a tiny change in how to call the function the task becomes essentially trivial: if we replace a named function with a (named or pure) function and not with an expression, the function you are looking for is actually too trival to justify any extra name for it:
replaceFunctionWithFunction[expr_, f_, g_] := expr /. f -> g
your examples would look like that for this:
replaceFunctionWithFunction[f[x] - 3, f, Sin]
replaceFunctionWithFunction[D[f[x], x], f, # &]
replaceFunctionWithFunction[f[x], f, 19 &]
Please note that this will do the (presumably) correct thing even with arbitrary arguments and number of arguments:
replaceFunctionWithFunction[D[f[a, 5], a] + f[a, b], f, #1*#2 &]
Things like the second and third of your examples would be formulated with pure functions (search for Function in the docs). The second could also be done with using Identity instead of #&. If you don't like the shortcut variant of Function you could also use the variant with named arguments, e.g. Function[x,x] instead of #&. Since Version 8 Mathematica also has the \[Function] special character which will provide a nice "mathematical" function notation, if you like that (e.g. x \[Function] x).
replace expression with expression
Finally I would like to mention that it is also possible to achieve the same thing in a relatively simple way if you replace expressions with expressions, as here:
replaceExpressionWithExpression[expr_, f_[arg_], g_] := expr /. f -> (Function[g] /. arg -> #)
which then would be called like this:
replaceExpressionWithExpression[f[x] - 3, f[x], Sin[x]]
replaceExpressionWithExpression[D[f[x], x], f[x], x]
replaceExpressionWithExpression[f[x], f[x], 19]
but this probably not as general (and elegant) as the "replace function with function" approach.
f[x]-3 /. f-> Sinor more generallyf[x]-3/. f-> Function[x,Sin[x]//Evaluate]?D[f[x],x]/. f-> #&– chris Nov 27 '12 at 12:17Degreeis not a function, but a constant likePi, so it's not a valid argument for MyFunction as you have specified it. You could definetoRadians[x_] := x Degreeand use that asg. – m_goldberg Nov 27 '12 at 14:01Sin[x]norxcan reliably be construed as "functions." Shouldn't the first example returnSin[x][x]-3? If that seems silly, supposeSinwere a function that returns a function, as insin[x_]:=Function[{y},y+x]. Now bothsin[x][x]andsin[x]make sense, but only the former conforms to the problem statement in the first line (and evaluates tox+x, by the way). – whuber Nov 27 '12 at 21:52