Is there a difference between Replace with parameter “All” and ReplaceAll
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.
replacement
add a comment |
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.
replacement
add a comment |
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.
replacement
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.
replacement
replacement
edited 1 hour ago
asked 3 hours ago
StarBucK
661211
661211
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
No, they're not the same thing
From the documentation of Replace
(4th to last point):
If
levelspec
includes multiple levels, expressions at deeper levels in a given subexpression are matched first.
From the documentation of ReplaceAll
(emphasis mine, 1st point):
ReplaceAll
looks at each part ofexpr
, tries all therules
on it, and then goes on to the next part ofexpr
. 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.
This means that ReplaceAll
goes from the outside in, while Replace
goes from the inside out. You can see this with the following example:
ReplaceAll[f[f[x]], f[x_] :> g[x]]
(* g[f[x]] *)
Replace[f[f[x]], f[x_] :> g[x], All]
(* g[g[x]] *)
Since f[f[x]]
matches the rule, ReplaceAll
does not consider f[x]
(because it is a subpart of something that has already been matched by a rule). Replace[…,All]
on the other hand happily replaces both occurrences, the inner one being first:
Replace[f[f[x]], f[x_] :> Echo@g[x], All]
(* >> g[x] *)
(* >> g[g[x]] *)
(* g[g[x]] *)
The second example
Looking at the second example from the updated question:
ReplaceAll[f[f[x]], f -> g]
(* g[g[x]] *)
Replace[f[f[x]], f -> g, All]
(* f[f[x]] *)
There are two things going on here:
Why does Replace
not replace anything?
This is another difference between Replace
and ReplaceAll
: Replace
has an option Heads
, which by default is False
. It controls whether the heads of expressions can be replaced. Specifying Heads->True
gives the desired result:
Replace[f[f[x]], f -> g, All, Heads -> True]
(* g[g[x]] *)
Why does ReplaceAll
suddenly replace both occurrences of f
with g
?
This is because the left side of the rule f->g
only matches f
, i.e. the head (or 0th part) of f[f[x]]
. According to the documentation "no further rules are tried on that part or on any of its subparts". And indeed, the f
(now g
) or any of its subparts (of which there are none) are not touched by any other rule. We can see this in more detail with the following two examples:
ReplaceAll[f[f[x]], {f -> g, g -> h}]
(* g[g[x]] *)
ReplaceAll[f[f[x]][x, y], {h_[x_] :> {h, x}}]
(* {f, f[x]}[x, y] *)
The first example demonstrates the first part: Even though g->h
would match the newly added g
, the rule is not applied, since that part has already been touched.
The second example demonstrates that after the head f[f[x]]
has been replaced, its subparts f
and f[x]
are not considered for further replacements.
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
1
Does the update answer your questions?
– Lukas Lang
1 hour ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
Yes - you can also considerReplaceAll[{f,f},f->g]
. Clearly, bothf
should 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
42 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188605%2fis-there-a-difference-between-replace-with-parameter-all-and-replaceall%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, they're not the same thing
From the documentation of Replace
(4th to last point):
If
levelspec
includes multiple levels, expressions at deeper levels in a given subexpression are matched first.
From the documentation of ReplaceAll
(emphasis mine, 1st point):
ReplaceAll
looks at each part ofexpr
, tries all therules
on it, and then goes on to the next part ofexpr
. 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.
This means that ReplaceAll
goes from the outside in, while Replace
goes from the inside out. You can see this with the following example:
ReplaceAll[f[f[x]], f[x_] :> g[x]]
(* g[f[x]] *)
Replace[f[f[x]], f[x_] :> g[x], All]
(* g[g[x]] *)
Since f[f[x]]
matches the rule, ReplaceAll
does not consider f[x]
(because it is a subpart of something that has already been matched by a rule). Replace[…,All]
on the other hand happily replaces both occurrences, the inner one being first:
Replace[f[f[x]], f[x_] :> Echo@g[x], All]
(* >> g[x] *)
(* >> g[g[x]] *)
(* g[g[x]] *)
The second example
Looking at the second example from the updated question:
ReplaceAll[f[f[x]], f -> g]
(* g[g[x]] *)
Replace[f[f[x]], f -> g, All]
(* f[f[x]] *)
There are two things going on here:
Why does Replace
not replace anything?
This is another difference between Replace
and ReplaceAll
: Replace
has an option Heads
, which by default is False
. It controls whether the heads of expressions can be replaced. Specifying Heads->True
gives the desired result:
Replace[f[f[x]], f -> g, All, Heads -> True]
(* g[g[x]] *)
Why does ReplaceAll
suddenly replace both occurrences of f
with g
?
This is because the left side of the rule f->g
only matches f
, i.e. the head (or 0th part) of f[f[x]]
. According to the documentation "no further rules are tried on that part or on any of its subparts". And indeed, the f
(now g
) or any of its subparts (of which there are none) are not touched by any other rule. We can see this in more detail with the following two examples:
ReplaceAll[f[f[x]], {f -> g, g -> h}]
(* g[g[x]] *)
ReplaceAll[f[f[x]][x, y], {h_[x_] :> {h, x}}]
(* {f, f[x]}[x, y] *)
The first example demonstrates the first part: Even though g->h
would match the newly added g
, the rule is not applied, since that part has already been touched.
The second example demonstrates that after the head f[f[x]]
has been replaced, its subparts f
and f[x]
are not considered for further replacements.
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
1
Does the update answer your questions?
– Lukas Lang
1 hour ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
Yes - you can also considerReplaceAll[{f,f},f->g]
. Clearly, bothf
should 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
42 mins ago
add a comment |
No, they're not the same thing
From the documentation of Replace
(4th to last point):
If
levelspec
includes multiple levels, expressions at deeper levels in a given subexpression are matched first.
From the documentation of ReplaceAll
(emphasis mine, 1st point):
ReplaceAll
looks at each part ofexpr
, tries all therules
on it, and then goes on to the next part ofexpr
. 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.
This means that ReplaceAll
goes from the outside in, while Replace
goes from the inside out. You can see this with the following example:
ReplaceAll[f[f[x]], f[x_] :> g[x]]
(* g[f[x]] *)
Replace[f[f[x]], f[x_] :> g[x], All]
(* g[g[x]] *)
Since f[f[x]]
matches the rule, ReplaceAll
does not consider f[x]
(because it is a subpart of something that has already been matched by a rule). Replace[…,All]
on the other hand happily replaces both occurrences, the inner one being first:
Replace[f[f[x]], f[x_] :> Echo@g[x], All]
(* >> g[x] *)
(* >> g[g[x]] *)
(* g[g[x]] *)
The second example
Looking at the second example from the updated question:
ReplaceAll[f[f[x]], f -> g]
(* g[g[x]] *)
Replace[f[f[x]], f -> g, All]
(* f[f[x]] *)
There are two things going on here:
Why does Replace
not replace anything?
This is another difference between Replace
and ReplaceAll
: Replace
has an option Heads
, which by default is False
. It controls whether the heads of expressions can be replaced. Specifying Heads->True
gives the desired result:
Replace[f[f[x]], f -> g, All, Heads -> True]
(* g[g[x]] *)
Why does ReplaceAll
suddenly replace both occurrences of f
with g
?
This is because the left side of the rule f->g
only matches f
, i.e. the head (or 0th part) of f[f[x]]
. According to the documentation "no further rules are tried on that part or on any of its subparts". And indeed, the f
(now g
) or any of its subparts (of which there are none) are not touched by any other rule. We can see this in more detail with the following two examples:
ReplaceAll[f[f[x]], {f -> g, g -> h}]
(* g[g[x]] *)
ReplaceAll[f[f[x]][x, y], {h_[x_] :> {h, x}}]
(* {f, f[x]}[x, y] *)
The first example demonstrates the first part: Even though g->h
would match the newly added g
, the rule is not applied, since that part has already been touched.
The second example demonstrates that after the head f[f[x]]
has been replaced, its subparts f
and f[x]
are not considered for further replacements.
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
1
Does the update answer your questions?
– Lukas Lang
1 hour ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
Yes - you can also considerReplaceAll[{f,f},f->g]
. Clearly, bothf
should 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
42 mins ago
add a comment |
No, they're not the same thing
From the documentation of Replace
(4th to last point):
If
levelspec
includes multiple levels, expressions at deeper levels in a given subexpression are matched first.
From the documentation of ReplaceAll
(emphasis mine, 1st point):
ReplaceAll
looks at each part ofexpr
, tries all therules
on it, and then goes on to the next part ofexpr
. 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.
This means that ReplaceAll
goes from the outside in, while Replace
goes from the inside out. You can see this with the following example:
ReplaceAll[f[f[x]], f[x_] :> g[x]]
(* g[f[x]] *)
Replace[f[f[x]], f[x_] :> g[x], All]
(* g[g[x]] *)
Since f[f[x]]
matches the rule, ReplaceAll
does not consider f[x]
(because it is a subpart of something that has already been matched by a rule). Replace[…,All]
on the other hand happily replaces both occurrences, the inner one being first:
Replace[f[f[x]], f[x_] :> Echo@g[x], All]
(* >> g[x] *)
(* >> g[g[x]] *)
(* g[g[x]] *)
The second example
Looking at the second example from the updated question:
ReplaceAll[f[f[x]], f -> g]
(* g[g[x]] *)
Replace[f[f[x]], f -> g, All]
(* f[f[x]] *)
There are two things going on here:
Why does Replace
not replace anything?
This is another difference between Replace
and ReplaceAll
: Replace
has an option Heads
, which by default is False
. It controls whether the heads of expressions can be replaced. Specifying Heads->True
gives the desired result:
Replace[f[f[x]], f -> g, All, Heads -> True]
(* g[g[x]] *)
Why does ReplaceAll
suddenly replace both occurrences of f
with g
?
This is because the left side of the rule f->g
only matches f
, i.e. the head (or 0th part) of f[f[x]]
. According to the documentation "no further rules are tried on that part or on any of its subparts". And indeed, the f
(now g
) or any of its subparts (of which there are none) are not touched by any other rule. We can see this in more detail with the following two examples:
ReplaceAll[f[f[x]], {f -> g, g -> h}]
(* g[g[x]] *)
ReplaceAll[f[f[x]][x, y], {h_[x_] :> {h, x}}]
(* {f, f[x]}[x, y] *)
The first example demonstrates the first part: Even though g->h
would match the newly added g
, the rule is not applied, since that part has already been touched.
The second example demonstrates that after the head f[f[x]]
has been replaced, its subparts f
and f[x]
are not considered for further replacements.
No, they're not the same thing
From the documentation of Replace
(4th to last point):
If
levelspec
includes multiple levels, expressions at deeper levels in a given subexpression are matched first.
From the documentation of ReplaceAll
(emphasis mine, 1st point):
ReplaceAll
looks at each part ofexpr
, tries all therules
on it, and then goes on to the next part ofexpr
. 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.
This means that ReplaceAll
goes from the outside in, while Replace
goes from the inside out. You can see this with the following example:
ReplaceAll[f[f[x]], f[x_] :> g[x]]
(* g[f[x]] *)
Replace[f[f[x]], f[x_] :> g[x], All]
(* g[g[x]] *)
Since f[f[x]]
matches the rule, ReplaceAll
does not consider f[x]
(because it is a subpart of something that has already been matched by a rule). Replace[…,All]
on the other hand happily replaces both occurrences, the inner one being first:
Replace[f[f[x]], f[x_] :> Echo@g[x], All]
(* >> g[x] *)
(* >> g[g[x]] *)
(* g[g[x]] *)
The second example
Looking at the second example from the updated question:
ReplaceAll[f[f[x]], f -> g]
(* g[g[x]] *)
Replace[f[f[x]], f -> g, All]
(* f[f[x]] *)
There are two things going on here:
Why does Replace
not replace anything?
This is another difference between Replace
and ReplaceAll
: Replace
has an option Heads
, which by default is False
. It controls whether the heads of expressions can be replaced. Specifying Heads->True
gives the desired result:
Replace[f[f[x]], f -> g, All, Heads -> True]
(* g[g[x]] *)
Why does ReplaceAll
suddenly replace both occurrences of f
with g
?
This is because the left side of the rule f->g
only matches f
, i.e. the head (or 0th part) of f[f[x]]
. According to the documentation "no further rules are tried on that part or on any of its subparts". And indeed, the f
(now g
) or any of its subparts (of which there are none) are not touched by any other rule. We can see this in more detail with the following two examples:
ReplaceAll[f[f[x]], {f -> g, g -> h}]
(* g[g[x]] *)
ReplaceAll[f[f[x]][x, y], {h_[x_] :> {h, x}}]
(* {f, f[x]}[x, y] *)
The first example demonstrates the first part: Even though g->h
would match the newly added g
, the rule is not applied, since that part has already been touched.
The second example demonstrates that after the head f[f[x]]
has been replaced, its subparts f
and f[x]
are not considered for further replacements.
edited 1 hour ago
answered 3 hours ago
Lukas Lang
5,9581727
5,9581727
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
1
Does the update answer your questions?
– Lukas Lang
1 hour ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
Yes - you can also considerReplaceAll[{f,f},f->g]
. Clearly, bothf
should 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
42 mins ago
add a comment |
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
1
Does the update answer your questions?
– Lukas Lang
1 hour ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
Yes - you can also considerReplaceAll[{f,f},f->g]
. Clearly, bothf
should 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
42 mins ago
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally)
– StarBucK
1 hour ago
1
1
Does the update answer your questions?
– Lukas Lang
1 hour ago
Does the update answer your questions?
– Lukas Lang
1 hour ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ?
– StarBucK
52 mins ago
Yes - you can also consider
ReplaceAll[{f,f},f->g]
. Clearly, both f
should be replaced, so both parts 1 and 2 of {f,f}
. Now consider ReplaceAll[f[f,f],f->g]
- this is exactly the same, only that now parts 0,1 and 2 of f[f,f]
need to be replaced.– Lukas Lang
42 mins ago
Yes - you can also consider
ReplaceAll[{f,f},f->g]
. Clearly, both f
should be replaced, so both parts 1 and 2 of {f,f}
. Now consider ReplaceAll[f[f,f],f->g]
- this is exactly the same, only that now parts 0,1 and 2 of f[f,f]
need to be replaced.– Lukas Lang
42 mins ago
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188605%2fis-there-a-difference-between-replace-with-parameter-all-and-replaceall%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown