Python: Choleski decomposition of matrix read from file with blank lines

时间:2017-08-04 13:02:40

标签: python numpy file-io

I have a txt file which represents the entries of a matrix. It is structured like this:

columns  rows              value

x1       x1                matrix value for x1-x1
x1       x2                matrix value for x1-x2
x1       x3                matrix value for x1-x3
.
. 
.
x1      x_n               matrix value for x1 - x_n
(blank line)
x2       x1               matrix value for x2 - x1
x2       x2               matrix value for x2 - x2 
x2       x3               matrix value for x2 - x3
.
.
.
x2        x_n             matrix value for x2 - x_n
(blank line)
.
.
.

and so on...

I need to read it and store the matrix values in a matrix, to be plotted with imshow and also put in a way such that I can act on it with the Cholesky decomposition implemented in numpy.

There are two problems I am facing :

1) skipping the blank lines in the file (if that matters, I know at which line they occur) I tried to solve this by isolating the third column from my original file and using the following on it

with open('thirdcolumn.txt') as f_in: 
    lines = list(line for line in (l.strip() for l in f_in) if line)

2) nevertheless, if I do that and reshape the matrix

matrix = np.reshape(lines,(n,n))

this is not good for the Cholesky decomposition:

No loop matching the specified signature and casting
was found for ufunc cholesky_lo

nor for plotting

TypeError: Image data can not convert to float

0 个答案:

没有答案