From e320299c7e507eed242b43c98f92e6a6991fde6b Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 30 May 2024 14:25:38 -0700 Subject: [PATCH] src/sage/structure/coerce.pyx (is_mpmath_type, py_scalar_parent): Handle mpmath types without Sage backend --- src/sage/structure/coerce.pyx | 44 +++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/sage/structure/coerce.pyx b/src/sage/structure/coerce.pyx index 26b9ee77cd7..9a6645421da 100644 --- a/src/sage/structure/coerce.pyx +++ b/src/sage/structure/coerce.pyx @@ -143,6 +143,13 @@ cpdef py_scalar_parent(py_type): Real Double Field sage: py_scalar_parent(gmpy2.mpc) # needs sage.rings.complex_double Complex Double Field + + sage: # needs mpmath + sage: import mpmath + sage: py_scalar_parent(mpmath.mpf) + Real Double Field + sage: py_scalar_parent(mpmath.mpc) # needs sage.rings.complex_double + Complex Double Field """ if issubclass(py_type, int): import sage.rings.integer_ring @@ -150,39 +157,46 @@ cpdef py_scalar_parent(py_type): if py_type is FractionType: import sage.rings.rational_field return sage.rings.rational_field.QQ - elif issubclass(py_type, float): + if issubclass(py_type, float): import sage.rings.real_double return sage.rings.real_double.RDF - elif issubclass(py_type, complex): + if issubclass(py_type, complex): import sage.rings.complex_double return sage.rings.complex_double.CDF - elif is_numpy_type(py_type): + if is_numpy_type(py_type): import numpy if issubclass(py_type, numpy.integer): import sage.rings.integer_ring return sage.rings.integer_ring.ZZ - elif issubclass(py_type, numpy.floating): + if issubclass(py_type, numpy.floating): import sage.rings.real_double return sage.rings.real_double.RDF - elif issubclass(py_type, numpy.complexfloating): + if issubclass(py_type, numpy.complexfloating): import sage.rings.complex_double return sage.rings.complex_double.CDF - else: - return None - elif issubclass(py_type, gmpy2.mpz): + return None + if issubclass(py_type, gmpy2.mpz): import sage.rings.integer_ring return sage.rings.integer_ring.ZZ - elif issubclass(py_type, gmpy2.mpq): + if issubclass(py_type, gmpy2.mpq): import sage.rings.rational_field return sage.rings.rational_field.QQ - elif issubclass(py_type, gmpy2.mpfr): + if issubclass(py_type, gmpy2.mpfr): import sage.rings.real_double return sage.rings.real_double.RDF - elif issubclass(py_type, gmpy2.mpc): + if issubclass(py_type, gmpy2.mpc): import sage.rings.complex_double return sage.rings.complex_double.CDF - else: + if is_mpmath_type(py_type): + import mpmath + if issubclass(py_type, mpmath.mpf): + from sage.rings.real_double import RDF + return RDF + if issubclass(py_type, mpmath.mpc): + from sage.rings.complex_double import CDF + return CDF return None + return None cpdef py_scalar_to_element(x): """ @@ -468,10 +482,10 @@ cpdef bint is_numpy_type(t) noexcept: return True return False + cpdef bint is_mpmath_type(t) noexcept: r""" - Check whether the type ``t`` is a type whose name starts with either - ``mpmath.`` or ``sage.libs.mpmath.``. + Check whether the type ``t`` is a type whose name starts with ``mpmath.`` EXAMPLES:: @@ -488,7 +502,7 @@ cpdef bint is_mpmath_type(t) noexcept: True """ return isinstance(t, type) and \ - strncmp((t).tp_name, "sage.libs.mpmath.", 17) == 0 + t.__module__.startswith("mpmath.") cdef class CoercionModel: