TESTS:
sage: from sage.structure.coerce_maps import test_CCallableConvertMap
sage: f = test_CCallableConvertMap(QQ, 'test')
sage: f(1/3)
-8/27
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
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
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
This morphism simply calls the codomain’s element_constructor method, passing in the codomain as the first argument.
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).
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.
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
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
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
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
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
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