Mathematica Asked by Sejwal on March 17, 2021
Result I get for the following replacement doesn’t seem to be correct.
{a*b*c*d*e} /. {d -> d*f, a -> a*b*c, b*c -> b*c*d*e, c*f -> c*f*g}
=> {a b c d^2 e^2}
It didn’t include d -> d*f
& c*f -> c*f*g
.
What am I missing?
In your original expression there are no c*f so that rule does not get applied.
However if you use ReplaceAll
it will create, in this case, an infinite recursive loop.
You can apply your rules sequentially by:
{a*b*c*d*e} /. d -> d*f /. a -> a*b*c /. b*c -> b*c*d*e /. c*f -> c*f*g
=> {a b^2 c^2 d e f}
Answered by Rudy Potter on March 17, 2021
To elaborate a bit on Rudy Potter's answer: Well one should keep in mind at least two things about multiplication in Mathematica using Times
:
Power
which is a complete nightmare when trying to replace parts of products.Lets follow Daniel Hubers advice from the comments and apply the rules in sequence using Fold
:
Fold[ReplaceAll, {a*b*c*d*e}, {d -> d*f, a -> a*b*c, b*c -> b*c*d*e, c*f -> c*f*g}]
resulting in
{a b^2 c^2 d e f}
Which is maybe not the result one would expect. Only the first two rules have an impact on the expression, since the third b*c -> b*c*d*e
does not work on b^2 c^2
and the last will most likely never work on a product involving e
and d
since c
and f
will never be adjacent. The potential looping problems might come in when using ReplaceRepeated
. A further question is what even to do with b^2 c^2
: use apply the rule to b*c*b*c
twice? If twice to the first b*c
or the second or one produced by the rule (here the looping would come into play if one where to use ReplaceRepeated
).
The question/stated problem looks not very well posed to me and maybe using solve with a corresponding equation system to manipulate the expression might be a better idea. Switching (if only temporary) from Times
to List
to manipulate the sequence of terms in the product which would guarantee a predicable order of terms could also be an idea:
resulting in
I would urge OP to rethink the problem/its formulation.
Answered by N0va on March 17, 2021
Your question concerns the subtle pattern matching that occurs for Flat
symbols, like Times
. When the replacement rule b*c -> b*c*d*e
is encountered, any expression with head Times
is examined to see if the elements b
and c
are present, and if so, the whole Times
expression is modified accordingly. Since ReplaceAll
works top down, the rule with Times
is used before the rules d -> d * f
or a -> a * b * c
. Once a portion of the expression is modified by a ReplaceAll
replacement, that portion of the expression is inert to further modification by any of the replacement rules. So, the first rule that fires is the the b*c -> b*c*d*e
rule acting on the complete Times
expression, and then no other rules are used, leading to the output that you observe.
Answered by Carl Woll on March 17, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP