Skip to content

Commit

Permalink
Restoring lru (#896)
Browse files Browse the repository at this point in the history
I did this before, but for some reason it wasn´t merged...
  • Loading branch information
mmatera authored Aug 2, 2023
1 parent eaace9f commit eb30221
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions mathics/core/convert/mpmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@
from mathics.core.systemsymbols import SymbolIndeterminate


# Another issue with the lru_cache: for mpmath, mpmath.mpc(0.,0.) and
# mpmath.mpf(0.) produces the same hash. As a result, depending on
# what is called first, the output of this function is different.
#
# note mmatera: When I start to pass the private doctests
# to pytests, I realize that WMA evaluates `0. I` to `Complex[0.,0.]`
# instead of `0.`. To fix this incompatibility, I removed from
# `from_sympy` the lines that convert `mpc(0.,0.)` to `mpf(0.0)`,
# and then this issue becomes evident.
#
# As we decide by now that performance comes after ensuring the compatibility
# and clarity of the code, I propose here to conserve the right tests,
# and commented out the cache and the lines that convert mpc(0,0) to MachineReal(0).
#
# @lru_cache(maxsize=1024)
@lru_cache(maxsize=1024, typed=True)
def from_mpmath(
value: Union[mpmath.mpf, mpmath.mpc],
precision: Optional[int] = None,
Expand All @@ -56,6 +42,13 @@ def from_mpmath(
# HACK: use str here to prevent loss of precision
return PrecisionReal(sympy.Float(str(value), precision=precision - 1))
elif isinstance(value, mpmath.mpc):
# Comment mmatera:
# In Python, and mpmath, `0.j` and `0.` are equivalent, in the sense
# that are considered equal numbers, and have the same associated
# hash.
# In WMA, this is not the case. To produce the
# Python's behavior, uncomment the following lines:
#
# if value.imag == 0.0:
# return from_mpmath(value.real, precision=precision)
val_re, val_im = value.real, value.imag
Expand Down

0 comments on commit eb30221

Please sign in to comment.