I am new to python so I am sorry if my formatting, etc is bad or if this question is trivial but I am trying to figure out why I am getting an index error in the third (and I assume second) to last lines of my code. I include the function just for context so you can see the QR function that the array I am calling is derived from. The command giving the index error works in the second line of the code but not the second to last. Can anyone explain why this is? I am thinking it has something to do with the fact that a_new is an array?
Also, from what I understand a[:,0] takes the first column from a matrix or array. If this understanding is not correct I would love to learn the right way to do this too.
Thanks!
a=np.random.random((3,3)); a
a[:,2]
###Gram-Schmidt operation for finding Q orthonormal matrix
###We begin with the reduced 3X3 case
def QR(a):
A0=a[:,0]; A0
A1=a[:,1]; A1
A2=a[:,2]; A2
Z0=A0
Z1=A1-np.vdot(A1,A0)/np.vdot(A0,A0)*A0
Z2=A2-np.vdot(A2,A0)/np.vdot(A0,A0)*A0-np.vdot(A2,Z1)/np.vdot(Z1,Z1)*Z1
Z0norm=np.linalg.norm(Z0); Z0norm
Z1norm=np.linalg.norm(Z1); Z1norm
Z2norm=np.linalg.norm(Z2); Z2norm
q0=Z0/Z0norm
q1=Z1/Z1norm
q2=Z2/Z2norm
a_new=np.array([[q0],[q1],[q2]])
return a_new
a_new=QR(a); a_new
a_new0=a_new[:,0]
a_new1=a_new[:,1]
g1=np.vdot(a_new1,a_new2); g1
EDIT: Sorry! Here is the error message:
a_new0=a_new[:,0] a_new1=a_new[:,1] Traceback (most recent call last): File "", line 1, in IndexError: index 1 is out of bounds for axis 1 with size 1
a_new
is 3x3? Did you try looking at it, or checkinga_new.shape
? (Spoiler alert: it's not.) – DSM Feb 15 at 20:06