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
-
My code opens up a text file containing a matrix:
1 2
3 4
and places it in listxas integers. -
The list
xis then converted into an arraya. -
The eigenvalues of
aare placed inland the eigenvectors are placed inv. -
I then want to take the exp(a) and place it in another array
exponent. -
Then I create an identity matrix
Lof whatever lengthlis. -
My for loop is supposed to take the values of
exponentand replace the 1’s across the diagonal of the identity matrix but I get an error sayinginvalid index to scalar variable.
What is wrong with my code?
exponentis a 1D array. This means thatexponent[0]is a scalar, andexponent[0][i]is trying to access it as if it were an array.Did you mean to say:
or even
?