def bmatrix(aaa):
    """Returns a LaTeX bmatrix

    :aaa: numpy array or pg.matrix
    :returns: LaTeX bmatrix as a string
    """
    output = "\\begin{bmatrix} "

    nrows,ncols = aaa.shape
    for r in range(nrows):
        for c in range(ncols):
            realp = aaa[r][c].real
            imagp = aaa[r][c].imag
            
            if abs(realp) < 1e-16:
                realp=0.0
            if abs(imagp) < 1e-16:
                imagp=0.0

            if imagp == 0.0:
                str_num = "{:.4}".format(realp)
            elif realp == 0.0:
                str_num = "{:.4}j".format(imagp)
            else:
                str_num = "{:.4}".format(aaa[r][c])

            output += str_num


            if c+1 ==ncols:
                output +=  " \\\\ "
            else:
                output +=  " & "

    output = output + "\\end{bmatrix}"
    
    return output