passagemath-objects 10.8.1a3__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (283) hide show
  1. passagemath_objects/__init__.py +3 -0
  2. passagemath_objects-10.8.1a3.dist-info/DELVEWHEEL +2 -0
  3. passagemath_objects-10.8.1a3.dist-info/METADATA +114 -0
  4. passagemath_objects-10.8.1a3.dist-info/RECORD +283 -0
  5. passagemath_objects-10.8.1a3.dist-info/WHEEL +5 -0
  6. passagemath_objects-10.8.1a3.dist-info/top_level.txt +3 -0
  7. passagemath_objects.libs/libgmp-10-60021eeab4282b29024e43b2b1412b53.dll +0 -0
  8. sage/all__sagemath_objects.py +46 -0
  9. sage/arith/all__sagemath_objects.py +5 -0
  10. sage/arith/long.pxd +411 -0
  11. sage/arith/numerical_approx.cp314-win_amd64.pyd +0 -0
  12. sage/arith/numerical_approx.pxd +35 -0
  13. sage/arith/numerical_approx.pyx +75 -0
  14. sage/arith/power.cp314-win_amd64.pyd +0 -0
  15. sage/arith/power.pxd +31 -0
  16. sage/arith/power.pyx +127 -0
  17. sage/categories/action.cp314-win_amd64.pyd +0 -0
  18. sage/categories/action.pxd +29 -0
  19. sage/categories/action.pyx +641 -0
  20. sage/categories/algebra_functor.py +745 -0
  21. sage/categories/all__sagemath_objects.py +33 -0
  22. sage/categories/basic.py +71 -0
  23. sage/categories/cartesian_product.py +292 -0
  24. sage/categories/category.py +3379 -0
  25. sage/categories/category_cy_helper.cp314-win_amd64.pyd +0 -0
  26. sage/categories/category_cy_helper.pxd +8 -0
  27. sage/categories/category_cy_helper.pyx +322 -0
  28. sage/categories/category_singleton.cp314-win_amd64.pyd +0 -0
  29. sage/categories/category_singleton.pxd +3 -0
  30. sage/categories/category_singleton.pyx +343 -0
  31. sage/categories/category_types.py +637 -0
  32. sage/categories/category_with_axiom.py +2889 -0
  33. sage/categories/covariant_functorial_construction.py +700 -0
  34. sage/categories/facade_sets.py +228 -0
  35. sage/categories/functor.cp314-win_amd64.pyd +0 -0
  36. sage/categories/functor.pxd +7 -0
  37. sage/categories/functor.pyx +659 -0
  38. sage/categories/homset.py +1289 -0
  39. sage/categories/homsets.py +364 -0
  40. sage/categories/isomorphic_objects.py +73 -0
  41. sage/categories/map.cp314-win_amd64.pyd +0 -0
  42. sage/categories/map.pxd +34 -0
  43. sage/categories/map.pyx +2106 -0
  44. sage/categories/morphism.cp314-win_amd64.pyd +0 -0
  45. sage/categories/morphism.pxd +14 -0
  46. sage/categories/morphism.pyx +895 -0
  47. sage/categories/objects.py +167 -0
  48. sage/categories/primer.py +1695 -0
  49. sage/categories/pushout.py +4847 -0
  50. sage/categories/quotients.py +64 -0
  51. sage/categories/realizations.py +200 -0
  52. sage/categories/sets_cat.py +3305 -0
  53. sage/categories/sets_with_partial_maps.py +52 -0
  54. sage/categories/subobjects.py +64 -0
  55. sage/categories/subquotients.py +21 -0
  56. sage/categories/with_realizations.py +311 -0
  57. sage/cpython/__init__.py +22 -0
  58. sage/cpython/_py2_random.py +619 -0
  59. sage/cpython/all.py +3 -0
  60. sage/cpython/atexit.cp314-win_amd64.pyd +0 -0
  61. sage/cpython/atexit.pyx +269 -0
  62. sage/cpython/builtin_types.cp314-win_amd64.pyd +0 -0
  63. sage/cpython/builtin_types.pyx +7 -0
  64. sage/cpython/cython_metaclass.cp314-win_amd64.pyd +0 -0
  65. sage/cpython/cython_metaclass.h +117 -0
  66. sage/cpython/cython_metaclass.pxd +3 -0
  67. sage/cpython/cython_metaclass.pyx +130 -0
  68. sage/cpython/debug.cp314-win_amd64.pyd +0 -0
  69. sage/cpython/debug.pyx +302 -0
  70. sage/cpython/dict_del_by_value.cp314-win_amd64.pyd +0 -0
  71. sage/cpython/dict_del_by_value.pxd +9 -0
  72. sage/cpython/dict_del_by_value.pyx +191 -0
  73. sage/cpython/dict_internal.h +80 -0
  74. sage/cpython/getattr.cp314-win_amd64.pyd +0 -0
  75. sage/cpython/getattr.pxd +9 -0
  76. sage/cpython/getattr.pyx +439 -0
  77. sage/cpython/pycore_long.h +97 -0
  78. sage/cpython/pycore_long.pxd +10 -0
  79. sage/cpython/python_debug.h +44 -0
  80. sage/cpython/python_debug.pxd +47 -0
  81. sage/cpython/pyx_visit.h +13 -0
  82. sage/cpython/string.cp314-win_amd64.pyd +0 -0
  83. sage/cpython/string.pxd +76 -0
  84. sage/cpython/string.pyx +34 -0
  85. sage/cpython/string_impl.h +60 -0
  86. sage/cpython/type.cp314-win_amd64.pyd +0 -0
  87. sage/cpython/type.pxd +2 -0
  88. sage/cpython/type.pyx +40 -0
  89. sage/cpython/wrapperdescr.pxd +67 -0
  90. sage/ext/all__sagemath_objects.py +3 -0
  91. sage/ext/ccobject.h +64 -0
  92. sage/ext/cplusplus.pxd +17 -0
  93. sage/ext/mod_int.h +30 -0
  94. sage/ext/mod_int.pxd +24 -0
  95. sage/ext/stdsage.pxd +39 -0
  96. sage/groups/all__sagemath_objects.py +1 -0
  97. sage/groups/group.cp314-win_amd64.pyd +0 -0
  98. sage/groups/group.pxd +14 -0
  99. sage/groups/group.pyx +296 -0
  100. sage/groups/old.cp314-win_amd64.pyd +0 -0
  101. sage/groups/old.pxd +14 -0
  102. sage/groups/old.pyx +219 -0
  103. sage/libs/all__sagemath_objects.py +3 -0
  104. sage/libs/gmp/__init__.py +1 -0
  105. sage/libs/gmp/all.pxd +6 -0
  106. sage/libs/gmp/binop.pxd +23 -0
  107. sage/libs/gmp/misc.pxd +8 -0
  108. sage/libs/gmp/mpf.pxd +88 -0
  109. sage/libs/gmp/mpn.pxd +57 -0
  110. sage/libs/gmp/mpq.pxd +57 -0
  111. sage/libs/gmp/mpz.pxd +202 -0
  112. sage/libs/gmp/pylong.cp314-win_amd64.pyd +0 -0
  113. sage/libs/gmp/pylong.pxd +12 -0
  114. sage/libs/gmp/pylong.pyx +150 -0
  115. sage/libs/gmp/random.pxd +25 -0
  116. sage/libs/gmp/randomize.pxd +59 -0
  117. sage/libs/gmp/types.pxd +53 -0
  118. sage/libs/gmpxx.pxd +19 -0
  119. sage/misc/abstract_method.py +276 -0
  120. sage/misc/all__sagemath_objects.py +43 -0
  121. sage/misc/bindable_class.py +253 -0
  122. sage/misc/c3_controlled.cp314-win_amd64.pyd +0 -0
  123. sage/misc/c3_controlled.pxd +2 -0
  124. sage/misc/c3_controlled.pyx +1402 -0
  125. sage/misc/cachefunc.cp314-win_amd64.pyd +0 -0
  126. sage/misc/cachefunc.pxd +43 -0
  127. sage/misc/cachefunc.pyx +3801 -0
  128. sage/misc/call.py +188 -0
  129. sage/misc/classcall_metaclass.cp314-win_amd64.pyd +0 -0
  130. sage/misc/classcall_metaclass.pxd +14 -0
  131. sage/misc/classcall_metaclass.pyx +599 -0
  132. sage/misc/constant_function.cp314-win_amd64.pyd +0 -0
  133. sage/misc/constant_function.pyx +130 -0
  134. sage/misc/decorators.py +739 -0
  135. sage/misc/fast_methods.cp314-win_amd64.pyd +0 -0
  136. sage/misc/fast_methods.pxd +20 -0
  137. sage/misc/fast_methods.pyx +351 -0
  138. sage/misc/flatten.py +90 -0
  139. sage/misc/fpickle.cp314-win_amd64.pyd +0 -0
  140. sage/misc/fpickle.pyx +176 -0
  141. sage/misc/function_mangling.cp314-win_amd64.pyd +0 -0
  142. sage/misc/function_mangling.pxd +11 -0
  143. sage/misc/function_mangling.pyx +308 -0
  144. sage/misc/inherit_comparison.cp314-win_amd64.pyd +0 -0
  145. sage/misc/inherit_comparison.pxd +5 -0
  146. sage/misc/inherit_comparison.pyx +105 -0
  147. sage/misc/instancedoc.cp314-win_amd64.pyd +0 -0
  148. sage/misc/instancedoc.pyx +331 -0
  149. sage/misc/lazy_attribute.cp314-win_amd64.pyd +0 -0
  150. sage/misc/lazy_attribute.pyx +607 -0
  151. sage/misc/lazy_format.py +132 -0
  152. sage/misc/lazy_import.cp314-win_amd64.pyd +0 -0
  153. sage/misc/lazy_import.pxd +13 -0
  154. sage/misc/lazy_import.pyx +1307 -0
  155. sage/misc/lazy_import_cache.py +36 -0
  156. sage/misc/lazy_list.cp314-win_amd64.pyd +0 -0
  157. sage/misc/lazy_list.pxd +19 -0
  158. sage/misc/lazy_list.pyx +1187 -0
  159. sage/misc/lazy_string.cp314-win_amd64.pyd +0 -0
  160. sage/misc/lazy_string.pxd +7 -0
  161. sage/misc/lazy_string.pyx +546 -0
  162. sage/misc/misc.py +980 -0
  163. sage/misc/misc_c.cp314-win_amd64.pyd +0 -0
  164. sage/misc/misc_c.pxd +3 -0
  165. sage/misc/misc_c.pyx +765 -0
  166. sage/misc/namespace_package.py +37 -0
  167. sage/misc/nested_class.cp314-win_amd64.pyd +0 -0
  168. sage/misc/nested_class.pxd +3 -0
  169. sage/misc/nested_class.pyx +394 -0
  170. sage/misc/persist.cp314-win_amd64.pyd +0 -0
  171. sage/misc/persist.pyx +1279 -0
  172. sage/misc/prandom.py +418 -0
  173. sage/misc/randstate.cp314-win_amd64.pyd +0 -0
  174. sage/misc/randstate.pxd +31 -0
  175. sage/misc/randstate.pyx +1096 -0
  176. sage/misc/repr.py +203 -0
  177. sage/misc/reset.cp314-win_amd64.pyd +0 -0
  178. sage/misc/reset.pyx +196 -0
  179. sage/misc/sage_ostools.cp314-win_amd64.pyd +0 -0
  180. sage/misc/sage_ostools.pyx +323 -0
  181. sage/misc/sage_timeit.py +275 -0
  182. sage/misc/sage_timeit_class.cp314-win_amd64.pyd +0 -0
  183. sage/misc/sage_timeit_class.pyx +120 -0
  184. sage/misc/sage_unittest.py +639 -0
  185. sage/misc/sageinspect.py +2792 -0
  186. sage/misc/session.cp314-win_amd64.pyd +0 -0
  187. sage/misc/session.pyx +392 -0
  188. sage/misc/superseded.py +576 -0
  189. sage/misc/test_nested_class.py +228 -0
  190. sage/misc/timing.py +264 -0
  191. sage/misc/unknown.py +222 -0
  192. sage/misc/verbose.py +253 -0
  193. sage/misc/weak_dict.cp314-win_amd64.pyd +0 -0
  194. sage/misc/weak_dict.pxd +15 -0
  195. sage/misc/weak_dict.pyx +1197 -0
  196. sage/modules/all__sagemath_objects.py +1 -0
  197. sage/modules/module.cp314-win_amd64.pyd +0 -0
  198. sage/modules/module.pxd +5 -0
  199. sage/modules/module.pyx +329 -0
  200. sage/rings/all__sagemath_objects.py +3 -0
  201. sage/rings/integer_fake.h +22 -0
  202. sage/rings/integer_fake.pxd +55 -0
  203. sage/rings/integer_fake.pyi +8 -0
  204. sage/sets/all__sagemath_objects.py +3 -0
  205. sage/sets/pythonclass.cp314-win_amd64.pyd +0 -0
  206. sage/sets/pythonclass.pxd +9 -0
  207. sage/sets/pythonclass.pyx +247 -0
  208. sage/structure/__init__.py +13 -0
  209. sage/structure/all.py +30 -0
  210. sage/structure/category_object.cp314-win_amd64.pyd +0 -0
  211. sage/structure/category_object.pxd +28 -0
  212. sage/structure/category_object.pyx +1090 -0
  213. sage/structure/coerce.cp314-win_amd64.pyd +0 -0
  214. sage/structure/coerce.pxd +44 -0
  215. sage/structure/coerce.pyx +2113 -0
  216. sage/structure/coerce_actions.cp314-win_amd64.pyd +0 -0
  217. sage/structure/coerce_actions.pxd +27 -0
  218. sage/structure/coerce_actions.pyx +988 -0
  219. sage/structure/coerce_dict.cp314-win_amd64.pyd +0 -0
  220. sage/structure/coerce_dict.pxd +51 -0
  221. sage/structure/coerce_dict.pyx +1557 -0
  222. sage/structure/coerce_exceptions.py +23 -0
  223. sage/structure/coerce_maps.cp314-win_amd64.pyd +0 -0
  224. sage/structure/coerce_maps.pxd +24 -0
  225. sage/structure/coerce_maps.pyx +656 -0
  226. sage/structure/debug_options.cp314-win_amd64.pyd +0 -0
  227. sage/structure/debug_options.pxd +6 -0
  228. sage/structure/debug_options.pyx +54 -0
  229. sage/structure/dynamic_class.py +541 -0
  230. sage/structure/element.cp314-win_amd64.pyd +0 -0
  231. sage/structure/element.pxd +271 -0
  232. sage/structure/element.pyx +4584 -0
  233. sage/structure/element_wrapper.cp314-win_amd64.pyd +0 -0
  234. sage/structure/element_wrapper.pxd +12 -0
  235. sage/structure/element_wrapper.pyx +582 -0
  236. sage/structure/factorization.py +1457 -0
  237. sage/structure/factorization_integer.py +154 -0
  238. sage/structure/factory.cp314-win_amd64.pyd +0 -0
  239. sage/structure/factory.pyx +863 -0
  240. sage/structure/formal_sum.py +489 -0
  241. sage/structure/gens_py.py +73 -0
  242. sage/structure/global_options.py +1725 -0
  243. sage/structure/indexed_generators.py +863 -0
  244. sage/structure/list_clone.cp314-win_amd64.pyd +0 -0
  245. sage/structure/list_clone.pxd +65 -0
  246. sage/structure/list_clone.pyx +1867 -0
  247. sage/structure/list_clone_demo.cp314-win_amd64.pyd +0 -0
  248. sage/structure/list_clone_demo.pyx +248 -0
  249. sage/structure/list_clone_timings.py +179 -0
  250. sage/structure/list_clone_timings_cy.cp314-win_amd64.pyd +0 -0
  251. sage/structure/list_clone_timings_cy.pyx +86 -0
  252. sage/structure/mutability.cp314-win_amd64.pyd +0 -0
  253. sage/structure/mutability.pxd +21 -0
  254. sage/structure/mutability.pyx +346 -0
  255. sage/structure/nonexact.py +69 -0
  256. sage/structure/parent.cp314-win_amd64.pyd +0 -0
  257. sage/structure/parent.pxd +112 -0
  258. sage/structure/parent.pyx +3087 -0
  259. sage/structure/parent_base.cp314-win_amd64.pyd +0 -0
  260. sage/structure/parent_base.pxd +13 -0
  261. sage/structure/parent_base.pyx +35 -0
  262. sage/structure/parent_gens.cp314-win_amd64.pyd +0 -0
  263. sage/structure/parent_gens.pxd +22 -0
  264. sage/structure/parent_gens.pyx +374 -0
  265. sage/structure/parent_old.cp314-win_amd64.pyd +0 -0
  266. sage/structure/parent_old.pxd +24 -0
  267. sage/structure/parent_old.pyx +278 -0
  268. sage/structure/proof/__init__.py +1 -0
  269. sage/structure/proof/all.py +243 -0
  270. sage/structure/proof/proof.py +300 -0
  271. sage/structure/richcmp.cp314-win_amd64.pyd +0 -0
  272. sage/structure/richcmp.pxd +212 -0
  273. sage/structure/richcmp.pyx +494 -0
  274. sage/structure/sage_object.cp314-win_amd64.pyd +0 -0
  275. sage/structure/sage_object.pxd +3 -0
  276. sage/structure/sage_object.pyx +1088 -0
  277. sage/structure/sage_object_test.py +19 -0
  278. sage/structure/sequence.py +937 -0
  279. sage/structure/set_factories.py +1178 -0
  280. sage/structure/set_factories_example.py +527 -0
  281. sage/structure/support_view.py +164 -0
  282. sage/structure/test_factory.py +56 -0
  283. sage/structure/unique_representation.py +1443 -0
@@ -0,0 +1,23 @@
1
+ # sage_setup: distribution = sagemath-objects
2
+ "Exceptions raised by the coercion model"
3
+
4
+ ###############################################################################
5
+ # Sage: Open Source Mathematical Software
6
+ # Copyright (C) 2009 Robert Bradshaw <robertwb@math.washington.edu>
7
+ # Distributed under the terms of the GNU General Public License (GPL)
8
+ # The full text of the GPL is available at:
9
+ # https://www.gnu.org/licenses/
10
+ ###############################################################################
11
+
12
+
13
+ class CoercionException(TypeError):
14
+ """
15
+ This is the baseclass of exceptions that the coercion model raises
16
+ when trying to discover coercions. We do not use standard Python
17
+ exceptions to avoid inadvertently catching and suppressing real errors.
18
+
19
+ Usually one raises this to indicate the attempted action is not
20
+ implemented/appropriate, but if there are other things to try not
21
+ to immediately abort to the user.
22
+ """
23
+ pass
@@ -0,0 +1,24 @@
1
+ # sage_setup: distribution = sagemath-objects
2
+ from sage.categories.map cimport Map
3
+
4
+
5
+ cdef class DefaultConvertMap_unique(Map):
6
+ pass
7
+
8
+
9
+ cdef class NamedConvertMap(Map):
10
+ cdef readonly method_name
11
+
12
+
13
+ cdef class TryMap(Map):
14
+ cdef Map _map_p
15
+ cdef Map _map_b
16
+ cdef _error_types
17
+
18
+
19
+ cdef class CallableConvertMap(Map):
20
+ cdef bint _parent_as_first_arg
21
+ cdef _func
22
+
23
+
24
+ cdef Map CCallableConvertMap(domain, codomain, void* func, name)
@@ -0,0 +1,656 @@
1
+ # sage_setup: distribution = sagemath-objects
2
+ """
3
+ Coerce maps
4
+ """
5
+ import types
6
+
7
+ from sage.structure.parent cimport Parent
8
+ from sage.structure.element cimport Element
9
+ from sage.sets.pythonclass cimport Set_PythonType
10
+
11
+ cdef object BuiltinMethodType = type(repr)
12
+
13
+ # COERCE TODO: remove or integrate better (as this bit is only checked on an error)
14
+ cdef bint print_warnings = 0
15
+
16
+
17
+ cdef class DefaultConvertMap_unique(Map):
18
+ """
19
+ This morphism simply defers action to the codomain's
20
+ element_constructor method, WITHOUT passing in the codomain as the
21
+ first argument.
22
+
23
+ This is used for creating elements that don't take a parent as the
24
+ first argument to their __init__ method, for example, Integers,
25
+ Rationals, Algebraic Reals... all have a unique parent. It is also
26
+ used when the element_constructor is a bound method (whose self
27
+ argument is assumed to be bound to the codomain).
28
+ """
29
+ def __init__(self, domain, codomain, category=None) -> None:
30
+ """
31
+ TESTS:
32
+
33
+ Maps of this type are morphisms in the category of sets with
34
+ partial maps (see :issue:`15618`)::
35
+
36
+ sage: f = GF(11).convert_map_from(GF(7)); f # needs sage.rings.finite_rings
37
+ Conversion map:
38
+ From: Finite Field of size 7
39
+ To: Finite Field of size 11
40
+ sage: f.parent() # needs sage.rings.finite_rings
41
+ Set of Morphisms
42
+ from Finite Field of size 7
43
+ to Finite Field of size 11
44
+ in Category of sets with partial maps
45
+
46
+ Test that :issue:`23211` is resolved::
47
+
48
+ sage: f._is_coercion # needs sage.rings.finite_rings
49
+ False
50
+ sage: QQ[['x']].coerce_map_from(QQ)._is_coercion
51
+ True
52
+ """
53
+ if not isinstance(domain, Parent):
54
+ domain = Set_PythonType(domain)
55
+ if category is None:
56
+ from sage.categories.sets_with_partial_maps import SetsWithPartialMaps
57
+ category = SetsWithPartialMaps()
58
+ parent = domain.Hom(codomain, category=category)
59
+ Map.__init__(self, parent)
60
+ self._coerce_cost = 100
61
+ if (<Parent>codomain)._element_constructor is None:
62
+ raise RuntimeError(f"BUG in coercion model, no element constructor for {type(codomain)}")
63
+
64
+ def _repr_type(self) -> str:
65
+ r"""
66
+ Return a printable type for this morphism.
67
+
68
+ EXAMPLES::
69
+
70
+ sage: f = GF(11).convert_map_from(GF(7)) # needs sage.rings.finite_rings
71
+ sage: f._repr_type() # needs sage.rings.finite_rings
72
+ 'Conversion'
73
+ """
74
+ return self._repr_type_str or ("Coercion" if self._is_coercion else "Conversion")
75
+
76
+ cpdef Element _call_(self, x):
77
+ """
78
+ Create an element of the codomain from a single element of the domain.
79
+
80
+ EXAMPLES::
81
+
82
+ sage: f = QQ[['x']].coerce_map_from(QQ)
83
+ sage: f(2/3).parent()
84
+ Power Series Ring in x over Rational Field
85
+ """
86
+ cdef Parent C = self._codomain
87
+ try:
88
+ return C._element_constructor(x)
89
+ except Exception:
90
+ if print_warnings:
91
+ print(type(C), C)
92
+ print(type(C._element_constructor), C._element_constructor)
93
+ raise
94
+
95
+ cpdef Element _call_with_args(self, x, args=(), kwds={}):
96
+ """
97
+ Create an element of the codomain from an element of the domain, with extra arguments.
98
+
99
+ EXAMPLES::
100
+
101
+ sage: f = QQ[['x']].coerce_map_from(QQ)
102
+ sage: f(2/3, 4)
103
+ 2/3 + O(x^4)
104
+ """
105
+ cdef Parent C = self._codomain
106
+ try:
107
+ if len(args) == 0:
108
+ if len(kwds) == 0:
109
+ return C._element_constructor(x)
110
+ else:
111
+ return C._element_constructor(x, **kwds)
112
+ else:
113
+ if len(kwds) == 0:
114
+ return C._element_constructor(x, *args)
115
+ else:
116
+ return C._element_constructor(x, *args, **kwds)
117
+ except Exception:
118
+ if print_warnings:
119
+ print(type(C), C)
120
+ print(type(C._element_constructor), C._element_constructor)
121
+ raise
122
+
123
+
124
+ cdef class NamedConvertMap(Map):
125
+ """
126
+ This is used for creating elements via the _xxx_ methods.
127
+
128
+ For example, many elements implement an _integer_ method to
129
+ convert to ZZ, or a _rational_ method to convert to QQ.
130
+ """
131
+
132
+ def __init__(self, domain, codomain, method_name):
133
+ """
134
+ EXAMPLES::
135
+
136
+ sage: # needs sage.symbolic
137
+ sage: from sage.structure.coerce_maps import NamedConvertMap
138
+ sage: var('t')
139
+ t
140
+ sage: mor = NamedConvertMap(SR, QQ['t'], '_polynomial_')
141
+ sage: mor(t^2/4 + 1)
142
+ 1/4*t^2 + 1
143
+ sage: mor = NamedConvertMap(SR, GF(7)[['t']], '_polynomial_')
144
+ sage: mor(t^2/4 + 1)
145
+ 1 + 2*t^2
146
+ """
147
+ if isinstance(domain, type):
148
+ domain = Set_PythonType(domain)
149
+ Map.__init__(self, domain, codomain)
150
+ self._coerce_cost = 400
151
+ self.method_name = method_name
152
+ self._repr_type_str = "Conversion via %s method" % self.method_name
153
+
154
+ cdef dict _extra_slots(self):
155
+ """
156
+ Helper for copying and pickling.
157
+
158
+ EXAMPLES::
159
+
160
+ sage: # needs sage.symbolic
161
+ sage: from sage.structure.coerce_maps import NamedConvertMap
162
+ sage: var('t')
163
+ t
164
+ sage: phi = NamedConvertMap(SR, QQ['t'], '_polynomial_')
165
+ sage: psi = copy(phi) # indirect doctest
166
+ sage: psi
167
+ Conversion via _polynomial_ method map:
168
+ From: Symbolic Ring
169
+ To: Univariate Polynomial Ring in t over Rational Field
170
+ sage: phi == psi # not implemented
171
+ True
172
+ sage: psi(t^2/4 + 1)
173
+ 1/4*t^2 + 1
174
+ sage: psi(t^2/4 + 1) == phi(t^2/4 + 1)
175
+ True
176
+ """
177
+ slots = Map._extra_slots(self)
178
+ slots['method_name'] = self.method_name
179
+ return slots
180
+
181
+ cdef _update_slots(self, dict _slots):
182
+ """
183
+ Helper for copying and pickling.
184
+
185
+ EXAMPLES::
186
+
187
+ sage: # needs sage.symbolic
188
+ sage: from sage.structure.coerce_maps import NamedConvertMap
189
+ sage: var('t')
190
+ t
191
+ sage: phi = NamedConvertMap(SR, QQ['t'], '_polynomial_')
192
+ sage: psi = copy(phi) # indirect doctest
193
+ sage: psi
194
+ Conversion via _polynomial_ method map:
195
+ From: Symbolic Ring
196
+ To: Univariate Polynomial Ring in t over Rational Field
197
+ sage: phi == psi # not implemented
198
+ True
199
+ sage: psi(t^2/4 + 1)
200
+ 1/4*t^2 + 1
201
+ sage: psi(t^2/4 + 1) == phi(t^2/4 + 1)
202
+ True
203
+ """
204
+ self.method_name = _slots['method_name']
205
+ Map._update_slots(self, _slots)
206
+
207
+ cpdef Element _call_(self, x):
208
+ """
209
+ EXAMPLES::
210
+
211
+ sage: from sage.structure.coerce_maps import NamedConvertMap
212
+ sage: f = NamedConvertMap(GF(5), QQ, '_integer_'); f
213
+ Conversion via _integer_ method map:
214
+ From: Finite Field of size 5
215
+ To: Rational Field
216
+ sage: f(19)
217
+ 4
218
+ sage: f(19).parent()
219
+ Rational Field
220
+ """
221
+ cdef Parent C = self._codomain
222
+ try:
223
+ method = getattr(x, self.method_name)
224
+ except AttributeError:
225
+ if print_warnings:
226
+ print(type(x), x)
227
+ print(type(C), C)
228
+ print(self.method_name)
229
+ raise TypeError(f"cannot coerce {x} to {C}")
230
+ cdef Map m
231
+ cdef Element e = method(C)
232
+ if e is None:
233
+ raise RuntimeError("BUG in coercion model: {} method of {} returned None".format(self.method_name, type(x)))
234
+ if e._parent is not C:
235
+ m = C._internal_convert_map_from(e._parent)
236
+ if m is None or m is self:
237
+ raise TypeError
238
+ e = m._call_(e)
239
+ return e
240
+
241
+ cpdef Element _call_with_args(self, x, args=(), kwds={}):
242
+ """
243
+ EXAMPLES::
244
+
245
+ sage: from sage.structure.coerce_maps import NamedConvertMap
246
+ sage: f = NamedConvertMap(SR, ZZ['x'], '_polynomial_') # needs sage.symbolic
247
+ sage: f(x^2 + 1, check=True) # needs sage.symbolic
248
+ x^2 + 1
249
+ """
250
+ cdef Parent C = self._codomain
251
+ return C._element_constructor(self._call_(x), *args, **kwds)
252
+
253
+
254
+ # Perhaps this could be a method, extracting (<PyMethodDescrObject *>(<object>Parent).coerce_map_from).d_method.ml_meth and/or PyCFunction_GET_FUNCTION(method)
255
+ # and constructing a CCallableConvertMap_class if it is bound to the codomain.
256
+
257
+ cdef class CallableConvertMap(Map):
258
+ def __init__(self, domain, codomain, func, parent_as_first_arg=None):
259
+ r"""
260
+ This lets one easily create maps from any callable object.
261
+
262
+ This is especially useful to create maps from bound methods.
263
+
264
+ EXAMPLES::
265
+
266
+ sage: from sage.structure.coerce_maps import CallableConvertMap
267
+ sage: def foo(P, x): return x/2
268
+ sage: f = CallableConvertMap(ZZ, QQ, foo)
269
+ sage: f(3)
270
+ 3/2
271
+ sage: f
272
+ Conversion via foo map:
273
+ From: Integer Ring
274
+ To: Rational Field
275
+
276
+ Create a homomorphism from `\RR` to `\RR^+` viewed as additive groups.
277
+
278
+ ::
279
+
280
+ sage: # needs sage.symbolic
281
+ sage: f = CallableConvertMap(RR, RR, exp, parent_as_first_arg=False)
282
+ sage: f(0)
283
+ 1.00000000000000
284
+ sage: f(1)
285
+ 2.71828182845905
286
+ sage: f(-3)
287
+ 0.0497870683678639
288
+ """
289
+ if isinstance(domain, type):
290
+ domain = Set_PythonType(domain)
291
+ Map.__init__(self, domain, codomain)
292
+ self._coerce_cost = 100
293
+ self._func = func
294
+ if parent_as_first_arg is None:
295
+ if isinstance(func, types.MethodType):
296
+ # can't easily access self
297
+ parent_as_first_arg = False
298
+ elif isinstance(func, BuiltinMethodType):
299
+ parent_as_first_arg = codomain is func.__self__
300
+ else:
301
+ parent_as_first_arg = True
302
+ self._parent_as_first_arg = parent_as_first_arg
303
+ try:
304
+ self._repr_type_str = "Conversion via %s" % self._func.__name__
305
+ except AttributeError:
306
+ self._repr_type_str = "Conversion via %s" % self._func
307
+
308
+ cdef dict _extra_slots(self):
309
+ """
310
+ Helper for copying and pickling.
311
+
312
+ EXAMPLES::
313
+
314
+ sage: from sage.structure.coerce_maps import CallableConvertMap
315
+ sage: def foo(P, x): return x^2
316
+ sage: f = CallableConvertMap(ZZ, ZZ, foo)
317
+ sage: g = copy(f) # indirect doctest
318
+ sage: f == g # not implemented (todo: implement comparison)
319
+ True
320
+ sage: f(3) == g(3)
321
+ True
322
+ """
323
+ slots = Map._extra_slots(self)
324
+ slots['_func'] = self._func
325
+ slots['_parent_as_first_arg'] = self._parent_as_first_arg
326
+ return slots
327
+
328
+ cdef _update_slots(self, dict _slots):
329
+ """
330
+ Helper for copying and pickling.
331
+
332
+ EXAMPLES::
333
+
334
+ sage: from sage.structure.coerce_maps import CallableConvertMap
335
+ sage: def foo(P, x): return x^2
336
+ sage: f = CallableConvertMap(ZZ, ZZ, foo)
337
+ sage: g = copy(f) # indirect doctest
338
+ sage: f == g # not implemented (todo: implement comparison)
339
+ True
340
+ sage: f(3) == g(3)
341
+ True
342
+ """
343
+ self._func = _slots['_func']
344
+ self._parent_as_first_arg = _slots['_parent_as_first_arg']
345
+ Map._update_slots(self, _slots)
346
+
347
+ cpdef Element _call_(self, x):
348
+ """
349
+ Because self._func may be anything we do a little bit of sanity
350
+ checking (the return value must be an element with the correct parent).
351
+
352
+ TESTS::
353
+
354
+ sage: from sage.structure.coerce_maps import CallableConvertMap
355
+ sage: def foo(P, x): return x
356
+ sage: f = CallableConvertMap(ZZ, ZZ, foo)
357
+ sage: f(0)
358
+ 0
359
+ sage: f = CallableConvertMap(ZZ, QQ, foo)
360
+ sage: f(0)
361
+ Traceback (most recent call last):
362
+ ...
363
+ RuntimeError: BUG in coercion model: <function foo at ...> returned element with wrong parent (expected Rational Field got Integer Ring)
364
+ sage: def foo(P, x): return None
365
+ sage: f = CallableConvertMap(ZZ, QQ, foo)
366
+ sage: f(0)
367
+ Traceback (most recent call last):
368
+ ...
369
+ RuntimeError: BUG in coercion model: <function foo at ...> returned None
370
+ """
371
+ cdef Element y
372
+ cdef Parent C = self._codomain
373
+ try:
374
+ if self._parent_as_first_arg:
375
+ y = self._func(C, x)
376
+ else:
377
+ y = self._func(x)
378
+ except Exception:
379
+ if print_warnings:
380
+ print(self._func)
381
+ print(C)
382
+ raise
383
+ if y is None:
384
+ raise RuntimeError("BUG in coercion model: {} returned None".format(self._func))
385
+ elif y._parent is not C:
386
+ raise RuntimeError("BUG in coercion model: {} returned element with wrong parent (expected {} got {})".format(self._func, C, y._parent))
387
+ return y
388
+
389
+ cpdef Element _call_with_args(self, x, args=(), kwds={}):
390
+ """
391
+ TESTS::
392
+
393
+ sage: from sage.structure.coerce_maps import CallableConvertMap
394
+ sage: def foo(P, x, y): return x or y
395
+ sage: f = CallableConvertMap(ZZ, ZZ, foo)
396
+ sage: f(0, 3)
397
+ 3
398
+ sage: f = CallableConvertMap(ZZ, QQ, foo)
399
+ sage: f(0, 3)
400
+ Traceback (most recent call last):
401
+ ...
402
+ RuntimeError: BUG in coercion model: <function foo at ...> returned element with wrong parent (expected Rational Field got Integer Ring)
403
+ sage: f(None, None)
404
+ Traceback (most recent call last):
405
+ ...
406
+ RuntimeError: BUG in coercion model: <function foo at ...> returned None
407
+ """
408
+ cdef Element y
409
+ try:
410
+ if self._parent_as_first_arg:
411
+ y = self._func(self._codomain, x, *args, **kwds)
412
+ else:
413
+ y = self._func(x, *args, **kwds)
414
+ except Exception:
415
+ if print_warnings:
416
+ print(self._func)
417
+ print(self._codomain)
418
+ raise
419
+ if y is None:
420
+ raise RuntimeError("BUG in coercion model: %s returned None" % (self._func))
421
+ elif y._parent is not self._codomain:
422
+ raise RuntimeError("BUG in coercion model: %s returned element with wrong parent (expected %s got %s)" % (self._func, self._codomain, y._parent))
423
+ return y
424
+
425
+
426
+ cdef class CCallableConvertMap_class(Map):
427
+ cdef Element (*_func)(Parent, object)
428
+ cdef public _name
429
+
430
+ def __init__(self, domain, codomain, name):
431
+ if isinstance(domain, type):
432
+ domain = Set_PythonType(domain)
433
+ Map.__init__(self, domain, codomain)
434
+ self._coerce_cost = 10
435
+ self._name = name
436
+
437
+ cpdef Element _call_(self, x):
438
+ """
439
+ TESTS::
440
+
441
+ sage: from sage.structure.coerce_maps import test_CCallableConvertMap
442
+ sage: f = test_CCallableConvertMap(QQ, 'test')
443
+ sage: f(1/3)
444
+ -8/27
445
+ """
446
+ return self._func(self._codomain, x)
447
+
448
+ def _repr_type(self):
449
+ """
450
+ EXAMPLES::
451
+
452
+ sage: from sage.structure.coerce_maps import test_CCallableConvertMap
453
+ sage: test_CCallableConvertMap(ZZ, 'any name')
454
+ Conversion via c call 'any name' map:
455
+ From: Integer Ring
456
+ To: Integer Ring
457
+ sage: test_CCallableConvertMap(ZZ, None)
458
+ Conversion via c call at 0x... map:
459
+ From: Integer Ring
460
+ To: Integer Ring
461
+ """
462
+ if self._name is None:
463
+ return "Conversion via c call at 0x%x" % <long>self._func
464
+ else:
465
+ return "Conversion via c call '%s'" % self._name
466
+
467
+
468
+ cdef Map CCallableConvertMap(domain, codomain, void* func, name):
469
+ """
470
+ Use this to create a map from domain to codomain by calling func
471
+ (which must be a function pointer taking a Parent and object, and
472
+ returning an Element in the given Parent).
473
+
474
+ This is the c analogue of CallableConvertMap.
475
+ """
476
+ # Cython doesn't yet accept function pointers as arguments,
477
+ # change this when it does.
478
+ cdef CCallableConvertMap_class map = CCallableConvertMap_class(domain, codomain, name)
479
+ map._func = <Element (*)(Parent, object)>func
480
+ return map
481
+
482
+ cpdef Element _ccall_test_function(codomain, x):
483
+ """
484
+ For testing CCallableConvertMap_class. Returns x*x*x-x in the codomain.
485
+
486
+ TESTS::
487
+
488
+ sage: from sage.structure.coerce_maps import _ccall_test_function
489
+ sage: _ccall_test_function(ZZ, 1)
490
+ 0
491
+ sage: _ccall_test_function(ZZ, 2)
492
+ 6
493
+ sage: _ccall_test_function(ZZ, -3)
494
+ -24
495
+ """
496
+ return codomain(x*x*x-x)
497
+
498
+
499
+ def test_CCallableConvertMap(domain, name=None):
500
+ """
501
+ For testing CCallableConvertMap_class.
502
+
503
+ TESTS::
504
+
505
+ sage: from sage.structure.coerce_maps import test_CCallableConvertMap
506
+ sage: f = test_CCallableConvertMap(ZZ, 'test'); f
507
+ Conversion via c call 'test' map:
508
+ From: Integer Ring
509
+ To: Integer Ring
510
+ sage: f(3)
511
+ 24
512
+ sage: f(9)
513
+ 720
514
+ """
515
+ return CCallableConvertMap(domain, domain, <void*>&_ccall_test_function, name)
516
+
517
+
518
+ cdef class ListMorphism(Map):
519
+
520
+ cdef Map _real_morphism
521
+
522
+ def __init__(self, domain, Map real_morphism):
523
+ if not isinstance(domain, Parent):
524
+ domain = Set_PythonType(domain)
525
+ Map.__init__(self, domain, real_morphism.codomain())
526
+ self._coerce_cost = real_morphism._coerce_cost + 3
527
+ self._real_morphism = real_morphism
528
+ self._repr_type_str = "List"
529
+
530
+ cdef dict _extra_slots(self):
531
+ slots = Map._extra_slots(self)
532
+ slots['_real_morphism'] = self._real_morphism
533
+ return slots
534
+
535
+ cdef _update_slots(self, dict _slots):
536
+ self._real_morphism = _slots['_real_morphism']
537
+ Map._update_slots(self, _slots)
538
+
539
+ cpdef Element _call_(self, x):
540
+ try:
541
+ x = x._data
542
+ except AttributeError:
543
+ x = list(x)
544
+ return self._real_morphism._call_(x)
545
+
546
+ cpdef Element _call_with_args(self, x, args=(), kwds={}):
547
+ try:
548
+ x = x._data
549
+ except AttributeError:
550
+ x = list(x)
551
+ return self._real_morphism._call_with_args(x, args, kwds)
552
+
553
+
554
+ cdef class TryMap(Map):
555
+ def __init__(self, Map morphism_preferred, Map morphism_backup, error_types=None):
556
+ """
557
+ TESTS::
558
+
559
+ sage: sage.structure.coerce_maps.TryMap(RDF.coerce_map_from(QQ), RDF.coerce_map_from(ZZ))
560
+ Traceback (most recent call last):
561
+ ...
562
+ TypeError: incorrectly matching parent
563
+ """
564
+ if (morphism_preferred.domain() is not morphism_backup.domain()
565
+ or morphism_preferred.codomain() is not morphism_backup.codomain()):
566
+ raise TypeError("incorrectly matching parent")
567
+ Map.__init__(self, morphism_preferred.parent())
568
+ self._map_p = morphism_preferred
569
+ self._map_b = morphism_backup
570
+ if error_types is None:
571
+ self._error_types = (ValueError, TypeError, AttributeError)
572
+ else:
573
+ self._error_types = error_types
574
+
575
+ cdef dict _extra_slots(self):
576
+ """
577
+ Helper for copying and pickling.
578
+
579
+ EXAMPLES::
580
+
581
+ sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x: 1/x)
582
+ sage: map2 = QQ.coerce_map_from(ZZ)
583
+ sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
584
+ sage: cmap = copy(map) # indirect doctest
585
+ sage: cmap == map # not implemented (todo: implement comparison)
586
+ True
587
+ sage: map(3) == cmap(3)
588
+ True
589
+ sage: map(0) == cmap(0)
590
+ True
591
+ """
592
+ slots = Map._extra_slots(self)
593
+ slots['_map_p'] = self._map_p
594
+ slots['_map_b'] = self._map_b
595
+ slots['_error_types'] = self._error_types
596
+ return slots
597
+
598
+ cdef _update_slots(self, dict _slots):
599
+ """
600
+ Helper for copying and pickling.
601
+
602
+ EXAMPLES::
603
+
604
+ sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x: 1/x)
605
+ sage: map2 = QQ.coerce_map_from(ZZ)
606
+ sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
607
+ sage: cmap = copy(map) # indirect doctest
608
+ sage: cmap == map # not implemented (todo: implement comparison)
609
+ True
610
+ sage: map(3) == cmap(3)
611
+ True
612
+ sage: map(0) == cmap(0)
613
+ True
614
+ """
615
+ self._map_p = _slots['_map_p']
616
+ self._map_b = _slots['_map_b']
617
+ self._error_types = _slots['_error_types']
618
+ Map._update_slots(self, _slots)
619
+
620
+ cpdef Element _call_(self, x):
621
+ """
622
+ EXAMPLES::
623
+
624
+ sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x: 1/x)
625
+ sage: map2 = QQ.coerce_map_from(ZZ)
626
+ sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
627
+ sage: map(3)
628
+ 1/3
629
+ sage: map(-7)
630
+ -1/7
631
+ sage: map(0)
632
+ 0
633
+ """
634
+ try:
635
+ return self._map_p._call_(x)
636
+ except self._error_types:
637
+ return self._map_b._call_(x)
638
+
639
+ cpdef Element _call_with_args(self, x, args=(), kwds={}):
640
+ """
641
+ EXAMPLES::
642
+
643
+ sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x, y: y/x)
644
+ sage: map2 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x, y: 23/1)
645
+ sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
646
+ sage: map._call_with_args(3, (2,))
647
+ 2/3
648
+ sage: map._call_with_args(-7, (5,))
649
+ -5/7
650
+ sage: map._call_with_args(0, (1,))
651
+ 23
652
+ """
653
+ try:
654
+ return self._map_p._call_with_args(x, args, kwds)
655
+ except self._error_types:
656
+ return self._map_b._call_with_args(x, args, kwds)