def readMatrix(text):
    """Reads a matrix from text in the format
    m n
    x11 x12 ... x1n
    ...
    xm1 xm2 ... xmn
    """
    items = text.split()
    if len(items) < 2: raise ValueError("Invalid matrix string")
    m,n = int(items[0]),int(items[1])
    if len(items) != 2 + m*n:
        raise ValueError("Invalid number of matrix elements, should be %d, instead got %d"%(m*n,len(items)-2))
    k = 2
    x = []
    for i in range(m):
        x.append([float(v) for v in items[k:k+n]])
        k += n
    return x