I need to simplify some algebraic expression, according to particular pattern, namely:

\begin{equation*} x^m = \left\{ \begin{array}{ll} 1 & \text{if } m \text{ is even} \\ x & \text{if } m \text{ is odd} \end{array}\right. \end{equation*}

for example:

$$ a x^3 + b x y-c x^7 y^2 $$ should be simplified to $$ ax + bxy - cxy^2 $$ I need something like:

a x^3 + b x y - c x^7 y^2 /.{x^_ -> ... }

but, that will recognize the parity of the each power in the expression.


How one can get only one term in the sum? For example, in the example there are three terms

$$ a x^3, \ b x y, \ -c x^7 y^2 $$

is there any possibility to decompose the full expression to

A = {a x^3, b x y, - c x^7 y^2}

3 Answers 3

expr = a x^3 + b x y - c x^7 y^2;

expr /. {x_^m_ /; EvenQ[m] :> 1, x_^m_ /; OddQ[m] :> x}
a x - c x + b x y
List @@ expr
{a x^3, b x y, -c x^7 y^2}

Be warned that Replace etc. can be fragile for mathematical manipulation.

As Pinguin Dirk noted I think you wanted matching for literal x; in that case you could use x in place of each x_ pattern in my replacement rules:

expr /. {x^m_ /; EvenQ[m] :> 1, x^m_ /; OddQ[m] :> x}
a x + b x y - c x y^2

Also, I used a longer form with Condition rather PatternTest, because I think the former is often easier to use for mathematical conditions. See this for more:

  • $\begingroup$ I understand the difference between "x" and "x_", but the last option also applies to ALL powers in the expression. How I can restrict to only particular monomials? (I do multi-dim Taylor expansion and should pick only particular variables from the expansion). $\endgroup$ Oct 16, 2013 at 12:39
  • $\begingroup$ I added my variation to the comments below Pinguin Dirk's answer. Also, thanks for the Accept. $\endgroup$
    – Mr.Wizard
    Oct 16, 2013 at 13:08

Yet another way, replacing only if m is an integer:

expr /. {x^m_Integer :> x^Mod[m, 2]}

a x + b x y - c x y^2

Note I am assuming that x is taken literally (we only match for x)

As for the question in the comments, a possible way would be to condition on the pattern, as Mr. Wizard does (in a similar way):

expr /. {patt_^m_Integer :> patt^Mod[m, 2] /; MemberQ[{x1, x2}, patt]}

where {x1,x2} are the bases you want to modify (I am using patt instead of x as the name of the pattern, to avoid confusion)

or also, as Mr. Wizard points out in the comments:

expr /. (x : x1 | x2 | x3)^m_Integer :> x^Mod[m, 2]
  • $\begingroup$ +1 for noting my oversight. You could add m_Integer to make this more robust. $\endgroup$
    – Mr.Wizard
    Oct 16, 2013 at 11:26
  • $\begingroup$ @Mr.Wizard: thanks - and edited! $\endgroup$ Oct 16, 2013 at 11:28
  • $\begingroup$ Thanks! And if there are more than one variable "x" (assume x1, x2, x3, and so on)? Needed something like: expr /. {x*^m_Integer :> x*^Mod[m, 2]}, where "" (star) is a linux-type expression for all possible values. (x -> x1, x2, x3....) just to avoid writing: /. {x1^m_Integer :> x1^Mod[m, 2],x2^m_Integer :> x2^Mod[m, 2], .... } $\endgroup$ Oct 16, 2013 at 12:29
  • $\begingroup$ I tried with "x_", but it replace ALL variables with powers and I want only particular variables (x1,x2,x3..). $\endgroup$ Oct 16, 2013 at 12:40
  • $\begingroup$ @AndrewKor: see the edit $\endgroup$ Oct 16, 2013 at 12:47

A little bit more compact answer to Q1

expr /. {_^_?EvenQ :> 1, x_^_?OddQ :> x}
a x - c x + b x y
  • $\begingroup$ +1 for brevity :-) I chose Condition as I think it is a bit more general for mathematical conditions. $\endgroup$
    – Mr.Wizard
    Oct 16, 2013 at 11:09
  • 1
    $\begingroup$ looking at the example, I guess he wants the pattern to match for x only, not for x_ (i.e. x, y,...), or am I mistaken? (I am not quite sure) $\endgroup$ Oct 16, 2013 at 11:22
  • 1
    $\begingroup$ You can make this shorter: expr /. {_^_?EvenQ :> 1, x_^_?OddQ :> x} or by Pinguin Dirk's reading: expr /. {x^_?EvenQ -> 1, x^_?OddQ -> x} $\endgroup$
    – Mr.Wizard
    Oct 16, 2013 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.