import numpy as np
with open('matrix.txt', 'r') as f:
x = []
for line in f:
x.append(map(int, line.split()))
f.close()
a = array(x)
l, v = eig(a)
exponent = array(exp(l))
L = identity(len(l))
for i in xrange(len(l)):
L[i][i] = exponent[0][i]
print L
Run Code Online (Sandbox Code Playgroud)
我的代码打开一个包含矩阵的文本文件:
1 2
3 4
并将其作为整数放在列表"x"中.然后将列表"x"转换为数组"a"."a"的特征值放在"l"中,特征向量放在"v"中.然后我想把exp(a)放在另一个数组"exponent"中.然后我创建一个长度为"l"的单位矩阵,并调用矩阵"L".我的for循环假设采用"exponent"的值并在单位矩阵的对角线上替换1,但是我得到一个错误,说"标量变量的索引无效".我的代码出了什么问题?
# Import plotting routines
from pylab import *
# 1D ODE that has a pitchfork bifurcation
# x_dot = r * x - x * x * x
def PitchforkODE(r,x):
return r * x - x * x * x
# 1D Euler
def OneDEuler(r,x,f,dt):
return x + dt * f(r,x)
# Improved 1D Euler
def ImprovedOneDEuler(r,x,f,dt):
xtemp = x + dt * f(r,x)
return x + dt * ( f(r,x) + f(r,xtemp) ) / 2.0
# 4th Order Runge-Kutta …Run Code Online (Sandbox Code Playgroud)