Skip to content

Commit

Permalink
src/sage/structure/coerce.pyx (is_mpmath_type, py_scalar_parent): Han…
Browse files Browse the repository at this point in the history
…dle mpmath types without Sage backend
  • Loading branch information
Matthias Koeppe committed Jun 1, 2024
1 parent 97ed62f commit e320299
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/sage/structure/coerce.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -143,46 +143,60 @@ 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
return sage.rings.integer_ring.ZZ
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):
"""
Expand Down Expand Up @@ -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::
Expand All @@ -488,7 +502,7 @@ cpdef bint is_mpmath_type(t) noexcept:
True
"""
return isinstance(t, type) and \
strncmp((<PyTypeObject*>t).tp_name, "sage.libs.mpmath.", 17) == 0
t.__module__.startswith("mpmath.")


cdef class CoercionModel:
Expand Down

0 comments on commit e320299

Please sign in to comment.