sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.35k stars 460 forks source link

Multivariate ring of polynomials over real numbers give incorrect comparison against 0? #35681

Open user202729 opened 1 year ago

user202729 commented 1 year ago

Is there an existing issue for this?

Did you read the documentation and troubleshoot guide?

Environment

- **Sage Version**: 9.8

Steps To Reproduce

R.<x> = ZZ[]
assert R(0) > R(-1)

R.<x> = RR[]
assert R(0) > R(-1)

R.<x, y> = ZZ[]
assert R(0) > R(-1)

R.<x, y> = RR[]
assert R(0) > R(-1)

Expected Behavior

All assertions pass.

Actual Behavior

The last assertion does not pass.

Additional Information

No response

videlec commented 1 year ago

The problem comes from the method PolyDict.rich_compare (from sage.rings.polynomial.polydict) that does not take care of constant polynomials.

Note that comparison of polynomials behaves strangely anyway

sage: R.<x,y> = ZZ[]
sage: -3*x > -1*x
False
sage: -3*x > 0*x
True
sage: -3*x > 1*x
False
user202729 commented 1 year ago

Reading the source code it looks like it compares by the leading term of the difference and assume omitted/zero coefficient = smallest. Maybe it's better to explicitly compare the coefficient with 0 instead?

The current situation is such that x^2>x and -x^2>x (as well as x^2>-x etc.) are both true (in all fields and univariate/multivariate cases).

Currently SageMath is implemented such that elements of GF(p) is comparable and so are elements of GF(p)[x]. (probably for ordered set etc.). It's not a field ordering however.