def pca_dim_reduction(x, basis, xmean=None):
    """(x, basis, xmean) --> xnew
    Dimensionality reduction with PCA.

    x: N*D data matrix
    basis: D*K basis matrix
    xmean: 1-D vector, mean vector used in PCA, if not set, use the mean of x instead

    xnew: N*K new data matrix
    """

    if xmean is None:
        xmean = x.mean(axis=0)

    xnew = (x - xmean).dot(basis)
    return xnew