I have an expression of the form:
expr=x+y+n+Sin[n]+n^2
I would like to replace each of the variables n
with a different variable, like:
x+y+n1+Sin[n2]+n3^2
The order doesn't matter, but it matters that each n
have a different name.
I can find the positions of the n
's simply with:
pos=Position[expr,n]
and this returns a list of positions {{1}, {2, 1}, {5, 1}}
as expected. I would then like to run through this list and make replacements in those positions. However, I don't want to have to do this by hand. Clearly I could do:
expr[[5,1]]=n3
but the real expression I have is many thousands of times larger.
I have tried using:
Apply[Part, Join[{expr}, pos[[3]]]]=n2
which would allow be to do this in a loop, or functionally, but this doesn't alter the element of the expression as I would like. I have tried playing with Hold
in various places but it doesn't seem to work.
Anything simple that I am missing?
expr /. n :> Unique@n
$\endgroup$