# Replacing terms in a sum according to a condition

If I have a generic expression such as

8 + 48*(Subscript[e,1])^2
+ 64*(Subscript[e,1])^3
+ 12*(Subscript[e,3])^6
+ ...
+ n*Subscript[e,j]^k + ...


I like to replace terms of the form n*(Subscript[e,j])^k with n if Mod[k,2]==0, otherwise with n*(Subscript[e,j]).

How can this be achieved?

EDIT:

If i have

Subscript[e, 5]*Subscript[e, 7]*Subscript[e, 4]*Subscript[e, 3]*Subscript[e, 4]*Subscript[e, k]*Subscript[e, j]


I would like to return Subscript[e, U] if k != j compared all products, the return value will Subscript[e, l], any idea? Thx

• can k be 1 or 0? – garej Mar 16 '16 at 9:20
• @garej yes, If k == 1 n*Subscript[e,j] else n – plus91 Mar 16 '16 at 9:23
• Something like expr /. n_. (t : Subscript[e, _])^k_. :> If[Mod[k, 2] == 0, n, n t] ... Look up _. – Szabolcs Mar 16 '16 at 9:30
• @garej thanks;) I do some testing and let you know ;) – plus91 Mar 16 '16 at 9:52
• your added edit seems like a completely new question. (??). If so you should ask a new question. As there are no exponents in that expression what is k? – george2079 Mar 16 '16 at 18:02

This replacement rule appears to give the intended result:

Subscript[e, j_]^(k_) :> Subscript[e, j]^Mod[k, 2]

8 + 48*(Subscript[e,1])^2+ 64*(Subscript[e,1])^3 + 12*(Subscript[e,3])^6 /.
Subscript[e, j_]^(k_) :> Subscript[e, j]^Mod[k, 2]
(* 68 + 64*Subscript[e, 1] *)

• LLlAMnYP if I want to incorporate this rule in the statement that I wrote in my previous comment? Thanks – plus91 Mar 16 '16 at 12:18
• @plus91 Presumably, like so: If[Mod[n, 2] == 0, IInterval[a^n, b^n, k], (Expand[(ToEuiForm[IInterval[a, b, k]])^n] /. Subscript[e, j_]^(k1_) :> Subscript[e, j]^Mod[k1, 2])]. I replaced the pattern name k_ with k1_ to avoid shadowing (the nasty red highlighting), but it's impossible to tell for sure, without the definition of ToEuiForm – LLlAMnYP Mar 16 '16 at 12:34
• LLlAMnYP Great! – plus91 Mar 16 '16 at 14:24
• LLlAMnYP I edited a question cause of new similar problem I'm trying to solve ;) – plus91 Mar 16 '16 at 14:31
• @plus91 Your edit to your question formulated a second, completely separate question. This is known as "moving the goalpost" and it is frowned upon here. Instead, I would suggest that you ask an entirely different question on that second point. – MarcoB Mar 16 '16 at 18:45