-
Notifications
You must be signed in to change notification settings - Fork 4
/
trac_13949.patch
324 lines (293 loc) · 11.6 KB
/
trac_13949.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# HG changeset patch
# User Nils Bruin <[email protected]>
# Date 1357956228 28800
# Node ID 87b153d6a814c1c16c570bdbbb533a5e1df5599d
# Parent 047b99ba3b95d05094047423b0e8ad38e0afc71e
#13949: let mutability on Matrix be indicated by a simple bint flag _is_immutable
* * *
#13949: make some matrix initialization more efficient for some classes and document how to
diff --git a/sage/matrix/matrix0.pxd b/sage/matrix/matrix0.pxd
--- a/sage/matrix/matrix0.pxd
+++ b/sage/matrix/matrix0.pxd
@@ -18,7 +18,7 @@
cdef public object _cache
cdef public object _subdivisions
cdef public object _base_ring
- cdef sage.structure.mutability.Mutability _mutability
+ cdef bint _is_immutable
cdef bint _will_use_strassen(self, Matrix right) except -2
cdef bint _will_use_strassen_echelon(self) except -2
diff --git a/sage/matrix/matrix0.pyx b/sage/matrix/matrix0.pyx
--- a/sage/matrix/matrix0.pyx
+++ b/sage/matrix/matrix0.pyx
@@ -98,8 +98,19 @@
"""
def __init__(self, parent):
"""
+ The initialization routine of the Matrix base class ensures that it sets
+ the attributes self._parent, self._base_ring, self._nrows, self._ncols.
+ It sets the latter ones by accessing the relevant information on parent,
+ which is often slower than what a more specific subclass can do.
+
+ Subclasses of Matrix can safely skip calling Matrix.__init__ provided they
+ take care of initializing these attributes themselves.
+
+ The private attributes self._is_immutable and self._cache are implicitly
+ initialized to valid values upon memory allocation.
+
EXAMPLES::
-
+
sage: import sage.matrix.matrix0
sage: A = sage.matrix.matrix0.Matrix(MatrixSpace(QQ,2))
sage: type(A)
@@ -109,9 +120,7 @@
self._base_ring = parent.base_ring()
self._nrows = parent.nrows()
self._ncols = parent.ncols()
- self._mutability = Mutability(False)
- self._cache = {}
-
+
def copy(self):
"""
Make a copy of self. If self is immutable, the copy will be
@@ -393,6 +402,8 @@
{}
"""
+ if self._cache is None:
+ self._cache = {}
return self._cache
###########################################################
@@ -417,10 +428,10 @@
If self is mutable, the cache of results about self is deleted.
"""
- if self._mutability._is_immutable:
+ if self._is_immutable:
raise ValueError("matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).")
else:
- self._cache = {}
+ self._cache = None
cdef check_bounds_and_mutability(self, Py_ssize_t i, Py_ssize_t j):
"""
@@ -433,10 +444,10 @@
If self is mutable, the cache of results about self is deleted.
"""
- if self._mutability._is_immutable:
+ if self._is_immutable:
raise ValueError("matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).")
else:
- self._cache = {}
+ self._cache = None
if i<0 or i >= self._nrows or j<0 or j >= self._ncols:
raise IndexError("matrix index out of range")
@@ -493,7 +504,7 @@
{[10 1]
[ 2 3]: 1}
"""
- self._mutability.set_immutable()
+ self._is_immutable = True
def is_immutable(self):
"""
@@ -511,7 +522,7 @@
sage: A.is_immutable()
True
"""
- return self._mutability._is_immutable
+ return self._is_immutable
def is_mutable(self):
"""
@@ -529,7 +540,7 @@
sage: A.is_mutable()
False
"""
- return self._mutability.is_mutable()
+ return not(self._is_immutable)
###########################################################
# Entry access
@@ -1532,7 +1543,7 @@
True
"""
data, version = self._pickle()
- return unpickle, (self.__class__, self._parent, self._mutability,
+ return unpickle, (self.__class__, self._parent, self._is_immutable,
self._cache, data, version)
def _pickle(self):
@@ -1612,7 +1623,7 @@
raise TypeError("ring must be a ring")
if ring is self._base_ring:
- if self._mutability._is_immutable:
+ if self._is_immutable:
return self
return self.__copy__()
@@ -2130,18 +2141,18 @@
# with_ versions of these methods for this situation
###################################################
cdef check_row_bounds_and_mutability(self, Py_ssize_t r1, Py_ssize_t r2):
- if self._mutability._is_immutable:
+ if self._is_immutable:
raise ValueError("matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).")
else:
- self._cache = {}
+ self._cache = None
if r1<0 or r1 >= self._nrows or r2<0 or r2 >= self._nrows:
raise IndexError("matrix row index out of range")
cdef check_column_bounds_and_mutability(self, Py_ssize_t c1, Py_ssize_t c2):
- if self._mutability._is_immutable:
+ if self._is_immutable:
raise ValueError("matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).")
else:
- self._cache = {}
+ self._cache = None
if c1<0 or c1 >= self._ncols or c2<0 or c2 >= self._ncols:
raise IndexError("matrix column index out of range")
@@ -4833,7 +4844,7 @@
# Unpickling
#######################
-def unpickle(cls, parent, mutability, cache, data, version):
+def unpickle(cls, parent, immutability, cache, data, version):
r"""
Unpickle a matrix. This is only used internally by Sage. Users
should never call this function directly.
@@ -4859,7 +4870,7 @@
A._parent = parent # make sure -- __new__ doesn't have to set it, but unpickle may need to know.
A._nrows = parent.nrows()
A._ncols = parent.ncols()
- A._mutability = mutability
+ A._is_immutable = immutability
A._base_ring = parent.base_ring()
A._cache = cache
if version >= 0:
diff --git a/sage/matrix/matrix_cyclo_dense.pyx b/sage/matrix/matrix_cyclo_dense.pyx
--- a/sage/matrix/matrix_cyclo_dense.pyx
+++ b/sage/matrix/matrix_cyclo_dense.pyx
@@ -720,7 +720,7 @@
sage: A.__hash__()
-18
"""
- if self._mutability._is_immutable:
+ if self._is_immutable:
return self._hash()
else:
raise TypeError, "mutable matrices are unhashable"
diff --git a/sage/matrix/matrix_dense.pyx b/sage/matrix/matrix_dense.pyx
--- a/sage/matrix/matrix_dense.pyx
+++ b/sage/matrix/matrix_dense.pyx
@@ -66,7 +66,7 @@
x = self.fetch('hash')
if not x is None: return x
- if not self._mutability._is_immutable:
+ if not self._is_immutable:
raise TypeError, "mutable matrices are unhashable"
v = self._list()
diff --git a/sage/matrix/matrix_integer_2x2.pyx b/sage/matrix/matrix_integer_2x2.pyx
--- a/sage/matrix/matrix_integer_2x2.pyx
+++ b/sage/matrix/matrix_integer_2x2.pyx
@@ -129,12 +129,16 @@
# x * def _unpickle
########################################################################
- def __cinit__(self):
+ def __cinit__(self, parent, entries,copy, coerce):
mpz_init(self.a)
mpz_init(self.b)
mpz_init(self.c)
mpz_init(self.d)
self._entries = &self.a
+ self._parent = parent
+ self._base_ring = ZZ
+ self._nrows = 2
+ self._ncols = 2
def __init__(self, parent, entries, copy, coerce):
"""
@@ -156,8 +160,6 @@
TypeError: cannot construct an element of
Space of 2x2 integer matrices from [11, 3]!
"""
- matrix.Matrix.__init__(self, parent)
-
cdef Py_ssize_t i, n
if entries is None:
@@ -344,7 +346,7 @@
mpz_set(x.b, self.b)
mpz_set(x.c ,self.c)
mpz_set(x.d, self.d)
- x._mutability = Mutability(False)
+ x._is_immutable = False
x._base_ring = self._base_ring
if self._subdivisions is not None:
x.subdivide(*self.subdivisions())
diff --git a/sage/matrix/matrix_integer_dense.pyx b/sage/matrix/matrix_integer_dense.pyx
--- a/sage/matrix/matrix_integer_dense.pyx
+++ b/sage/matrix/matrix_integer_dense.pyx
@@ -208,7 +208,8 @@
This is for internal use only, or if you really know what
you're doing.
"""
- matrix_dense.Matrix_dense.__init__(self, parent)
+ self._parent = parent
+ self._base_ring = ZZ
self._nrows = parent.nrows()
self._ncols = parent.ncols()
self._pivots = None
diff --git a/sage/matrix/matrix_mod2_dense.pyx b/sage/matrix/matrix_mod2_dense.pyx
--- a/sage/matrix/matrix_mod2_dense.pyx
+++ b/sage/matrix/matrix_mod2_dense.pyx
@@ -337,7 +337,7 @@
0
"""
- if not self._mutability._is_immutable:
+ if not self._is_immutable:
raise TypeError("mutable matrices are unhashable")
x = self.fetch('hash')
diff --git a/sage/matrix/matrix_mod2e_dense.pyx b/sage/matrix/matrix_mod2e_dense.pyx
--- a/sage/matrix/matrix_mod2e_dense.pyx
+++ b/sage/matrix/matrix_mod2e_dense.pyx
@@ -1575,8 +1575,8 @@
if self._entries.finite_field.degree > 4:
raise NotImplementedError("Cling is only implemented for degree <= 4.")
- if self._mutability._is_immutable:
- raise TypeError("Mutable matrices cannot be modified.")
+ if self._is_immutable:
+ raise TypeError("Immutable matrices cannot be modified.")
if len(C) != self._entries.finite_field.degree:
raise ValueError("The number of input matrices must be equal to the degree of the base field.")
diff --git a/sage/matrix/matrix_modn_dense.pyx b/sage/matrix/matrix_modn_dense.pyx
--- a/sage/matrix/matrix_modn_dense.pyx
+++ b/sage/matrix/matrix_modn_dense.pyx
@@ -608,7 +608,7 @@
x = self.fetch('hash')
if not x is None: return x
- if not self._mutability._is_immutable:
+ if not self._is_immutable:
raise TypeError, "mutable matrices are unhashable"
cdef Py_ssize_t i
diff --git a/sage/matrix/matrix_rational_dense.pyx b/sage/matrix/matrix_rational_dense.pyx
--- a/sage/matrix/matrix_rational_dense.pyx
+++ b/sage/matrix/matrix_rational_dense.pyx
@@ -1349,7 +1349,7 @@
raise TypeError("R must be a ring")
from matrix_modn_dense import MAX_MODULUS
if R == self._base_ring:
- if self._mutability._is_immutable:
+ if self._is_immutable:
return self
return self.__copy__()
elif is_IntegerRing(R):
diff --git a/sage/matrix/matrix_sparse.pyx b/sage/matrix/matrix_sparse.pyx
--- a/sage/matrix/matrix_sparse.pyx
+++ b/sage/matrix/matrix_sparse.pyx
@@ -60,7 +60,7 @@
if not is_Ring(ring):
raise TypeError, "input must be a ring"
if ring is self._base_ring:
- if self._mutability._is_immutable:
+ if self._is_immutable:
return self
return self.__copy__()
@@ -135,7 +135,7 @@
x = self.fetch('hash')
if not x is None: return x
- if not self._mutability._is_immutable:
+ if not self._is_immutable:
raise TypeError, "mutable matrices are unhashable"
v = self._dict()