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).'
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.'
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
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
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
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.'
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'
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.'
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)
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.'
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.'
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