4
$\begingroup$

I would like to replace variable with power. For example,

x^6 /. x^4 -> a + b

which I supposed to get $x^2(a+b)$ but I get $x^6$ and I can't use

x^6 /. x -> Sqrt[Sqrt[a + b]]

because I want to replace $x^4$ only.

$\endgroup$
  • 1
    $\begingroup$ Last@PolynomialReduce[x^6, {x^4 - a - b}, x] seems to work on this very specific problem. It's unlikely you will get a replacement rule to work, since they don't break things down algebraically (into x^4 * x^2, for example). They work on literal matches to patterns. $\endgroup$ – Michael E2 Nov 17 '16 at 3:53
  • $\begingroup$ @MichaelE2 If this question is more complicate, for example, we change $x^4->a+b$ to $x^4->x^2+a+b$, how to deal with this one? $\endgroup$ – Natthawin Cho Nov 17 '16 at 4:11
  • $\begingroup$ I don't understand: if you want to "replace $x^4$ only," as you state, then x^6 doesn't match what you want, and the answer you get is correct (no replacement happens). $\endgroup$ – Jens Nov 17 '16 at 4:37
  • 3
    $\begingroup$ x^6 /. x^n_ :> (a+b)^Quotient[n,4]* x^Mod[n,4] gives you x^2* (a+b) $\endgroup$ – Bill Nov 17 '16 at 4:59
  • 1
    $\begingroup$ My 5 cents to this beautiful question: rule = x^n_ /; n >= 4 && n \[Element] Integers -> x^(n - 4)*(a + b); and then x^6 /. rule gives (a + b) x^2. But what would you like to do with x^8 /. rule giving (a + b) x^4? $\endgroup$ – Alexei Boulbitch Nov 17 '16 at 7:56
1
$\begingroup$

Based on Bill's comment above the following use of TagSetDelayed ensures that multiples of exponent 4 are properly substituted all the time:

f[x_] := a + b + x^2;
x /: Power[x, n_?(Quotient[#, 4] >= 1 &)] := x^Mod[n, 4] f[x]^Quotient[n, 4]
Table[{i, x^i}, {i, 15}] // TableForm

enter image description here

If we Expand, the substitution works as well:

Expand[x^3 (a + b + x^2)] (* a x^3 + b x^3 + x^5 *)
(* a x^3 + b x^3 + x (a + b + x^2) *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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