Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make gcdx(0, 0) return (0, 0, 0) #40989

Merged
merged 4 commits into from
Apr 25, 2024

Conversation

albinahlback
Copy link
Contributor

As the title suggests. This should conform to GMP's definition: https://gmplib.org/manual/Number-Theoretic-Functions#index-Extended-GCD.

@Keno
Copy link
Member

Keno commented May 28, 2021

Is there a justification for why one is better than the other? This is a silent breaking change, so we cannot do this lightly.

@Keno Keno added the kind:breaking This change will break code label May 28, 2021
@oscardssmith oscardssmith added the domain:maths Mathematical functions label May 28, 2021
@albinahlback
Copy link
Contributor Author

Sure, I understand. Basically it is because of (1) standards, (2) expectation and (3) bad practice (IMO).

As mentioned it is the standard for GMP, but also for other programmes like Wolfram: https://www.wolframalpha.com/input/?i=extendedgcd%280%2C0%29.

When I write gcdx(a, b) I want an output (d, x, y) such that x a + y b = d. For the function to be well-defined, I think I speak for everyone that we want the "smallest" x and y (for a more precise specification of what smallest mean, see https://fungrim.org/entry/da7d00/ and https://fungrim.org/entry/633265/). Just as it would be wierd to solve 14 x + 12 y = 2 by setting (x, y) = (-11, 13) when (x, y) = (1, -1) is the "natural" solution, it would be just as wierd to solve 0 x + 0 y = 0 by setting x and y to something non-zero.

Continuing on this, I find it bad practice to not return the "smallest" values. If we can return a small number, why don't we?

Obviously this need to be documented, but the general behavior of gcdx should be defined as well.

@oscardssmith oscardssmith added the status:triage This should be discussed on a triage call label Feb 7, 2024
@oscardssmith
Copy link
Member

This makes sense to me. Adding triage to confirm. @Keno why is this a breaking change? I think as long as the result conforms with the definition, it's OK.

@albinahlback
Copy link
Contributor Author

This makes sense to me. Adding triage to confirm. @Keno why is this a breaking change? I think as long as the result conforms with the definition, it's OK.

Well, it is a breaking change as people may rely on specific return values. However, I still think it is good to proceed.

@LilithHafner
Copy link
Member

@nanosoldier runtests()

@oscardssmith
Copy link
Member

Triage approves on the argument that this is a pretty minor change, arguably more mathematically consistent, and agrees with GMP and Mathematica.

@LilithHafner LilithHafner added kind:minor change Marginal behavior change acceptable for a minor release and removed status:triage This should be discussed on a triage call kind:breaking This change will break code labels Feb 15, 2024
@nanosoldier
Copy link
Collaborator

Your job failed. Consult the server logs for more details (cc @maleadt).

@maleadt
Copy link
Member

maleadt commented Feb 16, 2024

Looks like an unrelated compilation failure (SuiteSparse checksum failure); let's try again:

@nanosoldier runtests()

@nanosoldier
Copy link
Collaborator

Your job failed. Consult the server logs for more details (cc @maleadt).

@maleadt
Copy link
Member

maleadt commented Feb 16, 2024

... wait, the head of this PR is ancient. Let's rebase first.

@maleadt
Copy link
Member

maleadt commented Feb 16, 2024

@nanosoldier runtests()

@nanosoldier
Copy link
Collaborator

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

@oscardssmith
Copy link
Member

oscardssmith commented Feb 16, 2024

lol. the one failure is Nemo which 5 years ago discovered that Nemo disagreed with julia and therefore in Nemocas/Nemo.jl#730 switched how their gcdx works. Specifically, see https://github.com/Nemocas/Nemo.jl/blob/a5a9c5c2d565a94ad8d75ed484848d2d60d19cb2/src/flint/fmpz.jl#L1282.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 22, 2024

Is this related to / close #39638?

@LilithHafner
Copy link
Member

no

@albinahlback
Copy link
Contributor Author

Can I bump this?

@LilithHafner LilithHafner added the needs news A NEWS entry is required for this change label Apr 25, 2024
@LilithHafner
Copy link
Member

Thanks for the bump.

This should get an entry in NEWS.md (follow the style of the other entries in that file)

Aside from that LGTM.

It would be nice to test gcdx on BigInt.

This removes the somewhat inconsistent behaviour of returning (0, 1, 0).

Also update NEWS.md accordingly.
@albinahlback
Copy link
Contributor Author

Haven't checked that it passes the tests. I also added some extra tests for gcdx and added tests for BigInt. I did not do anything more with rationals, though. Added an entry in NEWS.md.

Also test gcd, gcdx and lcm for BigInt, and add some additional
canonical tests for gcdx.
@LilithHafner LilithHafner removed the needs news A NEWS entry is required for this change label Apr 25, 2024
@LilithHafner
Copy link
Member

Elegant approach to testing. I like it!

@LilithHafner LilithHafner merged commit 0735854 into JuliaLang:master Apr 25, 2024
5 of 8 checks passed
@LilithHafner
Copy link
Member

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:maths Mathematical functions kind:minor change Marginal behavior change acceptable for a minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants