I'm trying to print this array to a text file but only boxes show up, why? Python
Question: 
    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)
    
    L = identity(len(l))
    
    for i in xrange(len(l)):
        L[i][i] = array(exp(l))[i]
    
    exponent = dot(dot(v,L),inv(v))
    
    
    file = open('expA.txt', 'w')
    file.writelines(exponent)
    file.close()
    

The variable `exponent` is a matrix and I want to print it to a file
`expA.txt`, however, when I run the code, only brackets show up.

**matrix.txt**

    
    
    2 2
    16 6
    

In addition (if you happen to know), the `exp(a)` is supposed to equal VLV^-1
but when I print the values of `exp(a)` and compare it to VLV^-1, I get
different values. The problem is:

>   1. Read in a text file in which you've specified a square matrix A;
>   2. Find A's eigenvalues (a vector l) and eigenvectors (a matrix V);
>   3. Apply the Python function exp() to the components of l and form a
> diagonal matrix L with those values on the diagonal;
>   4. Multiply the resulting matrix L with the eigenvector matrix from both
> sides, in this way: V L V-1. This undoes the diagonlization. It turns out
> that the result is the exponential of the matrix A; that is, eA = V L V-1.
>

I did this in my code and I got different values. Did I interpret this wrong?


Answer: I think your problem may be the line `file.writelines(exponent)`, where
`file.writelines` method is expecting an iterable container of strings.

Could you try using this instead:

    
    
    with open('expA.txt', 'w') as f:
      print >> f, exponent
    

By the way, you should try using `a = np.loadtxt('matrix.txt')` as an easier
way to read your input file.

