Ticket #10793: trac_10793_bug_in_matrix_construction.patch

File trac_10793_bug_in_matrix_construction.patch, 3.8 KB (added by novoselt, 10 years ago)
  • sage/matrix/matrix_space.py

    # HG changeset patch
    # User Andrey Novoseltsev <novoselt@gmail.com>
    # Date 1308264282 21600
    # Node ID 629b4721977a1f69f073070556d96331496f2280
    # Parent  f62dbed83e2af24356d8138b21ae45ab461cc632
    Don't allow matrix spaces to make matrices from matrices of wrong dimension.
    
    diff -r f62dbed83e2a -r 629b4721977a sage/matrix/matrix_space.py
    a b  
    10801080        return self.dimension()
    10811081
    10821082    def matrix(self, x=0, coerce=True, copy=True, rows=True):
    1083         """
    1084         Create a matrix in self. The entries can be specified either as a
    1085         single list of length nrows\*ncols, or as a list of lists.
     1083        r"""
     1084        Create a matrix in ``self``.
     1085       
     1086        INPUT:
     1087       
     1088        - ``x`` -- (default: 0) data to construct a new matrix from. Can be one
     1089          of the following:
     1090         
     1091          * 0, corresponding to the zero matrix;
     1092         
     1093          * a matrix, whose dimension must match ``self`` and whose base ring
     1094            must be convertible to the base ring of ``self``;
     1095           
     1096          * a list of entries corresponding to all elements of the new matrix;
     1097         
     1098          * a list of lists with each inner list being either a row or a column
     1099            of the new matrix.
     1100           
     1101        - ``coerce`` -- (default: ``True``) whether to coerce ``x`` into self;
     1102       
     1103        - ``copy`` -- (default: ``True``) whether to copy ``x`` during
     1104          construction (makes a difference only if ``x`` is a matrix in
     1105          ``self``);
     1106         
     1107        - ``rows`` -- (default: ``True``) whether entries are given row by row
     1108          or column by column.
     1109         
     1110        OUTPUT:
     1111       
     1112        - a matrix in ``self``.
    10861113       
    10871114        EXAMPLES::
    10881115       
     
    10991126            sage: M.matrix([1,2,3,4],rows=False)
    11001127            [1 3]
    11011128            [2 4]
     1129           
     1130        Note that the last "flip" cannot be performed if ``x`` is a matrix, no
     1131        matter what is ``rows`` (it used to be possible but was fixed by
     1132        Trac 10793)::
     1133       
     1134            sage: projection = matrix(ZZ,[[1,0,0],[0,1,0]])
     1135            sage: projection
     1136            [1 0 0]
     1137            [0 1 0]
     1138            sage: projection.parent()
     1139            Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
     1140            sage: M = MatrixSpace(ZZ, 3 , 2)
     1141            sage: M
     1142            Full MatrixSpace of 3 by 2 dense matrices over Integer Ring
     1143            sage: M(projection)
     1144            Traceback (most recent call last):
     1145            ...
     1146            ValueError: a matrix from
     1147            Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
     1148            cannot be converted to a matrix in
     1149            Full MatrixSpace of 3 by 2 dense matrices over Integer Ring!
     1150
     1151        If you really want to make from a matrix another matrix of different
     1152        dimensions, use either transpose method or explicit conversion to a
     1153        list::
     1154       
     1155            sage: M(projection.list())
     1156            [1 0]
     1157            [0 0]
     1158            [1 0]
    11021159        """
    11031160        if isinstance(x, (types.GeneratorType, xrange)):
    11041161            x = list(x)
     
    11101167                    return x
    11111168                else:
    11121169                    return x.__copy__()
    1113             x = x.list()
     1170            else:
     1171                if x.nrows() == self.__nrows and x.ncols() == self.__ncols:
     1172                    x = x.list()
     1173                else:
     1174                    raise ValueError("a matrix from %s cannot be converted to "
     1175                                     "a matrix in %s!" % (x.parent(), self))
    11141176        if isinstance(x, list) and len(x) > 0:
    11151177            if isinstance(x[0], list):
    11161178                x = sum(x,[])