passagemath-ntl 10.8.1a1__cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.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 (162) hide show
  1. passagemath_ntl/__init__.py +3 -0
  2. passagemath_ntl-10.8.1a1.dist-info/METADATA +121 -0
  3. passagemath_ntl-10.8.1a1.dist-info/RECORD +162 -0
  4. passagemath_ntl-10.8.1a1.dist-info/WHEEL +6 -0
  5. passagemath_ntl-10.8.1a1.dist-info/top_level.txt +3 -0
  6. passagemath_ntl.libs/libgf2x-a4cdec90.so.3.0.0 +0 -0
  7. passagemath_ntl.libs/libgmp-6e109695.so.10.5.0 +0 -0
  8. passagemath_ntl.libs/libmpfi-bcd96d8d.so.0.0.0 +0 -0
  9. passagemath_ntl.libs/libmpfr-104d25bb.so.6.2.2 +0 -0
  10. passagemath_ntl.libs/libntl-bacfd860.so.45.0.0 +0 -0
  11. sage/all__sagemath_ntl.py +7 -0
  12. sage/libs/all__sagemath_ntl.py +3 -0
  13. sage/libs/mpfi/__init__.pxd +287 -0
  14. sage/libs/mpfi/types.pxd +10 -0
  15. sage/libs/ntl/GF2.pxd +18 -0
  16. sage/libs/ntl/GF2E.pxd +28 -0
  17. sage/libs/ntl/GF2EX.pxd +12 -0
  18. sage/libs/ntl/GF2X.pxd +81 -0
  19. sage/libs/ntl/ZZ.pxd +93 -0
  20. sage/libs/ntl/ZZX.pxd +85 -0
  21. sage/libs/ntl/ZZ_p.pxd +28 -0
  22. sage/libs/ntl/ZZ_pE.pxd +37 -0
  23. sage/libs/ntl/ZZ_pEX.pxd +106 -0
  24. sage/libs/ntl/ZZ_pX.pxd +122 -0
  25. sage/libs/ntl/__init__.py +4 -0
  26. sage/libs/ntl/all.py +72 -0
  27. sage/libs/ntl/conversion.pxd +106 -0
  28. sage/libs/ntl/convert.cpython-314t-x86_64-linux-gnu.so +0 -0
  29. sage/libs/ntl/convert.pxd +7 -0
  30. sage/libs/ntl/convert.pyx +38 -0
  31. sage/libs/ntl/decl.pxi +18 -0
  32. sage/libs/ntl/error.cpython-314t-x86_64-linux-gnu.so +0 -0
  33. sage/libs/ntl/error.pyx +63 -0
  34. sage/libs/ntl/lzz_p.pxd +20 -0
  35. sage/libs/ntl/lzz_pX.pxd +59 -0
  36. sage/libs/ntl/mat_GF2.pxd +30 -0
  37. sage/libs/ntl/mat_GF2E.pxd +30 -0
  38. sage/libs/ntl/mat_ZZ.pxd +59 -0
  39. sage/libs/ntl/misc.pxi +33 -0
  40. sage/libs/ntl/ntl_GF2.cpython-314t-x86_64-linux-gnu.so +0 -0
  41. sage/libs/ntl/ntl_GF2.pxd +5 -0
  42. sage/libs/ntl/ntl_GF2.pyx +281 -0
  43. sage/libs/ntl/ntl_GF2E.cpython-314t-x86_64-linux-gnu.so +0 -0
  44. sage/libs/ntl/ntl_GF2E.pxd +8 -0
  45. sage/libs/ntl/ntl_GF2E.pyx +488 -0
  46. sage/libs/ntl/ntl_GF2EContext.cpython-314t-x86_64-linux-gnu.so +0 -0
  47. sage/libs/ntl/ntl_GF2EContext.pxd +9 -0
  48. sage/libs/ntl/ntl_GF2EContext.pyx +134 -0
  49. sage/libs/ntl/ntl_GF2EX.cpython-314t-x86_64-linux-gnu.so +0 -0
  50. sage/libs/ntl/ntl_GF2EX.pxd +10 -0
  51. sage/libs/ntl/ntl_GF2EX.pyx +251 -0
  52. sage/libs/ntl/ntl_GF2X.cpython-314t-x86_64-linux-gnu.so +0 -0
  53. sage/libs/ntl/ntl_GF2X.pxd +5 -0
  54. sage/libs/ntl/ntl_GF2X.pyx +771 -0
  55. sage/libs/ntl/ntl_GF2X_linkage.pxi +404 -0
  56. sage/libs/ntl/ntl_ZZ.cpython-314t-x86_64-linux-gnu.so +0 -0
  57. sage/libs/ntl/ntl_ZZ.pxd +7 -0
  58. sage/libs/ntl/ntl_ZZ.pyx +541 -0
  59. sage/libs/ntl/ntl_ZZX.cpython-314t-x86_64-linux-gnu.so +0 -0
  60. sage/libs/ntl/ntl_ZZX.pxd +7 -0
  61. sage/libs/ntl/ntl_ZZX.pyx +1206 -0
  62. sage/libs/ntl/ntl_ZZ_p.cpython-314t-x86_64-linux-gnu.so +0 -0
  63. sage/libs/ntl/ntl_ZZ_p.pxd +10 -0
  64. sage/libs/ntl/ntl_ZZ_p.pyx +508 -0
  65. sage/libs/ntl/ntl_ZZ_pContext.cpython-314t-x86_64-linux-gnu.so +0 -0
  66. sage/libs/ntl/ntl_ZZ_pContext.pxd +22 -0
  67. sage/libs/ntl/ntl_ZZ_pContext.pyx +201 -0
  68. sage/libs/ntl/ntl_ZZ_pE.cpython-314t-x86_64-linux-gnu.so +0 -0
  69. sage/libs/ntl/ntl_ZZ_pE.pxd +11 -0
  70. sage/libs/ntl/ntl_ZZ_pE.pyx +349 -0
  71. sage/libs/ntl/ntl_ZZ_pEContext.cpython-314t-x86_64-linux-gnu.so +0 -0
  72. sage/libs/ntl/ntl_ZZ_pEContext.pxd +23 -0
  73. sage/libs/ntl/ntl_ZZ_pEContext.pyx +226 -0
  74. sage/libs/ntl/ntl_ZZ_pEX.cpython-314t-x86_64-linux-gnu.so +0 -0
  75. sage/libs/ntl/ntl_ZZ_pEX.pxd +10 -0
  76. sage/libs/ntl/ntl_ZZ_pEX.pyx +1255 -0
  77. sage/libs/ntl/ntl_ZZ_pEX_linkage.pxi +420 -0
  78. sage/libs/ntl/ntl_ZZ_pX.cpython-314t-x86_64-linux-gnu.so +0 -0
  79. sage/libs/ntl/ntl_ZZ_pX.pxd +17 -0
  80. sage/libs/ntl/ntl_ZZ_pX.pyx +1532 -0
  81. sage/libs/ntl/ntl_lzz_p.cpython-314t-x86_64-linux-gnu.so +0 -0
  82. sage/libs/ntl/ntl_lzz_p.pxd +8 -0
  83. sage/libs/ntl/ntl_lzz_p.pyx +440 -0
  84. sage/libs/ntl/ntl_lzz_pContext.cpython-314t-x86_64-linux-gnu.so +0 -0
  85. sage/libs/ntl/ntl_lzz_pContext.pxd +7 -0
  86. sage/libs/ntl/ntl_lzz_pContext.pyx +137 -0
  87. sage/libs/ntl/ntl_lzz_pX.cpython-314t-x86_64-linux-gnu.so +0 -0
  88. sage/libs/ntl/ntl_lzz_pX.pxd +10 -0
  89. sage/libs/ntl/ntl_lzz_pX.pyx +902 -0
  90. sage/libs/ntl/ntl_mat_GF2.cpython-314t-x86_64-linux-gnu.so +0 -0
  91. sage/libs/ntl/ntl_mat_GF2.pxd +8 -0
  92. sage/libs/ntl/ntl_mat_GF2.pyx +612 -0
  93. sage/libs/ntl/ntl_mat_GF2E.cpython-314t-x86_64-linux-gnu.so +0 -0
  94. sage/libs/ntl/ntl_mat_GF2E.pxd +10 -0
  95. sage/libs/ntl/ntl_mat_GF2E.pyx +752 -0
  96. sage/libs/ntl/ntl_mat_ZZ.cpython-314t-x86_64-linux-gnu.so +0 -0
  97. sage/libs/ntl/ntl_mat_ZZ.pxd +6 -0
  98. sage/libs/ntl/ntl_mat_ZZ.pyx +1522 -0
  99. sage/libs/ntl/ntl_tools.pxd +3 -0
  100. sage/libs/ntl/ntlwrap.h +53 -0
  101. sage/libs/ntl/ntlwrap_impl.h +743 -0
  102. sage/libs/ntl/types.pxd +157 -0
  103. sage/libs/ntl/vec_GF2.pxd +26 -0
  104. sage/libs/ntl/vec_GF2E.pxd +2 -0
  105. sage/matrix/all__sagemath_ntl.py +1 -0
  106. sage/matrix/matrix_modn_dense_double.pxd +10 -0
  107. sage/matrix/matrix_modn_dense_float.pxd +9 -0
  108. sage/matrix/matrix_modn_dense_template.pxi +3475 -0
  109. sage/matrix/matrix_modn_dense_template_header.pxi +15 -0
  110. sage/matrix/matrix_modn_sparse.pxd +8 -0
  111. sage/misc/all__sagemath_ntl.py +1 -0
  112. sage/rings/all__sagemath_ntl.py +7 -0
  113. sage/rings/bernmm.cpython-314t-x86_64-linux-gnu.so +0 -0
  114. sage/rings/bernmm.pyx +161 -0
  115. sage/rings/bernoulli_mod_p.cpython-314t-x86_64-linux-gnu.so +0 -0
  116. sage/rings/bernoulli_mod_p.pyx +312 -0
  117. sage/rings/finite_rings/all__sagemath_ntl.py +1 -0
  118. sage/rings/finite_rings/finite_field_ntl_gf2e.py +309 -0
  119. sage/rings/finite_rings/residue_field_ntl_gf2e.cpython-314t-x86_64-linux-gnu.so +0 -0
  120. sage/rings/finite_rings/residue_field_ntl_gf2e.pyx +140 -0
  121. sage/rings/padics/all__sagemath_ntl.py +5 -0
  122. sage/rings/padics/padic_ZZ_pX_CA_element.cpython-314t-x86_64-linux-gnu.so +0 -0
  123. sage/rings/padics/padic_ZZ_pX_CA_element.pxd +25 -0
  124. sage/rings/padics/padic_ZZ_pX_CA_element.pyx +2366 -0
  125. sage/rings/padics/padic_ZZ_pX_CR_element.cpython-314t-x86_64-linux-gnu.so +0 -0
  126. sage/rings/padics/padic_ZZ_pX_CR_element.pxd +33 -0
  127. sage/rings/padics/padic_ZZ_pX_CR_element.pyx +3282 -0
  128. sage/rings/padics/padic_ZZ_pX_FM_element.cpython-314t-x86_64-linux-gnu.so +0 -0
  129. sage/rings/padics/padic_ZZ_pX_FM_element.pxd +12 -0
  130. sage/rings/padics/padic_ZZ_pX_FM_element.pyx +1737 -0
  131. sage/rings/padics/padic_ZZ_pX_element.cpython-314t-x86_64-linux-gnu.so +0 -0
  132. sage/rings/padics/padic_ZZ_pX_element.pxd +6 -0
  133. sage/rings/padics/padic_ZZ_pX_element.pyx +919 -0
  134. sage/rings/padics/padic_ext_element.cpython-314t-x86_64-linux-gnu.so +0 -0
  135. sage/rings/padics/padic_ext_element.pxd +38 -0
  136. sage/rings/padics/padic_ext_element.pyx +513 -0
  137. sage/rings/padics/pow_computer_ext.cpython-314t-x86_64-linux-gnu.so +0 -0
  138. sage/rings/padics/pow_computer_ext.pxd +107 -0
  139. sage/rings/padics/pow_computer_ext.pyx +2402 -0
  140. sage/rings/polynomial/all__sagemath_ntl.py +1 -0
  141. sage/rings/polynomial/evaluation_ntl.cpython-314t-x86_64-linux-gnu.so +0 -0
  142. sage/rings/polynomial/evaluation_ntl.pxd +7 -0
  143. sage/rings/polynomial/evaluation_ntl.pyx +70 -0
  144. sage/rings/polynomial/polynomial_gf2x.cpython-314t-x86_64-linux-gnu.so +0 -0
  145. sage/rings/polynomial/polynomial_gf2x.pxd +10 -0
  146. sage/rings/polynomial/polynomial_gf2x.pyx +364 -0
  147. sage/rings/polynomial/polynomial_integer_dense_ntl.cpython-314t-x86_64-linux-gnu.so +0 -0
  148. sage/rings/polynomial/polynomial_integer_dense_ntl.pxd +8 -0
  149. sage/rings/polynomial/polynomial_integer_dense_ntl.pyx +1128 -0
  150. sage/rings/polynomial/polynomial_modn_dense_ntl.cpython-314t-x86_64-linux-gnu.so +0 -0
  151. sage/rings/polynomial/polynomial_modn_dense_ntl.pxd +36 -0
  152. sage/rings/polynomial/polynomial_modn_dense_ntl.pyx +2050 -0
  153. sage/rings/polynomial/polynomial_template.pxi +839 -0
  154. sage/rings/polynomial/polynomial_template_header.pxi +11 -0
  155. sage/rings/polynomial/polynomial_zz_pex.cpython-314t-x86_64-linux-gnu.so +0 -0
  156. sage/rings/polynomial/polynomial_zz_pex.pxd +12 -0
  157. sage/rings/polynomial/polynomial_zz_pex.pyx +778 -0
  158. sage/rings/real_mpfi.pxd +50 -0
  159. sage/schemes/all__sagemath_ntl.py +1 -0
  160. sage/schemes/hyperelliptic_curves/all__sagemath_ntl.py +1 -0
  161. sage/schemes/hyperelliptic_curves/hypellfrob.cpython-314t-x86_64-linux-gnu.so +0 -0
  162. sage/schemes/hyperelliptic_curves/hypellfrob.pyx +253 -0
@@ -0,0 +1,839 @@
1
+ # sage_setup: distribution = sagemath-ntl
2
+ """
3
+ Polynomial Template for C/C++ Library Interfaces
4
+ """
5
+
6
+ # ***************************************************************************
7
+ # Copyright (C) 2008 Martin Albrecht <M.R.Albrecht@rhul.ac.uk>
8
+ # Copyright (C) 2008 Robert Bradshaw
9
+ #
10
+ # This program is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation, either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ # https://www.gnu.org/licenses/
15
+ # ***************************************************************************
16
+
17
+
18
+ from sage.rings.polynomial.polynomial_element cimport Polynomial
19
+ from sage.structure.element cimport ModuleElement, Element, RingElement
20
+ from sage.structure.element import coerce_binop
21
+ from sage.structure.richcmp cimport rich_to_bool
22
+ from sage.rings.fraction_field_element import FractionFieldElement
23
+ from sage.rings.integer cimport Integer
24
+
25
+ try:
26
+ from cypari2.gen import Gen as pari_gen
27
+ except ImportError:
28
+ pari_gen = ()
29
+
30
+ import operator
31
+
32
+
33
+ def make_element(parent, args):
34
+ return parent(*args)
35
+
36
+
37
+ cdef inline Polynomial_template element_shift(self, int n):
38
+ if not isinstance(self, Polynomial_template):
39
+ if n > 0:
40
+ error_msg = "Cannot shift %s << %n." % (self, n)
41
+ else:
42
+ error_msg = "Cannot shift %s >> %n." % (self, n)
43
+ raise TypeError(error_msg)
44
+
45
+ if n == 0:
46
+ return self
47
+
48
+ cdef celement *gen = celement_new((<Polynomial_template>self)._cparent)
49
+ celement_gen(gen, 0, (<Polynomial_template>self)._cparent)
50
+ celement_pow(gen, gen, abs(n), NULL, (<Polynomial_template>self)._cparent)
51
+ cdef type T = type(self)
52
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
53
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
54
+ r._parent = (<Polynomial_template>self)._parent
55
+ r._cparent = (<Polynomial_template>self)._cparent
56
+
57
+ if n > 0:
58
+ celement_mul(&r.x, &(<Polynomial_template>self).x, gen, (<Polynomial_template>self)._cparent)
59
+ else:
60
+ celement_floordiv(&r.x, &(<Polynomial_template>self).x, gen, (<Polynomial_template>self)._cparent)
61
+
62
+ celement_delete(gen, (<Polynomial_template>self)._cparent)
63
+ return r
64
+
65
+ cdef class Polynomial_template(Polynomial):
66
+ r"""
67
+ Template for interfacing to external C / C++ libraries for implementations of polynomials.
68
+
69
+ AUTHORS:
70
+
71
+ - Robert Bradshaw (2008-10): original idea for templating
72
+ - Martin Albrecht (2008-10): initial implementation
73
+
74
+ This file implements a simple templating engine for linking univariate
75
+ polynomials to their C/C++ library implementations. It requires a
76
+ 'linkage' file which implements the ``celement_`` functions (see
77
+ :mod:`sage.libs.ntl.ntl_GF2X_linkage` for an example). Both parts are
78
+ then plugged together by inclusion of the linkage file when inheriting from
79
+ this class. See :mod:`sage.rings.polynomial.polynomial_gf2x` for an
80
+ example.
81
+
82
+ We illustrate the generic glueing using univariate polynomials over
83
+ `\mathop{\mathrm{GF}}(2)`.
84
+
85
+ .. NOTE::
86
+
87
+ Implementations using this template MUST implement coercion from base
88
+ ring elements and :meth:`get_unsafe`. See
89
+ :class:`~sage.rings.polynomial.polynomial_gf2x.Polynomial_GF2X` for an
90
+ example.
91
+ """
92
+ def __init__(self, parent, x=None, check=True, is_gen=False, construct=False):
93
+ """
94
+ EXAMPLES::
95
+
96
+ sage: P.<x> = GF(2)[]
97
+ sage: P(0)
98
+ 0
99
+ sage: P(GF(2)(1))
100
+ 1
101
+ sage: P(3)
102
+ 1
103
+ sage: P([1,0,1])
104
+ x^2 + 1
105
+ sage: P(list(map(GF(2),[1,0,1])))
106
+ x^2 + 1
107
+ """
108
+ cdef celement *gen
109
+ cdef celement *monomial
110
+ cdef Py_ssize_t deg
111
+
112
+ Polynomial.__init__(self, parent, is_gen=is_gen)
113
+
114
+ (<Polynomial_template>self)._cparent = get_cparent(self._parent)
115
+
116
+ if is_gen:
117
+ celement_construct(&self.x, (<Polynomial_template>self)._cparent)
118
+ celement_gen(&self.x, 0, (<Polynomial_template>self)._cparent)
119
+
120
+ elif isinstance(x, Polynomial_template):
121
+ try:
122
+ celement_construct(&self.x, (<Polynomial_template>self)._cparent)
123
+ celement_set(&self.x, &(<Polynomial_template>x).x, (<Polynomial_template>self)._cparent)
124
+ except NotImplementedError:
125
+ raise TypeError("%s not understood" % x)
126
+
127
+ elif isinstance(x, (int, Integer)):
128
+ try:
129
+ celement_construct(&self.x, (<Polynomial_template>self)._cparent)
130
+ celement_set_si(&self.x, int(x), (<Polynomial_template>self)._cparent)
131
+ except NotImplementedError:
132
+ raise TypeError("%s not understood" % x)
133
+
134
+ elif isinstance(x, (list, tuple)):
135
+ celement_construct(&self.x, (<Polynomial_template>self)._cparent)
136
+ gen = celement_new((<Polynomial_template>self)._cparent)
137
+ monomial = celement_new((<Polynomial_template>self)._cparent)
138
+
139
+ celement_set_si(&self.x, 0, (<Polynomial_template>self)._cparent)
140
+ celement_gen(gen, 0, (<Polynomial_template>self)._cparent)
141
+
142
+ deg = 0
143
+ for e in x:
144
+ # r += parent(e)*power
145
+ celement_pow(monomial, gen, deg, NULL, (<Polynomial_template>self)._cparent)
146
+ celement_mul(monomial, &(<Polynomial_template>self.__class__(parent, e)).x, monomial, (<Polynomial_template>self)._cparent)
147
+ celement_add(&self.x, &self.x, monomial, (<Polynomial_template>self)._cparent)
148
+ deg += 1
149
+
150
+ celement_delete(gen, (<Polynomial_template>self)._cparent)
151
+ celement_delete(monomial, (<Polynomial_template>self)._cparent)
152
+
153
+ elif isinstance(x, dict):
154
+ celement_construct(&self.x, (<Polynomial_template>self)._cparent)
155
+ gen = celement_new((<Polynomial_template>self)._cparent)
156
+ monomial = celement_new((<Polynomial_template>self)._cparent)
157
+
158
+ celement_set_si(&self.x, 0, (<Polynomial_template>self)._cparent)
159
+ celement_gen(gen, 0, (<Polynomial_template>self)._cparent)
160
+
161
+ for deg, coef in x.items():
162
+ celement_pow(monomial, gen, deg, NULL, (<Polynomial_template>self)._cparent)
163
+ celement_mul(monomial, &(<Polynomial_template>self.__class__(parent, coef)).x, monomial, (<Polynomial_template>self)._cparent)
164
+ celement_add(&self.x, &self.x, monomial, (<Polynomial_template>self)._cparent)
165
+
166
+ celement_delete(gen, (<Polynomial_template>self)._cparent)
167
+ celement_delete(monomial, (<Polynomial_template>self)._cparent)
168
+
169
+ elif isinstance(x, pari_gen):
170
+ k = (<Polynomial_template>self)._parent.base_ring()
171
+ x = [k(w) for w in x.list()]
172
+ self.__class__.__init__(self, parent, x, check=True, is_gen=False, construct=construct)
173
+ elif isinstance(x, Polynomial):
174
+ k = (<Polynomial_template>self)._parent.base_ring()
175
+ x = [k(w) for w in list(x)]
176
+ Polynomial_template.__init__(self, parent, x, check=True, is_gen=False, construct=construct)
177
+ elif isinstance(x, FractionFieldElement) and (x.parent().base() is parent or x.parent().base() == parent) and x.denominator() == 1:
178
+ x = x.numerator()
179
+ self.__class__.__init__(self, parent, x, check=check, is_gen=is_gen, construct=construct)
180
+ else:
181
+ x = parent.base_ring()(x)
182
+ self.__class__.__init__(self, parent, x, check=check, is_gen=is_gen, construct=construct)
183
+
184
+ def __reduce__(self):
185
+ """
186
+ EXAMPLES::
187
+
188
+ sage: P.<x> = GF(2)[]
189
+ sage: loads(dumps(x)) == x
190
+ True
191
+ """
192
+ return make_element, ((<Polynomial_template>self)._parent, (self.list(), False, self.is_gen()))
193
+
194
+ cpdef list list(self, bint copy=True):
195
+ """
196
+ EXAMPLES::
197
+
198
+ sage: P.<x> = GF(2)[]
199
+ sage: x.list()
200
+ [0, 1]
201
+ sage: list(x)
202
+ [0, 1]
203
+ """
204
+ cdef Py_ssize_t i
205
+ return [self[i] for i in range(celement_len(&self.x, (<Polynomial_template>self)._cparent))]
206
+
207
+ def __dealloc__(self):
208
+ """
209
+ EXAMPLES::
210
+
211
+ sage: P.<x> = GF(2)[]
212
+ sage: del x
213
+
214
+ TESTS:
215
+
216
+ The following has been a problem in a preliminary version of
217
+ :issue:`12313`::
218
+
219
+ sage: # needs sage.rings.finite_rings
220
+ sage: K.<z> = GF(4)
221
+ sage: P.<x> = K[]
222
+ sage: del P
223
+ sage: del x
224
+ sage: import gc
225
+ sage: _ = gc.collect()
226
+ """
227
+ celement_destruct(&self.x, (<Polynomial_template>self)._cparent)
228
+
229
+ cpdef _add_(self, right):
230
+ """
231
+ EXAMPLES::
232
+
233
+ sage: P.<x> = GF(2)[]
234
+ sage: x + 1
235
+ x + 1
236
+ """
237
+ cdef type T = type(self)
238
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
239
+
240
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
241
+ r._parent = (<Polynomial_template>self)._parent
242
+ r._cparent = (<Polynomial_template>self)._cparent
243
+ celement_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent)
244
+ #assert(r._parent(pari(self) + pari(right)) == r)
245
+ return r
246
+
247
+ cpdef _sub_(self, right):
248
+ """
249
+ EXAMPLES::
250
+
251
+ sage: P.<x> = GF(2)[]
252
+ sage: x - 1
253
+ x + 1
254
+ """
255
+ cdef type T = type(self)
256
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
257
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
258
+ r._parent = (<Polynomial_template>self)._parent
259
+ r._cparent = (<Polynomial_template>self)._cparent
260
+ celement_sub(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent)
261
+ #assert(r._parent(pari(self) - pari(right)) == r)
262
+ return r
263
+
264
+ def __neg__(self):
265
+ """
266
+ EXAMPLES::
267
+
268
+ sage: P.<x> = GF(2)[]
269
+ sage: -x
270
+ x
271
+ """
272
+ cdef type T = type(self)
273
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
274
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
275
+ r._parent = (<Polynomial_template>self)._parent
276
+ r._cparent = (<Polynomial_template>self)._cparent
277
+ celement_neg(&r.x, &self.x, (<Polynomial_template>self)._cparent)
278
+ #assert(r._parent(-pari(self)) == r)
279
+ return r
280
+
281
+ cpdef _lmul_(self, Element left):
282
+ """
283
+ EXAMPLES::
284
+
285
+ sage: P.<x> = GF(2)[]
286
+ sage: t = x^2 + x + 1
287
+ sage: 0*t
288
+ 0
289
+ sage: 1*t
290
+ x^2 + x + 1
291
+
292
+ sage: R.<y> = GF(5)[]
293
+ sage: u = y^2 + y + 1
294
+ sage: 3*u
295
+ 3*y^2 + 3*y + 3
296
+ sage: 5*u
297
+ 0
298
+ sage: (2^81)*u
299
+ 2*y^2 + 2*y + 2
300
+ sage: (-2^81)*u
301
+ 3*y^2 + 3*y + 3
302
+
303
+ ::
304
+
305
+ sage: P.<x> = GF(2)[]
306
+ sage: t = x^2 + x + 1
307
+ sage: t*0
308
+ 0
309
+ sage: t*1
310
+ x^2 + x + 1
311
+
312
+ sage: R.<y> = GF(5)[]
313
+ sage: u = y^2 + y + 1
314
+ sage: u*3
315
+ 3*y^2 + 3*y + 3
316
+ sage: u*5
317
+ 0
318
+ """
319
+ cdef type T = type(self)
320
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
321
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
322
+ r._parent = (<Polynomial_template>self)._parent
323
+ r._cparent = (<Polynomial_template>self)._cparent
324
+ celement_mul_scalar(&r.x, &(<Polynomial_template>self).x, left, (<Polynomial_template>self)._cparent)
325
+ return r
326
+
327
+ cpdef _mul_(self, right):
328
+ """
329
+ EXAMPLES::
330
+
331
+ sage: P.<x> = GF(2)[]
332
+ sage: x*(x+1)
333
+ x^2 + x
334
+ """
335
+ cdef type T = type(self)
336
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
337
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
338
+ r._parent = (<Polynomial_template>self)._parent
339
+ r._cparent = (<Polynomial_template>self)._cparent
340
+ celement_mul(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent)
341
+ #assert(r._parent(pari(self) * pari(right)) == r)
342
+ return r
343
+
344
+ @coerce_binop
345
+ def gcd(self, Polynomial_template other):
346
+ """
347
+ Return the greatest common divisor of ``self`` and ``other``.
348
+
349
+ EXAMPLES::
350
+
351
+ sage: P.<x> = GF(2)[]
352
+ sage: f = x*(x+1)
353
+ sage: f.gcd(x+1)
354
+ x + 1
355
+ sage: f.gcd(x^2)
356
+ x
357
+
358
+ TESTS:
359
+
360
+ Ensure non-invertible elements does not crash Sage (:issue:`37317`)::
361
+
362
+ sage: R.<x> = Zmod(4)[]
363
+ sage: f = R(2 * x)
364
+ sage: f.gcd(f)
365
+ Traceback (most recent call last):
366
+ ...
367
+ ValueError: leading coefficient must be invertible
368
+
369
+ ::
370
+
371
+ sage: f = x^2 + 3 * x + 1
372
+ sage: g = x^2 + x + 1
373
+ sage: f.gcd(g)
374
+ Traceback (most recent call last):
375
+ ...
376
+ RuntimeError: FLINT gcd calculation failed
377
+ """
378
+ if celement_is_zero(&self.x, (<Polynomial_template>self)._cparent):
379
+ return other
380
+ if celement_is_zero(&other.x, (<Polynomial_template>self)._cparent):
381
+ return self
382
+ if celement_equal(&self.x, &other.x, (<Polynomial_template>self)._cparent):
383
+ # note: gcd(g, g) "canonicalizes" the generator i.e. make polynomials monic
384
+ # c.f. ring/ring.pyx:445
385
+ return self.monic()
386
+
387
+ cdef type T = type(self)
388
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
389
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
390
+ r._parent = (<Polynomial_template>self)._parent
391
+ r._cparent = (<Polynomial_template>self)._cparent
392
+ celement_gcd(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, (<Polynomial_template>self)._cparent)
393
+ #assert(r._parent(pari(self).gcd(pari(other))) == r)
394
+ return r
395
+
396
+ @coerce_binop
397
+ def xgcd(self, Polynomial_template other):
398
+ """
399
+ Compute extended gcd of ``self`` and ``other``.
400
+
401
+ EXAMPLES::
402
+
403
+ sage: P.<x> = GF(7)[]
404
+ sage: f = x*(x+1)
405
+ sage: f.xgcd(x+1)
406
+ (x + 1, 0, 1)
407
+ sage: f.xgcd(x^2)
408
+ (x, 1, 6)
409
+ """
410
+ if(celement_is_zero(&self.x, (<Polynomial_template>self)._cparent)):
411
+ return other, self._parent(0), self._parent(1)
412
+ if(celement_is_zero(&other.x, (<Polynomial_template>self)._cparent)):
413
+ return self, self._parent(1), self._parent(0)
414
+
415
+ cdef type T = type(self)
416
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
417
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
418
+ r._parent = (<Polynomial_template>self)._parent
419
+ r._cparent = (<Polynomial_template>self)._cparent
420
+
421
+ cdef Polynomial_template s = <Polynomial_template>T.__new__(T)
422
+ celement_construct(&s.x, (<Polynomial_template>self)._cparent)
423
+ s._parent = (<Polynomial_template>self)._parent
424
+ s._cparent = (<Polynomial_template>self)._cparent
425
+
426
+ cdef Polynomial_template t = <Polynomial_template>T.__new__(T)
427
+ celement_construct(&t.x, (<Polynomial_template>self)._cparent)
428
+ t._parent = (<Polynomial_template>self)._parent
429
+ t._cparent = (<Polynomial_template>self)._cparent
430
+
431
+ celement_xgcd(&r.x, &s.x, &t.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, (<Polynomial_template>self)._cparent)
432
+ #rp, sp, tp = pari(self).xgcd(pari(other))
433
+ #assert(r._parent(rp) == r)
434
+ #assert(s._parent(sp) == s)
435
+ #assert(t._parent(tp) == t)
436
+ return r,s,t
437
+
438
+ cpdef _floordiv_(self, right):
439
+ """
440
+ EXAMPLES::
441
+
442
+ sage: P.<x> = GF(2)[]
443
+ sage: x//(x + 1)
444
+ 1
445
+ sage: (x + 1)//x
446
+ 1
447
+ sage: F = GF(47)
448
+ sage: R.<x> = F[]
449
+ sage: x // 1
450
+ x
451
+ sage: x // F(1)
452
+ x
453
+ sage: 1 // x
454
+ 0
455
+ sage: parent(x // 1)
456
+ Univariate Polynomial Ring in x over Finite Field of size 47
457
+ sage: parent(1 // x)
458
+ Univariate Polynomial Ring in x over Finite Field of size 47
459
+ """
460
+ cdef Polynomial_template _right = <Polynomial_template>right
461
+
462
+ if celement_is_zero(&_right.x, (<Polynomial_template>self)._cparent):
463
+ raise ZeroDivisionError
464
+ cdef type T = type(self)
465
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
466
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
467
+ r._parent = (<Polynomial_template>self)._parent
468
+ r._cparent = (<Polynomial_template>self)._cparent
469
+ #assert(r._parent(pari(self) // pari(right)) == r)
470
+ celement_floordiv(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent)
471
+ return r
472
+
473
+ cpdef _mod_(self, other):
474
+ """
475
+ EXAMPLES::
476
+
477
+ sage: P.<x> = GF(2)[]
478
+ sage: (x^2 + 1) % x^2
479
+ 1
480
+
481
+ TESTS:
482
+
483
+ We test that :issue:`10578` is fixed::
484
+
485
+ sage: P.<x> = GF(2)[]
486
+ sage: x % 1r
487
+ 0
488
+ """
489
+ cdef Polynomial_template _other = <Polynomial_template>other
490
+
491
+ if celement_is_zero(&_other.x, (<Polynomial_template>self)._cparent):
492
+ raise ZeroDivisionError
493
+
494
+ cdef type T = type(self)
495
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
496
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
497
+ r._parent = (<Polynomial_template>self)._parent
498
+ r._cparent = (<Polynomial_template>self)._cparent
499
+ celement_mod(&r.x, &(<Polynomial_template>self).x, &_other.x, (<Polynomial_template>self)._cparent)
500
+ #assert(r._parent(pari(self) % pari(other)) == r)
501
+ return r
502
+
503
+ @coerce_binop
504
+ def quo_rem(self, Polynomial_template right):
505
+ """
506
+ EXAMPLES::
507
+
508
+ sage: P.<x> = GF(2)[]
509
+ sage: f = x^2 + x + 1
510
+ sage: f.quo_rem(x + 1)
511
+ (x, 1)
512
+ """
513
+ if celement_is_zero(&right.x, (<Polynomial_template>self)._cparent):
514
+ raise ZeroDivisionError
515
+
516
+ cdef type T = type(self)
517
+ cdef Polynomial_template q = <Polynomial_template>T.__new__(T)
518
+ celement_construct(&q.x, (<Polynomial_template>self)._cparent)
519
+ q._parent = (<Polynomial_template>self)._parent
520
+ q._cparent = (<Polynomial_template>self)._cparent
521
+
522
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
523
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
524
+ r._parent = (<Polynomial_template>self)._parent
525
+ r._cparent = (<Polynomial_template>self)._cparent
526
+
527
+ celement_quorem(&q.x, &r.x, &(<Polynomial_template>self).x, &right.x, (<Polynomial_template>self)._cparent)
528
+ return q,r
529
+
530
+ def __bool__(self):
531
+ """
532
+ EXAMPLES::
533
+
534
+ sage: P.<x> = GF(2)[]
535
+ sage: bool(x), x.is_zero()
536
+ (True, False)
537
+ sage: bool(P(0)), P(0).is_zero()
538
+ (False, True)
539
+ """
540
+ return not celement_is_zero(&self.x, (<Polynomial_template>self)._cparent)
541
+
542
+ cpdef _richcmp_(self, other, int op):
543
+ """
544
+ EXAMPLES::
545
+
546
+ sage: P.<x> = GF(2)[]
547
+ sage: x != 1
548
+ True
549
+ sage: x < 1
550
+ False
551
+ sage: x > 1
552
+ True
553
+ """
554
+ cdef int c
555
+ c = celement_cmp(&self.x, &(<Polynomial_template>other).x, self._cparent)
556
+ return rich_to_bool(op, c)
557
+
558
+ def __hash__(self):
559
+ """
560
+ EXAMPLES::
561
+
562
+ sage: P.<x> = GF(2)[]
563
+ sage: {x:1}
564
+ {x: 1}
565
+ """
566
+ cdef long result = 0 # store it in a c-int and just let the overflowing additions wrap
567
+ cdef long result_mon
568
+ cdef long c_hash
569
+ cdef long var_name_hash
570
+ cdef int i
571
+ for i from 0<= i <= self.degree():
572
+ if i == 1:
573
+ # we delay the hashing until now to not waste it one a constant poly
574
+ var_name_hash = hash(self.variable_name())
575
+ # I'm assuming (incorrectly) that hashes of zero indicate that the element is 0.
576
+ # This assumption is not true, but I think it is true enough for the purposes and it
577
+ # it allows us to write fast code that omits terms with 0 coefficients. This is
578
+ # important if we want to maintain the '==' relationship with sparse polys.
579
+ c_hash = hash(self[i])
580
+ if c_hash != 0:
581
+ if i == 0:
582
+ result += c_hash
583
+ else:
584
+ # Hash (self[i], generator, i) as a tuple according to the algorithm.
585
+ result_mon = c_hash
586
+ result_mon = (1000003 * result_mon) ^ var_name_hash
587
+ result_mon = (1000003 * result_mon) ^ i
588
+ result += result_mon
589
+ if result == -1:
590
+ return -2
591
+ return result
592
+
593
+ def __pow__(self, ee, modulus):
594
+ """
595
+ EXAMPLES::
596
+
597
+ sage: P.<x> = GF(2)[]
598
+ sage: x^1000
599
+ x^1000
600
+ sage: (x+1)^2
601
+ x^2 + 1
602
+ sage: (x+1)^(-2)
603
+ 1/(x^2 + 1)
604
+ sage: f = x^9 + x^7 + x^6 + x^5 + x^4 + x^2 + x
605
+ sage: h = x^10 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + 1
606
+ sage: (f^2) % h
607
+ x^9 + x^8 + x^7 + x^5 + x^3
608
+ sage: pow(f, 2, h)
609
+ x^9 + x^8 + x^7 + x^5 + x^3
610
+
611
+ TESTS:
612
+
613
+ Ensure modulo `0` and modulo `1` does not crash (:issue:`37169`)::
614
+
615
+ sage: R.<x> = GF(2)[]
616
+ sage: pow(x + 1, 2, R.zero())
617
+ Traceback (most recent call last):
618
+ ...
619
+ ZeroDivisionError: modulus must be nonzero
620
+ sage: pow(x + 1, 2, R.one())
621
+ 0
622
+
623
+ ::
624
+
625
+ sage: R.<x> = GF(2^8)[]
626
+ sage: pow(x + 1, 2, R.zero())
627
+ Traceback (most recent call last):
628
+ ...
629
+ ZeroDivisionError: modulus must be nonzero
630
+ sage: pow(x + 1, 2, R.one())
631
+ 0
632
+ """
633
+ if not isinstance(self, Polynomial_template):
634
+ raise NotImplementedError("%s^%s not defined." % (ee, self))
635
+ cdef bint recip = 0, do_sig
636
+
637
+ cdef long e
638
+ try:
639
+ e = ee
640
+ except OverflowError:
641
+ return Polynomial.__pow__(self, ee, modulus)
642
+ if e != ee:
643
+ raise TypeError("Only integral powers defined.")
644
+ elif e < 0:
645
+ recip = 1 # delay because powering frac field elements is slow
646
+ e = -e
647
+
648
+ if not self:
649
+ return (<Polynomial_template>self)._parent(int(not e))
650
+
651
+ cdef type T = type(self)
652
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
653
+
654
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
655
+ parent = (<Polynomial_template>self)._parent
656
+ r._parent = parent
657
+ r._cparent = (<Polynomial_template>self)._cparent
658
+
659
+ if modulus is None:
660
+ celement_pow(&r.x, &(<Polynomial_template>self).x, e, NULL, (<Polynomial_template>self)._cparent)
661
+ else:
662
+ if parent is not (<Polynomial_template>modulus)._parent and parent != (<Polynomial_template>modulus)._parent:
663
+ modulus = parent.coerce(modulus)
664
+ if celement_is_zero(&(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent):
665
+ raise ZeroDivisionError("modulus must be nonzero")
666
+ if celement_is_one(&(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent):
667
+ return parent.zero()
668
+ celement_pow(&r.x, &(<Polynomial_template>self).x, e, &(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent)
669
+
670
+ #assert(r._parent(pari(self)**ee) == r)
671
+ if recip:
672
+ return ~r
673
+ else:
674
+ return r
675
+
676
+ def __copy__(self):
677
+ """
678
+ EXAMPLES::
679
+
680
+ sage: P.<x> = GF(2)[]
681
+ sage: copy(x) is x
682
+ False
683
+ sage: copy(x) == x
684
+ True
685
+ """
686
+ cdef type T = type(self)
687
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
688
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
689
+ r._parent = (<Polynomial_template>self)._parent
690
+ r._cparent = (<Polynomial_template>self)._cparent
691
+ celement_set(&r.x, &self.x, (<Polynomial_template>self)._cparent)
692
+ return r
693
+
694
+ def is_gen(self):
695
+ """
696
+ EXAMPLES::
697
+
698
+ sage: P.<x> = GF(2)[]
699
+ sage: x.is_gen()
700
+ True
701
+ sage: (x+1).is_gen()
702
+ False
703
+ """
704
+ cdef celement *gen = celement_new((<Polynomial_template>self)._cparent)
705
+ celement_gen(gen, 0, (<Polynomial_template>self)._cparent)
706
+ cdef bint r = celement_equal(&self.x, gen, (<Polynomial_template>self)._cparent)
707
+ celement_delete(gen, (<Polynomial_template>self)._cparent)
708
+ return r
709
+
710
+ def shift(self, int n):
711
+ """
712
+ EXAMPLES::
713
+
714
+ sage: P.<x> = GF(2)[]
715
+ sage: f = x^3 + x^2 + 1
716
+ sage: f.shift(1)
717
+ x^4 + x^3 + x
718
+ sage: f.shift(-1)
719
+ x^2 + x
720
+ """
721
+ return element_shift(self, n)
722
+
723
+ def __lshift__(self, int n):
724
+ """
725
+ EXAMPLES::
726
+
727
+ sage: P.<x> = GF(2)[]
728
+ sage: f = x^3 + x^2 + 1
729
+ sage: f << 1
730
+ x^4 + x^3 + x
731
+ sage: f << -1
732
+ x^2 + x
733
+ """
734
+ return element_shift(self, n)
735
+
736
+ def __rshift__(self, int n):
737
+ """
738
+ EXAMPLES::
739
+
740
+ sage: P.<x> = GF(2)[]
741
+ sage: x>>1
742
+ 1
743
+ sage: (x^2 + x)>>1
744
+ x + 1
745
+ sage: (x^2 + x) >> -1
746
+ x^3 + x^2
747
+ """
748
+ return element_shift(self, -n)
749
+
750
+ cpdef bint is_zero(self) except -1:
751
+ """
752
+ EXAMPLES::
753
+
754
+ sage: P.<x> = GF(2)[]
755
+ sage: x.is_zero()
756
+ False
757
+ """
758
+ return celement_is_zero(&self.x, (<Polynomial_template>self)._cparent)
759
+
760
+ cpdef bint is_one(self) except -1:
761
+ """
762
+ EXAMPLES::
763
+
764
+ sage: P.<x> = GF(2)[]
765
+ sage: P(1).is_one()
766
+ True
767
+ """
768
+ return celement_is_one(&self.x, (<Polynomial_template>self)._cparent)
769
+
770
+ def degree(self):
771
+ """
772
+ EXAMPLES::
773
+
774
+ sage: P.<x> = GF(2)[]
775
+ sage: x.degree()
776
+ 1
777
+ sage: P(1).degree()
778
+ 0
779
+ sage: P(0).degree()
780
+ -1
781
+ """
782
+ return Integer(celement_len(&self.x, (<Polynomial_template>self)._cparent)-1)
783
+
784
+ cpdef Polynomial truncate(self, long n):
785
+ r"""
786
+ Return this polynomial mod `x^n`.
787
+
788
+ EXAMPLES::
789
+
790
+ sage: R.<x> =GF(2)[]
791
+ sage: f = sum(x^n for n in range(10)); f
792
+ x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
793
+ sage: f.truncate(6)
794
+ x^5 + x^4 + x^3 + x^2 + x + 1
795
+
796
+ If the precision is higher than the degree of the polynomial then
797
+ the polynomial itself is returned::
798
+
799
+ sage: f.truncate(10) is f
800
+ True
801
+
802
+ If the precision is negative, the zero polynomial is returned::
803
+
804
+ sage: f.truncate(-1)
805
+ 0
806
+ """
807
+ if n >= celement_len(&self.x, (<Polynomial_template>self)._cparent):
808
+ return self
809
+
810
+ cdef type T = type(self)
811
+ cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
812
+ celement_construct(&r.x, (<Polynomial_template>self)._cparent)
813
+ r._parent = (<Polynomial_template>self)._parent
814
+ r._cparent = (<Polynomial_template>self)._cparent
815
+ if n <= 0:
816
+ return r
817
+ celement_truncate(&r.x, &self.x, n, (<Polynomial_template>self)._cparent)
818
+ return r
819
+
820
+ def _singular_(self, singular=None):
821
+ r"""
822
+ Return Singular representation of this polynomial.
823
+
824
+ INPUT:
825
+
826
+ - ``singular`` -- Singular interpreter (default: default interpreter)
827
+
828
+ EXAMPLES::
829
+
830
+ sage: P.<x> = PolynomialRing(GF(7))
831
+ sage: f = 3*x^2 + 2*x + 5
832
+ sage: singular(f) # needs sage.libs.singular
833
+ 3*x^2+2*x-2
834
+ """
835
+ if singular is None:
836
+ from sage.interfaces.singular import singular
837
+
838
+ self.parent()._singular_(singular).set_ring() # this is expensive
839
+ return singular(self._singular_init_())