0
$\begingroup$

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

$\endgroup$
13
  • $\begingroup$ can k be 1 or 0? $\endgroup$
    – garej
    Commented Mar 16, 2016 at 9:20
  • $\begingroup$ @garej yes, If k == 1 n*Subscript[e,j] else n $\endgroup$
    – plus91
    Commented Mar 16, 2016 at 9:23
  • $\begingroup$ Something like expr /. n_. (t : Subscript[e, _])^k_. :> If[Mod[k, 2] == 0, n, n t] ... Look up _. $\endgroup$
    – Szabolcs
    Commented Mar 16, 2016 at 9:30
  • $\begingroup$ @garej thanks;) I do some testing and let you know ;) $\endgroup$
    – plus91
    Commented Mar 16, 2016 at 9:52
  • 1
    $\begingroup$ 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? $\endgroup$
    – george2079
    Commented Mar 16, 2016 at 18:02

1 Answer 1

2
$\begingroup$

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] *)
$\endgroup$
6
  • $\begingroup$ LLlAMnYP if I want to incorporate this rule in the statement that I wrote in my previous comment? Thanks $\endgroup$
    – plus91
    Commented Mar 16, 2016 at 12:18
  • $\begingroup$ @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 $\endgroup$
    – LLlAMnYP
    Commented Mar 16, 2016 at 12:34
  • $\begingroup$ LLlAMnYP Great! $\endgroup$
    – plus91
    Commented Mar 16, 2016 at 14:24
  • $\begingroup$ LLlAMnYP I edited a question cause of new similar problem I'm trying to solve ;) $\endgroup$
    – plus91
    Commented Mar 16, 2016 at 14:31
  • $\begingroup$ @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. $\endgroup$
    – MarcoB
    Commented Mar 16, 2016 at 18:45

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.