# Replacing functions and variables

Posted 7 months ago
907 Views
|
8 Replies
|
2 Total Likes
|
 Say I have $f(x,y) + \partial_x f(x,y)$ e.g. fun = f[x,y] + D[f[x,y],x] I would like to use the substitution $f(x,y) \rightarrow \delta^2g(\delta x, \delta y)$ to get a new function. I have tried various things such as fun /. {f[x, y] -> \[Delta]^2 g[\[Delta] x, \[Delta]y]} fun /. {f -> \[Delta]^2 g} fun /. {{f -> \[Delta]^2 g}, {x -> \[Delta] x}, {y -> \[Delta] y}} fun /. {{f[x, y] -> \[Delta]^2 g[\[Delta] x, \[Delta]y]}, {D[f[x, y], x] -> D[\[Delta]^2 g[\[Delta] x, \[Delta] y], x]}} none of which produce the desired output of $\delta^2g(\delta x, \delta y) + \delta^3\partial_xg(\delta x, \delta y)$. The last(4th) attempt seems to be the closest, but the output is a two element list, with each element having a correct and incorrect term.
8 Replies
Sort By:
Posted 7 months ago
 Use Function: fun /. f -> Function[{x, y}, \[Delta]^2 g[\[Delta] x, \[Delta] y]] 
Posted 7 months ago
 Thanks!
Posted 7 months ago

Unless I missed something there should be d^2 next to the first term, or?

## Here is what you can do:

fun /. f -> Function[{x, y}, d^2 g[ d x, d y]]


or, less verbose

fun /. f -> ( d^2 g[ d #, d #2]& )


## Why did your examples fail?

Nothing prevents D[f[x,y],x] from evaluation so it evaluates to Derivative[1, 0][f][x, y].

• This is why your first example fails, it does not much it.
• The second example does not work because you are replacing f, which is an operator with a product of \[Delta]^2 g where g was supposed to be an operator but how could MMA know it.
• Same story with the third example, additionaly, if you have a list of list of replacements you will get a list. You should provide a flat list of rules to get one result only.

• If you would've applied a flat list in the forth example, you'd get a correct result. But writing rules for each derivative is not something one would like to do. My solution avoids that because if you replace f in Derivative[...][f] with a function, Derivative will take care about applying the chain rule.

Posted 7 months ago
 Thanks! Very helpful. Yes, there should be a d^2. I've edited it now.
Posted 5 months ago
 On a related note, how would you substitute for a derivative? Say I have fun = D[f[x, y, t], x] + D[f[x, y, t], {x, 2}] + D[f[x, y, t], {x, 3}] and want to make the substitution $\partial_xf \rightarrow g$ so that fun goes from $$\partial_xf +\partial_{xx}f + \partial_{xxx}f$$ to $$g + \partial_xg + \partial_{xx}g.$$This doesn't work: fun /. D[f[x, y, t], x] -> (g[#, #2, #3] &) and fun /. f^(1,0,0)->(g[#,#2,#3]&) only replaces the one derivative.
Posted 5 months ago
 This will handle explicit derivatives: fun /. Derivative[j_, 0, 0][f][x, y, t] -> Derivative[j - 1, 0, 0][g][x, y, t] It won't take integrals though (should that be needed).
 Say I have fun = D[f[x,y],{y,2}] F = Subscript[f, 1][x,y] + \[Delta]^2 Subscript[f, 2][x,y] I want to replace f with F so I do fun /. f -> F which gives ((Subscript[f, 0][x,y]+\[Delta]^2 Subscript[f, 2][x,y]+\[Delta]^4 Subscript[f, 4][x,y])^(0,2))[x,y] Why doesn't the expansion derivative get evaluated? E.g. why is the output $\partial_y^2(f_0 + \delta^2f_2+\delta^4f_4)$ instead of $\partial_y^2f_0 + \delta^2\partial_y^2f_2 + \delta^4\partial_y^2f_4$?
 Subscript may format like Derivative. But it is an "inert" wrapper basically, conveying no mathematical content. How that plays out might be seen from looking at FullForm. In[81]:= FullForm[fun = D[f[x, y], {y, 2}]] (* Derivative[0, 2][f][x, y] *) In[82]:= FullForm[ F = Subscript[f, 1][x, y] + \[Delta]^2 Subscript[f, 2][x, y]] (* Plus[Subscript[f, 1][x, y], Times[Power[\[Delta], 2], Subscript[f, 2][x, y]]]*) In[83]:= FullForm[fun /. f -> F] (* Derivative[0, 2][ Plus[Subscript[f, 1][x, y], Times[Power[\[Delta], 2], Subscript[f, 2][x, y]]]][x,y] *)