I've been looking this up, but I couldn't find anything that helped. I'm having some trouble making an array of the size I want.
So far, I have created a list of lists with this code:
n=4
def matrixA(k):
    A=[]
    for m in range(0,k):
        row=[]
        #A.append([])
        for n in range(0,k):
            if (n==(m+1)) or (n==(m-1)):
                row.append(-deltaX/(deltaX)**2)
            if (n==m):
                row.append(2*deltaX/(deltaX)**2)
            else:
                row.append(0)
        A.append(row)
    return A
pprint.pprint(matrixA(n))
print len(matrixA(n))
I get this output.
[[128.0, -64.0, 0, 0, 0],
 [-64.0, 0, 128.0, -64.0, 0, 0],
 [0, -64.0, 0, 128.0, -64.0, 0],
 [0, 0, -64.0, 0, 128.0]]
4
Now, I want to make this an array of size (4,4). My problem is that when I do the following (converting the list to an array and trying to shape it):
A=numpy.array(matrixA(n))
print A
print "This is its shape:",A.shape
A.shape=(n,n)
I obtain:
[[128.0, -64.0, 0, 0, 0] [-64.0, 0, 128.0, -64.0, 0, 0]
 [0, -64.0, 0, 128.0, -64.0, 0] [0, 0, -64.0, 0, 128.0]]
This is its shape: (4,)
And then an error:
ValueError: total size of new array must be unchanged
How am I supposed to get an array of size (4,4) from here then?
 
     
     
    