Re: algebraic substitution rules
- To: mathgroup at smc.vnet.net
- Subject: [mg30747] Re: algebraic substitution rules
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 9 Sep 2001 03:26:42 -0400 (EDT)
- References: <9ncfq6$pt5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Your example below does not work for two reasons:
1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2,
a_->Expand[a]}
1 + x^2 + x^3 + x^4
1) because -> (Rule) is used instead of :>(RuleDelayed), Expand[a]
evaluates to a, giving
a ->a before the replacement is attempted.
A simple example is
x(1+x)/.a_->Expand[a]
x*(1 + x)
This is corrected by using :> which prevents the evaluation of the right
side before replacement is attempted (hence the name "RuleDelayed").
x(1+x)/.a_:>Expand[a]
x + x^2
However this still does not work on your example:
1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2,
a_:>Expand[a]}
1 + x^2 + x^3 + x^4
This is because
2) When the replacement is attempted it is first tried on the whole
expression 1+x^2+x^3+x^4. The rule a_:>Expand[a] applies and replaces it
with Expand[1+x^2+x^3+x^4], which gives 1+x^2+x^3+x^4. The process then
terminates - it does not look inside the result of a replacement
We can use a two-step replacement
1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2}/.
a_:>Expand[a]
3 + 4*x + 2*x^2
or
Expand[1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2}]
3 + 4*x + 2*x^2
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Cattiaux Isabelle" <Isabelle.Cattiaux at univ-valenciennes.fr> wrote in
message news:9ncfq6$pt5$1 at smc.vnet.net...
>
> Hi,
>
> Could someone tell me why the first substitution rule
> works and the second doesn't
>
> In[1]:==
> 1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2}
>
> Out[1]==
> 2 + x + x(1 + x)+ (1 + x)^2
>
> In[78]:==
> 1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2,a_->Expand[a]}
>
> Out[78]==
> 1 + x^2 + x^3 + x^4
>
> --
> Isabelle Cattiaux-Huillard
> Universite de Valenciennes
>