diff -r 84796f8739fd -r 6005cba118b0 sage/matrix/matrix_space.py
|
a
|
b
|
|
| 342 | 342 | [1 1] |
| 343 | 343 | [0 1] |
| 344 | 344 | """ |
| 345 | | if entries is None: |
| 346 | | entries = 0 |
| 347 | | |
| 348 | | if entries == 0 and hasattr(self, '__zero_matrix'): |
| | 345 | if entries is None or entries == 0: |
| 349 | 346 | return self.zero_matrix() |
| 350 | 347 | |
| 351 | 348 | if isinstance(entries, (list, tuple)) and len(entries) > 0 and \ |
| … |
… |
|
| 1016 | 1013 | try: |
| 1017 | 1014 | z = self.__zero_matrix |
| 1018 | 1015 | except AttributeError: |
| 1019 | | z = self(0) |
| | 1016 | z = self.matrix(0) |
| 1020 | 1017 | self.__zero_matrix = z |
| 1021 | 1018 | return z.__copy__() |
| 1022 | 1019 | |
| … |
… |
|
| 1053 | 1050 | [1 3] |
| 1054 | 1051 | [2 4] |
| 1055 | 1052 | """ |
| | 1053 | if x is 0 or x is None: |
| | 1054 | return self.__matrix_class(self, x, False, False) |
| | 1055 | |
| 1056 | 1056 | if isinstance(x, (types.GeneratorType, xrange)): |
| 1057 | 1057 | x = list(x) |
| 1058 | 1058 | elif isinstance(x, (int, integer.Integer)) and x==1: |
diff -r 84796f8739fd -r 6005cba118b0 sage/matrix/matrix_window.pxd
|
a
|
b
|
|
| 3 | 3 | cdef class MatrixWindow: |
| 4 | 4 | cdef Py_ssize_t _row, _col, _nrows, _ncols |
| 5 | 5 | cdef Matrix _matrix |
| 6 | | cdef object _zero |
| 7 | | |
| | 6 | cdef object _cached_zero |
| | 7 | |
| 8 | 8 | # YOU *REALLY SHOULD* OVERRIDE THESE: |
| 9 | 9 | cpdef add(MatrixWindow self, MatrixWindow A) |
| 10 | 10 | cpdef subtract(MatrixWindow self, MatrixWindow A) |
| … |
… |
|
| 32 | 32 | Py_ssize_t n_rows, Py_ssize_t n_cols) |
| 33 | 33 | cpdef matrix(MatrixWindow self) |
| 34 | 34 | cpdef swap_rows(MatrixWindow self, Py_ssize_t a, Py_ssize_t b) |
| 35 | | |
| 36 | | |
| 37 | | |
| | 35 | cdef object _zero(self) |
diff -r 84796f8739fd -r 6005cba118b0 sage/matrix/matrix_window.pyx
|
a
|
b
|
|
| 34 | 34 | M._col = col |
| 35 | 35 | M._nrows = n_rows |
| 36 | 36 | M._ncols = n_cols |
| 37 | | M._zero = self._zero |
| | 37 | M._cached_zero = self._cached_zero |
| 38 | 38 | return M |
| 39 | 39 | |
| 40 | 40 | def __init__(MatrixWindow self, Matrix matrix, |
| … |
… |
|
| 44 | 44 | self._col = col |
| 45 | 45 | self._nrows = nrows |
| 46 | 46 | self._ncols = ncols |
| 47 | | self._zero = matrix.base_ring()(0) # expensive |
| | 47 | |
| | 48 | cdef object _zero(self): |
| | 49 | if self._cached_zero is None: |
| | 50 | self._cached_zero = self._matrix.base_ring()(0) # expensive |
| | 51 | return self._cached_zero |
| 48 | 52 | |
| 49 | 53 | cpdef MatrixWindow matrix_window(MatrixWindow self, Py_ssize_t row, Py_ssize_t col, |
| 50 | 54 | Py_ssize_t n_rows, Py_ssize_t n_cols): |
| … |
… |
|
| 151 | 155 | |
| 152 | 156 | cpdef set_to_zero(MatrixWindow self): |
| 153 | 157 | cdef Py_ssize_t i, j |
| 154 | | z = self._zero |
| | 158 | z = self._zero() |
| 155 | 159 | for i from 0 <= i < self._nrows: |
| 156 | 160 | for j from 0 <= j < self._ncols: |
| 157 | 161 | self.set_unsafe(i, j, z) |
| … |
… |
|
| 194 | 198 | raise ArithmeticError, "incompatible dimensions" |
| 195 | 199 | for i from 0 <= i < A._nrows: |
| 196 | 200 | for j from 0 <= j < B._ncols: |
| 197 | | s = self._zero |
| | 201 | s = self._zero() |
| 198 | 202 | for k from 0 <= k < A._ncols: |
| 199 | 203 | s = s + A.get_unsafe(i, k) * B.get_unsafe(k, j) |
| 200 | 204 | self.set_unsafe(i, j, s) |
| … |
… |
|
| 234 | 238 | return echelon.pivots() |
| 235 | 239 | |
| 236 | 240 | cpdef bint element_is_zero(MatrixWindow self, Py_ssize_t i, Py_ssize_t j): |
| 237 | | return self._matrix.get_unsafe(i+self._row, j+self._col) == self._zero |
| | 241 | return self._matrix.get_unsafe(i+self._row, j+self._col) == self._zero() |