Skip to content

Commit 9230344

Browse files
committed
WIP three_sum
1 parent d9b2512 commit 9230344

File tree

2 files changed

+108
-36
lines changed

2 files changed

+108
-36
lines changed

src/sage/matrix/matrix_cmr_sparse.pyx

+61-14
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
586586
"""
587587
cdef Matrix_cmr_chr_sparse sum, first, second
588588
cdef CMR_CHRMAT *sum_mat
589-
first = Matrix_cmr_chr_sparse._from_data(first_mat, immutable=False)
590-
second = Matrix_cmr_chr_sparse._from_data(second_mat, immutable=False)
589+
first = Matrix_cmr_chr_sparse._from_data(first_mat)
590+
second = Matrix_cmr_chr_sparse._from_data(second_mat)
591591

592592
if nonzero_block not in ["top_right", "bottom_left"]:
593593
raise ValueError("Unknown two sum mode", nonzero_block)
@@ -701,6 +701,48 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
701701
sum = Matrix_cmr_chr_sparse._from_cmr(sum_mat)
702702
return sum
703703

704+
def three_sum_wide_wide(first_mat, second_mat):
705+
r"""
706+
Return the 3-sum matrix constructed from the given matrices ``first_mat`` and
707+
``second_mat``.
708+
709+
The first matrix is `M_1=\begin{bmatrix} A & a & a \\ c^T & 0 & \pm 1\end{bmatrix}`
710+
and the second matrix is `M_2=\begin{bmatrix} \pm 1 & 0 & b^T \\ d & d & B\end{bmatrix}`. Then
711+
the three sum is defined in [Sch1986]_, Ch. 19.4.:=
712+
`M_1 \oplus_3 M_2 =\begin{bmatrix}A & ab^T\\ dc^T & B\end{bmatrix}`.
713+
"""
714+
pass
715+
# m1 = first_mat.nrows()
716+
# n1 = first_mat.ncols()
717+
# m2 = second_mat.nrows()
718+
# n2 = second_mat.ncols()
719+
# first_subcol = first_mat.matrix_from_rows_and_columns(range(m1 - 1), [n1 - 1])
720+
# second_subcol = first_mat.delete_rows([second_row_index]).columns()[second_col_index1]
721+
# first_submat = first_mat.delete_columns([first_col_index1, first_col_index2])
722+
# second_submat = second_mat.delete_columns([second_col_index1, second_col_index2])
723+
# first_row = first_submat.rows()[first_row_index]
724+
# second_row = second_submat.rows()[second_row_index]
725+
# first_submat = first_submat.delete_rows([first_row_index])
726+
# second_submat = second_submat.delete_rows([second_row_index])
727+
# first_subrows = first_submat.rows()
728+
# second_subrows = second_submat.rows()
729+
# upper_right_rows = first_subcol.tensor_product(second_row).rows()
730+
# lower_left_rows = second_subcol.tensor_product(first_row).rows()
731+
# n1 = len(first_submat.rows())
732+
# n2 = len(second_submat.rows())
733+
# row_list = []
734+
# for i in range(n1):
735+
# r = list(first_subrows[i])
736+
# u = list(upper_right_rows[i])
737+
# r.extend(u)
738+
# row_list.append(r)
739+
# for i in range(n2):
740+
# r = list(lower_left_rows[i])
741+
# u = list(second_subrows[i])
742+
# r.extend(u)
743+
# row_list.append(r)
744+
# return Matrix_cmr_chr_sparse._from_data(row_list, immutable=False)
745+
704746
def three_sum(first_mat, second_mat, first_col_index1, first_col_index2, second_col_index1, second_col_index2):
705747
r"""
706748
Return the 3-sum matrix constructed from the given matrices ``first_mat`` and
@@ -754,6 +796,8 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
754796
[0 0 0 0 1 1]
755797
[1 1 0 0 0 1]
756798
"""
799+
print(first_mat)
800+
print(second_mat)
757801
fc = len(first_mat.columns())
758802
sc = len(second_mat.columns())
759803
fr = len(first_mat.rows())
@@ -794,8 +838,14 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
794838
break
795839
if not (valid1 and valid2):
796840
raise ValueError('indicated columns of Matrices are not of appropriate form for 3-sum')
841+
print(first_row_index)
842+
print(second_row_index)
843+
print(first_mat)
844+
test1 = first_mat.delete_rows([first_row_index])
845+
print(test1)
846+
print(test1.columns())
797847
first_subcol = first_mat.delete_rows([first_row_index]).columns()[first_col_index1]
798-
second_subcol = first_mat.delete_rows([second_row_index]).columns()[second_col_index1]
848+
second_subcol = second_mat.delete_rows([second_row_index]).columns()[second_col_index1]
799849
first_submat = first_mat.delete_columns([first_col_index1, first_col_index2])
800850
second_submat = second_mat.delete_columns([second_col_index1, second_col_index2])
801851
first_row = first_submat.rows()[first_row_index]
@@ -804,8 +854,14 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
804854
second_submat = second_submat.delete_rows([second_row_index])
805855
first_subrows = first_submat.rows()
806856
second_subrows = second_submat.rows()
857+
print(first_subcol)
858+
print(second_subcol)
859+
print(second_row)
860+
print(first_row)
807861
upper_right_rows = first_subcol.tensor_product(second_row).rows()
808862
lower_left_rows = second_subcol.tensor_product(first_row).rows()
863+
print(upper_right_rows)
864+
print(lower_left_rows)
809865
n1 = len(first_submat.rows())
810866
n2 = len(second_submat.rows())
811867
row_list = []
@@ -823,22 +879,13 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
823879

824880
def delete_rows(self, indices):
825881
rows = self.rows()
826-
row_list = []
827882
n = len(rows)
828-
for i in indices:
829-
if i >= n:
830-
raise ValueError('Found index greater than matrix size')
831-
rows.pop(i)
832-
for r in rows:
833-
x = []
834-
for i in range(len(r)):
835-
x.append(r[i])
836-
row_list.append(x)
883+
row_list = [rows[i] for i in range(n) if i not in indices]
837884
return Matrix_cmr_chr_sparse._from_data(row_list, immutable=False)
838885

839886
def delete_columns(self, indices):
840887
rows = self.rows()
841-
n = len(rows)
888+
n = self.ncols()
842889
row_list = []
843890
for i in indices:
844891
if i >= n:

src/sage/matrix/seymour_decomposition.pyx

+47-22
Original file line numberDiff line numberDiff line change
@@ -544,36 +544,61 @@ cdef class ThreeSumNode(SumNode):
544544

545545
def is_distributed_ranks(self):
546546
r"""
547-
EXAMPLES::
548-
549-
sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
550-
sage: R12 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 9, 12, sparse=True),
551-
....: [[1, -1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
552-
....: [0, 0, 0, 1, -1, 0, 0, 0, 1 , 1, 1, 1],
553-
....: [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
554-
....: [ 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
555-
....: [ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, -1, -1],
556-
....: [ 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0],
557-
....: [ 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, -1, -1],
558-
....: [ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0],
559-
....: [ 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1]])
560-
sage: result, certificate = R12.is_totally_unimodular(certificate=True)
561-
sage: C = certificate._children()[0]; C
562-
ThreeSumNode (9×12) with 2 children
563-
sage: C.is_distributed_ranks()
564-
True
565-
sage: C.is_concentrated_rank()
566-
False
547+
EXAMPLES::
548+
549+
sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
550+
sage: R12_large = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 9, 12, sparse=True),
551+
....: [[1, -1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
552+
....: [0, 0, 0, 1, -1, 0, 0, 0, 1 , 1, 1, 1],
553+
....: [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
554+
....: [ 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
555+
....: [ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, -1, -1],
556+
....: [ 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0],
557+
....: [ 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, -1, -1],
558+
....: [ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0],
559+
....: [ 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1]])
560+
sage: result, certificate = R12_large.is_totally_unimodular(certificate=True)
561+
sage: C = certificate._children()[0]; C
562+
ThreeSumNode (9×12) with 2 children
563+
sage: C.is_distributed_ranks()
564+
True
565+
sage: C.is_concentrated_rank()
566+
False
567567
"""
568568
return <bint> CMRmatroiddecThreeSumDistributedRanks(self._dec)
569569

570570
def is_concentrated_rank(self):
571571
return <bint> CMRmatroiddecThreeSumConcentratedRank(self._dec)
572572

573573
def block_matrix_form(self):
574+
r"""
575+
EXAMPLES::
576+
577+
sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
578+
sage: R12 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 6, 6, sparse=True),
579+
....: [[1,0,1,1,0,0],[0,1,1,1,0,0],[1,0,1,0,1,1],
580+
....: [0,-1,0,-1,1,1],[1,0,1,0,1,0],[0,-1,0,-1,0,1]])
581+
sage: R12
582+
[ 1 0 1 1 0 0]
583+
[ 0 1 1 1 0 0]
584+
[ 1 0 1 0 1 1]
585+
[ 0 -1 0 -1 1 1]
586+
[ 1 0 1 0 1 0]
587+
[ 0 -1 0 -1 0 1]
588+
sage: result, certificate = R12.is_totally_unimodular(certificate=True)
589+
sage: C = certificate._children()[0]; C
590+
ThreeSumNode (6×6) with 2 children
591+
sage: C.block_matrix_form()
592+
[ 0 0 1 -1 1 0]
593+
[ 1 1 1 0 0 0]
594+
[ 0 1 0 1 -1 0]
595+
[ 0 0 0 1 0 1]
596+
[ 1 0 1 0 1 1]
597+
[ 1 0 1 0 0 1]
598+
"""
574599
M1, M2 = self.summand_matrices()
575-
x, y= len(M1.columns()), len(M2.columns())
576-
return Matrix_cmr_chr_sparse.two_sum(M1, M2, x - 1, x-2, y - 1, y - 2)
600+
x = M1.ncols()
601+
return Matrix_cmr_chr_sparse.three_sum(M1, M2, x - 2, x - 1, 0, 1)
577602

578603

579604
cdef class BaseGraphicNode(DecompositionNode):

0 commit comments

Comments
 (0)