Here is my extremly basic understanding of Replace and ReplaceAll.
This post is also a way for me to check if I understood the mechanism behind, if you see mistakes in my explanation don't hesitate to correct me !
Replace is a function that will apply replacement rules on part of expression.
However, it will apply the replacement rules at specific level given in parameters (by default it will be {0} corresponding to the whole tree).
So here :
Replace[x^2 + 1, x^2 -> a]
It doesn't do anything as x^2 is a subpart of the tree but it is not the whole tree in itself.
I could do :
Replace[x^2 + 1, x^2 -> a, All]
To make it work. Then the code will look at all the levels of the trees (thus all the subtrees) and look for a matching replacement.
I could also do :
ReplaceAll[x^2 + 1, x^2 -> a]
And here is my question : is there actually any difference between using
Replace[expr, rule, All]
and
ReplaceAll[expr,rule]
or it is indeed the same thing ?
Another question linked to the answer
Then I don't understand this behavior :
diffReplaceReplaceAll = g[g[x]];
Replace[diffReplaceReplaceAll, g -> c, All]
g[g[x]]
g[g[x]][[1]]
g[x]
ReplaceAll[diffReplaceReplaceAll, g -> c]
c[c[x]]
If I take strictly what you say, ReplaceAll shoud return c[g[x]]
Indeed, it goes from the outside which is g[g[x]] (the whole tree), it looks at each part. So first it tries with the Head (the 0 part), which is $g$, it replaces it by $c$. And... it should stop here right ? Thus we would have c[g[x]] as a result. But it continues and replaces the second g. Why ?
My problem is very probably linked to a not fully understanding of what a part precisely is. But if I'm not wrong the 0'th part is the head and the 1st part is g[x] here right ?
I also have a problem with Replace : why if it goes from the inside to the outside I don't have at least g[c[x]] ?
Remark : I don't fully get your example as I'm not familiar with ":>", I am reading about it now.
I'm sorry but I still have problem to understand, here is the code I consider :
expr = g[g[x]];
ReplaceAll[expr, g -> fonction]
ReplaceAll[expr, g[x_] -> fonction[x]]
fonction[fonction[x]]
fonction[g[x]]
I will write what I understand step by step of what is happening : the first ReplaceAll : ReplaceAll[expr, g -> fonction]
ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part or on any of its subparts.
Allright, I will look at each part of expr.
- I look at the 0'th part of expr which is the Head : g. I have a matching pattern, g becomes fonction. I don't have to look at subparts of g as I had a matching (but even in this case, g doesn't have subparts so it wouldn't change anything).
- I go at the next part (1st part) of expr : fonction[g[x]][[1]] = g[x] it doesn't match.
- I go at the first subpart of g[x] : g[x][[0]] = g, it matches. I replace. I don't have to look at the subparts of g as it matched. At this point I thus have fonction[fonction[x]]
- I look at the 2ndt part of expr : fonction[fonction[x]][[2]] : it doesn't exist.
Thus the code stops and the function returns : fonction[fonction[x]]
Now for the second ReplaceAll : ReplaceAll[expr, g[x_] -> fonction[x]]
- I look at the 0'th part of expr : g[g[x]][[0]] = g : no matching
- I look at the subparts of g : it doesn't have, so it stops for that.
- Thus, now I look at the 1st part of expr : g[g[x]][[1]] = g[x] It matches : g[x] -> fonction[x]. I thus now have g[fonction[x]]... which I know is wrong.
Here is how I understood the algorithm and I still have a problem. I don't understand what precisely does the algorithm then.
And is the 0'th part really the head or the full expression ? Because in the documentation of Replace for example :
The default value for levelspec in Replace is {0}, corresponding to the whole expression.
ReplaceAll[{f,f},f->g]. Clearly, bothfshould be replaced, so both parts 1 and 2 of{f,f}. Now considerReplaceAll[f[f,f],f->g]- this is exactly the same, only that now parts 0,1 and 2 off[f,f]need to be replaced. – Lukas Lang Dec 30 '18 at 19:450has nothing to do with part0: Mathematica distinguishes between part and level specifications. E.g. inf[g[x],y], part 0 isf, 1 isg[x], 2 isy,{1,0}isgand{1,1}isx. Level 0 on the other hand is the whole expressionf[g[x],y], level 1 is the list of expressions that are parts of level 0, so{f,g[x],y}(assuming heads are included). Level 2 is similarly given by{g,x}. See alsoLevel[f[g[x],y],{1},Heads->True]– Lukas Lang Dec 30 '18 at 22:43ReplaceAllVerbose/ReplaceVerbosefunctions above for other examples, to see in what order parts are visited and what happens to them. They do not support list of rules, but otherwise they should be fully equivalent to the built-in versions in terms of results and functionality. – Lukas Lang Dec 30 '18 at 22:57