Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an equation that I evaluate at some point (let's say $x=1$) that have terms of the form

f[1,y] D[g[1,y],{y,2}]

Is there an easy way to replace [1,y] by [x,y] with a simple replacement rule? The thing is that

g[1,y] /. g[1,y] -> g[x,y]

will indeed replace g, but it won't work for any of its derivatives, and there are far too many functions and derivatives for me to make a replacement rule for all of them.

Would there be a clever way to do this? So far, my only solution has been copy-pasting my equations in Word and using the text replacement tool in there. It would be neat not having to go outside of Mathematica to do this though.

share|improve this question
I think you cannot replace it in arguments but you can replace it in Module before you start to work on it. You can check conditions or assign default values in function arguments.This example may be relevant f[k_] = Module[{s = k /. k -> 3}, s]; called as f[2] return 3. – Rorschach Sep 11 '13 at 18:04
up vote 6 down vote accepted

Taking the question as "how to replace the first argument in $g(a,b)$ as well as all of its derivatives", you can do this:

Check the InputForm of the derivative:

D[g[1, y], {y, 2}] // InputForm

(* ==> Derivative[0, 2][g][1, y] *)

Add a corresponding pattern to the replacement rules:

f[1, y] D[g[1, y], {y, 2}] /. 
    {g[1, y] -> g[x, y], Derivative[d___][g][1, y] :> Derivative[d][g][x, y]}
share|improve this answer
The latter Rule can be changed to RuleDelayed to localize d. I can't edit this because change is less then 6 characters :) – ybeltukov Sep 11 '13 at 18:13
@ybeltukov Thanks, done :) – Szabolcs Sep 11 '13 at 18:23
@ybeltukov You're a quick learner I see. ;-) – Mr.Wizard Sep 11 '13 at 18:26
That helps, thanks! Would there be a way to quickly adapt this replacement rule for multiple functions f, g, h, etc. without having to specify the functions themselves - say, if I wanted to do this for 100 different functions? I've tried pure functions but to no avail. – physguy Sep 11 '13 at 19:34
@physguy You need to use a pattern for the function name (g) as well. – Szabolcs Sep 11 '13 at 19:38

A more general approach that replaces argument lists [1, y] anywhere (note, that this might result in unwanted replacements compared to Szabolcs's solution):

(f[1, y] D[g[1, y], {y, 2}]) /. h_[1, y] :> h[x, y]    

(* ==>  f[x, y]*Derivative[0, 2][g][x, y] *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.