def eig(self, *args, **kw): from openopt import EIG p = EIG(self.A, *args, **kw) r = p.solve() vectors = [] for i in range(len(p.eigenvalues)): vectors.append(self.decode(p.eigenvectors[:, i])) r.eigenvectors = vectors return r
from openopt import EIG # create a 5 x 5 matrix import numpy.random as nr nr.seed(0) N = 5 A = nr.rand(N, N) #define prob p = EIG(A, goal = {'lm':3}) # search for 3 eigenvalues of largest magnitude # or goal={'largest magnitude':3}, with or without space inside, case-insensitive # for whole list of available goals see http://openopt.org/EIG #solve r = p.solve('arpack') # arpack is name of the involved solver print(r.eigenvalues) # [ 0.14607289-0.19602952j -0.65372843+0.j 2.89776724+0.j ] # for i-th eigenvalue r.eigenvectors[:,i] is corresponding vector, # as well as it is done for numpy/scipy functions print(r.eigenvectors) ''' [[-0.10391145-0.56334829j 0.19592536+0.j 0.43733688+0.j ] [-0.20999235+0.1812288j -0.03219327+0.j 0.49662623+0.j ] [-0.21334642+0.21648181j -0.55544796+0.j 0.42977207+0.j ] [ 0.34828527+0.36295959j 0.62338178+0.j 0.38727512+0.j ] [ 0.04820760-0.49714496j -0.51327338+0.j 0.47687818+0.j ]] '''
# Also, we could use matrix multiplication, eg f5 = dot(someMatrix, f4): # create a matrix rng = 1.5 + cos(range(2*n)).reshape(-1, 1) # define 2n x 1 vector R = dot(rng, rng.T) # create a matrix of shape 2n x 2n # use matrix dot product f3 = dot(R, f3) # choose a point Point = {a:-1, b: [2, -3], c: [4, 5, 6, -7]} # Create Python list of Automaic differentiation results: C = [f.D(Point, exactShape = True) for f in (f1, f2, f3)] # define a prob p = EIG(C, goal={'lm':3}) # solve r = p.solve('arpack') # requires SciPy installed # or goal={'largest magnitude':3}, with or without space inside, case-insensitive # for whole list of available goals see http://openopt.org/EIG # or use numpy_eig solver instead to search all eigenvalues / eigenvectors: # it requires only NumPy installed #p = EIG(C) #r = p.solve('numpy_eig') print(r.eigenvalues) # [-221.93543679+0.j 627.10390438+0.j 625.02348702+0.j] # let's print eigenvector for 1st of the obtained eigenvalues with largest magnitude:
from openopt import EIG # create a 5 x 5 matrix import numpy.random as nr nr.seed(0) N = 5 A = nr.rand(N, N) #define prob p = EIG(A, goal={'lm': 3}) # search for 3 eigenvalues of largest magnitude # or goal={'largest magnitude':3}, with or without space inside, case-insensitive # for whole list of available goals see http://openopt.org/EIG #solve r = p.solve('arpack') # arpack is name of the involved solver print( r.eigenvalues ) # [ 0.14607289-0.19602952j -0.65372843+0.j 2.89776724+0.j ] # for i-th eigenvalue r.eigenvectors[:,i] is corresponding vector, # as well as it is done for numpy/scipy functions print(r.eigenvectors) ''' [[-0.10391145-0.56334829j 0.19592536+0.j 0.43733688+0.j ] [-0.20999235+0.1812288j -0.03219327+0.j 0.49662623+0.j ] [-0.21334642+0.21648181j -0.55544796+0.j 0.42977207+0.j ] [ 0.34828527+0.36295959j 0.62338178+0.j 0.38727512+0.j ] [ 0.04820760-0.49714496j -0.51327338+0.j 0.47687818+0.j ]] '''
# An example of OpenOpt EIG, see http://openopt.org/EIG for more examples and details from openopt import EIG # create a 5 x 5 matrix import numpy.random as nr nr.seed(0) N = 5 A = nr.rand(N, N) #define prob p = EIG(A) #solve r = p.solve('numpy_eig') # solver numpy.linalg.eig will be used print(r.eigenvalues) # [ 2.89776724+0.j -0.65372843+0.j 0.14607289+0.19602952j 0.14607289-0.19602952j -0.08530815+0.j] # for i-th eigenvalue r.eigenvectors[:,i] is corresponding vector, # as well as it is done for numpy/scipy functions print(r.eigenvectors) ''' [[ 0.43733688+0.j -0.19592536+0.j 0.57285154+0.j 0.57285154+0.j 0.63764724+0.j ] [ 0.49662623+0.j 0.03219327+0.j -0.14013112+0.23938241j -0.14013112-0.23938241j -0.53642409+0.j ] [ 0.42977207+0.j 0.55544796+0.j -0.17419089+0.24907549j -0.17419089-0.24907549j 0.29171743+0.j ] [ 0.38727512+0.j -0.62338178+0.j -0.42011495-0.27666898j -0.42011495+0.27666898j -0.45403266+0.j ] [ 0.47687818+0.j 0.51327338+0.j 0.48015310-0.13758665j 0.48015310+0.13758665j 0.12004364+0.j ]] '''
# Also, we could use matrix multiplication, eg f5 = dot(someMatrix, f4): # create a matrix rng = 1.5 + cos(range(2 * n)).reshape(-1, 1) # define 2n x 1 vector R = dot(rng, rng.T) # create a matrix of shape 2n x 2n # use matrix dot product f3 = dot(R, f3) # choose a point Point = {a: -1, b: [2, -3], c: [4, 5, 6, -7]} # Create Python list of Automaic differentiation results: C = [f.D(Point, exactShape=True) for f in (f1, f2, f3)] # define a prob p = EIG(C, goal={'lm': 3}) # solve r = p.solve('arpack') # requires SciPy installed # or goal={'largest magnitude':3}, with or without space inside, case-insensitive # for whole list of available goals see http://openopt.org/EIG # or use numpy_eig solver instead to search all eigenvalues / eigenvectors: # it requires only NumPy installed #p = EIG(C) #r = p.solve('numpy_eig') print(r.eigenvalues) # [-221.93543679+0.j 627.10390438+0.j 625.02348702+0.j] # let's print eigenvector for 1st of the obtained eigenvalues with largest magnitude: