Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" A binary matrix datatype in Cython
It's almost a copy of the bitset datatype, but allows a differentiation of the rows of the matrix. That's the only advantage compared to storing all the rows of a matrix in a loooooonng bitset.
A ``binary_matrix_t`` structure contains :
- ``Py_ssize_t n_cols`` -- number of columns
- ``Py_ssize_t n_rows`` -- number of rows
- ``bitset_t * rows`` -- ``rows[i]`` points toward a block of type ``bitset_t`` containing the bits of row `i`.
"""
from sage.data_structures.binary_matrix cimport * include 'sage/data_structures/bitset.pxi'
cdef inline binary_matrix_init(binary_matrix_t m, Py_ssize_t n_rows, Py_ssize_t n_cols): r""" Allocate an empty binary matrix with ``n_rows`` rows and ``n_cols`` columns. """ cdef Py_ssize_t i
raise MemoryError
cdef inline binary_matrix_free(binary_matrix_t m): r""" Frees the memory allocated by the matrix """ cdef Py_ssize_t i
cdef inline binary_matrix_fill(binary_matrix_t m, bint bit): r""" Fill the whole matrix with a bit """ cdef Py_ssize_t i
if bit: # set the matrix to 1 for i in range(m.n_rows): bitset_set_first_n(m.rows[i], m.n_cols) else: for i in range(m.n_rows): bitset_clear(m.rows[i])
cdef inline binary_matrix_complement(binary_matrix_t m): r""" Complements all of the matrix' bits. """ cdef Py_ssize_t i
cdef inline binary_matrix_set1(binary_matrix_t m, Py_ssize_t row, Py_ssize_t col): r""" Sets an entry to 1 """
cdef inline binary_matrix_set0(binary_matrix_t m, Py_ssize_t row, Py_ssize_t col): r""" Sets an entry to 0 """
cdef inline binary_matrix_set(binary_matrix_t m, Py_ssize_t row, Py_ssize_t col, bint value): r""" Sets an entry """ bitset_set_to(m.rows[row],col,value)
cdef inline bint binary_matrix_get(binary_matrix_t m, Py_ssize_t row, Py_ssize_t col): r""" Returns the value of a given entry """
cdef inline binary_matrix_print(binary_matrix_t m): r""" Prints the binary matrix """ cdef Py_ssize_t i,j import sys for i in range(m.n_rows): for j in range(m.n_cols): sys.stdout.write("1" if binary_matrix_get(m, i, j) else ".",) print("") |