def vec(matrix):
    """
    Vectorize, i.e. "vec", a matrix by column stacking.

    For example the 2x2 matrix A

    A = [[a, b]
         [c, d]]      becomes  |A>> := vec(A) = (a, c, b, d)^T ,

    where |A>> denotes the vec'ed version of A and T is a transpose.

    :param matrix: A N x M numpy array.
    :return: Returns a column vector with  N x M rows.
    """
    return matrix.T.reshape((-1, 1))