22
$\begingroup$

How do I substitue z^2->x in the following polynomial z^4+z^2+4?

z^4+z^2+4 /. z^2->x 

gives

z^4+x+4 
$\endgroup$
3
  • 2
    $\begingroup$ wow, are you guys telling me that the most advanced computer algebra software in the world cannot do a simple replacement of one variable for another without the user having to do some very unintuitive workarounds? $\endgroup$ Commented Feb 23, 2014 at 22:07
  • 1
    $\begingroup$ see mathematica.stackexchange.com/questions/42121/… $\endgroup$ Commented Feb 24, 2014 at 5:34
  • 1
    $\begingroup$ z^4 + z^2 + 4 /. z^y_Integer -> x^(y/2) gives 4+x+x^2 $\endgroup$ Commented Oct 5, 2016 at 19:16

7 Answers 7

24
$\begingroup$

The reason why the replacement doesn't work is that replacement rules are not mathematical replacements, but pure structural replacements. Therefore the replacement z^2->x just looks for occurrences of the pattern z^2 and replaces that with x. Now z^4 doesn't match that pattern.

Also note that rules operate on the internal form, which doesn't always match the displayed form. For example, one would expect a-2b /. 2b->c to result in a-c, but it actually results in a-2b again, because internally the expression reads Plus[a, Times[-2, b]] (you can see that by applying FullForm), while 2b is Times[2,b].

To do the replacement wanted, one has to use a method which is aware of the mathematics instead of just the structure. One possibility is

Solve[p==z^4+z^2+4 && x==z^2, {p}, {z}] 

which means "Solve the equations given for p while eliminating z". The result then is

{{p->4+x+x^2}} 

Note that the curly braces around z are mandatory because otherwise Mathematica interprets it as domain, resulting in an error message because z is of course no valid domain. Also note that the documentation page of Solve omits the possibility of giving a list of variables to eliminate as third argument (at least I didn't find it). However, you'll find it in a Mathematica tutorial on eliminating variables (but there they use the third argument without braces, which at least for me results in an error message, as written above).

$\endgroup$
19
$\begingroup$
In[409]:= PolynomialReduce[z^4 + z^2 + 4, z^2 - x, {z, x}][[2]] Out[409]= 4 + x + x^2 

This is similar to the Solve approach in that both use algebraic means to effect the substitution. But one can be a bit more general using PolynomialReduce (by taking advantage of term orders, say).

For further detail on this approach, might have a look at some responses to these questions:

$\endgroup$
0
14
$\begingroup$
 z^4 + z^2 + 4 /. z^(a_Integer) -> x^(1/2 a) 

yields

4 + x + x^2 
$\endgroup$
10
$\begingroup$

How about using Eliminate and Solve?

Solve[Eliminate[{expr == z^4 + z^2 + 4, z^2 == x}, z], expr] 

Alternatively we can use the third argument of Solve to tell it which variables should be eliminated:

Solve[{expr == z^4 + z^2 + 4, z^2 == x}, expr, {z}] 

Note that the braces around z are mandatory, otherwise it will be interpreted as the domain. (Thanks @celtschk!)

$\endgroup$
3
  • $\begingroup$ I thought Solve had an option to eliminate some variables while solving, but I couldn't find it. So I used Eliminate explicitly. $\endgroup$ Commented Jan 23, 2012 at 21:07
  • 1
    $\begingroup$ In reference.wolfram.com/mathematica/tutorial/… Solve is given as Solve[eqns, vars, elims]. And indeed, Solve[p==z^4+z^2+4&&x==z^2,{p},{z}] gives {{p -> 4 + x + x^2}}. Note that the braces around z are mandatory, or it will be interpreted as domain. $\endgroup$ Commented Jan 23, 2012 at 21:18
  • $\begingroup$ @celtschk Thanks! I remembered something like that, but I didn't add the braces, so it didn't work $\endgroup$ Commented Jan 23, 2012 at 23:42
5
$\begingroup$

I'd do

z^4 + z^2 + 4 /. z -> Sqrt[x] (* --> 4 + x + x^2 *) 

In this particular case, at least (which of course may not work in more general situations, for example with odd powers of z, since I randomly picked the sign)

$\endgroup$
3
  • $\begingroup$ It wasn't random. It's the principal value, just like Artes' solution. $\endgroup$ Commented Jan 23, 2012 at 23:06
  • $\begingroup$ @Simon Well, ok, I agree. $\endgroup$ Commented Jan 23, 2012 at 23:29
  • $\begingroup$ The easiest way. $\endgroup$ Commented May 1, 2020 at 10:24
3
$\begingroup$

In the same vein as Daniel's answer (since the functions are definitely related):

First[GroebnerBasis[{z^4 + z^2 + 4, z^2 - x}, x, z]] 
$\endgroup$
2
$\begingroup$

Using rules one could,

rule = {z^s_ /; s >= 2 /; Mod[s, 2] == 0 -> x^(s/2)} l //. rulesk 

4 + x + x^2

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.