OK. My original code for printing as an array only works if you want to print once. The mat.reshape()
method doesn't just return the reshaped matrix it alters mmat
itself. This means the next time through the loop any linalg
routines will fail.
To avoid this we need to reshape a copy()
of mat
. I've also added a tmp
variable for clarity.
import numpy as np
mat = np.array([[1,2,3],
[4,5,6],
[7,8,9]]) # initialize mat to see format
handle = open('myfile.dat', 'ab')
for n in range(N):
# perform linalg calculations on mat ...
meta = foo # based on current mat
tmp = np.hstack( ([[n]], [[meta]], (mat.copy()).reshape(1,mat.size)) )
np.savetxt(handle, tmp, fmt='%+.8e')
handle.close()
This gets the context data n
and meta
in this case. I can live with n
being saved as a float
.
I did some bench marking to check the i/o cost. I set N=100,000 for the loop, and average the run time for 6 runs:
- no i/o, just computations: 9.1 sec
- as coded above: 17.2 sec
- open 'myfile.dat' to append each iteration: 30.6 sec
So the i/o doubles the runtime and, as expected, constantly opening and closing a file is a bad plan.