2
$\begingroup$

I found Apply a function to all coefficients of a polynomial but I could not understand

I want to apply Round to the coeffricients of $x^2 + 3.1x^3 + 5.4x^4$. The Map function tries to Round the entire expression like 3.1x^3

$\endgroup$
1
  • 2
    $\begingroup$ Try: x^2 + 3.1 x^3 + 5.4 x^4 /. x_?NumericQ :> Round[x] This results in: x^2 + 3 x^3 + 5 x^4 $\endgroup$ Mar 2, 2023 at 17:48

2 Answers 2

2
$\begingroup$

I would use 3-arg Collect for this purpose:

Collect[x^2+3.1 x^3+5.4 x^4, x, Round]

x^2 + 3 x^3 + 5 x^4

$\endgroup$
2
  • $\begingroup$ Note that if there is a variable in any of the coefficients, this does not behave as expected, e.g., Collect[a*x^2 + 3.1 x^3 + 5.4 x^4, x, Round] $\endgroup$
    – Bob Hanlon
    Mar 2, 2023 at 20:21
  • $\begingroup$ Why I cannot turn this into a function? polyRound[poly0_] := Collect[poly0_, x, Round] gives Round[Pattern[ 2 x +1.5 3 x ,_]] $\endgroup$
    – user91691
    Mar 3, 2023 at 11:15
1
$\begingroup$
Clear[x, expr]
expr = x^2 + 3.1 x^3 + 5.4 x^4;
crules = Merge[CoefficientRules[expr], Round @@ # &] // Normal

FromCoefficientRules[crules, Variables[expr]]

x^2 + 3 x^3 + 5 x^4

$\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.