2
$\begingroup$

I have an expression, not necessarily a polynomial, and want to take all the coefficients on whatever terms there are and replace them with the RootApproximant versions of them. For example,

a = 0.111803 z + 0.298142396

I would like the result of my replacement to look like

a = 1/(4 Sqrt[5]) z + 2/(3 Sqrt[5]).

I have tried to use some conditional replacement rules, but I cannot figure out how to take the coefficient and use it in a function, then put it back in front of the symbolic part. I have tried this

a /. t_ /; NumberQ[t] -> RootApproximant[t]

But this doesn't understand what the final t in the RootApproximant is.

$\endgroup$
1
  • $\begingroup$ a /. r_Real :> RootApproximant[r] $\endgroup$
    – Bob Hanlon
    Jun 8 at 13:32

1 Answer 1

3
$\begingroup$

The problem is that you're using a Rule, instead of a RuleDelayed, and so it's evaluating the RHS with the symbol t in as the argument of RootApproximant, which leads to the error message. Using RuleDelayed fixes it:

a /. t_ /; NumberQ[t] :> RootApproximant[t]
(* z*Root[1-10*#1+10*#1^2+<<5>>-6*#1^9+3*#1^10&,1,0] + ... *)
$\endgroup$
1
  • $\begingroup$ Thank you! That has indeed solved the issue $\endgroup$
    – D. Brown
    Jun 8 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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