# 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
|
|
| 22 | 22 | include "../ext/python.pxi" |
| 23 | 23 | |
| 24 | 24 | import sage.modules.free_module |
| | 25 | import warnings |
| 25 | 26 | |
| 26 | 27 | cdef class Matrix(matrix0.Matrix): |
| 27 | 28 | ################################################### |
| … |
… |
|
| 1485 | 1486 | k = k + 1 |
| 1486 | 1487 | return A |
| 1487 | 1488 | |
| 1488 | | def delete_columns(self, cols_to_delete, index_check=False): |
| | 1489 | def delete_columns(self, dcols, index_check=True): |
| 1489 | 1490 | """ |
| 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. |
| 1491 | 1492 | |
| 1492 | 1493 | INPUT: |
| 1493 | 1494 | |
| 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. |
| 1496 | 1497 | |
| 1497 | 1498 | SEE ALSO: |
| 1498 | | The functions, "delete_rows()" "matrix_from_columns()" |
| | 1499 | The functions :meth: ``delete_rows`` and :meth: ``matrix_from_columns`` |
| 1499 | 1500 | |
| 1500 | 1501 | EXAMPLES:: |
| 1501 | 1502 | |
| … |
… |
|
| 1508 | 1509 | [ 5 7] |
| 1509 | 1510 | [ 9 11] |
| 1510 | 1511 | |
| 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. :: |
| 1512 | 1513 | |
| 1513 | 1514 | sage: A.delete_columns((2,0,2)) |
| 1514 | 1515 | [ 1 3] |
| 1515 | 1516 | [ 5 7] |
| 1516 | 1517 | [ 9 11] |
| 1517 | 1518 | |
| 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. :: |
| 1519 | 1520 | |
| 1520 | 1521 | sage: A.delete_columns([-1,2,4]) |
| | 1522 | doctest:1: UserWarning: [4, -1] contains invalid indices. |
| 1521 | 1523 | [ 0 1 3] |
| 1522 | 1524 | [ 4 5 7] |
| 1523 | 1525 | [ 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] |
| 1528 | 1530 | |
| 1529 | 1531 | AUTHORS: |
| 1530 | | - Wai Yan Pong (2012-03-05) |
| | 1532 | - Wai Yan Pong (2012-03-05) |
| 1531 | 1533 | """ |
| 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)) |
| 1534 | 1536 | cdef list cols, diff_cols |
| 1535 | 1537 | |
| 1536 | 1538 | 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)))) |
| 1538 | 1540 | 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] |
| 1541 | 1543 | return self.matrix_from_columns(cols) |
| 1542 | 1544 | |
| 1543 | 1545 | def matrix_from_rows(self, rows): |
| … |
… |
|
| 1572 | 1574 | k += 1 |
| 1573 | 1575 | return A |
| 1574 | 1576 | |
| 1575 | | def delete_rows(self, rows_to_delete, index_check=False): |
| | 1577 | |
| | 1578 | def delete_rows(self, drows, index_check=True): |
| 1576 | 1579 | """ |
| 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. |
| 1578 | 1581 | |
| 1579 | 1582 | INPUT: |
| 1580 | 1583 | |
| 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. |
| 1583 | 1586 | |
| 1584 | 1587 | SEE ALSO: |
| 1585 | | The functions "delete_columns()", "matrix_from_rows()" |
| | 1588 | The functions :meth: ``delete_columns`` and :meth: ``matrix_from_rows`` |
| 1586 | 1589 | |
| 1587 | 1590 | EXAMPLES:: |
| 1588 | 1591 | |
| … |
… |
|
| 1595 | 1598 | [ 3 4 5] |
| 1596 | 1599 | [ 9 10 11] |
| 1597 | 1600 | |
| 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.:: |
| 1599 | 1602 | |
| 1600 | 1603 | sage: A.delete_rows((2,0,2)) |
| 1601 | 1604 | [ 3 4 5] |
| 1602 | 1605 | [ 9 10 11] |
| 1603 | 1606 | |
| 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. |
| 1605 | 1608 | |
| 1606 | 1609 | sage: A.delete_rows([-1,2,4]) |
| | 1610 | doctest:1: UserWarning: [4, -1] contains invalid indices. |
| 1607 | 1611 | [ 0 1 2] |
| 1608 | 1612 | [ 3 4 5] |
| 1609 | 1613 | [ 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] |
| 1614 | 1618 | |
| 1615 | 1619 | AUTHORS: |
| 1616 | | - Wai Yan Pong (2012-03-05) |
| | 1620 | - Wai Yan Pong (2012-03-05) |
| 1617 | 1621 | """ |
| 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)) |
| 1620 | 1624 | cdef list rows, diff_rows |
| 1621 | 1625 | |
| 1622 | 1626 | 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)))) |
| 1624 | 1628 | 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] |
| 1627 | 1631 | return self.matrix_from_rows(rows) |
| 1628 | 1632 | |
| 1629 | 1633 | def matrix_from_rows_and_columns(self, rows, columns): |