| 1 | r""" |
|---|
| 2 | Matrix Spaces. |
|---|
| 3 | |
|---|
| 4 | You can create any space $\text{Mat}_{n\times m}(R)$ of either dense |
|---|
| 5 | or sparse matrices with given number of rows and columns over any |
|---|
| 6 | commutative or noncommutative ring. |
|---|
| 7 | |
|---|
| 8 | EXAMPLES: |
|---|
| 9 | sage: MS = MatrixSpace(QQ,6,6,sparse=True); MS |
|---|
| 10 | Full MatrixSpace of 6 by 6 sparse matrices over Rational Field |
|---|
| 11 | sage: MS.base_ring() |
|---|
| 12 | Rational Field |
|---|
| 13 | sage: MS = MatrixSpace(ZZ,3,5,sparse=False); MS |
|---|
| 14 | Full MatrixSpace of 3 by 5 dense matrices over Integer Ring |
|---|
| 15 | |
|---|
| 16 | TESTS: |
|---|
| 17 | sage: matrix(RR,2,2,sparse=True) |
|---|
| 18 | [0.000000000000000 0.000000000000000] |
|---|
| 19 | [0.000000000000000 0.000000000000000] |
|---|
| 20 | sage: matrix(GF(11),2,2,sparse=True) |
|---|
| 21 | [0 0] |
|---|
| 22 | [0 0] |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | # System imports |
|---|
| 26 | import types |
|---|
| 27 | import random |
|---|
| 28 | import weakref |
|---|
| 29 | |
|---|
| 30 | # SAGE matrix imports |
|---|
| 31 | import matrix |
|---|
| 32 | import matrix_generic_dense |
|---|
| 33 | import matrix_generic_sparse |
|---|
| 34 | |
|---|
| 35 | import matrix_modn_dense |
|---|
| 36 | import matrix_modn_sparse |
|---|
| 37 | |
|---|
| 38 | import matrix_mod2_dense |
|---|
| 39 | #import matrix_mod2_sparse |
|---|
| 40 | |
|---|
| 41 | import matrix_integer_dense |
|---|
| 42 | import matrix_integer_sparse |
|---|
| 43 | |
|---|
| 44 | import matrix_rational_dense |
|---|
| 45 | import matrix_rational_sparse |
|---|
| 46 | |
|---|
| 47 | import matrix_mpolynomial_dense |
|---|
| 48 | |
|---|
| 49 | #import padics.matrix_padic_capped_relative_dense |
|---|
| 50 | |
|---|
| 51 | ## import matrix_cyclo_dense |
|---|
| 52 | ## import matrix_cyclo_sparse |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | # IMPORTANT - these two guys get imported below only later |
|---|
| 56 | # since they currently force numpy to import, which takes |
|---|
| 57 | # a *long* time. |
|---|
| 58 | #import matrix_real_double_dense |
|---|
| 59 | #import matrix_complex_double_dense |
|---|
| 60 | |
|---|
| 61 | import sage.groups.matrix_gps.matrix_group_element |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | # SAGE imports |
|---|
| 65 | import sage.structure.parent_gens as parent_gens |
|---|
| 66 | import sage.rings.ring as ring |
|---|
| 67 | import sage.rings.rational_field as rational_field |
|---|
| 68 | import sage.rings.integer_ring as integer_ring |
|---|
| 69 | import sage.rings.integer as integer |
|---|
| 70 | import sage.rings.field as field |
|---|
| 71 | import sage.rings.finite_field as finite_field |
|---|
| 72 | import sage.rings.principal_ideal_domain as principal_ideal_domain |
|---|
| 73 | import sage.rings.integral_domain as integral_domain |
|---|
| 74 | import sage.rings.number_field.all |
|---|
| 75 | import sage.rings.integer_mod_ring |
|---|
| 76 | import sage.rings.polynomial.multi_polynomial_ring_generic |
|---|
| 77 | import sage.rings.padics.padic_ring_capped_relative |
|---|
| 78 | import sage.misc.latex as latex |
|---|
| 79 | #import sage.rings.real_double as real_double |
|---|
| 80 | |
|---|
| 81 | import sage.modules.free_module_element |
|---|
| 82 | import sage.modules.free_module |
|---|
| 83 | |
|---|
| 84 | from sage.structure.sequence import Sequence |
|---|
| 85 | |
|---|
| 86 | def is_MatrixSpace(x): |
|---|
| 87 | """ |
|---|
| 88 | returns true if self is an instance of MatrixSpace |
|---|
| 89 | returns false if self is not an instance of MatrixSpace |
|---|
| 90 | |
|---|
| 91 | EXAMPLES: |
|---|
| 92 | sage: MS = MatrixSpace(QQ,2) |
|---|
| 93 | sage: A = MS.random_element() |
|---|
| 94 | sage: is_MatrixSpace(MS) |
|---|
| 95 | True |
|---|
| 96 | sage: is_MatrixSpace(A) |
|---|
| 97 | False |
|---|
| 98 | sage: is_MatrixSpace(5) |
|---|
| 99 | False |
|---|
| 100 | """ |
|---|
| 101 | |
|---|
| 102 | return isinstance(x, MatrixSpace_generic) |
|---|
| 103 | |
|---|
| 104 | _cache = {} |
|---|
| 105 | def MatrixSpace(base_ring, nrows, ncols=None, sparse=False): |
|---|
| 106 | """ |
|---|
| 107 | Create with the command |
|---|
| 108 | |
|---|
| 109 | MatrixSpace(base_ring , nrows [, ncols] [, sparse]) |
|---|
| 110 | |
|---|
| 111 | The default value of the optional argument sparse is False. |
|---|
| 112 | The default value of the optional argument ncols is nrows. |
|---|
| 113 | |
|---|
| 114 | INPUT: |
|---|
| 115 | base_ring -- a ring |
|---|
| 116 | nrows -- int, the number of rows |
|---|
| 117 | ncols -- (default nrows) int, the number of columns |
|---|
| 118 | sparse -- (default false) whether or not matrices are given |
|---|
| 119 | a sparse representation |
|---|
| 120 | OUTPUT: |
|---|
| 121 | The unique space of all nrows x ncols matrices over base_ring. |
|---|
| 122 | |
|---|
| 123 | EXAMPLES: |
|---|
| 124 | sage: MS = MatrixSpace(RationalField(),2) |
|---|
| 125 | sage: MS.base_ring() |
|---|
| 126 | Rational Field |
|---|
| 127 | sage: MS.dimension() |
|---|
| 128 | 4 |
|---|
| 129 | sage: MS.dims() |
|---|
| 130 | (2, 2) |
|---|
| 131 | sage: B = MS.basis() |
|---|
| 132 | sage: B |
|---|
| 133 | [ |
|---|
| 134 | [1 0] |
|---|
| 135 | [0 0], |
|---|
| 136 | [0 1] |
|---|
| 137 | [0 0], |
|---|
| 138 | [0 0] |
|---|
| 139 | [1 0], |
|---|
| 140 | [0 0] |
|---|
| 141 | [0 1] |
|---|
| 142 | ] |
|---|
| 143 | sage: B[0] |
|---|
| 144 | [1 0] |
|---|
| 145 | [0 0] |
|---|
| 146 | sage: B[1] |
|---|
| 147 | [0 1] |
|---|
| 148 | [0 0] |
|---|
| 149 | sage: B[2] |
|---|
| 150 | [0 0] |
|---|
| 151 | [1 0] |
|---|
| 152 | sage: B[3] |
|---|
| 153 | [0 0] |
|---|
| 154 | [0 1] |
|---|
| 155 | sage: A = MS.matrix([1,2,3,4]) |
|---|
| 156 | sage: A |
|---|
| 157 | [1 2] |
|---|
| 158 | [3 4] |
|---|
| 159 | sage: MS2 = MatrixSpace(RationalField(),2,3) |
|---|
| 160 | sage: B = MS2.matrix([1,2,3,4,5,6]) |
|---|
| 161 | sage: A*B |
|---|
| 162 | [ 9 12 15] |
|---|
| 163 | [19 26 33] |
|---|
| 164 | |
|---|
| 165 | sage: M = MatrixSpace(ZZ, 10) |
|---|
| 166 | sage: M |
|---|
| 167 | Full MatrixSpace of 10 by 10 dense matrices over Integer Ring |
|---|
| 168 | sage: loads(M.dumps()) == M |
|---|
| 169 | True |
|---|
| 170 | |
|---|
| 171 | """ |
|---|
| 172 | if not sage.rings.ring.is_Ring(base_ring): |
|---|
| 173 | raise TypeError, "base_ring (=%s) must be a ring"%base_ring |
|---|
| 174 | |
|---|
| 175 | if ncols is None: ncols = nrows |
|---|
| 176 | nrows = int(nrows); ncols = int(ncols); sparse=bool(sparse) |
|---|
| 177 | key = (base_ring, nrows, ncols, sparse) |
|---|
| 178 | if _cache.has_key(key): |
|---|
| 179 | M = _cache[key]() |
|---|
| 180 | if not M is None: return M |
|---|
| 181 | |
|---|
| 182 | M = MatrixSpace_generic(base_ring, nrows, ncols, sparse) |
|---|
| 183 | |
|---|
| 184 | _cache[key] = weakref.ref(M) |
|---|
| 185 | return M |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | class MatrixSpace_generic(parent_gens.ParentWithGens): |
|---|
| 190 | """ |
|---|
| 191 | The space of all nrows x ncols matrices over base_ring. |
|---|
| 192 | |
|---|
| 193 | EXAMPLES: |
|---|
| 194 | sage: MatrixSpace(ZZ,10,5) |
|---|
| 195 | Full MatrixSpace of 10 by 5 dense matrices over Integer Ring |
|---|
| 196 | sage: MatrixSpace(ZZ,10,2^33) |
|---|
| 197 | Traceback (most recent call last): # 32-bit |
|---|
| 198 | ... # 32-bit |
|---|
| 199 | ValueError: number of rows and columns must be less than 2^32 (on a 32-bit computer -- use a 64-bit computer for bigger matrices) # 32-bit |
|---|
| 200 | Full MatrixSpace of 10 by 8589934592 dense matrices over Integer Ring # 64-bit |
|---|
| 201 | """ |
|---|
| 202 | def __init__(self, base_ring, |
|---|
| 203 | nrows, |
|---|
| 204 | ncols=None, |
|---|
| 205 | sparse=False): |
|---|
| 206 | parent_gens.ParentWithGens.__init__(self, base_ring) |
|---|
| 207 | if not isinstance(base_ring, ring.Ring): |
|---|
| 208 | raise TypeError, "base_ring must be a ring" |
|---|
| 209 | if ncols == None: ncols = nrows |
|---|
| 210 | nrows = int(nrows) |
|---|
| 211 | ncols = int(ncols) |
|---|
| 212 | if nrows < 0: |
|---|
| 213 | raise ArithmeticError, "nrows must be nonnegative" |
|---|
| 214 | if ncols < 0: |
|---|
| 215 | raise ArithmeticError, "ncols must be nonnegative" |
|---|
| 216 | |
|---|
| 217 | if sage.misc.misc.is_64bit(): |
|---|
| 218 | if nrows >= 2**64 or ncols >= 2**64: |
|---|
| 219 | raise ValueError, "number of rows and columns must be less than 2^64" |
|---|
| 220 | else: |
|---|
| 221 | if nrows >= 2**32 or ncols >= 2**32: |
|---|
| 222 | raise ValueError, "number of rows and columns must be less than 2^32 (on a 32-bit computer -- use a 64-bit computer for bigger matrices)" |
|---|
| 223 | |
|---|
| 224 | self.__nrows = nrows |
|---|
| 225 | self.__is_sparse = sparse |
|---|
| 226 | if ncols == None: |
|---|
| 227 | self.__ncols = nrows |
|---|
| 228 | else: |
|---|
| 229 | self.__ncols = ncols |
|---|
| 230 | self.__matrix_class = self._get_matrix_class() |
|---|
| 231 | |
|---|
| 232 | def __reduce__(self): |
|---|
| 233 | """ |
|---|
| 234 | EXAMPLES: |
|---|
| 235 | sage: A = Mat(ZZ,5,7,sparse=True) |
|---|
| 236 | sage: A |
|---|
| 237 | Full MatrixSpace of 5 by 7 sparse matrices over Integer Ring |
|---|
| 238 | sage: loads(dumps(A)) == A |
|---|
| 239 | True |
|---|
| 240 | """ |
|---|
| 241 | return MatrixSpace, (self.base_ring(), self.__nrows, self.__ncols, self.__is_sparse) |
|---|
| 242 | |
|---|
| 243 | def __call__(self, entries=0, coerce=True, copy=True): |
|---|
| 244 | """ |
|---|
| 245 | EXAMPLES: |
|---|
| 246 | sage: k = GF(7); G = MatrixGroup([matrix(k,2,[1,1,0,1]), matrix(k,2,[1,0,0,2])]) |
|---|
| 247 | sage: g = G.0 |
|---|
| 248 | sage: MatrixSpace(k,2)(g) |
|---|
| 249 | [1 1] |
|---|
| 250 | [0 1] |
|---|
| 251 | """ |
|---|
| 252 | if entries is None: |
|---|
| 253 | entries = 0 |
|---|
| 254 | |
|---|
| 255 | if entries == 0 and hasattr(self, '__zero_matrix'): |
|---|
| 256 | return self.zero_matrix() |
|---|
| 257 | |
|---|
| 258 | if isinstance(entries, list) and len(entries) > 0 and \ |
|---|
| 259 | sage.modules.free_module_element.is_FreeModuleElement(entries[0]): |
|---|
| 260 | if self.__is_sparse: |
|---|
| 261 | e = {} |
|---|
| 262 | zero = self.base_ring()(0) |
|---|
| 263 | for i in xrange(len(entries)): |
|---|
| 264 | for j, x in entries[i].iteritems(): |
|---|
| 265 | if x != zero: |
|---|
| 266 | e[(i,j)] = x |
|---|
| 267 | entries = e |
|---|
| 268 | else: |
|---|
| 269 | entries = sum([v.list() for v in entries],[]) |
|---|
| 270 | if not self.__is_sparse and isinstance(entries, dict): |
|---|
| 271 | entries = dict_to_list(entries, self.__nrows, self.__ncols) |
|---|
| 272 | coerce = True |
|---|
| 273 | copy = False |
|---|
| 274 | elif self.__is_sparse and isinstance(entries, (list, tuple)): |
|---|
| 275 | entries = list_to_dict(entries, self.__nrows, self.__ncols) |
|---|
| 276 | coerce = True |
|---|
| 277 | copy = False |
|---|
| 278 | elif sage.groups.matrix_gps.matrix_group_element.is_MatrixGroupElement(entries): |
|---|
| 279 | return self(entries.matrix(), copy=False) |
|---|
| 280 | return self.matrix(entries, copy=copy, coerce=coerce) |
|---|
| 281 | |
|---|
| 282 | def change_ring(self, R): |
|---|
| 283 | """ |
|---|
| 284 | Return matrix space over R with otherwise same parameters as self. |
|---|
| 285 | |
|---|
| 286 | INPUT: |
|---|
| 287 | R -- ring |
|---|
| 288 | |
|---|
| 289 | OUTPUT: |
|---|
| 290 | a matrix space |
|---|
| 291 | |
|---|
| 292 | EXAMPLES: |
|---|
| 293 | sage: Mat(QQ,3,5).change_ring(GF(7)) |
|---|
| 294 | Full MatrixSpace of 3 by 5 dense matrices over Finite Field of size 7 |
|---|
| 295 | """ |
|---|
| 296 | try: |
|---|
| 297 | return self.__change_ring[R] |
|---|
| 298 | except AttributeError: |
|---|
| 299 | self.__change_ring = {} |
|---|
| 300 | except KeyError: |
|---|
| 301 | pass |
|---|
| 302 | M = MatrixSpace(R, self.__nrows, self.__ncols, self.__is_sparse) |
|---|
| 303 | self.__change_ring[R] = M |
|---|
| 304 | return M |
|---|
| 305 | |
|---|
| 306 | def base_extend(self, R): |
|---|
| 307 | """ |
|---|
| 308 | Return base extension of this matrix space to R. |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | INPUT: |
|---|
| 312 | R -- ring |
|---|
| 313 | |
|---|
| 314 | OUTPUT: |
|---|
| 315 | a matrix space |
|---|
| 316 | |
|---|
| 317 | EXAMPLES: |
|---|
| 318 | sage: Mat(ZZ,3,5).base_extend(QQ) |
|---|
| 319 | Full MatrixSpace of 3 by 5 dense matrices over Rational Field |
|---|
| 320 | sage: Mat(QQ,3,5).base_extend(GF(7)) |
|---|
| 321 | Traceback (most recent call last): |
|---|
| 322 | ... |
|---|
| 323 | TypeError: no base extension defined |
|---|
| 324 | """ |
|---|
| 325 | if R.has_coerce_map_from(self.base_ring()): |
|---|
| 326 | return self.change_ring(R) |
|---|
| 327 | raise TypeError, "no base extension defined" |
|---|
| 328 | |
|---|
| 329 | def construction(self): |
|---|
| 330 | from sage.categories.pushout import MatrixFunctor |
|---|
| 331 | return MatrixFunctor(self.__nrows, self.__ncols), self.base_ring() |
|---|
| 332 | |
|---|
| 333 | def _coerce_impl(self, x): |
|---|
| 334 | """ |
|---|
| 335 | EXAMPLES: |
|---|
| 336 | sage: MS1 = MatrixSpace(QQ,3) |
|---|
| 337 | sage: MS2 = MatrixSpace(ZZ,4,5,true) |
|---|
| 338 | sage: A = MS1(range(9)) |
|---|
| 339 | sage: D = MS2(range(20)) |
|---|
| 340 | sage: MS1._coerce_(A) |
|---|
| 341 | [0 1 2] |
|---|
| 342 | [3 4 5] |
|---|
| 343 | [6 7 8] |
|---|
| 344 | sage: MS2._coerce_(D) |
|---|
| 345 | [ 0 1 2 3 4] |
|---|
| 346 | [ 5 6 7 8 9] |
|---|
| 347 | [10 11 12 13 14] |
|---|
| 348 | [15 16 17 18 19] |
|---|
| 349 | """ |
|---|
| 350 | if isinstance(x, matrix.Matrix): |
|---|
| 351 | if self.is_sparse() and x.is_dense(): |
|---|
| 352 | raise TypeError, "cannot coerce dense matrix into sparse space for arithmetic" |
|---|
| 353 | if x.nrows() == self.nrows() and x.ncols() == self.ncols(): |
|---|
| 354 | if self.base_ring().has_coerce_map_from(x.base_ring()): |
|---|
| 355 | return self(x) |
|---|
| 356 | raise TypeError, "no canonical coercion" |
|---|
| 357 | return self._coerce_try(x, self.base_ring()) |
|---|
| 358 | |
|---|
| 359 | def __cmp__(self, other): |
|---|
| 360 | """ |
|---|
| 361 | Compare this matrix space with other. Sparse and dense matrix |
|---|
| 362 | spaces with otherwise the same parameters are considered |
|---|
| 363 | equal. |
|---|
| 364 | |
|---|
| 365 | If other is not a matrix space, return something arbitrary but |
|---|
| 366 | deterministic. Otherwise, compare based on base ring, then on |
|---|
| 367 | number of rows and columns. |
|---|
| 368 | |
|---|
| 369 | EXAMPLES: |
|---|
| 370 | sage: Mat(ZZ,1000) == Mat(QQ,1000) |
|---|
| 371 | False |
|---|
| 372 | sage: Mat(ZZ,10) == Mat(ZZ,10) |
|---|
| 373 | True |
|---|
| 374 | sage: Mat(ZZ,10, sparse=False) == Mat(ZZ,10, sparse=True) |
|---|
| 375 | True |
|---|
| 376 | """ |
|---|
| 377 | if isinstance(other, MatrixSpace_generic): |
|---|
| 378 | return cmp((self.base_ring(), self.__nrows, self.__ncols), |
|---|
| 379 | (other.base_ring(), other.__nrows, other.__ncols)) |
|---|
| 380 | return cmp(type(self), type(other)) |
|---|
| 381 | |
|---|
| 382 | def _repr_(self): |
|---|
| 383 | """ |
|---|
| 384 | Returns the string representation of a MatrixSpace |
|---|
| 385 | |
|---|
| 386 | EXAMPLES: |
|---|
| 387 | sage: MS = MatrixSpace(ZZ,2,4,true) |
|---|
| 388 | sage: repr(MS) |
|---|
| 389 | 'Full MatrixSpace of 2 by 4 sparse matrices over Integer Ring' |
|---|
| 390 | sage: MS |
|---|
| 391 | Full MatrixSpace of 2 by 4 sparse matrices over Integer Ring |
|---|
| 392 | """ |
|---|
| 393 | if self.is_sparse(): |
|---|
| 394 | s = "sparse" |
|---|
| 395 | else: |
|---|
| 396 | s = "dense" |
|---|
| 397 | return "Full MatrixSpace of %s by %s %s matrices over %s"%( |
|---|
| 398 | self.__nrows, self.__ncols, s, self.base_ring()) |
|---|
| 399 | |
|---|
| 400 | def _latex_(self): |
|---|
| 401 | r""" |
|---|
| 402 | Returns the latex representation of a MatrixSpace |
|---|
| 403 | |
|---|
| 404 | EXAMPLES: |
|---|
| 405 | sage: MS3 = MatrixSpace(QQ,6,6,true) |
|---|
| 406 | sage: latex(MS3) |
|---|
| 407 | \mbox{\rm Mat}_{6\times 6}(\mathbf{Q}) |
|---|
| 408 | """ |
|---|
| 409 | return "\\mbox{\\rm Mat}_{%s\\times %s}(%s)"%(self.nrows(), self.ncols(), |
|---|
| 410 | latex.latex(self.base_ring())) |
|---|
| 411 | |
|---|
| 412 | def _get_matrix_class(self): |
|---|
| 413 | """ |
|---|
| 414 | Returns the class of self |
|---|
| 415 | |
|---|
| 416 | EXAMPLES: |
|---|
| 417 | sage: MS1 = MatrixSpace(QQ,4) |
|---|
| 418 | sage: MS2 = MatrixSpace(ZZ,4,5,true) |
|---|
| 419 | sage: MS1._get_matrix_class() |
|---|
| 420 | <type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'> |
|---|
| 421 | sage: MS2._get_matrix_class() |
|---|
| 422 | <type 'sage.matrix.matrix_integer_sparse.Matrix_integer_sparse'> |
|---|
| 423 | """ |
|---|
| 424 | R = self.base_ring() |
|---|
| 425 | if self.is_dense(): |
|---|
| 426 | if sage.rings.integer_ring.is_IntegerRing(R): |
|---|
| 427 | return matrix_integer_dense.Matrix_integer_dense |
|---|
| 428 | elif sage.rings.rational_field.is_RationalField(R): |
|---|
| 429 | return matrix_rational_dense.Matrix_rational_dense |
|---|
| 430 | elif R==sage.rings.real_double.RDF: |
|---|
| 431 | import matrix_real_double_dense |
|---|
| 432 | return matrix_real_double_dense.Matrix_real_double_dense |
|---|
| 433 | elif R==sage.rings.complex_double.CDF: |
|---|
| 434 | import matrix_complex_double_dense |
|---|
| 435 | return matrix_complex_double_dense.Matrix_complex_double_dense |
|---|
| 436 | elif sage.rings.integer_mod_ring.is_IntegerModRing(R) and R.order() < matrix_modn_dense.MAX_MODULUS: |
|---|
| 437 | if R.order() == 2: |
|---|
| 438 | return matrix_mod2_dense.Matrix_mod2_dense |
|---|
| 439 | return matrix_modn_dense.Matrix_modn_dense |
|---|
| 440 | elif sage.rings.polynomial.multi_polynomial_ring_generic.is_MPolynomialRing(R) and R.base_ring().is_field(): |
|---|
| 441 | return matrix_mpolynomial_dense.Matrix_mpolynomial_dense |
|---|
| 442 | #elif isinstance(R, sage.rings.padics.padic_ring_capped_relative.pAdicRingCappedRelative): |
|---|
| 443 | # return padics.matrix_padic_capped_relative_dense |
|---|
| 444 | # the default |
|---|
| 445 | return matrix_generic_dense.Matrix_generic_dense |
|---|
| 446 | |
|---|
| 447 | else: |
|---|
| 448 | if sage.rings.integer_mod_ring.is_IntegerModRing(R) and R.order() < matrix_modn_sparse.MAX_MODULUS: |
|---|
| 449 | return matrix_modn_sparse.Matrix_modn_sparse |
|---|
| 450 | elif sage.rings.rational_field.is_RationalField(R): |
|---|
| 451 | return matrix_rational_sparse.Matrix_rational_sparse |
|---|
| 452 | elif sage.rings.integer_ring.is_IntegerRing(R): |
|---|
| 453 | return matrix_integer_sparse.Matrix_integer_sparse |
|---|
| 454 | # the default |
|---|
| 455 | return matrix_generic_sparse.Matrix_generic_sparse |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | def basis(self): |
|---|
| 459 | """ |
|---|
| 460 | Returns a basis for this matrix space. |
|---|
| 461 | |
|---|
| 462 | WARNING: This will of course compute every generator of this |
|---|
| 463 | matrix space. So for large matrices, this could take a long |
|---|
| 464 | time, waste a massive amount of memory (for dense matrices), |
|---|
| 465 | and is likely not very useful. Don't use this on large matrix |
|---|
| 466 | spaces. |
|---|
| 467 | |
|---|
| 468 | EXAMPLES: |
|---|
| 469 | sage: Mat(ZZ,2,2).basis() |
|---|
| 470 | [ |
|---|
| 471 | [1 0] |
|---|
| 472 | [0 0], |
|---|
| 473 | [0 1] |
|---|
| 474 | [0 0], |
|---|
| 475 | [0 0] |
|---|
| 476 | [1 0], |
|---|
| 477 | [0 0] |
|---|
| 478 | [0 1] |
|---|
| 479 | ] |
|---|
| 480 | """ |
|---|
| 481 | v = [self.zero_matrix() for _ in range(self.dimension())] |
|---|
| 482 | one = self.base_ring()(1) |
|---|
| 483 | i = 0 |
|---|
| 484 | for r in range(self.__nrows): |
|---|
| 485 | for c in range(self.__ncols): |
|---|
| 486 | v[i][r,c] = one |
|---|
| 487 | v[i].set_immutable() |
|---|
| 488 | i += 1 |
|---|
| 489 | return Sequence(v, universe=self, check=False, immutable=True, cr=True) |
|---|
| 490 | |
|---|
| 491 | def dimension(self): |
|---|
| 492 | """ |
|---|
| 493 | Returns (m rows) * (n cols) of self as Integer |
|---|
| 494 | |
|---|
| 495 | EXAMPLES: |
|---|
| 496 | sage: MS = MatrixSpace(ZZ,4,6) |
|---|
| 497 | sage: u = MS.dimension() |
|---|
| 498 | sage: u - 24 == 0 |
|---|
| 499 | True |
|---|
| 500 | """ |
|---|
| 501 | return self.__nrows * self.__ncols |
|---|
| 502 | |
|---|
| 503 | def dims(self): |
|---|
| 504 | """ |
|---|
| 505 | Returns (m row, n col) representation of self dimension |
|---|
| 506 | |
|---|
| 507 | EXAMPLES: |
|---|
| 508 | sage: MS = MatrixSpace(ZZ,4,6) |
|---|
| 509 | sage: MS.dims() |
|---|
| 510 | (4, 6) |
|---|
| 511 | """ |
|---|
| 512 | return (self.__nrows, self.__ncols) |
|---|
| 513 | |
|---|
| 514 | def identity_matrix(self): |
|---|
| 515 | """ |
|---|
| 516 | Create an identity matrix in self. (Must be a space of square matrices). |
|---|
| 517 | |
|---|
| 518 | EXAMPLES: |
|---|
| 519 | sage: MS1 = MatrixSpace(ZZ,4) |
|---|
| 520 | sage: MS2 = MatrixSpace(QQ,3,4) |
|---|
| 521 | sage: I = MS1.identity_matrix() |
|---|
| 522 | sage: I |
|---|
| 523 | [1 0 0 0] |
|---|
| 524 | [0 1 0 0] |
|---|
| 525 | [0 0 1 0] |
|---|
| 526 | [0 0 0 1] |
|---|
| 527 | sage: Er = MS2.identity_matrix() |
|---|
| 528 | Traceback (most recent call last): |
|---|
| 529 | ... |
|---|
| 530 | TypeError: self must be a space of square matrices |
|---|
| 531 | """ |
|---|
| 532 | if self.__nrows != self.__ncols: |
|---|
| 533 | raise TypeError, "self must be a space of square matrices" |
|---|
| 534 | A = self(0) |
|---|
| 535 | for i in xrange(self.__nrows): |
|---|
| 536 | A[i,i] = 1 |
|---|
| 537 | return A |
|---|
| 538 | |
|---|
| 539 | def is_dense(self): |
|---|
| 540 | """ |
|---|
| 541 | Returns True if matrices in self are dense and False otherwise. |
|---|
| 542 | |
|---|
| 543 | EXAMPLES: |
|---|
| 544 | sage: Mat(RDF,2,3).is_sparse() |
|---|
| 545 | False |
|---|
| 546 | sage: Mat(RR,123456,22,sparse=True).is_sparse() |
|---|
| 547 | True |
|---|
| 548 | """ |
|---|
| 549 | return not self.__is_sparse |
|---|
| 550 | |
|---|
| 551 | def is_sparse(self): |
|---|
| 552 | """ |
|---|
| 553 | Returns True if matrices in self are sparse and False otherwise. |
|---|
| 554 | |
|---|
| 555 | EXAMPLES: |
|---|
| 556 | sage: Mat(GF(2011),10000).is_sparse() |
|---|
| 557 | False |
|---|
| 558 | sage: Mat(GF(2011),10000,sparse=True).is_sparse() |
|---|
| 559 | True |
|---|
| 560 | """ |
|---|
| 561 | return self.__is_sparse |
|---|
| 562 | |
|---|
| 563 | def gen(self, n): |
|---|
| 564 | """ |
|---|
| 565 | Return the n-th generator of this matrix space. |
|---|
| 566 | |
|---|
| 567 | This doesn't compute all basis matrices, so it is reasonably intelligent. |
|---|
| 568 | |
|---|
| 569 | EXAMPLES: |
|---|
| 570 | sage: M = Mat(GF(7),10000,5); M.ngens() |
|---|
| 571 | 50000 |
|---|
| 572 | sage: a = M.10 |
|---|
| 573 | sage: a[:4] |
|---|
| 574 | [0 0 0 0 0] |
|---|
| 575 | [0 0 0 0 0] |
|---|
| 576 | [1 0 0 0 0] |
|---|
| 577 | [0 0 0 0 0] |
|---|
| 578 | """ |
|---|
| 579 | if hasattr(self, '__basis'): |
|---|
| 580 | return self.__basis[n] |
|---|
| 581 | r = n // self.__ncols |
|---|
| 582 | c = n - (r * self.__ncols) |
|---|
| 583 | z = self.zero_matrix() |
|---|
| 584 | z[r,c] = 1 |
|---|
| 585 | return z |
|---|
| 586 | |
|---|
| 587 | def zero_matrix(self): |
|---|
| 588 | """ |
|---|
| 589 | Return the zero matrix. |
|---|
| 590 | """ |
|---|
| 591 | try: |
|---|
| 592 | z = self.__zero_matrix |
|---|
| 593 | except AttributeError: |
|---|
| 594 | z = self(0) |
|---|
| 595 | self.__zero_matrix = z |
|---|
| 596 | return z.__copy__() |
|---|
| 597 | |
|---|
| 598 | def ngens(self): |
|---|
| 599 | """ |
|---|
| 600 | Return the number of generators of this matrix space, which is the number |
|---|
| 601 | of entries in the matrices in this space. |
|---|
| 602 | |
|---|
| 603 | EXAMPLES: |
|---|
| 604 | sage: M = Mat(GF(7),100,200); M.ngens() |
|---|
| 605 | 20000 |
|---|
| 606 | """ |
|---|
| 607 | return self.dimension() |
|---|
| 608 | |
|---|
| 609 | def matrix(self, x=0, coerce=True, copy=True): |
|---|
| 610 | """ |
|---|
| 611 | Create a matrix in self. The entries can be specified either |
|---|
| 612 | as a single list of length nrows*ncols, or as a list of |
|---|
| 613 | lists. |
|---|
| 614 | |
|---|
| 615 | EXAMPLES: |
|---|
| 616 | sage: M = MatrixSpace(ZZ, 2) |
|---|
| 617 | sage: M.matrix([[1,0],[0,-1]]) |
|---|
| 618 | [ 1 0] |
|---|
| 619 | [ 0 -1] |
|---|
| 620 | sage: M.matrix([1,0,0,-1]) |
|---|
| 621 | [ 1 0] |
|---|
| 622 | [ 0 -1] |
|---|
| 623 | """ |
|---|
| 624 | if isinstance(x, (types.GeneratorType, xrange)): |
|---|
| 625 | x = list(x) |
|---|
| 626 | elif isinstance(x, (int, integer.Integer)) and x==1: |
|---|
| 627 | return self.identity_matrix() |
|---|
| 628 | if matrix.is_Matrix(x): |
|---|
| 629 | if x.parent() is self: |
|---|
| 630 | if x.is_immutable(): |
|---|
| 631 | return x |
|---|
| 632 | else: |
|---|
| 633 | return x.copy() |
|---|
| 634 | x = x.list() |
|---|
| 635 | if isinstance(x, list) and len(x) > 0: |
|---|
| 636 | if isinstance(x[0], list): |
|---|
| 637 | x = sum(x,[]) |
|---|
| 638 | elif hasattr(x[0], "is_vector"): # TODO: is this the best way to test that? |
|---|
| 639 | e = [] |
|---|
| 640 | for v in x: |
|---|
| 641 | e = e + v.list() |
|---|
| 642 | copy = False # deep copy? |
|---|
| 643 | x = e |
|---|
| 644 | elif isinstance(x[0], tuple): |
|---|
| 645 | x = list(sum(x,())) |
|---|
| 646 | return self.__matrix_class(self, entries=x, copy=copy, coerce=coerce) |
|---|
| 647 | |
|---|
| 648 | def matrix_space(self, nrows=None, ncols=None, sparse=False): |
|---|
| 649 | """ |
|---|
| 650 | Return the matrix space with given number of rows, columns and |
|---|
| 651 | sparcity over the same base ring as self, and defaults the |
|---|
| 652 | same as self. |
|---|
| 653 | |
|---|
| 654 | EXAMPLES: |
|---|
| 655 | sage: M = Mat(GF(7),100,200) |
|---|
| 656 | sage: M.matrix_space(5000) |
|---|
| 657 | Full MatrixSpace of 5000 by 200 dense matrices over Finite Field of size 7 |
|---|
| 658 | sage: M.matrix_space(ncols=5000) |
|---|
| 659 | Full MatrixSpace of 100 by 5000 dense matrices over Finite Field of size 7 |
|---|
| 660 | sage: M.matrix_space(sparse=True) |
|---|
| 661 | Full MatrixSpace of 100 by 200 sparse matrices over Finite Field of size 7 |
|---|
| 662 | """ |
|---|
| 663 | if nrows is None: |
|---|
| 664 | nrows = self.__nrows |
|---|
| 665 | if ncols is None: |
|---|
| 666 | ncols = self.__ncols |
|---|
| 667 | return MatrixSpace(self.base_ring(), nrows, ncols, |
|---|
| 668 | sparse=sparse) |
|---|
| 669 | |
|---|
| 670 | def ncols(self): |
|---|
| 671 | """ |
|---|
| 672 | Return the number of columns of matrices in this space. |
|---|
| 673 | |
|---|
| 674 | EXAMPLES: |
|---|
| 675 | sage: M = Mat(ZZ['x'],200000,500000,sparse=True) |
|---|
| 676 | sage: M.ncols() |
|---|
| 677 | 500000 |
|---|
| 678 | """ |
|---|
| 679 | return self.__ncols |
|---|
| 680 | |
|---|
| 681 | def nrows(self): |
|---|
| 682 | """ |
|---|
| 683 | Return the number of rows of matrices in this space. |
|---|
| 684 | |
|---|
| 685 | EXAMPLES: |
|---|
| 686 | sage: M = Mat(ZZ,200000,500000) |
|---|
| 687 | sage: M.nrows() |
|---|
| 688 | 200000 |
|---|
| 689 | """ |
|---|
| 690 | return self.__nrows |
|---|
| 691 | |
|---|
| 692 | def row_space(self): |
|---|
| 693 | """ |
|---|
| 694 | Return the module spanned by all rows of matrices in this matrix space. |
|---|
| 695 | This is a free module of rank the number of rows. It will be sparse |
|---|
| 696 | or dense as this matrix space is sparse or dense. |
|---|
| 697 | |
|---|
| 698 | EXAMPLES: |
|---|
| 699 | sage: M = Mat(ZZ,20,5,sparse=False); M.row_space() |
|---|
| 700 | Ambient free module of rank 5 over the principal ideal domain Integer Ring |
|---|
| 701 | """ |
|---|
| 702 | try: |
|---|
| 703 | return self.__row_space |
|---|
| 704 | except AttributeError: |
|---|
| 705 | self.__row_space = sage.modules.free_module.FreeModule(self.base_ring(), |
|---|
| 706 | self.ncols(), sparse=self.is_sparse()) |
|---|
| 707 | return self.__row_space |
|---|
| 708 | |
|---|
| 709 | def column_space(self): |
|---|
| 710 | """ |
|---|
| 711 | Return the module spanned by all columns of matrices in this matrix space. |
|---|
| 712 | This is a free module of rank the number of columns. It will be sparse |
|---|
| 713 | or dense as this matrix space is sparse or dense. |
|---|
| 714 | |
|---|
| 715 | EXAMPLES: |
|---|
| 716 | sage: M = Mat(GF(9,'a'),20,5,sparse=True); M.column_space() |
|---|
| 717 | Sparse vector space of dimension 20 over Finite Field in a of size 3^2 |
|---|
| 718 | """ |
|---|
| 719 | try: |
|---|
| 720 | return self.__column_space |
|---|
| 721 | except AttributeError: |
|---|
| 722 | self.__column_space = sage.modules.free_module.FreeModule(self.base_ring(), self.nrows(), |
|---|
| 723 | sparse=self.is_sparse()) |
|---|
| 724 | return self.__column_space |
|---|
| 725 | |
|---|
| 726 | def random_element(self, density=1, *args, **kwds): |
|---|
| 727 | """ |
|---|
| 728 | INPUT: |
|---|
| 729 | density -- integer (default: 1) rough measure of the proportion of nonzero |
|---|
| 730 | entries in the random matrix |
|---|
| 731 | *args, **kwds -- rest of parameters may be passed to the random_element function |
|---|
| 732 | of the base ring. ("may be", since this function calls the randomize |
|---|
| 733 | function on the zero matrix, which need not call the random_element function |
|---|
| 734 | of the base ring at all in general.) |
|---|
| 735 | |
|---|
| 736 | EXAMPLES: |
|---|
| 737 | sage: Mat(ZZ,2,5).random_element() # random output |
|---|
| 738 | [-1 -1 0 -2 0] |
|---|
| 739 | [ 0 -1 2 -1 -1] |
|---|
| 740 | sage: Mat(QQ,2,5).random_element(density=0.5) # random output |
|---|
| 741 | [-1/2 0 -1/2 1/2 0] |
|---|
| 742 | [ 0 0 -1 0 0] |
|---|
| 743 | sage: Mat(QQ,3,sparse=True).random_element() # random output |
|---|
| 744 | [ 1 2 1] |
|---|
| 745 | [ 0 1 -3] |
|---|
| 746 | [1/2 -1 0] |
|---|
| 747 | sage: Mat(GF(9,'a'),3,sparse=True).random_element() # random output |
|---|
| 748 | [ a a + 1 0] |
|---|
| 749 | [ 2*a 2*a + 1 a + 2] |
|---|
| 750 | [ 1 0 1] |
|---|
| 751 | """ |
|---|
| 752 | Z = self.zero_matrix() |
|---|
| 753 | Z.randomize(density, *args, **kwds) |
|---|
| 754 | return Z |
|---|
| 755 | |
|---|
| 756 | _random = 1 |
|---|
| 757 | |
|---|
| 758 | def dict_to_list(entries, nrows, ncols): |
|---|
| 759 | v = [0]*(nrows*ncols) |
|---|
| 760 | for ij, y in entries: |
|---|
| 761 | i,j = ij |
|---|
| 762 | v[i*ncols + j] = y |
|---|
| 763 | return v |
|---|
| 764 | |
|---|
| 765 | def list_to_dict(entries, nrows, ncols): |
|---|
| 766 | d = {} |
|---|
| 767 | if ncols == 0 or nrows == 0: |
|---|
| 768 | return d |
|---|
| 769 | for i in range(len(entries)): |
|---|
| 770 | x = entries[i] |
|---|
| 771 | if x != 0: |
|---|
| 772 | col = i % ncols |
|---|
| 773 | row = i // ncols |
|---|
| 774 | d[(row,col)] = x |
|---|
| 775 | return d |
|---|
| 776 | |
|---|
| 777 | |
|---|