本文整理汇总了Python中sympy.Matrix.eigenvects方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.eigenvects方法的具体用法?Python Matrix.eigenvects怎么用?Python Matrix.eigenvects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Matrix
的用法示例。
在下文中一共展示了Matrix.eigenvects方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import eigenvects [as 别名]
def main():
fp=open('clusters','w')
fp2=open('eigenvalues and best cluster number k','w')
fp3=open('cluster sizes','w')
trainingSet=[]
loadDataset1('roadNet-PA.csv',trainingSet)
G=Graph_build(trainingSet)
print len(G.nodes())
N_to_M, M_to_N=Node_num_and_matrix_num(G)
similarity_matrix=similarity_cal(G,N_to_M)
degree_matrix=degree_cal(similarity_matrix)
L_matrix=L_cal(degree_matrix,similarity_matrix)
M=Matrix(L_matrix)
print 'start eigenvectors'
MM=M.eigenvects()
print 'end eigenvectors'
v,w=get_eigen(L_matrix)
L=len(G.nodes())
k=get_k(MM)
w=get_new_eigenvectors(MM,w)
for x in range(len(v)):
fp2.write(repr(v[x])+'\n')
Y=get_Y(w,L,k)
Y_pred=KMeans(n_clusters=k, max_iter=600).fit_predict(Y)
clusterList=clusters_cal(k,Y_pred,M_to_N)
for x in range(len(clusterList)):
fp.write('This is '+repr(x)+'th cluster and it has members:\n')
fp.write(repr(clusterList[x])+'\n\n\n\n')
fp3.write(repr(len(clusterList[x]))+'\n')
for x in range(len(clusterList)):
G_new=new_Graph_build(G,clusterList[x])
random_mqi(G_new,x)
示例2: etape
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import eigenvects [as 别名]
def etape(self, frontier, i_graph):
""" Calculates the most probable seed within the infected nodes"""
# Taking the actual submatrix, not the laplacian matrix. The change
# lies in the total number of connections (The diagonal terms) for the
# infected nodes connected to uninfected ones in the initial graph
i_laplacian_matrix = nx.laplacian_matrix(i_graph)
for i in range(0, len(i_graph.nodes())):
if frontier.has_node(i_graph.nodes()[i]):
i_laplacian_matrix[i, i] +=\
frontier.node[i_graph.nodes()[i]]['clear']
# SymPy
Lm = Matrix(i_laplacian_matrix.todense())
i = self.Sym2NumArray(Matrix(Lm.eigenvects()[0][2][0])).argmax()
# NumPy
# val, vect = linalg.eigh(i_laplacian_matrix.todense())
#i = vect[0].argmax()
# SciPY
# val, vect = eigs(i_laplacian_matrix.rint())
# i = vect[:, 0].argmax()
seed = (i_graph.nodes()[i])
return seed
示例3: test_eigen
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import eigenvects [as 别名]
def test_eigen():
x,y = symbols('xy')
R = Rational
assert eye(3).charpoly(x) == Poly((x-1)**3, x)
assert eye(3).charpoly(y) == Poly((y-1)**3, y)
M = Matrix([[1,0,0],
[0,1,0],
[0,0,1]])
assert M.eigenvals() == {S.One: 3}
assert canonicalize(M.eigenvects()) == canonicalize(
[(1, 3, [Matrix([1,0,0]),
Matrix([0,1,0]),
Matrix([0,0,1])])])
M = Matrix([[0,1,1],
[1,0,0],
[1,1,1]])
assert M.eigenvals() == {2*S.One: 1, -S.One: 1, S.Zero: 1}
assert canonicalize(M.eigenvects()) == canonicalize(
[( 2, 1, [Matrix([R(2,3), R(1,3), 1])]),
(-1, 1, [Matrix([-1, 1, 0])]),
( 0, 1, [Matrix([ 0,-1, 1])])])
eps = Symbol('eps',real=True)
M = Matrix([[abs(eps), I*eps ],
[-I*eps, abs(eps) ]])
assert canonicalize(M.eigenvects()) == canonicalize(
[( 2*abs(eps), 1, [ Matrix([[I*eps/abs(eps)],[1]]) ] ),
( 0, 1, [Matrix([[-I*eps/abs(eps)],[1]])]) ])
示例4: test_sparse_matrix
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import eigenvects [as 别名]
#.........这里部分代码省略.........
# test_inverse
A = eye(4)
assert A.inv() == eye(4)
assert A.inv("LU") == eye(4)
assert A.inv("ADJ") == eye(4)
A = SMatrix([[2,3,5],
[3,6,2],
[8,3,6]])
Ainv = A.inv()
assert A*Ainv == eye(3)
assert A.inv("LU") == Ainv
assert A.inv("ADJ") == Ainv
# test_cross
v1 = Matrix(1,3,[1,2,3])
v2 = Matrix(1,3,[3,4,5])
assert v1.cross(v2) == Matrix(1,3,[-2,4,-2])
assert v1.norm(v1) == 14
# test_cofactor
assert eye(3) == eye(3).cofactorMatrix()
test = SMatrix([[1,3,2],[2,6,3],[2,3,6]])
assert test.cofactorMatrix() == SMatrix([[27,-6,-6],[-12,2,3],[-3,1,0]])
test = SMatrix([[1,2,3],[4,5,6],[7,8,9]])
assert test.cofactorMatrix() == SMatrix([[-3,6,-3],[6,-12,6],[-3,6,-3]])
# test_jacobian
x = Symbol('x')
y = Symbol('y')
L = SMatrix(1,2,[x**2*y, 2*y**2 + x*y])
syms = [x,y]
assert L.jacobian(syms) == Matrix([[2*x*y, x**2],[y, 4*y+x]])
L = SMatrix(1,2,[x, x**2*y**3])
assert L.jacobian(syms) == SMatrix([[1, 0], [2*x*y**3, x**2*3*y**2]])
# test_QR
A = Matrix([[1,2],[2,3]])
Q, S = A.QRdecomposition()
R = Rational
assert Q == Matrix([[5**R(-1,2), (R(2)/5)*(R(1)/5)**R(-1,2)], [2*5**R(-1,2), (-R(1)/5)*(R(1)/5)**R(-1,2)]])
assert S == Matrix([[5**R(1,2), 8*5**R(-1,2)], [0, (R(1)/5)**R(1,2)]])
assert Q*S == A
assert Q.T * Q == eye(2)
# test nullspace
# first test reduced row-ech form
R = Rational
M = Matrix([[5,7,2,1],
[1,6,2,-1]])
out, tmp = M.rref()
assert out == Matrix([[1,0,-R(2)/23,R(13)/23],
[0,1,R(8)/23, R(-6)/23]])
M = Matrix([[1,3,0,2,6,3,1],
[-2,-6,0,-2,-8,3,1],
[3,9,0,0,6,6,2],
[-1,-3,0,1,0,9,3]])
out, tmp = M.rref()
assert out == Matrix([[1,3,0,0,2,0,0],
[0,0,0,1,2,0,0],
[0,0,0,0,0,1,R(1)/3],
[0,0,0,0,0,0,0]])
# now check the vectors
basis = M.nullspace()
assert basis[0] == Matrix([[-3,1,0,0,0,0,0]])
assert basis[1] == Matrix([[0,0,1,0,0,0,0]])
assert basis[2] == Matrix([[-2,0,0,-2,1,0,0]])
assert basis[3] == Matrix([[0,0,0,0,0,R(-1)/3, 1]])
# test eigen
x = Symbol('x')
y = Symbol('y')
eye3 = eye(3)
assert eye3.charpoly(x) == (1-x)**3
assert eye3.charpoly(y) == (1-y)**3
# test values
M = Matrix([(0,1,-1),
(1,1,0),
(-1,0,1) ])
vals = M.eigenvals()
vals.sort()
assert vals == [-1, 1, 2]
R = Rational
M = Matrix([ [1,0,0],
[0,1,0],
[0,0,1]])
assert M.eigenvects() == [[1, 3, [Matrix(1,3,[1,0,0]), Matrix(1,3,[0,1,0]), Matrix(1,3,[0,0,1])]]]
M = Matrix([ [5,0,2],
[3,2,0],
[0,0,1]])
assert M.eigenvects() == [[1, 1, [Matrix(1,3,[R(-1)/2,R(3)/2,1])]],
[2, 1, [Matrix(1,3,[0,1,0])]],
[5, 1, [Matrix(1,3,[1,1,0])]]]
assert M.zeros((3, 5)) == SMatrix(3, 5, {})
示例5: test_eigen
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import eigenvects [as 别名]
def test_eigen():
x,y = symbols('xy')
R = Rational
assert eye(3).charpoly(x) == Poly((x-1)**3, x)
assert eye(3).charpoly(y) == Poly((y-1)**3, y)
M = Matrix([[1,0,0],
[0,1,0],
[0,0,1]])
assert M.eigenvals() == {S.One: 3}
assert canonicalize(M.eigenvects()) == canonicalize(
[(1, 3, [Matrix([1,0,0]),
Matrix([0,1,0]),
Matrix([0,0,1])])])
M = Matrix([[0,1,1],
[1,0,0],
[1,1,1]])
assert M.eigenvals() == {2*S.One: 1, -S.One: 1, S.Zero: 1}
assert canonicalize(M.eigenvects()) == canonicalize(
[( 2, 1, [Matrix([R(2,3), R(1,3), 1])]),
(-1, 1, [Matrix([-1, 1, 0])]),
( 0, 1, [Matrix([ 0,-1, 1])])])
M = Matrix([ [1, -1],
[1, 3]])
assert canonicalize(M.eigenvects()) == canonicalize(
[[2, 2, [Matrix(1,2,[-1,1])]]])
M = Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ])
a=R(15,2)
b=3*33**R(1,2)
c=R(13,2)
d=(R(33,8) + 3*b/8)
e=(R(33,8) - 3*b/8)
def NS(e, n):
return str(N(e, n))
r = [
(a - b/2, 1, [Matrix([(12 + 24/(c - b/2))/((c - b/2)*e) + 3/(c - b/2),
(6 + 12/(c - b/2))/e,1])]),
( 0, 1, [Matrix([1,-2,1])]),
(a + b/2, 1, [Matrix([(12 + 24/(c + b/2))/((c + b/2)*d) + 3/(c + b/2),
(6 + 12/(c + b/2))/d,1])]),
]
r1 = [(NS(r[i][0],2),NS(r[i][1],2),[NS(j,2) for j in r[i][2][0]]) for i in range(len(r))]
r = M.eigenvects()
r2=[(NS(r[i][0],2),NS(r[i][1],2),[NS(j,2) for j in r[i][2][0]]) for i in range(len(r))]
assert sorted(r1) == sorted(r2)
eps = Symbol('eps',real=True)
M = Matrix([[abs(eps), I*eps ],
[-I*eps, abs(eps) ]])
assert canonicalize(M.eigenvects()) == canonicalize(
[( 2*abs(eps), 1, [ Matrix([[I*eps/abs(eps)],[1]]) ] ),
( 0, 1, [Matrix([[-I*eps/abs(eps)],[1]])]) ])