diff --git a/mathics/core/convert/mpmath.py b/mathics/core/convert/mpmath.py index b396636c0..cf4895667 100644 --- a/mathics/core/convert/mpmath.py +++ b/mathics/core/convert/mpmath.py @@ -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, @@ -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