Skip to content

Instantly share code, notes, and snippets.

@matsuken92
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsuken92/2ad112ace8560a73ac30 to your computer and use it in GitHub Desktop.
Save matsuken92/2ad112ace8560a73ac30 to your computer and use it in GitHub Desktop.
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html
#numpy.linalg.eig(a)[source]
#Compute the eigenvalues and right eigenvectors of a square array.
#
#Parameters:
#a : (..., M, M) array
#Matrices for which the eigenvalues and right eigenvectors will be computed
#Returns:
#w : (..., M) array
#The eigenvalues, each repeated according to its multiplicity. The eigenvalues are not necessarily ordered. The resulting array will be #always be of complex type. When a is real the resulting eigenvalues will be real (0 imaginary part) or occur in conjugate pairs
#v : (..., M, M) array
#The normalized (unit “length”) eigenvectors, such that the column v[:,i] is the eigenvector corresponding to the eigenvalue w[i].
#Raises:
#LinAlgError :
#If the eigenvalue computation does not converge.
A = np.array(
[[ 2, 1],
[-0.5,-1.5]])
la, v = np.linalg.eig(A)
print "la",la
print "v",v
print v[0][1]**2 + v[1][1]**2
print v[0][1]**2 / v[1][1]**2
# eig.rb
# (This code is reference of https://gist.github.com/maehrm/43058b429cdb179671c5)
## coding: utf-8
#require 'matrix'
#
#a = Matrix[[2, 1], [-0.5, -1.5]]
#p a.eigensystem.eigenvalues # => [1.8507810593582121, -1.3507810593582121]
#p a.eigensystem.eigenvector_matrix # => Matrix[[0.9890493903846761, -0.31580578969441475], [-0.14758490227560742, 1.0581960585437078]]
ruby_v1 = -0.31580578969441475
ruby_v2 = 1.0581960585437078
print (ruby_v1)**2 + ruby_v2**2
print (ruby_v1)**2 / ruby_v2**2
# ---------- output -------------#
# la [ 1.85078106 -1.35078106]
# v [[ 0.98904939 -0.28597431]
# [-0.1475849 0.95823729]]
# 1.0
# 0.089065168985
#
# 1.21951219512
# 0.089065168985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment