I ran into a problem with CoefficientRules
behaving differently with exact and numerical coefficients, where in the numerical case it behaves as if the entire expression had an N[...]
around it, numericizing all numbers, including indices.
E.g.,
CoefficientRules[A[0] x + A[1] y + 1, {x, y}]
works as intended, but
CoefficientRules[A[0] x + A[1] y + 1., {x, y}]
does not, as it has A[0.]
and A[1.]
in the result.
Is there an easy way to stop this from happening or to convert back the wrong results to their correct form?
Edit: one general workaround I found is to use subscripts such as Subscript[A,0]
instead of expressions that could be interpreted as function evaluations. That also works with multiple indices and non-integer parameters, for which some of the other suggested workarounds do not.
CoefficientList
andCoefficientArrays
, which effectively carry out the same task asCoefficientRules
but present their result differently, behave in the way you expect, I.e. they do not numericise indices. Perhaps one of those could be used as a workaround as well. $\endgroup$A
toNHoldAll
? $\endgroup$CoefficientRules[A[0] x + A[1] y + 1., {x, y}] /. A[n_] :> A[Round[n]]
or more generallyCoefficientRules[A[0] x + A[1] y + 1., {x, y}] /. f_[n_] :> f[Round[n]]
$\endgroup$2
orSqrt[2]
or2/3
etc. $\endgroup$Trace[CoefficientRules[A[0] x + A[1] y + 1., {x, y}]]
(ortraceView2[..]
-- look up on site) shows it's intentional. But if so, the docs should point it out, which should be reported/suggested to WRI. $\endgroup$