Skip to content

Fix a bug with diophantine() when cornacchia() is not called properly #26780

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

Merged
merged 8 commits into from
Jul 8, 2024

Conversation

zylipku
Copy link
Contributor

@zylipku zylipku commented Jul 8, 2024

Occasionally diophantine() and diop_quadratic() result in a DN form $X^2 - DY^2 = N$ which cannot be handled by the current cornacchia() properly. For example, previously the diophantine solver will miss some solutions:

>>> eq1 = x**2 - 15*x + y**2 - 8*y # also mentioned in #18628
>>> diophantine(eq1)
{(0, 0), (15, 0), (15, 8), (0, 8)}
>>> eq1.subs({x: 16, y: 4})
0
>>> eq2 = 2*x**2 - 9*x + 4*y**2 - 8*y + 14
>>> diophantine(eq2)
set()
>>> eq2.subs({x: 2, y: 1})
0

Both the two examples lead to a function call of cornacchia(1, X, 1) with X > 1, which incorrrectly turns out to have no solution by using cornacchia(). The solver can only be applied when $a+b\le m$. See reference from docs:
http://www.numbertheory.org/php/cornacchia.html
An additional check has been added before the algorithm based on the fact that $ax^2 + by^2 = m$ admits a solution for $a + b > m$ only if $x = 0$ or $y = 0$. A test function including both the two examples has been appended as well.

References to other Issues or PRs

Fixes #18628

Brief description of what is fixed or changed

An additional check has been added before the cornacchia algorithm body based on the fact that $ax^2 + by^2 = m$ admits a solution for $a + b > m$ only if $x = 0$ or $y = 0$.

Other comments

Release Notes

  • solvers
    • Fix a bug with cornacchia(). Formerly diophantine() and diop_quadratic() may miss some solutions for quadratic equations.

zylipku added 2 commits July 8, 2024 10:06
Occasionally diophantine() and diop_quadratic() results in a DN form
$X^2 - DY^2 = N$ which cannot be handled by the current cornacchia()
properly. For example, previously

>>> eq1 = x**2 - 15*x + y**2 - 8*y # also mentioned in sympy#18628
>>> diophantine(eq1)
{(0, 0), (15, 0), (15, 8), (0, 8)}
>>> eq1.subs({x: 16, y: 4})
0

>>> eq2 = 2*x**2 - 9*x + 4*y**2 - 8*y + 14
>>> diophantine(eq2)
set()
>>> eq2.subs({x: 2, y: 1})
0

Both the examples leads to a function call cornacchia(1, X, 1) with
X > 1, which turns out to have no solution for cornacchia(). The
solver can only be applied when $a+b<=m$,
ref: http://www.numbertheory.org/php/cornacchia.html
An additional check has been added before the algorithm based on the
fact that $ax^2 + by*2 = m$ admits a solution for $a + b > m$ only if
$x = 0$ or $y = 0$. A test function includes both the two examples
has been appended as well.
@sympy-bot
Copy link

sympy-bot commented Jul 8, 2024

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • solvers
    • Fix a bug with cornacchia(). Formerly diophantine() and diop_quadratic() may miss some solutions for quadratic equations. (#26780 by @zylipku)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.14.

Click here to see the pull request description that was parsed.
Occasionally diophantine() and diop_quadratic() result in a DN form $X^2 - DY^2 = N$ which cannot be handled by the current cornacchia() properly. For example, previously the diophantine solver will miss some solutions:
```
>>> eq1 = x**2 - 15*x + y**2 - 8*y # also mentioned in #18628
>>> diophantine(eq1)
{(0, 0), (15, 0), (15, 8), (0, 8)}
>>> eq1.subs({x: 16, y: 4})
0
```
```
>>> eq2 = 2*x**2 - 9*x + 4*y**2 - 8*y + 14
>>> diophantine(eq2)
set()
>>> eq2.subs({x: 2, y: 1})
0
```
Both the two examples lead to a function call of cornacchia(1, X, 1) with X > 1, which incorrrectly turns out to have no solution by using cornacchia(). The solver can only be applied when $a+b\le m$. See reference from docs:
http://www.numbertheory.org/php/cornacchia.html 
An additional check has been added before the algorithm based on the fact that $ax^2 + by^2 = m$ admits a solution for $a + b > m$ only if $x = 0$ or $y = 0$. A test function including both the two examples has been appended as well.

<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Fixes #18628 

#### Brief description of what is fixed or changed
An additional check has been added before the cornacchia algorithm body based on the fact that $ax^2 + by^2 = m$ admits a solution for $a + b > m$ only if $x = 0$ or $y = 0$.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* solvers
  * Fix a bug with cornacchia(). Formerly diophantine() and diop_quadratic() may miss some solutions for quadratic equations.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

Copy link

github-actions bot commented Jul 8, 2024

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

zylipku and others added 5 commits July 8, 2024 20:58
Co-authored-by: haru-44 <36563693+haru-44@users.noreply.github.com>
Co-authored-by: haru-44 <36563693+haru-44@users.noreply.github.com>
Co-authored-by: haru-44 <36563693+haru-44@users.noreply.github.com>
@haru-44
Copy link
Member

haru-44 commented Jul 8, 2024

thanks! looks good.

@oscarbenjamin
Copy link
Collaborator

Thanks

@oscarbenjamin oscarbenjamin merged commit 72e62fb into sympy:master Jul 8, 2024
38 checks passed
@zylipku zylipku deleted the fix-cornacchia-miss branch July 8, 2024 15:40
@zylipku zylipku restored the fix-cornacchia-miss branch July 8, 2024 15:40
@zylipku zylipku deleted the fix-cornacchia-miss branch July 8, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

diophantine(), diop_quadratic() missing solutions
4 participants