My problem is very simple. From two different functions $f_1$ and $f_2$, I create two multivariate polynomials.

Because of theoretical reasons those two functions (evaluated with the same input $x$) should send the same output.

Now when I want to check this I always get false. The reason is the following I declare $P_1=f_1(x)$ and $P_2=f_2(x)$ and when I see the polynomials $P_1$ and $P_2$ they are actually equal but the first one has integer coefficients and the second one has rational coefficients (when the coefficient is $2$ in the first one, in the second the coefficient will be $2.$).

Here is the question, how can I formally check that I have found the same polynomial in both cases? I think this would involve to convert the coefficients of the first one into rational but I am not able to find in the help of Mathematica how to do it, and then to check equality with $P_1===P_2$.

  • 1
    $\begingroup$ 2. is represented as floating point, not a Rational. IntegerQ[2.] is False; IntegerQ[2/1] is True (evaluates to 2). $\endgroup$ Jul 24, 2015 at 16:33
  • $\begingroup$ Check documentation for Rationalize. $\endgroup$ Jul 24, 2015 at 16:53

1 Answer 1

 f1[x_, y_] = x + 2 y^2
 f2[x_, y_] = x + 2. y^2

 Quiet[{0} == Union @
        Flatten @ Chop[
          CoefficientList[f1[x, y], {x, y}] - 
          CoefficientList[f2[x, y], {x, y}]]]


The Quiet is needed to handle the case where the polynomials do not share all the same terms.

This will fail in the maybe pathological case where you have a numerically small high order term, e.g., 1 + x + 10.^-17 x^2 will show not equal to 1 + x.

You might also more simply do:

N[f1[x, y]] == N[f2[x, y]]

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.