I have a polynomial in x, for example,
x^4/s + x^开发者_如何学JAVA3 + x^2*s + x^3*s^2 + x
What I want to do is:
based on the result of (the exponent of x) mod 3, if it's 0, change the x^* to 1;
if it's 1, change the x^* to 2;
if it is 2, change x^* to 3.
So I want to get:
x^4 => 2
x^3 => 1
x^2 => 3
x^1 => 2
therefore, for the given example, I get
2/s+1+3s+s^2+2
How to do this programmably? Thanks!
The following:
(x^4/s + x^3 + x^2*s + x^3*s^2 + x) /. x^(a : _ : 1) :> (Mod[a, 3] + 1)
seems to do it.
Edit: Answering the comment:
In[4]:= (x^4/s + x^3 + x^2*s + x^3*s^2 + x) /.
x^(a : _ : 1) :> (Mod[a, 3] /. {0 :> m, 1 :> n, 2 :> p})
Out[4]= m + n + n/s + p s + m s^2
精彩评论