Ticket #1482 (closed defect: fixed)
[with patch, with 2 positive reviews] xgcd suboptimal output
| Reported by: | nbruin | Owned by: | dmharvey |
|---|---|---|---|
| Priority: | major | Milestone: | sage-2.9.2 |
| Component: | basic arithmetic | Keywords: | |
| Cc: | Author(s): | ||
| Report Upstream: | Reviewer(s): | ||
| Merged in: | Work issues: |
Description
I was expecting xgcd(x,y) to produce u,v of minimal absolute value such that u*x+v*y=gcd(x,y). However, presently it doesn't in the edge case where x divides y or vice versa (i.e., where (u,v)=(1,0) or (u,v)=(0,1) would be valid)
sage: xgcd(2,4) (2, -1, 1) sage: xgcd(4,2) (2, 1, -1) sage: xgcd(12,2) (2, 1, -5)
This is not a bug in the strictest sense of the word, since the documentation does not promise u,v to be minimal, but for a lot of users minimal (u,v) would be the expected behaviour.
As far as I can see, this is directly the answer of mpz_gcdext. Possible solutions:
- fix mpz_gcdext
- test in sage.rings.integer.Integer._xgcd if the gcd equals self.abs() or right.abs() and return (u,v)=(+-1,0) or (u,v)=(0,+-1) as appropriate (this is cython code
- do this test in the python wrapper sage.rings.integer.Integer.xgcd instead.
Since xgcds are so crucial, I am hesitant to fix it myself. Can someone who knows the code well see what the appropriate solution is?

