# Replacement rules and algebra

When your input is 1 + x^2 + x^3 /. x^2 -> 0, you get 1 + x^3. What should you type in to get 1?

• That's a tricky question because 1 + x^2 + x^3 /. _ -> 1 does that. – Kuba Aug 27 '16 at 20:39
• 1 + x^2 + x^3 /. {x^2 -> 0, x^3 -> 0} – alancalvitti Aug 27 '16 at 20:39
• expr = 1 + x^2 + x^3; expr /. First@Solve[expr == 1, x] gives 1 :) – Nasser Aug 27 '16 at 21:15
• 1 + x^2 + x^3 /. x -> 0 – Bob Hanlon Aug 27 '16 at 21:18

With respect to a comment that should be incorporated into the question, there is PolynomialMod:

PolynomialMod[
a + b x + c y + d z + e x^2 + f x^2 y + g z^2 + h x^3 z + k x y z + l x^2 y^2 z^2,
x^2]
(*  a + b x + c y + d z + k x y z + g z^2  *)


One could use a replacement rule, too:

a + b x + c y + d z + e x^2 + f x^2 y + g z^2 + h x^3 z + k x y z +
l x^2 y^2 z^2 /. x^p_Integer /; p > 1 -> 0
(*  a + b x + c y + d z + k x y z + g z^2  *)


As for most of what's posted right now, I think everyone but Bob is pulling your leg a little:

1 + x^2 + x^3 /. x -> 0 – Bob Hanlon 40 mins ago

But this will set all the terms with a factor of x to the first power to zero, too, which fits with the original question but not the comment.

The thing to keep in mind about replacement rules is that whatever subexpression matches the pattern on the left-hand is replaced by the corresponding expression on the left-hand side. As expression-tree transformations, they sometimes do not behave quite like the algebra transformations one might be used to. For instance, you cannot replace the factor of x^2 in x^3, because as an expression, x^3 does not contain x^2, just Power[x, 3]. There's no 2 and certainly not Power[x, 2]. What's more troublesome is that x^2 * x automatically becomes x^3 inside Mathematica, unless you do some fancy things to interfere with the normal evaluation of expressions.

The point is that sometimes there are functions like PolynomialMod[] that are more appropriate for doing algebra than replacement rules.

One way is to use a variable in the argument of the index:

1 + x^2 + x^3 /. x^i_ -> 0

• i_. will help with x too. – Kuba Aug 27 '16 at 20:40
• No way of using the fact that x^3is a multiple of x^2? Imagine I want to get rid of any monomial that contains x^2 in a polynomial in three variables like a + b x + c y + d z + e x^2 + f x^2 y + g z^2 + h x^3 z + k x y z + l x^2 y^2 z^2. Mathematica can surely do that. – André Bellaïche Aug 27 '16 at 21:00

I think that what you are looking for is first order series expansion wrt x

1 + x^2 + x^3;
% + O[x]^2
(*1 + O[x]^2*)

a + b x + c y + d z + e x^2 + f x^2 y + g z^2 + h x^3 z + k x y z + l x^2^2 z^2;
% + O[x]^2
(*(a + c y + d z + g z^2) + (b + k y z) x + O[x]^2 *)


The O[x]^2 (conversion from SeriesData) can be eliminated by applying Normal.