#NEWLINE# def svd_reduce( self , max_rank , to_retain = None ) : #NEWLINE# #INDENT# '\n        Reduce the rank of the matrix by retaining some SVD components.\n\n        This corresponds to the "Broyden Rank Reduction Inverse"\n        algorithm described in [1]_.\n\n        Note that the SVD decomposition can be done by solving only a\n        problem whose size is the effective rank of this matrix, which\n        is viable even for large problems.\n\n        Parameters\n        ----------\n        max_rank : int\n            Maximum rank of this matrix after reduction.\n        to_retain : int, optional\n            Number of SVD components to retain when reduction is done\n            (ie. rank > max_rank). Default is ``max_rank - 2``.\n\n        References\n        ----------\n        .. [1] B.A. van der Rotten, PhD thesis,\n           "A limited memory Broyden method to solve high-dimensional\n           systems of nonlinear equations". Mathematisch Instituut,\n           Universiteit Leiden, The Netherlands (2003).\n\n           http://www.math.leidenuniv.nl/scripties/Rotten.pdf\n\n        ' #NEWLINE# if ( self . collapsed is not None ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# p = max_rank #NEWLINE# if ( m is not None ) : #NEWLINE# #INDENT# q = to_retain #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# q = ( p - 2 ) #NEWLINE# #UNINDENT# if self . cs : #NEWLINE# #INDENT# p = min ( p , len ( self . cs [ 0 ] ) ) #NEWLINE# #UNINDENT# q = max ( 0 , min ( q , ( p - 1 ) ) ) #NEWLINE# m = len ( self . cs ) #NEWLINE# if ( m < p ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# C = np . array ( self . cs ) . T #NEWLINE# D = np . array ( self . ds ) . T #NEWLINE# ( D , R ) = qr ( D , mode = 'economic' ) #NEWLINE# C = dot ( C , R . T . conj ( ) ) #NEWLINE# ( U , S , WH ) = svd ( C , full_matrices = False , compute_uv = True ) #NEWLINE# C = dot ( C , inv ( WH ) ) #NEWLINE# D = dot ( D , WH . T . conj ( ) ) #NEWLINE# for k in xrange ( q ) : #NEWLINE# #INDENT# self . cs [ k ] = C [ : , k ] . copy ( ) #NEWLINE# self . ds [ k ] = D [ : , k ] . copy ( ) #NEWLINE# #UNINDENT# del self . cs [ q : ] #NEWLINE# del self . ds [ q : ]