213 - A15 - Gram-Schmidt

148 days ago by Professor213

# Do this first cell in worksheet in Octave mode # If copying to the sagecell, then \n needs to be changed to \\n 
       
A = floor(rand(4,4)*10)-5; det(A) A =[0 2 2 -1; -2 -1 -5 1; 2 -1 2 4; 2 3 -5 1]; # Comment this out if you want to get another random matrix. Just check to see that det(a) is nonzero v1 = A(:,1); v2 = A(:,2); v3 = A(:,3); v4 = A(:,4); disp("Here are the original vectors") V = [v1 v2 v3 v4]; printf ("% 4.0f % 4.0f % 4.0f % 4.0f \n", V'); disp("Now apply Gram-Schmidt") u1 = v1; u2 = v2 - (v2'*u1)/(u1'*u1)*u1; u3 = v3 - (v3'*u1)/(u1'*u1)*u1 - (v3'*u2)/(u2'*u2)*u2; u4 = v4 - (v4'*u1)/(u1'*u1)*u1 - (v4'*u2)/(u2'*u2)*u2 - (v4'*u3)/(u3'*u3)*u3; Q = [u1 u2 u3 u4]; printf ("% 5.3f % 5.3f % 5.3f % 5.3f \n", Q'); # if desired, make the vectors all unit vectors too for orthonormal disp(" and after making vectors all unit vectors...") Q = [u1/norm(u1) u2/norm(u2) u3/norm(u3) u4/norm(u4)]; printf ("% 5.3f % 5.3f % 5.3f % 5.3f \n", Q'); disp(' ') disp(' Check for orthogonality') printf ("u1 with u2: % 9.8f \n",u1'*u2) printf ("u1 with u3: % 9.8f \n",u1'*u3) printf ("u1 with u4: % 9.8f \n",u1'*u4) printf ("u2 with u3: % 9.8f \n",u2'*u3) printf ("u2 with u4: % 9.8f \n",u2'*u4) printf ("u3 with u4: % 9.8f \n",u3'*u4) 
       
ans = -172
Here are the original vectors
   0    2    2   -1 
  -2   -1   -5    1 
   2   -1    2    4 
   2    3   -5    1 
Now apply Gram-Schmidt
 0.000  2.000  3.667  1.345 
-2.000  0.000 -4.333  1.138 
 2.000 -2.000 -0.333  1.241 
 2.000  2.000 -4.000 -0.103 
 and after making vectors all unit vectors...
 0.000  0.577  0.527  0.623 
-0.577  0.000 -0.623  0.527 
 0.577 -0.577 -0.048  0.575 
 0.577  0.577 -0.575 -0.048 
 
 Check for orthogonality
u1 with u2:  0.00000000 
u1 with u3:  0.00000000 
u1 with u4:  0.00000000 
u2 with u3:  0.00000000 
u2 with u4: -0.00000000 
u3 with u4: -0.00000000 
ans = -172
Here are the original vectors
   0    2    2   -1 
  -2   -1   -5    1 
   2   -1    2    4 
   2    3   -5    1 
Now apply Gram-Schmidt
 0.000  2.000  3.667  1.345 
-2.000  0.000 -4.333  1.138 
 2.000 -2.000 -0.333  1.241 
 2.000  2.000 -4.000 -0.103 
 and after making vectors all unit vectors...
 0.000  0.577  0.527  0.623 
-0.577  0.000 -0.623  0.527 
 0.577 -0.577 -0.048  0.575 
 0.577  0.577 -0.575 -0.048 
 
 Check for orthogonality
u1 with u2:  0.00000000 
u1 with u3:  0.00000000 
u1 with u4:  0.00000000 
u2 with u3:  0.00000000 
u2 with u4: -0.00000000 
u3 with u4: -0.00000000 
# Notice, this also give us the A = QR factorization format short R = Q'*A Q*R - A 
       
R =

   3.46410   1.73205   1.15470   2.30940
   0.00000   3.46410  -2.88675  -2.30940
  -0.00000   0.00000   6.95222  -1.91785
   0.00000  -0.00000  -0.00000   2.15758

ans =

   3.3269e-16   4.4409e-16   0.0000e+00   2.2204e-16
   0.0000e+00  -2.2204e-16   8.8818e-16  -2.2204e-16
   8.8818e-16  -2.2204e-16   8.8818e-16   1.7764e-15
   4.4409e-16   8.8818e-16   0.0000e+00   0.0000e+00
R =

   3.46410   1.73205   1.15470   2.30940
   0.00000   3.46410  -2.88675  -2.30940
  -0.00000   0.00000   6.95222  -1.91785
   0.00000  -0.00000  -0.00000   2.15758

ans =

   3.3269e-16   4.4409e-16   0.0000e+00   2.2204e-16
   0.0000e+00  -2.2204e-16   8.8818e-16  -2.2204e-16
   8.8818e-16  -2.2204e-16   8.8818e-16   1.7764e-15
   4.4409e-16   8.8818e-16   0.0000e+00   0.0000e+00
# Let's do it in 3D in Sage mode # a = random_matrix(QQ,3,3)] a = matrix(QQ,[[1,-1,0],[-1,1/2,1],[0,-2,0]]) v1 =vector(QQ,[1,-1,0]) v2 =vector(QQ,[-1,1/2,-2]) v3 =vector(QQ,[0,1,0]) G = plot(v1,color='red',opacity=0.2)+plot(v2,color='blue',opacity=0.2)+plot(v3,color='orange',opacity=0.2) u1 = v1 u2 = v2 - v2.inner_product(u1)/u1.inner_product(u1)*u1 u3 = v3 - v3.inner_product(u1)/u1.inner_product(u1)*u1 - v3.inner_product(u2)/u2.inner_product(u2)*u2 print(u1) print(u2) print(u3) G += plot(u1,color='red')+plot(u2,color='blue')+plot(u3,color='orange') G += plot(u1/norm(u1),color='black')+plot(u2/norm(u2),color='black')+plot(u3/norm(u3),color='black') G.show(spin=true) 
       
(1, -1, 0)
(-1/4, -1/4, -2)
(16/33, 16/33, -4/33)
(1, -1, 0)
(-1/4, -1/4, -2)
(16/33, 16/33, -4/33)
u1.inner_product(u2) 
       
0
0
# Let's do it in 3D in Sage mode v1 =vector(QQbar,[1+I,-1,0]) v2 =vector(QQbar,[-1,1/2,-2-I]) v3 =vector(QQbar,[I,1,0]) u1 = v1 u2 = v2 - v2.inner_product(u1)/u1.inner_product(u1)*u1 u3 = v3 - v3.inner_product(u1)/u1.inner_product(u1)*u1 - v3.inner_product(u2)/u2.inner_product(u2)*u2 show(u1) show(u2) show(u3) 
       

                                
                            

                                
u1.inner_product(u2) 
       
0
0