4

Q1:

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.

Q2:

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}
mmal
  • 3,508
  • 2
  • 18
  • 38
Arnold Klein
  • 315
  • 2
  • 8

3 Answers3

5
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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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). – Arnold Klein Oct 16 '13 at 12:39
  • I added my variation to the comments below Pinguin Dirk's answer. Also, thanks for the Accept. – Mr.Wizard Oct 16 '13 at 13:08
4

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]
Pinguin Dirk
  • 6,519
  • 1
  • 26
  • 36
3

A little bit more compact answer to Q1

expr /. {_^_?EvenQ :> 1, x_^_?OddQ :> x}
a x - c x + b x y
ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • +1 for brevity :-) I chose Condition as I think it is a bit more general for mathematical conditions. – Mr.Wizard Oct 16 '13 at 11:09
  • 1
    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) – Pinguin Dirk Oct 16 '13 at 11:22
  • 1
    You can make this shorter: expr /. {_^_?EvenQ :> 1, x_^_?OddQ :> x} or by Pinguin Dirk's reading: expr /. {x^_?EvenQ -> 1, x^_?OddQ -> x} – Mr.Wizard Oct 16 '13 at 11:32