Ticket #11589: trac_11589_copy_vs_creation_of_zero.patch

File trac_11589_copy_vs_creation_of_zero.patch, 4.8 KB (added by SimonKing, 10 years ago)

Some fine tuning for the creation of zero matrices

  • sage/matrix/matrix_generic_sparse.pyx

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1310472107 -7200
    # Node ID 5a197f2f4e9f791f5684ff08a6f430e944bfbf73
    # Parent  869d869262ce549c749c3da1e7123c2e62197faf
    #11589: Fine tuning for creation of matrices over finite fields.
    
    diff --git a/sage/matrix/matrix_generic_sparse.pyx b/sage/matrix/matrix_generic_sparse.pyx
    a b  
    8383    def __cinit__(self, parent, entries=0, coerce=True, copy=True):
    8484        self._entries = {}  # crucial so that pickling works
    8585   
    86     def __init__(self, parent, entries=0, coerce=True, copy=True):
     86    def __init__(self, parent, entries=None, coerce=True, copy=True):
    8787        cdef Py_ssize_t i, j
    8888        matrix.Matrix.__init__(self, parent)
    8989
    9090        R = self._base_ring
    9191        self._zero = R(0)
    9292        if not isinstance(entries, (list, dict)):
    93             x = R(entries)
     93            if entries is None:
     94                x = R.zero_element()
     95            else:
     96                x = R(entries)
    9497            entries = {}
    9598            if x != self._zero:
    9699                if self._nrows != self._ncols:
  • sage/matrix/matrix_integer_sparse.pyx

    diff --git a/sage/matrix/matrix_integer_sparse.pyx b/sage/matrix/matrix_integer_sparse.pyx
    a b  
    9292        cdef void** X
    9393
    9494        # fill in entries in the dict case
     95        if entries is None: return
    9596        if isinstance(entries, dict):
    9697            R = self.base_ring()
    9798            for ij, x in entries.iteritems():
  • sage/matrix/matrix_rational_dense.pyx

    diff --git a/sage/matrix/matrix_rational_dense.pyx b/sage/matrix/matrix_rational_dense.pyx
    a b  
    174174        sage_free(self._entries)
    175175        sage_free(self._matrix)
    176176   
    177     def __init__(self, parent, entries=0, coerce=True, copy=True):
     177    def __init__(self, parent, entries=None, coerce=True, copy=True):
    178178   
    179179        cdef Py_ssize_t i
    180180        cdef Rational z
    181181
     182        if entries is None: return
    182183        if isinstance(entries, (list, tuple)):
    183184            if len(entries) != self._nrows * self._ncols:
    184185                raise TypeError("entries has the wrong length")
  • sage/matrix/matrix_rational_sparse.pyx

    diff --git a/sage/matrix/matrix_rational_sparse.pyx b/sage/matrix/matrix_rational_sparse.pyx
    a b  
    9999        cdef Rational z
    100100        cdef void** X
    101101
     102        if entries is None: return
    102103        # fill in entries in the dict case
    103104        if isinstance(entries, dict):
    104105            R = self.base_ring()
  • sage/matrix/matrix_space.py

    diff --git a/sage/matrix/matrix_space.py b/sage/matrix/matrix_space.py
    a b  
    276276
    277277        EXAMPLE::
    278278
    279             sage: MS = MatrixSpace(GF(2),200,200)
     279            sage: MS = MatrixSpace(GF(2),20,20)
    280280            sage: MS._copy_zero
    281281            False
    282282
    283283            sage: MS = MatrixSpace(GF(3),20,20)
    284284            sage: MS._copy_zero
    285285            True
    286 
    287286            sage: MS = MatrixSpace(GF(3),200,200)
    288287            sage: MS._copy_zero
    289288            False
    290289
    291290            sage: MS = MatrixSpace(ZZ,200,200)
    292291            sage: MS._copy_zero
     292            False
     293            sage: MS = MatrixSpace(ZZ,30,30)
     294            sage: MS._copy_zero
    293295            True
     296
     297            sage: MS = MatrixSpace(QQ,200,200)
     298            sage: MS._copy_zero
     299            False
     300            sage: MS = MatrixSpace(QQ,20,20)
     301            sage: MS._copy_zero
     302            False
     303
    294304        """
    295305        if self.__is_sparse:
    296306            return False
    297307        elif self.__matrix_class is sage.matrix.matrix_mod2_dense.Matrix_mod2_dense:
    298308            return False
    299         elif self.__matrix_class == sage.matrix.matrix_modn_dense.Matrix_modn_dense:
    300             if self.__nrows > 100 and self.__ncols > 100:
     309        elif self.__matrix_class == sage.matrix.matrix_rational_dense.Matrix_rational_dense:
     310            return False
     311        elif self.__nrows > 40 and self.__ncols > 40:
    301312                return False
    302             else:
    303                 return True
    304313        else:
    305314            return True
    306315
    307     def __call__(self, entries=0, coerce=True, copy=True, rows=None):
     316    def __call__(self, entries=None, coerce=True, copy=True, rows=None):
    308317        """
    309318        EXAMPLES::
    310319       
     
    393402            if self._copy_zero: # faster to copy than to create a new one.
    394403                return self.zero_matrix().__copy__()
    395404            else:
    396                 return self.__matrix_class(self, 0, coerce=coerce, copy=copy)
     405                return self.__matrix_class(self, None, coerce=coerce, copy=copy)
    397406
    398407        if isinstance(entries, (list, tuple)) and len(entries) > 0 and \
    399408           sage.modules.free_module_element.is_FreeModuleElement(entries[0]):