4
$\begingroup$

Given a polynomial, for instance A+A^2+A^3, how can I replace A->x, ending with x+A^2+A^3?

Attempts:

The problem is I cannot do it without also replacing the higher-order terms. For example, A+A^2+A^3 /.{A^exp_/;exp==1->x} does nothing. Another obvious choice, A+A^2+A^3 /.{A^1->x}, returns x+x^2+x^3. On the other hand, the inverse works: A+A^2+A^3 /.{A^exp_/;exp!=1->x^exp} does lead to A+x^2+x^3.

This leads to a second question: Why does A^1 matches any power of A but A^x_/;x==1 matches nothing?

$\endgroup$
1
  • $\begingroup$ How would you want to treat constants? eg 1 + A + A^2 + A^3 $\endgroup$ Commented Oct 24, 2022 at 13:44

3 Answers 3

6
$\begingroup$

What about

poly + SeriesCoefficient[poly,{A,0,1}]*(x-A)
$\endgroup$
5
$\begingroup$
rule = {{b_. Power[c_, d_] :> b Power[c, d], a_. A :> a x}};

A + A^2 + A^3 /. rule

{A^2 + A^3 + x}


a A + 4 A^2 + A^3 /. rule

{4 A^2 + A^3 + a x}

$\endgroup$
3
$\begingroup$
ReplaceAll[w A + x A^2 + y A^3, {p_Power :> p, A -> z}]
A^2 x + A^3 y + w z
$\endgroup$

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.