Skip to content

Commit

Permalink
src/sage/libs/cmr/cmr.pxd, src/sage/matrix/matrix_cmr_sparse.pyx: Upd…
Browse files Browse the repository at this point in the history
…ate for new CMR version
  • Loading branch information
Matthias Koeppe committed Dec 11, 2023
1 parent 2ee073a commit cb9363f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 21 deletions.
67 changes: 60 additions & 7 deletions src/sage/libs/cmr/cmr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

# (progn (replace-regexp "/[*]\\(.\\|\n\\)*?[*]/" "" nil (point) (point-max)) (replace-regexp "[;{}]" "" nil (point) (point-max)) (replace-regexp "CMR_EXPORT *" "" nil (point) (point-max)) (replace-regexp "bool" "bint" nil (point) (point-max)))

from libc.stdint cimport uint32_t, int64_t

cdef extern from "stdbool.h":

ctypedef int bool
Expand Down Expand Up @@ -46,6 +48,21 @@ cdef extern from "cmr/matrix.h":
CMR_ERROR CMRsubmatCreate1x1(CMR* cmr, size_t row, size_t column, CMR_SUBMAT** psubmatrix)
CMR_ERROR CMRsubmatFree(CMR* cmr, CMR_SUBMAT** psubmatrix)

ctypedef struct CMR_INTMAT:
size_t numRows
size_t numColumns
size_t numNonzeros
size_t* rowSlice
size_t* entryColumns
int* entryValues

CMR_ERROR CMRintmatCreate(CMR* cmr, CMR_INTMAT** presult, int numRows, int numColumns, int numNonzeros)
CMR_ERROR CMRintmatSortNonzeros(CMR* cmr, CMR_INTMAT* matrix)
# CMR_ERROR CMRintmatPrintDense(CMR* cmr, CMR_INTMAT* matrix, FILE* stream, char zeroChar, bint header)
CMR_ERROR CMRintmatFindEntry(CMR_INTMAT* matrix, size_t row, size_t column, size_t* pentry)
CMR_ERROR CMRintmatZoomSubmat(CMR* cmr, CMR_INTMAT* matrix, CMR_SUBMAT* submatrix, CMR_INTMAT** presult)
CMR_ERROR CMRintmatFree(CMR* cmr, CMR_INTMAT** pmatrix)

ctypedef struct CMR_CHRMAT:
size_t numRows
size_t numColumns
Expand All @@ -61,12 +78,8 @@ cdef extern from "cmr/matrix.h":
CMR_ERROR CMRchrmatZoomSubmat(CMR* cmr, CMR_CHRMAT* matrix, CMR_SUBMAT* submatrix, CMR_CHRMAT** presult)
CMR_ERROR CMRchrmatFree(CMR* cmr, CMR_CHRMAT** pmatrix)

cdef extern from "cmr/k_modular.h":

CMR_ERROR CMRtestUnimodularity(CMR* cmr, CMR_CHRMAT* matrix, int* pisUnimodular)
CMR_ERROR CMRtestStrongUnimodularity(CMR* cmr, CMR_CHRMAT* matrix, bool* pisStronglyUnimodular)
CMR_ERROR CMRtestKmodularity(CMR* cmr, CMR_CHRMAT* matrix, bool* pisKmodular, size_t* pk)
CMR_ERROR CMRtestStrongKmodularity(CMR* cmr, CMR_CHRMAT* matrix, bool* pisStronglyKmodular, size_t* pk)
CMR_ERROR CMRchrmatToInt(CMR* cmr, CMR_CHRMAT* matrix, CMR_INTMAT** presult)
CMR_ERROR CMRintmatToChr(CMR* cmr, CMR_INTMAT* matrix, CMR_CHRMAT** presult)

cdef extern from "cmr/camion.h":

Expand Down Expand Up @@ -362,7 +375,14 @@ cdef extern from "cmr/regular.h":

cdef extern from "cmr/tu.h":

const int CMR_TU_ALGORITHM_DECOMPOSITION
const int CMR_TU_ALGORITHM_SUBMATRIX
const int CMR_TU_ALGORITHM_PARTITION

ctypedef int CMR_TU_ALGORITHM

ctypedef struct CMR_TU_PARAMETERS:
CMR_TU_ALGORITHM algorithm
CMR_REGULAR_PARAMETERS regular

CMR_ERROR CMRparamsTotalUnimodularityInit(CMR_TU_PARAMETERS* params)
Expand All @@ -378,6 +398,39 @@ cdef extern from "cmr/tu.h":
CMR_ERROR CMRtestTotalUnimodularity(CMR* cmr, CMR_CHRMAT* matrix, bool* pisTotallyUnimodular, CMR_DEC** pdec, CMR_SUBMAT** psubmatrix, CMR_TU_PARAMETERS* params, CMR_TU_STATISTICS* stats, double timeLimit)


cdef extern from "cmr/equimodular.h":

ctypedef struct CMR_EQUIMODULAR_PARAMETERS:
CMR_TU_PARAMETERS tu

CMR_ERROR CMRparamsEquimodularityInit(CMR_EQUIMODULAR_PARAMETERS* params)

ctypedef struct CMR_EQUIMODULAR_STATISTICS:
uint32_t totalCount
double totalTime
double linalgTime
CMR_TU_STATISTICS tu

CMR_ERROR CMRstatsEquimodularityInit(CMR_EQUIMODULAR_STATISTICS* stats)

CMR_ERROR CMRtestEquimodularity(CMR* cmr, CMR_INTMAT* matrix,
bool* pisEquimodular, int64_t *pgcdDet,
CMR_EQUIMODULAR_PARAMETERS* params, CMR_EQUIMODULAR_STATISTICS* stats,
double timeLimit)
CMR_ERROR CMRtestStrongEquimodularity(CMR* cmr, CMR_INTMAT* matrix,
bool* pisStronglyEquimodular, int64_t *pgcdDet,
CMR_EQUIMODULAR_PARAMETERS* params, CMR_EQUIMODULAR_STATISTICS* stats,
double timeLimit)
CMR_ERROR CMRtestUnimodularity(CMR* cmr, CMR_INTMAT* matrix,
bool* pisUnimodular,
CMR_EQUIMODULAR_PARAMETERS* params, CMR_EQUIMODULAR_STATISTICS* stats,
double timeLimit)
CMR_ERROR CMRtestStrongUnimodularity(CMR* cmr, CMR_INTMAT* matrix,
bool* pisStronglyUnimodular,
CMR_EQUIMODULAR_PARAMETERS* params, CMR_EQUIMODULAR_STATISTICS* stats,
double timeLimit)


cdef extern from "cmr/ctu.h":

ctypedef struct CMR_CTU_STATISTICS:
Expand All @@ -388,7 +441,7 @@ cdef extern from "cmr/ctu.h":
CMR_ERROR CMRstatsComplementTotalUnimodularityInit(CMR_CTU_STATISTICS* stats)
# CMR_ERROR CMRstatsComplementTotalUnimodularityPrint(FILE* stream, CMR_CTU_STATISTICS* stats, const char* prefix)
CMR_ERROR CMRcomplementRowColumn(CMR* cmr, CMR_CHRMAT* matrix, size_t complementRow, size_t complementColumn, CMR_CHRMAT** presult)
CMR_ERROR CMRtestComplementTotalUnimodularity(CMR* cmr, CMR_CHRMAT* matrix, bool* pisComplementTotallyUnimodular, size_t* pcomplementRow, size_t* pcomplementColumn, CMR_CTU_STATISTICS* stats)
CMR_ERROR CMRtestComplementTotalUnimodularity(CMR* cmr, CMR_CHRMAT* matrix, bool* pisComplementTotallyUnimodular, size_t* pcomplementRow, size_t* pcomplementColumn, CMR_CTU_STATISTICS* stats, double timeLimit)


# Our global CMR environment
Expand Down
40 changes: 26 additions & 14 deletions src/sage/matrix/matrix_cmr_sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ 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 is_unimodular(self):
def is_unimodular(self, time_limit=60.0):
r"""
Return whether ``self`` is a unimodular matrix.
Expand Down Expand Up @@ -653,17 +653,20 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: M.is_unimodular()
False
"""
cdef CMR_INTMAT *int_mat = NULL
cdef bool result

sig_on()
try:
CMR_CALL(CMRtestUnimodularity(cmr, self._mat, &result))
CMR_CALL(CMRchrmatToInt(cmr, self._mat, &int_mat))
CMR_CALL(CMRtestUnimodularity(cmr, int_mat, &result, NULL, NULL, time_limit))
finally:
CMRintmatFree(cmr, &int_mat)
sig_off()

return <bint> result

def is_strongly_unimodular(self):
def is_strongly_unimodular(self, time_limit=60.0):
r"""
Return whether ``self`` is a strongly unimodular matrix.
Expand All @@ -690,17 +693,20 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: M.is_strongly_unimodular()
True
"""
cdef CMR_INTMAT *int_mat = NULL
cdef bool result

sig_on()
try:
CMR_CALL(CMRtestStrongUnimodularity(cmr, self._mat, &result))
CMR_CALL(CMRchrmatToInt(cmr, self._mat, &int_mat))
CMR_CALL(CMRtestStrongUnimodularity(cmr, int_mat, &result, NULL, NULL, time_limit))
finally:
CMRintmatFree(cmr, &int_mat)
sig_off()

return <bint> result

def modulus(self):
def modulus(self, time_limit=60.0):
r"""
Return the integer `k` such that ``self`` is `k`-modular.
Expand Down Expand Up @@ -733,21 +739,24 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
[0 1 3]
sage: M.modulus()
"""
cdef CMR_INTMAT *int_mat = NULL
cdef bool result
cdef size_t k
cdef int64_t k

sig_on()
try:
CMR_CALL(CMRtestKmodularity(cmr, self._mat, &result, &k))
CMR_CALL(CMRchrmatToInt(cmr, self._mat, &int_mat))
CMR_CALL(CMRtestEquimodularity(cmr, int_mat, &result, &k, NULL, NULL, time_limit))
finally:
CMRintmatFree(cmr, &int_mat)
sig_off()

if result:
return Integer(k)
else:
return None

def strong_modulus(self):
def strong_modulus(self, time_limit=60.0):
r"""
Return the integer `k` such that ``self`` is strongly `k`-modular.
Expand Down Expand Up @@ -780,21 +789,24 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: M.strong_modulus()
1
"""
cdef CMR_INTMAT *int_mat = NULL
cdef bool result
cdef size_t k
cdef int64_t k

sig_on()
try:
CMR_CALL(CMRtestStrongKmodularity(cmr, self._mat, &result, &k))
CMR_CALL(CMRchrmatToInt(cmr, self._mat, &int_mat))
CMR_CALL(CMRtestStrongEquimodularity(cmr, int_mat, &result, &k, NULL, NULL, time_limit))
finally:
CMRintmatFree(cmr, &int_mat)
sig_off()

if result:
return Integer(k)
else:
return None

def is_k_modular(self, k):
def is_k_modular(self, k, time_limit=60.0):
r"""
Return whether ``self`` is `k`-modular.
Expand Down Expand Up @@ -833,13 +845,13 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: M.is_k_modular(1)
False
"""
result = self.modulus()
result = self.modulus(time_limit=time_limit)
if not result:
return False
else:
return result == k

def is_strongly_k_modular(self, k):
def is_strongly_k_modular(self, k, time_limit=60.0):
r"""
Return whether ``self`` is strongly `k`-modular.
Expand All @@ -864,7 +876,7 @@ cdef class Matrix_cmr_chr_sparse(Matrix_cmr_sparse):
sage: M.is_strongly_k_modular(1)
True
"""
result = self.strong_modulus()
result = self.strong_modulus(time_limit=time_limit)
if not result:
return False
else:
Expand Down

0 comments on commit cb9363f

Please sign in to comment.