#NEWLINE# def whitener( C , rtol = 1e-15 ) : #NEWLINE# #INDENT# '\n  Calculate the whitening transform for signals with covariance C.\n\n  The whitening transform is used to remove covariance between\n  signals, and can be regarded as principal component analysis with\n  rescaling and an optional rotation. The whitening transform is\n  calculated as C^{-1/2}, and implemented to work with rank-deficient\n  covariance matrices.\n\n  Parameters\n  ----------\n  C : array-like, shape (p, p)\n    Covariance matrix of the signals to be whitened.\n  rtol : float, optional\n    Cut-off value specified as the fraction of the largest eigenvalue\n    of C.\n\n  Returns\n  -------\n  W : array, shape (p, p)\n    Symmetric matrix with spatial filters in the rows.\n\n  See also\n  --------\n  car : common average reference\n\n  Examples\n  --------\n  >>> X = np.random.randn(3, 100) \n  >>> W = whitener(np.cov(X))\n  >>> X_2 = np.dot(W, X - np.mean(X, axis=1).reshape(-1, 1))\n\n  The covariance of X_2 is now close to the identity matrix:\n  >>> np.linalg.norm(np.cov(X_2) - np.eye(3)) < 1e-10\n  True\n  ' #NEWLINE# ( e , E ) = np . linalg . eigh ( C ) #NEWLINE# return reduce ( np . dot , [ E , np . diag ( ( np . where ( ( e > ( np . max ( e ) * E ) ) , e , np . inf ) ** ( - 0.5 ) ) ) , E . T ] )