Say I have an expression (call it expr) involving a function, f[x]. I'd like to be able to evaluate that for a particular choice of f[x] without setting that choice for the whole session. I thought to do this using a replacement,
expr /. f[x_]->x^2
(where expr is some expression involving f[x] and I want to set f to x^2), but this doesn't work on derivatives, e.g., if expr contains f'[x] then it will stay as f'[x] rather than become 2x.
What's the best solution to this problem?
FullForm[f'[x]]to understand why, and figure out the appropriate replacement rule. – István Zachar Jan 10 '14 at 18:45fby a pure function if you want things like derivatives to work.f->Function[x, x^2]– Rojo Jan 11 '14 at 00:41If you or someone else wouldn't mind explaining, is there a reason to prefer either this solution or your Block solution?
– Adam Jan 11 '14 at 02:14Blocksolution is slightly more general, and is the general solution for what you explicitly asked for: "evaluate something for a particular choice of some symbol without it affecting the whole session" – Rojo May 30 '14 at 19:04