Chapter3 Orthogonality

Ex:3.1.1 Pg: 143

In [1]:
from numpy import mat,transpose
x1=mat([[2],[2],[-1]])
print 'x1=\n',x1
x2=mat([[-1],[2],[2]])
print 'x2=\n',x2
print "x1'*x2=\n",(transpose(x1)*x2)
print 'Therefore,X1 is orthogonal to x2 .Both have length of sqrt(14).'
x1=
[[ 2]
 [ 2]
 [-1]]
x2=
[[-1]
 [ 2]
 [ 2]]
x1'*x2=
[[0]]
Therefore,X1 is orthogonal to x2 .Both have length of sqrt(14).

Ex:3.1.3 Pg: 145

In [2]:
from numpy import mat,linalg,atleast_2d
A=mat([[1, 3],[2, 6],[3, 9]])
print 'A=\n',A
def nullspace(A, atol=1e-13, rtol=0):
       
       A = atleast_2d(A)
       u, s, vh = linalg.svd(A)
       tol = max(atol, rtol * s[0])
       nnz = (s >= tol).sum()
       ns = vh[nnz:].conj().T
       return ns
ns=nullspace(A)
print 'Null space=\n',ns
print 'A(1,:)*ns=',(A[0]*ns)
print 'A(2,:)*ns=',(A[1]*ns)
print 'A(3,:)*ns=',(A[2]*ns)
print 'This shows that the null space of A is orthogonal to the row space.'
A=
[[1 3]
 [2 6]
 [3 9]]
Null space=
[[-0.9486833 ]
 [ 0.31622777]]
A(1,:)*ns= [[ -2.22044605e-16]]
A(2,:)*ns= [[ -4.44089210e-16]]
A(3,:)*ns= [[ -4.44089210e-16]]
This shows that the null space of A is orthogonal to the row space.

Ex:3.2.1 Pg: 155

In [3]:
from numpy import mat,transpose,sqrt,transpose
b=mat([[1],[2],[3]])
print 'b=\n',b
a=mat([[1],[1],[1]])
print 'a=\n',a
x=(transpose(a)*b)/(transpose(a)*a)
x=x[0,0]
print 'Projection p of b onto the line through a is x***a=\n',(x*a)
cos_theta=(transpose(a)*b)[0,0]/(sqrt(transpose(a)*a)[0,0]*sqrt(transpose(b)*b)[0,0])
print 'cos(thetha) =',cos_theta
b=
[[1]
 [2]
 [3]]
a=
[[1]
 [1]
 [1]]
Projection p of b onto the line through a is x***a=
[[2]
 [2]
 [2]]
cos(thetha) = 0.925820099773

Ex:3.2.2 Pg: 156

In [4]:
from numpy import mat,transpose
a=mat([[1.],[1],[1]])
print 'a=\n',a
P=(a*transpose(a))/(transpose(a)*a)[0,0]
print 'Matrix that projects onto a line through a=(1,1,1) is\n',P
a=
[[ 1.]
 [ 1.]
 [ 1.]]
Matrix that projects onto a line through a=(1,1,1) is
[[ 0.33333333  0.33333333  0.33333333]
 [ 0.33333333  0.33333333  0.33333333]
 [ 0.33333333  0.33333333  0.33333333]]

Ex:3.2.3 Pg: 156

In [5]:
from numpy import mat,transpose,sin,pi,cos
thetha=45# #Taking some value for thetha
a=mat([[cos(thetha*pi/180)],[sin(thetha*pi/180)]])
print 'a=\n',a
P=(a*transpose(a))/(transpose(a)*a)
print 'Projection of line onto the thetha-direction(thetha taken as 45) in the x-y plane passing through a is\n',P
a=
[[ 0.70710678]
 [ 0.70710678]]
Projection of line onto the thetha-direction(thetha taken as 45) in the x-y plane passing through a is
[[ 0.5  0.5]
 [ 0.5  0.5]]

Ex:3.3.1 Pg: 165

In [6]:
from numpy import mat,transpose,linalg,random
A=random.rand(4,4)
print 'A=\n',A
P=A*linalg.inv(transpose(A)*A)*transpose(A)
print 'P=A*inv(A''*A)*A'
print 'Projection of a  invertible 4x4 matrix on to the whole space is:\n',P
print 'Its identity matrix.'
A=
[[ 0.09246118  0.04909557  0.71167391  0.77035248]
 [ 0.4079182   0.15730679  0.91259156  0.21064709]
 [ 0.02898159  0.87211114  0.11852139  0.69009963]
 [ 0.16889615  0.35412933  0.60067694  0.1180308 ]]
P=A*inv(A*A)*A
Projection of a  invertible 4x4 matrix on to the whole space is:
[[  3.38771356e-02  -8.74775251e-02  -1.51348916e-02   1.06873528e+00]
 [ -8.74775251e-02   1.39664206e-03   1.08138671e+00   4.69417392e-03]
 [ -1.51348916e-02   1.08138671e+00  -1.24018336e-04  -6.61277992e-02]
 [  1.06873528e+00   4.69417392e-03  -6.61277992e-02  -7.30165590e-03]]
Its identity matrix.

Ex:3.3.2 Pg: 166

In [7]:
from numpy import mat,transpose,zeros,linalg
print 'b=C+Dt'
print 'Ax=b'
A=mat([[1, -1],[1, 1],[1, 2]])
print 'A=\n',A
b=mat([[1],[1],[3]])
print 'b=\n',b
print 'If Ax=b could be solved then they would be no errors, they can''t be solved because the points are not on a line.Therefore they are solved by least squares.'
print 'so,A''Ax**=A''b'
x=zeros([1,2])
x=linalg.solve((transpose(A)*A), (transpose(A)*b))
print 'C** =',x[0,0]
print 'D**=',x[1,0]
print 'The best line is 9/7+4/7t'
b=C+Dt
Ax=b
A=
[[ 1 -1]
 [ 1  1]
 [ 1  2]]
b=
[[1]
 [1]
 [3]]
If Ax=b could be solved then they would be no errors, they cant be solved because the points are not on a line.Therefore they are solved by least squares.
so,AAx**=Ab
C** = 1.28571428571
D**= 0.571428571429
The best line is 9/7+4/7t

Ex:3.4.1 Pg: 175

In [8]:
from numpy import mat,transpose,sin,pi,cos
thetha=45##Taking some value for thetha.
Q=mat([[cos(pi/180*thetha),-sin(thetha*pi/180)],[sin(pi/180*thetha),cos(pi/180*thetha)]])
print 'Q=\n',Q
print "\nQ'=inv(Q)=\n",transpose(Q)
print '\nQ rotates every vector through an angle thetha, and Q'' rotates it back through -thetha.The columns are clearly orthogonal and they are orthonormal because sin**2(theta)+cos**2(thetha)=1.'
Q=
[[ 0.70710678 -0.70710678]
 [ 0.70710678  0.70710678]]

Q'=inv(Q)=
[[ 0.70710678  0.70710678]
 [-0.70710678  0.70710678]]

Q rotates every vector through an angle thetha, and Q rotates it back through -thetha.The columns are clearly orthogonal and they are orthonormal because sin**2(theta)+cos**2(thetha)=1.

Ex:3.4.2 Pg: 175

In [9]:
from numpy import mat,transpose
print 'Any permutation matrix is an orthogonal matrix.The columns are certainly unit vectors and certainly orthogonal-because the 1 appears in a differnt place in each column'
P=mat([[0, 1, 0],[0, 0 ,1],[1, 0, 0]])
print 'P=\n',P
print "inv(P)=P'=\n",transpose(P)
print "And,P'*P=\n",(transpose(P)*P)
Any permutation matrix is an orthogonal matrix.The columns are certainly unit vectors and certainly orthogonal-because the 1 appears in a differnt place in each column
P=
[[0 1 0]
 [0 0 1]
 [1 0 0]]
inv(P)=P'=
[[0 0 1]
 [1 0 0]
 [0 1 0]]
And,P'*P=
[[1 0 0]
 [0 1 0]
 [0 0 1]]

Ex:3.4.3 Pg: 175

In [10]:
from numpy import mat,transpose,random
print 'If we project b=(x,y,z) onto the x-y plane then its projection is p=(x,y,0),and is the sum of projection onto x- any y-axes.'
b=random.rand(3,1)
q1=mat([[1],[0],[0]])
print 'q1=\n',q1
q2=mat([[0],[1],[0]])
print 'q2=\n',q2
P=q1*transpose(q1)+q2*transpose(q2)
print 'Overall projection matrix,P=\n',P
print 'and,P[x#y#z]=[x#y#0]'
print 'Projection onto a plane=sum of projections onto orthonormal q1 and q2.'
If we project b=(x,y,z) onto the x-y plane then its projection is p=(x,y,0),and is the sum of projection onto x- any y-axes.
q1=
[[1]
 [0]
 [0]]
q2=
[[0]
 [1]
 [0]]
Overall projection matrix,P=
[[1 0 0]
 [0 1 0]
 [0 0 0]]
and,P[x#y#z]=[x#y#0]
Projection onto a plane=sum of projections onto orthonormal q1 and q2.

Ex:3.4.4 Pg: 166

In [19]:
from numpy import mat,transpose,random,zeros
print 'y=C+Dt'
print 'Ax=b'
A=mat([[1, -3],[1, 0],[1, 3]])
print 'A=\n',A
y=random.rand(3,1)
print 'y=\n',y
print 'the columns of A are orthogonal,so'
x=zeros([1,2])
print "C** =\n",( (mat([1, 1, 1])*y)/(transpose(A[:,1])*A[:,1]) )

print "D** =\n",( mat([-3, 0 ,3]*y)/(transpose(A[:,1])*A[:,1]) )
print 'C** gives the besy fit ny horizontal line, whereas D**t is the best fit by a straight line through the origin.'
y=C+Dt
Ax=b
A=
[[ 1 -3]
 [ 1  0]
 [ 1  3]]
y=
[[ 0.72089857]
 [ 0.89298883]
 [ 0.60457288]]
the columns of A are orthogonal,so
C** =
[[ 0.12324779]]
D** =
[[-0.12014976  0.          0.12014976]
 [-0.14883147  0.          0.14883147]
 [-0.10076215  0.          0.10076215]]
C** gives the besy fit ny horizontal line, whereas D**t is the best fit by a straight line through the origin.

Ex:3.4.5 Pg: 166

In [11]:
from numpy import mat,transpose,zeros,shape,linalg
A=mat([[1, 0 ,1],[1, 0, 0],[2, 1, 0]]) #independent vectors stored in columns of A
print 'A=\n',A
m,n=shape(A)
V=mat(zeros([n,n]))
R=mat(zeros([n,n]))
for k in range(0,n):
    V[:,k]=A[:,k]
    for j in range(0,k-1):
        R[j,k]=transpose(V[:,j])*A[:,k]
        V[:,k]=V[:,k]-R[j,k]*V[:,j]
    
    R[k,k]=linalg.norm(V[:,k])
    V[:,k]=V[:,k]/R[k,k]

print 'Q=\n',V
A=
[[1 0 1]
 [1 0 0]
 [2 1 0]]
Q=
[[ 0.40824829  0.          0.91287093]
 [ 0.40824829  0.         -0.18257419]
 [ 0.81649658  1.         -0.36514837]]