Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}
share|improve this question

3 Answers 3

up vote 5 down vote accepted
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:

share|improve this answer
    
Thank You very much!!! –  A.K. Oct 16 '13 at 11:49
    
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). –  A.K. 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

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]
share|improve this answer
    
+1 for noting my oversight. You could add m_Integer to make this more robust. –  Mr.Wizard Oct 16 '13 at 11:26
    
@Mr.Wizard: thanks - and edited! –  Pinguin Dirk Oct 16 '13 at 11:28
    
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], .... } –  A.K. Oct 16 '13 at 12:29
    
I tried with "x_", but it replace ALL variables with powers and I want only particular variables (x1,x2,x3..). –  A.K. Oct 16 '13 at 12:40
    
@AndrewKor: see the edit –  Pinguin Dirk Oct 16 '13 at 12:47

A little bit more compact answer to Q1

expr /. {_^_?EvenQ :> 1, x_^_?OddQ :> x}
a x - c x + b x y
share|improve this answer
    
+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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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