#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import scipy.linalg


# PCA Run principal component analysis on the dataset X
#   [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X
#   Returns the eigenvectors U, the eigenvalues (on diagonal) in S
#
def pca(X):
    m, n = np.shape(X)

    # You need to return the following variables correctly.
    U = np.zeros(n)
    S = np.zeros(n)

    # ====================== YOUR CODE HERE ======================
    # Instructions: You should first compute the covariance matrix. Then, you
    #               should use the "svd" function to compute the eigenvectors
    #               and eigenvalues of the covariance matrix.
    #
    # Note: When computing the covariance matrix, remember to divide by m (the
    #       number of examples).
    #
    Sigma = 1 / m * np.dot(X.T, X)
    U, S, vh = np.linalg.svd(Sigma, full_matrices=True)
    S = scipy.linalg.diagsvd(S, len(S), len(S))
    return U, S
