def diagonalize_blocks(arr, blocksize):
    """ Diagonalize sections of columns of an array for the whole array

    Parameters
    ----------

    arr : numpy array
        Input array

    blocksize : int
        number of rows/colums forming one block

    Returns
    -------
    numpy ndarray with shape (columns 'arr' * blocksize,
                              columns 'arr' * blocksize)

    Example
    --------

    arr:      output: (blocksize = 3)
        3 1     3 0 0 1 0 0
        4 2     0 4 0 0 2 0
        5 3     0 0 5 0 0 3
        6 9     6 0 0 9 0 0
        7 6     0 7 0 0 6 0
        8 4     0 0 8 0 0 4
    """

    nr_col = arr.shape[1]
    nr_row = arr.shape[0]

    if np.mod(nr_row, blocksize):
        raise ValueError(
            'Number of rows of input array must be a multiple of blocksize')

    arr_diag = np.zeros((nr_row, blocksize*nr_col))

    for col_ind, col_val in enumerate(arr.T):
        col_start = col_ind*blocksize
        col_end = blocksize + col_ind*blocksize
        for _ind in range(int(nr_row/blocksize)):
            row_start = _ind*blocksize
            row_end = blocksize + _ind * blocksize
            arr_diag[row_start:row_end,
                     col_start:col_end] = np.diag(col_val[row_start:row_end])

    return arr_diag
def diagonalize_blocks ( arr , blocksize ) : nr_col = arr . shape [ 1 ] nr_row = arr . shape [ 0 ] if np . mod ( nr_row , blocksize ) : raise ValueError ( 'Number of rows of input array must be a multiple of blocksize' ) arr_diag = np . zeros ( ( nr_row , blocksize * nr_col ) ) for col_ind , col_val in enumerate ( arr . T ) : col_start = col_ind * blocksize col_end = blocksize + col_ind * blocksize for _ind in range ( int ( nr_row / blocksize ) ) : row_start = _ind * blocksize row_end = blocksize + _ind * blocksize arr_diag [ row_start : row_end , col_start : col_end ] = np . diag ( col_val [ row_start : row_end ] ) return arr_diag
Diagonalize sections of columns of an array for the whole array

    Parameters
    ----------

    arr : numpy array
        Input array

    blocksize : int
        number of rows/colums forming one block

    Returns
    -------
    numpy ndarray with shape (columns 'arr' * blocksize,
                              columns 'arr' * blocksize)

    Example
    --------

    arr:      output: (blocksize = 3)
        3 1     3 0 0 1 0 0
        4 2     0 4 0 0 2 0
        5 3     0 0 5 0 0 3
        6 9     6 0 0 9 0 0
        7 6     0 7 0 0 6 0
        8 4     0 0 8 0 0 4