Coerce maps

class sage.structure.coerce_maps.CCallableConvertMap_class
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()

TESTS:

sage: from sage.structure.coerce_maps import test_CCallableConvertMap
sage: f = test_CCallableConvertMap(QQ, 'test')
sage: f(1/3)
-8/27
_name
_repr_type()

EXAMPLES:

sage: from sage.structure.coerce_maps import test_CCallableConvertMap
sage: test_CCallableConvertMap(ZZ, 'any name')
Conversion via c call 'any name' map:
  From: Integer Ring
  To:   Integer Ring
sage: test_CCallableConvertMap(ZZ, None)  # random address
Conversion via c call at 0xc339000 map:
  From: Integer Ring
  To:   Integer Ring
class sage.structure.coerce_maps.CallableConvertMap
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()

Because self._func may be anything we do a little bit of sanity checking (the return value must be an element with the correct parent).

TESTS:

sage: from sage.structure.coerce_maps import CallableConvertMap
sage: def foo(P, x): return x
sage: f = CallableConvertMap(ZZ, ZZ, foo)
sage: f(0)
0
sage: f = CallableConvertMap(ZZ, QQ, foo)
sage: f(0)
...
RuntimeError: BUG in coercion model: <function foo at ...> returned element with wrong parent (expected Rational Field got Integer Ring)
sage: f(None)
...
RuntimeError: BUG in coercion model: <function foo at ...> returned None
_call_with_args()

TESTS:

sage: from sage.structure.coerce_maps import CallableConvertMap
sage: def foo(P, x, y): return x or y
sage: f = CallableConvertMap(ZZ, ZZ, foo)
sage: f(0, 3)
3
sage: f = CallableConvertMap(ZZ, QQ, foo)
sage: f(0, 3)
...
RuntimeError: BUG in coercion model: <function foo at ...> returned element with wrong parent (expected Rational Field got Integer Ring)
sage: f(None, None)
...
RuntimeError: BUG in coercion model: <function foo at ...> returned None
class sage.structure.coerce_maps.DefaultConvertMap

This morphism simply calls the codomain’s element_constructor method, passing in the codomain as the first argument.

__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()
_call_with_args()
_force_use
_is_coercion
class sage.structure.coerce_maps.DefaultConvertMap_unique

This morphism simply defers action to the codomain’s element_constructor method, WITHOUT passing in the codomain as the first argument.

This is used for creating elements that don’t take a parent as the first argument to their __init__ method, for example, Integers, Rationals, Algebraic Reals... all have a unique parent. It is also used when the element_constructor is a bound method (whose self argument is assumed to be bound to the codomain).

static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()
_call_with_args()
class sage.structure.coerce_maps.ListMorphism
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()
_call_with_args()
class sage.structure.coerce_maps.NamedConvertMap

This is used for creating a elements via the _xxx_ methods.

For example, many elements implement an _integer_ method to convert to ZZ, or a _rational_ method to convert to QQ.

__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()

EXAMPLES:

sage: from sage.structure.coerce_maps import NamedConvertMap
sage: f = NamedConvertMap(GF(5), QQ, '_integer_'); f
Conversion via _integer_ method map:
  From: Finite Field of size 5
  To:   Rational Field
sage: f(19)
4
sage: f(19).parent()
Rational Field
_call_with_args()

EXAMPLES:

sage: from sage.structure.coerce_maps import NamedConvertMap
sage: f = NamedConvertMap(SR, ZZ['x'], '_polynomial_')
sage: f(x^2+1, check=True)
x^2 + 1
_force_use
method_name
class sage.structure.coerce_maps.TryMap
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_call_()

EXAMPLES:

sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x: 1/x)
sage: map2 = QQ.coerce_map_from(ZZ)
sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
sage: map(3)
1/3
sage: map(-7)
-1/7
sage: map(0)
0
_call_with_args()

EXAMPLES:

sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x, y:  y/x)
sage: map2 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x, y: 23/1)
sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
sage: map._call_with_args(3, (2,))
2/3
sage: map._call_with_args(-7, (5,))
-5/7
sage: map._call_with_args(0, (1,))
23
sage.structure.coerce_maps._ccall_test_function()

For testing CCallableConvertMap_class. Returns x*x*x-x in the codomain.

TESTS:

sage: from sage.structure.coerce_maps import _ccall_test_function
sage: _ccall_test_function(ZZ, 1)
0
sage: _ccall_test_function(ZZ, 2)
6
sage: _ccall_test_function(ZZ, -3)
-24
sage.structure.coerce_maps.test_CCallableConvertMap()

For testing CCallableConvertMap_class.

TESTS:

sage: from sage.structure.coerce_maps import test_CCallableConvertMap
sage: f = test_CCallableConvertMap(ZZ, 'test'); f
Conversion via c call 'test' map:
  From: Integer Ring
  To:   Integer Ring
sage: f(3)
24
sage: f(9)
720

Previous topic

Coerce actions

Next topic

Miscellaneous

This Page