from pprint import pprint
from numpy import array, zeros, diagflat, dot, transpose
from scipy.linalg import eig
A = array([[0,1,1,1,1,0,1],
[1,0,0,0,0,0,0],
[1,1,0,0,0,0,0],
[0,1,1,0,1,0,0],
[1,0,1,1,0,1,0],
[1,0,0,0,1,0,0],
[0,0,0,0,1,0,0]])
diag = []
for i in range(0,7):
tmp = 0.0
for j in range(0,7):
tmp += A[i,j]
diag.append(1.0/tmp)
D = diagflat(diag)
tA = dot(transpose(A),D)
import numpy as np
from pprint import pprint
np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
#pprint(A)
#pprint(D)
#pprint(transpose(A))
pprint(dot(transpose(A),D))
x = array([1,0,0,0,0,0,0])
#pprint(x)
#pprint(dot(tA,dot(tA,dot(tA,x))))
l, V = eig(tA)
v0 = V[:,0]
pprint(v0)
array([[ 0.000, 1.000, 0.500, 0.000, 0.250, 0.500, 0.000], [ 0.200, 0.000, 0.500, 0.333, 0.000, 0.000, 0.000], [ 0.200, 0.000, 0.000, 0.333, 0.250, 0.000, 0.000], [ 0.200, 0.000, 0.000, 0.000, 0.250, 0.000, 0.000], [ 0.200, 0.000, 0.000, 0.333, 0.000, 0.500, 1.000], [ 0.000, 0.000, 0.000, 0.000, 0.250, 0.000, 0.000], [ 0.200, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000]]) array([ 0.69945653+0.j, 0.38286042+0.j, 0.32395882+0.j, 0.24296911+0.j, 0.41231122+0.j, 0.10307780+0.j, 0.13989131+0.j])
l, V = eig(tA)
v0 = V[:,0]
pprint(v0)
array([ 0.69945653+0.j, 0.38286042+0.j, 0.32395882+0.j, 0.24296911+0.j, 0.41231122+0.j, 0.10307780+0.j, 0.13989131+0.j])
x = array([1,0,0,0,0,0,0])
pprint(dot(tA,dot(tA,x)))
for i in range(0,10):
x = dot(tA,x)
print(x)
array([ 0.350, 0.167, 0.117, 0.050, 0.267, 0.050, 0.000]) [ 0.000 0.200 0.200 0.200 0.200 0.000 0.200] [ 0.350 0.167 0.117 0.050 0.267 0.050 0.000] [ 0.317 0.145 0.153 0.137 0.112 0.067 0.070] [ 0.283 0.186 0.137 0.091 0.212 0.028 0.063] [ 0.321 0.155 0.140 0.110 0.164 0.053 0.057] [ 0.293 0.171 0.142 0.105 0.184 0.041 0.064] [ 0.308 0.165 0.140 0.105 0.178 0.046 0.059] [ 0.302 0.166 0.141 0.106 0.178 0.045 0.062] [ 0.304 0.166 0.140 0.105 0.180 0.045 0.060] [ 0.304 0.166 0.141 0.106 0.178 0.045 0.061]
また,対角化行列$P$ を求めて,対角化せよ. $$ A\, = \, \left[ \begin {array}{ccc} 0&1&-2\\ -3&7&-3 \\ 3&-5&5 \end {array} \right] $$
import numpy as np
from pprint import pprint
np.set_printoptions(precision=3, suppress=True)
A = np.array([[0,1,-2],[-3,7,-3],[3,-5,5]])
l, P = np.linalg.eig( A )
pprint(l)
pprint(P)
array([ 1., 9., 2.]) array([[-0.905, 0.229, -0.707], [-0.302, 0.688, 0. ], [ 0.302, -0.688, 0.707]])
dA = np.dot(np.linalg.inv(P),np.dot(A,P))
pprint(dA)
array([[ 1., 0., 0.], [-0., 9., -0.], [ 0., 0., 2.]])