Ticket #11528: trac_11528_matrix_delete_rows_and_columns.patch

File trac_11528_matrix_delete_rows_and_columns.patch, 6.7 KB (added by pong, 14 months ago)

updated version

  • sage/matrix/matrix1.pyx

    # HG changeset patch
    # User Wai Yan Pong <wypong00@gmail.com>
    # Date 1332992965 25200
    # Node ID 9ede43b74ea63b3e95c9a1eb8049f4e25b084bc6
    # Parent  eaaab86e254be84d3f50bc5ec024a1e73953bafe
    Trac 11528: delete rows and delete columns from a matrix
    
    diff --git a/sage/matrix/matrix1.pyx b/sage/matrix/matrix1.pyx
    a b  
    2222include "../ext/python.pxi" 
    2323 
    2424import sage.modules.free_module 
     25import warnings 
    2526 
    2627cdef class Matrix(matrix0.Matrix): 
    2728    ################################################### 
     
    14851486            k = k + 1 
    14861487        return A 
    14871488 
    1488     def delete_columns(self, cols_to_delete, index_check=False): 
     1489    def delete_columns(self, dcols, index_check=True): 
    14891490        """ 
    1490         Return the matrix constructed from deleting the columns with indices in the columns_to_delete list. 
     1491        Return the matrix constructed from deleting the columns with indices in the ``dcols`` list. 
    14911492 
    14921493        INPUT: 
    14931494 
    1494         * "cols_to_delete" - list of indices of columns to be deleted from self. 
    1495         * "index_check" - checks whether any index in "cols_to_delete" is out of range. Defaults to False. 
     1495        * ``dcols`` - list of indices of columns to be deleted from self. 
     1496        * ``index_check`` - checks whether any index in ``dcols`` is out of range. Defaults to True. 
    14961497 
    14971498        SEE ALSO: 
    1498             The functions, "delete_rows()" "matrix_from_columns()" 
     1499            The functions :meth: ``delete_rows`` and :meth: ``matrix_from_columns`` 
    14991500 
    15001501        EXAMPLES:: 
    15011502 
     
    15081509            [ 5  7] 
    15091510            [ 9 11] 
    15101511 
    1511         cols_to_delete can be a tuple. Also only the underlying set of indices matters.:: 
     1512        ``dcols`` can be a tuple. But only the underlying set of indices matters. :: 
    15121513 
    15131514            sage: A.delete_columns((2,0,2)) 
    15141515            [ 1  3] 
    15151516            [ 5  7] 
    15161517            [ 9 11] 
    15171518 
    1518         There is an option to check whether the any index is out of range. Defaults to False.:: 
     1519        The default is to check whether the any index in ``dcols`` is out of range. :: 
    15191520 
    15201521            sage: A.delete_columns([-1,2,4]) 
     1522            doctest:1: UserWarning: [4, -1] contains invalid indices. 
    15211523            [ 0  1  3] 
    15221524            [ 4  5  7] 
    15231525            [ 8  9 11] 
    1524             sage: A.delete_columns([-1,2,4], index_check=True) 
    1525             Traceback (most recent call last): 
    1526             ... 
    1527             IndexError: column indices in [4, -1] are out of range. 
     1526            sage: A.delete_columns([-1,2,4], index_check=False) 
     1527            [ 0  1  3] 
     1528            [ 4  5  7] 
     1529            [ 8  9 11] 
    15281530 
    15291531        AUTHORS: 
    1530         - Wai Yan Pong (2012-03-05) 
     1532            - Wai Yan Pong (2012-03-05) 
    15311533        """ 
    1532         if not (PY_TYPE_CHECK(cols_to_delete, list) or PY_TYPE_CHECK(cols_to_delete, tuple)): 
    1533             raise TypeError, "columns (=%s) must be a list of integers"%cols_to_delete 
     1534        if not (PY_TYPE_CHECK(dcols, list) or PY_TYPE_CHECK(dcols, tuple)): 
     1535            raise TypeError("{l} must be a list or a tuple".format(l=dcols)) 
    15341536        cdef list cols, diff_cols 
    15351537 
    15361538        if index_check: 
    1537             diff_cols = list(set(cols_to_delete).difference(set(range(self._ncols)))) 
     1539            diff_cols = list(set(dcols).difference(set(range(self._ncols)))) 
    15381540            if not (diff_cols == []): 
    1539                 raise IndexError, "column indices in %s are out of range."%diff_cols 
    1540         cols = list(set(range(self.ncols())) - set(cols_to_delete)) 
     1541                warnings.warn("{d} contains invalid indices.".format(d=diff_cols)) 
     1542        cols = [k for k in range(self._ncols) if not k in dcols] 
    15411543        return self.matrix_from_columns(cols) 
    15421544 
    15431545    def matrix_from_rows(self, rows): 
     
    15721574            k += 1 
    15731575        return A 
    15741576 
    1575     def delete_rows(self, rows_to_delete, index_check=False): 
     1577 
     1578    def delete_rows(self, drows, index_check=True): 
    15761579        """ 
    1577         Return the matrix constructed from deleting the rows with indices in the rows_to_delete list. 
     1580        Return the matrix constructed from deleting the rows with indices in the ``drows`` list. 
    15781581 
    15791582        INPUT: 
    15801583 
    1581         * "rows_to_delete" - list of indices of rows to be deleted from self. 
    1582         * "index_check" - checks whether any index in "rows_to_delete" is out of range. Defaults to False. 
     1584        * ``drows`` - list of indices of rows to be deleted from self. 
     1585        * ``index_check`` - checks whether any index in ``drows`` is out of range. Defaults to True. 
    15831586 
    15841587        SEE ALSO: 
    1585             The functions "delete_columns()", "matrix_from_rows()" 
     1588            The functions :meth: ``delete_columns`` and :meth: ``matrix_from_rows`` 
    15861589 
    15871590        EXAMPLES:: 
    15881591 
     
    15951598            [ 3  4  5] 
    15961599            [ 9 10 11] 
    15971600 
    1598         rows_to_delete can be a tuple. Also only the underlying set of indices matters.:: 
     1601        ``drows`` can be a tuple. But only the underlying set of indices matters.:: 
    15991602 
    16001603            sage: A.delete_rows((2,0,2)) 
    16011604            [ 3  4  5] 
    16021605            [ 9 10 11] 
    16031606 
    1604         There is an option to check whether the any index is out of range. Defaults to False.:: 
     1607        The default is to check whether the any index in ``drows`` is out of range. 
    16051608 
    16061609            sage: A.delete_rows([-1,2,4]) 
     1610            doctest:1: UserWarning: [4, -1] contains invalid indices. 
    16071611            [ 0  1  2] 
    16081612            [ 3  4  5] 
    16091613            [ 9 10 11] 
    1610             sage: A.delete_rows([-1,2,4], index_check=True) 
    1611             Traceback (most recent call last): 
    1612             ... 
    1613             IndexError: row indices in [4, -1] are out of range. 
     1614            sage: A.delete_rows([-1,2,4], index_check=False) 
     1615            [ 0  1  2] 
     1616            [ 3  4  5] 
     1617            [ 9 10 11] 
    16141618 
    16151619        AUTHORS: 
    1616         - Wai Yan Pong (2012-03-05) 
     1620            - Wai Yan Pong (2012-03-05) 
    16171621        """ 
    1618         if not (PY_TYPE_CHECK(rows_to_delete, list) or PY_TYPE_CHECK(rows_to_delete, tuple)): 
    1619             raise TypeError, "columns (=%s) must be a list of integers"%rows_to_delete 
     1622        if not (PY_TYPE_CHECK(drows, list) or PY_TYPE_CHECK(drows, tuple)): 
     1623            raise TypeError("{l} must be a list or a tuple".format(l=drows)) 
    16201624        cdef list rows, diff_rows 
    16211625 
    16221626        if index_check: 
    1623             diff_rows = list(set(rows_to_delete).difference(set(range(self._nrows)))) 
     1627            diff_rows = list(set(drows).difference(set(range(self._nrows)))) 
    16241628            if not (diff_rows == []): 
    1625                 raise IndexError, "row indices in %s are out of range."%diff_rows 
    1626         rows = list(set(range(self.nrows())) - set(rows_to_delete)) 
     1629                warnings.warn("{d} contains invalid indices.".format(d=diff_rows)) 
     1630        rows = [k for k in range(self._nrows) if not k in drows] 
    16271631        return self.matrix_from_rows(rows) 
    16281632 
    16291633    def matrix_from_rows_and_columns(self, rows, columns):