Ticket #8096: 8096-zero-matrix.patch

File 8096-zero-matrix.patch, 3.7 KB (added by robertwb, 3 years ago)

apply on top of previous

  • sage/matrix/matrix_space.py

    diff -r 84796f8739fd -r 6005cba118b0 sage/matrix/matrix_space.py
    a b  
    342342            [1 1] 
    343343            [0 1] 
    344344        """ 
    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: 
    349346            return self.zero_matrix() 
    350347 
    351348        if isinstance(entries, (list, tuple)) and len(entries) > 0 and \ 
     
    10161013        try: 
    10171014            z = self.__zero_matrix 
    10181015        except AttributeError: 
    1019             z = self(0) 
     1016            z = self.matrix(0) 
    10201017            self.__zero_matrix = z 
    10211018        return z.__copy__() 
    10221019     
     
    10531050            [1 3] 
    10541051            [2 4] 
    10551052        """ 
     1053        if x is 0 or x is None: 
     1054            return self.__matrix_class(self, x, False, False)  
     1055         
    10561056        if isinstance(x, (types.GeneratorType, xrange)): 
    10571057            x = list(x) 
    10581058        elif isinstance(x, (int, integer.Integer)) and x==1: 
  • sage/matrix/matrix_window.pxd

    diff -r 84796f8739fd -r 6005cba118b0 sage/matrix/matrix_window.pxd
    a b  
    33cdef class MatrixWindow: 
    44    cdef Py_ssize_t _row, _col, _nrows, _ncols 
    55    cdef Matrix _matrix 
    6     cdef object _zero 
    7  
     6    cdef object _cached_zero 
     7     
    88    # YOU *REALLY SHOULD* OVERRIDE THESE: 
    99    cpdef add(MatrixWindow self, MatrixWindow A) 
    1010    cpdef subtract(MatrixWindow self, MatrixWindow A) 
     
    3232                                         Py_ssize_t n_rows, Py_ssize_t n_cols) 
    3333    cpdef matrix(MatrixWindow self) 
    3434    cpdef swap_rows(MatrixWindow self, Py_ssize_t a, Py_ssize_t b) 
    35  
    36  
    37              
     35    cdef object _zero(self) 
  • sage/matrix/matrix_window.pyx

    diff -r 84796f8739fd -r 6005cba118b0 sage/matrix/matrix_window.pyx
    a b  
    3434        M._col = col 
    3535        M._nrows = n_rows 
    3636        M._ncols = n_cols 
    37         M._zero = self._zero 
     37        M._cached_zero = self._cached_zero 
    3838        return M 
    3939 
    4040    def __init__(MatrixWindow self, Matrix matrix, 
     
    4444        self._col = col 
    4545        self._nrows = nrows 
    4646        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 
    4852 
    4953    cpdef MatrixWindow matrix_window(MatrixWindow self, Py_ssize_t row, Py_ssize_t col, 
    5054                                    Py_ssize_t n_rows, Py_ssize_t n_cols): 
     
    151155                 
    152156    cpdef set_to_zero(MatrixWindow self): 
    153157        cdef Py_ssize_t i, j 
    154         z = self._zero 
     158        z = self._zero() 
    155159        for i from 0 <= i < self._nrows: 
    156160            for j from 0 <= j < self._ncols: 
    157161                self.set_unsafe(i, j, z) 
     
    194198            raise ArithmeticError, "incompatible dimensions" 
    195199        for i from 0 <= i < A._nrows: 
    196200            for j from 0 <= j < B._ncols: 
    197                 s = self._zero 
     201                s = self._zero() 
    198202                for k from 0 <= k < A._ncols: 
    199203                    s = s + A.get_unsafe(i, k) * B.get_unsafe(k, j) 
    200204                self.set_unsafe(i, j, s) 
     
    234238        return echelon.pivots() 
    235239         
    236240    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()