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.

  • $\begingroup$ 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. $\endgroup$
    – Sejwal
    Sep 11, 2013 at 18:04

2 Answers 2


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]}
  • $\begingroup$ The latter Rule can be changed to RuleDelayed to localize d. I can't edit this because change is less then 6 characters :) $\endgroup$
    – ybeltukov
    Sep 11, 2013 at 18:13
  • $\begingroup$ @ybeltukov Thanks, done :) $\endgroup$
    – Szabolcs
    Sep 11, 2013 at 18:23
  • $\begingroup$ @ybeltukov You're a quick learner I see. ;-) $\endgroup$
    – Mr.Wizard
    Sep 11, 2013 at 18:26
  • $\begingroup$ 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. $\endgroup$
    – physguy
    Sep 11, 2013 at 19:34
  • $\begingroup$ @physguy You need to use a pattern for the function name (g) as well. $\endgroup$
    – Szabolcs
    Sep 11, 2013 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] *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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