Esempio n. 1
0
    def squarify(triangular_matrix: csr_matrix) -> csr_matrix:
        """Mirror a triangular matrix at the diagonal to make it a square matrix.

        The input matrix *must* be upper triangular to begin with, otherwise
        the results will be incorrect. No guard rails!
        """
        assert (triangular_matrix.shape[0] == triangular_matrix.shape[1]
                ), "needs to be square matrix"
        # The matrix is already upper diagonal. Use the transpose method, see
        # https://stackoverflow.com/a/58806735/2340703.
        return (triangular_matrix + triangular_matrix.T -
                scipy.sparse.diags(triangular_matrix.diagonal()))