def reblock_2x2(B):
    if not isinstance(B, BlockMatrix) or not all(d > 2 for d in B.blocks.shape):
        return B

    BM = BlockMatrix  # for brevity's sake
    return BM([[   B.blocks[0,  0],  BM(B.blocks[0,  1:])],
               [BM(B.blocks[1:, 0]), BM(B.blocks[1:, 1:])]])
Reblock a BlockMatrix so that it has 2x2 blocks of block matrices
dataset/ETHPy150Open sympy/sympy/sympy/matrices/expressions/blockmatrix.py/reblock_2x2