Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cmr and add PivotsNode #28

Merged
merged 13 commits into from
Feb 29, 2024
6 changes: 3 additions & 3 deletions build/pkgs/cmr/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tarball=cmr-0+VERSION.tar.gz
sha1=9f6fd03d6bea2ff692c368b07aae7f86e10aaee4
md5=579a141647ef790809f6cd54e7cbb704
cksum=4013467134
sha1=6e55e008de787217fa5b021c693100b98365b311
md5=51844915d4bcd1200a18a46e8ce764f2
cksum=3720610576
upstream_url=https://github.com/discopt/cmr/archive/VERSION.tar.gz
2 changes: 1 addition & 1 deletion build/pkgs/cmr/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224beac5b4186dc6b3bbcbaf19d8e8ffdd63507a
8b0dda14be73d9afc5eeda1461191b2b062072db

This file was deleted.

25 changes: 7 additions & 18 deletions src/sage/libs/cmr/cmr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ cdef extern from "cmr/matroid.h":
size_t* CMRmatroiddecPivotColumns(CMR_MATROID_DEC* dec)
# CMR_ERROR CMRmatroiddecPrint(CMR* cmr, CMR_MATROID_DEC* dec, FILE* stream, size_t indent, bool printChildren, bool printParentRowsColumns, bool printMatrices, bool printGraphs, bool printReductions, bool printPivots)
CMR_ERROR CMRmatroiddecFree(CMR* cmr, CMR_MATROID_DEC** pdec)
CMR_ERROR CMRmatroiddecCreateMatrixRoot(CMR* cmr, CMR_MATROID_DEC** pdec, bool isTernary, CMR_CHRMAT* matrix)
CMR_ERROR CMRmatroiddecUpdateOneSum(CMR* cmr, CMR_MATROID_DEC* dec, size_t numChildren)
CMR_ERROR CMRmatroiddecFreeNode(CMR* cmr, CMR_MATROID_DEC** pdec)

cdef extern from "cmr/separation.h":

Expand Down Expand Up @@ -363,8 +362,8 @@ cdef extern from "cmr/network.h":
CMR_ERROR CMRnetworkStatsInit(CMR_NETWORK_STATISTICS* stats)
# CMR_ERROR CMRstatsNetworkPrint(FILE* stream, CMR_NETWORK_STATISTICS* stats, const char* prefix)
CMR_ERROR CMRnetworkComputeMatrix(CMR* cmr, CMR_GRAPH* digraph, CMR_CHRMAT** pmatrix, CMR_CHRMAT** ptranspose, bool* arcsReversed, int numForestArcs, CMR_GRAPH_EDGE* forestArcs, int numCoforestArcs, CMR_GRAPH_EDGE* coforestArcs, bool* pisCorrectForest)
CMR_ERROR CMRnetworkTestMatrix(CMR* cmr, CMR_CHRMAT* matrix, bool* pisNetwork, CMR_GRAPH** pdigraph, CMR_GRAPH_EDGE** pforestArcs, CMR_GRAPH_EDGE** pcoforestArcs, bool** parcsReversed, CMR_SUBMAT** psubmatrix, CMR_NETWORK_STATISTICS* stats, double timeLimit)
CMR_ERROR CMRnetworkTestTranspose(CMR* cmr, CMR_CHRMAT* matrix, bool* pisConetwork, CMR_GRAPH** pdigraph, CMR_GRAPH_EDGE** pforestArcs, CMR_GRAPH_EDGE** pcoforestArcs, bool** parcsReversed, CMR_SUBMAT** psubmatrix, CMR_NETWORK_STATISTICS* stats, double timeLimit)
CMR_ERROR CMRnetworkTestMatrix(CMR* cmr, CMR_CHRMAT* matrix, bool* pisNetwork, bool* psupportIsGraphic, CMR_GRAPH** pdigraph, CMR_GRAPH_EDGE** pforestArcs, CMR_GRAPH_EDGE** pcoforestArcs, bool** parcsReversed, CMR_SUBMAT** psubmatrix, CMR_NETWORK_STATISTICS* stats, double timeLimit)
CMR_ERROR CMRnetworkTestTranspose(CMR* cmr, CMR_CHRMAT* matrix, bool* pisConetwork, bool* psupportIsCographic, CMR_GRAPH** pdigraph, CMR_GRAPH_EDGE** pforestArcs, CMR_GRAPH_EDGE** pcoforestArcs, bool** parcsReversed, CMR_SUBMAT** psubmatrix, CMR_NETWORK_STATISTICS* stats, double timeLimit)

cdef extern from "cmr/regular.h":

Expand Down Expand Up @@ -403,6 +402,7 @@ cdef extern from "cmr/regular.h":
CMR_ERROR CMRregularStatsInit(CMR_REGULAR_STATS* stats)
# CMR_ERROR CMRstatsRegularPrint(FILE* stream, CMR_REGULAR_STATS* stats, const char* prefix)
CMR_ERROR CMRregularTest(CMR* cmr, CMR_CHRMAT* matrix, bint *pisRegular, CMR_MATROID_DEC** pdec, CMR_MINOR** pminor, CMR_REGULAR_PARAMS* params, CMR_REGULAR_STATS* stats, double timeLimit)
CMR_ERROR CMRregularCompleteDecomposition(CMR* cmr, CMR_MATROID_DEC* dec, CMR_REGULAR_PARAMS* params, CMR_REGULAR_STATS* stats, double timeLimit)


cdef extern from "cmr/tu.h":
Expand Down Expand Up @@ -434,6 +434,7 @@ cdef extern from "cmr/tu.h":
CMR_ERROR CMRtuStatsInit(CMR_TU_STATS* stats)
# CMR_ERROR CMRtuStatsPrint(FILE* stream, CMR_TU_STATS* stats, const char* prefix)
CMR_ERROR CMRtuTest(CMR* cmr, CMR_CHRMAT* matrix, bool* pisTotallyUnimodular, CMR_MATROID_DEC** pdec, CMR_SUBMAT** psubmatrix, CMR_TU_PARAMS* params, CMR_TU_STATS* stats, double timeLimit)
CMR_ERROR CMRtuCompleteDecomposition(CMR* cmr, CMR_MATROID_DEC* dec, CMR_REGULAR_PARAMS* params, CMR_REGULAR_STATS* stats, double timeLimit)


cdef extern from "cmr/equimodular.h":
Expand Down Expand Up @@ -516,6 +517,8 @@ cdef extern from "cmr/balanced.h":

# cdef extern from "cmr/block_decomposition.h":

# ctypedef struct CMR_MATRIX

# ctypedef struct CMR_BLOCK:
# CMR_MATRIX* matrix
# CMR_MATRIX* transpose
Expand All @@ -536,20 +539,6 @@ cdef extern from "cmr/balanced.h":
# )


# cdef extern from "cmr/matrix_internal.h":
# ctypedef struct CMR_MATRIX:
# size_t numRows
# size_t numColumns
# size_t numNonzeros
# size_t* rowSlice
# size_t* entryColumns
# void* entryValues

# CMR_ERROR CMRsortSubmatrix(CMR* cmr, CMR_SUBMAT* submatrix)

# CMR_ERROR CMRchrmatFilter(CMR* cmr, CMR_CHRMAT* matrix, size_t numRows, size_t* rows, size_t numColumns, size_t* columns, CMR_CHRMAT** presult)


# Our global CMR environment
cdef CMR *cmr

Expand Down
155 changes: 140 additions & 15 deletions src/sage/matrix/matrix_cmr_sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,72 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sum.set_immutable()
return sum

def three_sum_cmr(first_mat, second_mat,
first_index1, first_index2, second_index1, second_index2,
three_sum_strategy="distributed_ranks"):
r"""


EXAMPLES::

sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
sage: M1 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 2, 3, sparse=True),
....: [[1, 2, 3], [4, 5, 6]]); M1
[1 2 3]
[4 5 6]
sage: M2 = Matrix_cmr_chr_sparse(MatrixSpace(ZZ, 2, 3, sparse=True),
....: [[7, 8, 9], [-1, -2, -3]]); M2
[ 7 8 9]
[-1 -2 -3]
"""
cdef Matrix_cmr_chr_sparse sum, first, second
cdef CMR_CHRMAT *sum_mat
first = Matrix_cmr_chr_sparse._from_data(first_mat, immutable=False)
second = Matrix_cmr_chr_sparse._from_data(second_mat, immutable=False)

if three_sum_strategy not in ["distributed_ranks", "concentrated_rank"]:
raise ValueError("Unknown three sum mode", three_sum_strategy)

if three_sum_strategy == "distributed_ranks":
row1 = first_index1
column2 = first_index2
column1 = second_index1
row2 = second_index2
if row1 < 0 or row1 >= first._mat.numRows:
raise ValueError("First marker 1 should be a row index of the first matrix")
if column2 < 0 or column2 >= first._mat.numColumns:
raise ValueError("First marker 2 should be a column index of the first matrix")
if column1 < 0 or column1 >= second._mat.numColumns:
raise ValueError("Second marker 1 should be a column index of the second matrix")
if row2 < 0 or row2 >= second._mat.numRows:
raise ValueError("Second marker 2 should be a row index of the second matrix")
first_marker1 = CMRrowToElement(row1)
first_marker2 = CMRcolumnToElement(column2)
second_marker1 = CMRcolumnToElement(column1)
second_marker2 = CMRrowToElement(row2)
else:
row1 = first_index1
row2 = first_index2
column1 = second_index1
column2 = second_index2
if row1 < 0 or row1 >= first._mat.numRows:
raise ValueError("First marker 1 should be a Row index of the first matrix")
if row2 < 0 or row2 >= first._mat.numRows:
raise ValueError("First marker 2 should be a Row index of the first matrix")
if column1 < 0 or column1 >= second._mat.numColumns:
raise ValueError("Second marker 1 should be a column index of the second matrix")
if column2 < 0 or column2 >= second._mat.numColumns:
raise ValueError("Second marker 2 should be a column index of the second matrix")
first_marker1 = CMRrowToElement(row1)
first_marker2 = CMRrowToElement(row2)
second_marker1 = CMRcolumnToElement(column1)
second_marker2 = CMRcolumnToElement(column2)

cdef int8_t characteristic = 0
CMR_CALL(CMRthreeSum(cmr, first._mat, second._mat, first_marker1, second_marker1, first_marker2, second_marker2, characteristic, &sum_mat))
sum = Matrix_cmr_chr_sparse._from_cmr(sum_mat)
return sum

def three_sum(first_mat, second_mat, first_col_index1, first_col_index2, second_col_index1, second_col_index2):
r"""
Return the 3-sum matrix constructed from the given matrices ``first_mat`` and
Expand Down Expand Up @@ -731,6 +797,62 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
row_list.append(x)
return Matrix_cmr_chr_sparse._from_data(row_list, immutable=False)

def binary_pivot(self, row, column):
cdef Matrix_cmr_chr_sparse result
cdef size_t pivot_row = row
cdef size_t pivot_column = column
cdef CMR_CHRMAT *result_mat

CMR_CALL(CMRchrmatBinaryPivot(cmr, self._mat, pivot_row, pivot_column, &result_mat))
result = Matrix_cmr_chr_sparse._from_cmr(result_mat)
return result

def binary_pivots(self, rows, columns):
cdef Matrix_cmr_chr_sparse result
cdef size_t* pivot_rows
cdef size_t* pivot_columns
cdef CMR_CHRMAT *result_mat

npivots = len(rows)
if len(columns) != npivots:
raise ValueError("The pivot rows and columns must have the same length")

for i in range(npivots):
pivot_rows[i] = rows[i]
pivot_columns[i] = columns[i]

CMR_CALL(CMRchrmatBinaryPivots(cmr, self._mat, npivots, pivot_rows, pivot_columns, &result_mat))
result = Matrix_cmr_chr_sparse._from_cmr(result_mat)
return result

def ternary_pivot(self, row, column):
cdef Matrix_cmr_chr_sparse result
cdef size_t pivot_row = row
cdef size_t pivot_column = column
cdef CMR_CHRMAT *result_mat

CMR_CALL(CMRchrmatTernaryPivot(cmr, self._mat, pivot_row, pivot_column, &result_mat))
result = Matrix_cmr_chr_sparse._from_cmr(result_mat)
return result

def ternary_pivots(self, rows, columns):
cdef Matrix_cmr_chr_sparse result
cdef size_t* pivot_rows
cdef size_t* pivot_columns
cdef CMR_CHRMAT *result_mat

cdef size_t npivots = len(rows)
if len(columns) != npivots:
raise ValueError("The pivot rows and columns must have the same length")

for i in range(npivots):
pivot_rows[i] = rows[i]
pivot_columns[i] = columns[i]

CMR_CALL(CMRchrmatTernaryPivots(cmr, self._mat, npivots, pivot_rows, pivot_columns, &result_mat))
result = Matrix_cmr_chr_sparse._from_cmr(result_mat)
return result

def is_unimodular(self, time_limit=60.0):
r"""
Return whether ``self`` is a unimodular matrix.
Expand Down Expand Up @@ -1163,6 +1285,7 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
Graphics object consisting of 21 graphics primitives
"""
cdef bool result
cdef bool support_result
cdef CMR_GRAPH *digraph = NULL
cdef CMR_GRAPH_EDGE* forest_arcs = NULL
cdef CMR_GRAPH_EDGE* coforest_arcs = NULL
Expand All @@ -1173,11 +1296,11 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sig_on()
try:
if certificate:
CMR_CALL(CMRnetworkTestMatrix(cmr, self._mat, &result, &digraph, &forest_arcs,
CMR_CALL(CMRnetworkTestMatrix(cmr, self._mat, &result, &support_result, &digraph, &forest_arcs,
&coforest_arcs, &arcs_reversed, &submatrix, &stats,
time_limit))
else:
CMR_CALL(CMRnetworkTestMatrix(cmr, self._mat, &result, NULL, NULL,
CMR_CALL(CMRnetworkTestMatrix(cmr, self._mat, &result, &support_result, NULL, NULL,
NULL, NULL, NULL, &stats, time_limit))
finally:
sig_off()
Expand All @@ -1203,8 +1326,8 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
series_parallel_ok=True,
check_graphic_minors_planar=False,
complete_tree='if_regular',
construct_matrices=False,
construct_transposes=False,
three_sum_pivot_children=False,
three_sum_strategy=None,
construct_graphs=False):
r"""
Return whether the linear matroid of ``self`` over `\GF{2}` is regular.
Expand Down Expand Up @@ -1317,8 +1440,8 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
series_parallel_ok=series_parallel_ok,
check_graphic_minors_planar=check_graphic_minors_planar,
complete_tree=complete_tree,
construct_matrices=construct_matrices,
construct_transposes=construct_transposes,
three_sum_pivot_children=three_sum_pivot_children,
three_sum_strategy=three_sum_strategy,
construct_graphs=construct_graphs)

_set_cmr_regular_parameters(&params, kwds)
Expand All @@ -1343,8 +1466,8 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
series_parallel_ok=True,
check_graphic_minors_planar=False,
complete_tree='if_regular',
construct_matrices=False,
construct_transposes=False,
three_sum_pivot_children=False,
three_sum_strategy=None,
construct_graphs=False):
r"""
Return whether ``self`` is a totally unimodular matrix.
Expand Down Expand Up @@ -1386,11 +1509,10 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
Full MatrixSpace of 6 by 14 sparse matrices over Integer Ring
sage: from sage.matrix.matrix_cmr_sparse import Matrix_cmr_chr_sparse
sage: MFR2cmr = Matrix_cmr_chr_sparse(MS2, MFR2)
sage: MFR2cmr.is_totally_unimodular(certificate=True, construct_matrices=True)
sage: MFR2cmr.is_totally_unimodular(certificate=True)
(False, (None, ((0, 1, 2), (3, 4, 5))))
sage: result, certificate = MFR2cmr.is_totally_unimodular(certificate=True,
....: complete_tree=True,
....: construct_matrices=True)
....: complete_tree=True)
sage: result, certificate
(False, (None, ((0, 1, 2), (3, 4, 5))))
sage: submatrix = MFR2.matrix_from_rows_and_columns(*certificate[1]); submatrix
Expand All @@ -1409,12 +1531,14 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
cdef CMR_MATROID_DEC **pdec = &dec
cdef CMR_SUBMAT **psubmat = &submat

if three_sum_pivot_children:
raise NotImplementedError
cdef dict kwds = dict(use_direct_graphicness_test=use_direct_graphicness_test,
series_parallel_ok=series_parallel_ok,
check_graphic_minors_planar=check_graphic_minors_planar,
complete_tree=complete_tree,
construct_matrices=construct_matrices,
construct_transposes=construct_transposes,
three_sum_pivot_children=three_sum_pivot_children,
three_sum_strategy=three_sum_strategy,
construct_graphs=construct_graphs)

params.algorithm = CMR_TU_ALGORITHM_DECOMPOSITION
Expand Down Expand Up @@ -1466,8 +1590,9 @@ cdef _set_cmr_regular_parameters(CMR_REGULAR_PARAMS *params, dict kwds):
params.seriesParallel = kwds['series_parallel_ok']
params.planarityCheck = kwds['check_graphic_minors_planar']
params.completeTree = kwds['complete_tree'] is True
# params.threeSumPivotChildren = _cmr_dec_construct(kwds['construct_matrices'])
# params.threeSumStrategy = _cmr_dec_construct(kwds['construct_transposes'])
params.threeSumPivotChildren = kwds['three_sum_pivot_children']
if kwds['three_sum_strategy'] is not None:
params.threeSumStrategy = kwds['three_sum_strategy']
params.graphs = _cmr_dec_construct(kwds['construct_graphs'])


Expand Down
Loading
Loading