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
我的代碼打開了包含矩陣的文本文件:
1 2
3 4
,並將其放置在列表中的「x」爲整數。列表「x」然後被轉換成數組「a」。 「a」的特徵值置於「l」中,特徵向量置於「v」中。然後我想把exp(a)放到另一個數組「exponent」中。然後我創建一個任意長度爲「l」的單位矩陣,並稱之爲矩陣「L」。我的for循環假設採用「指數」的值並將1代入單位矩陣的對角線,但我得到一個錯誤,說「無效的標量變量索引」。我的代碼有什麼問題?有一個標量變量錯誤的索引是什麼意思?蟒蛇
請回復追蹤:) –