Skip to content

Conversation

oscarbenjamin
Copy link
Collaborator

@oscarbenjamin oscarbenjamin commented Jun 26, 2023

References to other Issues or PRs

mpmath/mpmath#704

Brief description of what is fixed or changed

Other comments

Release Notes

  • core
    • Some fixes were made for compatibility with recent changes in mpmath (since mpmath's 1.3 release). SymPy versions <=1.12 may be incompatible with mpmath versions >1.3.

@sympy-bot
Copy link

sympy-bot commented Jun 26, 2023

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:

  • core
    • Some fixes were made for compatibility with recent changes in mpmath (since mpmath's 1.3 release). SymPy versions <=1.12 may be incompatible with mpmath versions >1.3. (#25290 by @oscarbenjamin and @smichr)

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

Click here to see the pull request description that was parsed.
<!-- 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. -->

https://github.com/mpmath/mpmath/issues/704

#### Brief description of what is fixed or changed


#### 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 -->
* core
   * Some fixes were made for compatibility with recent changes in mpmath (since mpmath's 1.3 release). SymPy versions `<=1.12` may be incompatible with mpmath versions `>1.3`.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@sylee957
Copy link
Member

Is it the changes here compatible with previous mpmath versions (<1.3)?

@oscarbenjamin
Copy link
Collaborator Author

Is it the changes here compatible with previous mpmath versions (<1.3)?

I think so. CI runs here with 1.3. These changes are needed for compatibility with the next release of mpmath (1.4?).

The changes here mostly reduce dependence on mpmath. They are basically:

  1. Change import from mpmath.ctx_mp_python import mpnumeric to from mpmath.ctx_mp import mpnumeric (this should always have been the direct import).
  2. Don't use mpmath.rational for anything.
  3. Do some extra string validation before calling mpmath's parsing function.

I tried running core and polys tests with mpmath 1.2.1 and also mpmath 0.19 which is apparently the minimum version:

sympy/setup.py

Line 37 in ad95bcc

min_mpmath_version = '0.19'

There were no test failures.

@bjodah
Copy link
Member

bjodah commented Jun 28, 2023

I think this looks good!

@@ -1021,6 +1021,8 @@ class Float(Number):

is_Float = True

_remove_non_digits = str.maketrans(dict.fromkeys("-+_."))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this include eE for exponential notation, too? or maybe better,

if num.startswith('_') or not num.translate(cls._remove_non_digits)

could be

if num.startswith('_') or not all(_.translate(cls._remove_non_digits) for _ in num.lower().split('e'))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be. I was just checking for things that the new from_str in mpmath accepts that were previously not accepted:

In [7]: from mpmath.libmp import from_str

In [8]: from_str("_1", 10)
Out[8]: (0, 1, 0, 1)

In [9]: from_str("e1", 10)
---------------------------------------------------------------------------
ValueError

Feel free to push a more comprehensive test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which version allows In[8] to succeed? With 1.3.0 it fails (as I hope it should, since underscore is used to separate digits and if it is present without a bounding digit on each side it could be an entry error):

>>> from mpmath.libmp import from_str
>>> from_str('_1',10)
Traceback (most recent call last):
...
ValueError: could not convert string to float: '_1'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oscarbenjamin , the question above was my only remaining question before committing this. An underscore prefaced number should not be translated as a number since this is a valid python identifier. SymPy (with this PR) will no loger allow a terminal underscore on a number since this is ambiguous: underscores should come between digits.

@smichr smichr marked this pull request as draft August 17, 2023 01:09
@smichr
Copy link
Member

smichr commented Aug 17, 2023

I separated out the length function to a new addition to ntheory.

@oscarbenjamin oscarbenjamin added this to the SymPy 1.13 milestone Aug 17, 2023
@oscarbenjamin
Copy link
Collaborator Author

I've added the 1.13 milestone. We definitely need to get this in before the next release and especially before the next release of mpmath (no idea when that will be).

@smichr smichr marked this pull request as ready for review August 21, 2023 18:26
@smichr
Copy link
Member

smichr commented Aug 21, 2023

@oscarbenjamin , maybe check in on the test suite if this fails. There is a test that is timing out and I don't think it's hitting anything that I have changed.

/c

@smichr smichr merged commit 004a33f into sympy:master Aug 22, 2023
@oscarbenjamin oscarbenjamin deleted the pr_mpmath branch October 10, 2023 11:09
@oscarbenjamin oscarbenjamin mentioned this pull request Nov 1, 2023
8 tasks
@oscarbenjamin
Copy link
Collaborator Author

@smichr you made the changes here far too complicated for me to be able to backport to 1.12.1 in gh-25859.

What should have been a relatively simple PR here got turned into a big mess with a lot of changes everywhere including depending on gh-25534.

I'm just going to backport my original commit eef227f.

If there is some problem with just using the commit then what is/was needed is a simple fix rather than a 50 file refactor.

Comment on lines 4550 to 4554
def sympify_mpmath_mpq(x):
p, q = x._mpq_
return Rational(p, q, 1)

_sympy_converter[type(mpmath.rational.mpq(1, 2))] = sympify_mpmath_mpq
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow this got removed from the PR defeating the original purpose of compatibility with mpmath.

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.

5 participants