from numpy import array,random,transpose
n=random.randint(2,9)
a=random.rand(1,n)
b=random.rand(1,n)
for i in range(0,n):
a[0,i]=round(a[0,i]*10)
b[0,i]=round(b[0,i]*10)
print 'n = ',n
print 'a = ',a
print 'b = ',b
print 'Then, (a|b) = \n\n',a*transpose(b)
from numpy import array,random,transpose
a=random.rand(1,2)
b=random.rand(1,2)
for i in range(0,2):
a[0,i]=round(a[0,i]*10)
b[0,i]=round(b[0,i]*10)
print 'a = ',a
print 'b = ',b
x1 = a[0,0]#
x2 = a[0,1]#
y1 = b[0,0]#
y2 = b[0,1]#
t = x1*y1 - x2*y1 - x1*y2 + 4*x2*y2#
print 'Then, a|b = ',t
from numpy import array,random,transpose,linalg,sqrt
print 'x1 and x2 are two real nos. i.e., x1**2 + x2**2 = 1'
x1 = random.rand()
x2 = sqrt(1 - x1**2)
print 'x1 = ',x1
print 'x2 = ',x2
B = array([[x1, x2, 0],[0, 1, 0],[0, 0, 1]])
print 'B = \n',B
print 'Applying Gram-Schmidt process to B:'
a1 = array([x1, x2, 0])
a2 = array([0 ,1 ,0]) - x2 * array([x1 ,x2 ,0])
a3 = array([0, 0, 1])
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
U = array([[a1],[a2/x1],[a3]])
print 'U = \n',U
M = array([[1, 0, 0],[-x2/x1, 1/x1, 0],[0, 0, 1]])
print 'M = \n',M
print 'inverse(M) * U = ',linalg.inv(M) * U
print 'So, B = inverse(M) * U'
from numpy import array,random,transpose,linalg,sqrt
#a = round(rand(1,2) * 10)#
a=random.rand(1,2)
for j in [0,1]:
a[0,j]=round(a[0,j]*10)
x = a[0,0]
y = a[0,1]
b = [-y, x]#
print '(x,y) = ',a
print '(-y,x) = ',b
print 'Inner product of these vectors is:'
t = -x*y + y*x#
print '(x,y)|(-y,x) = ',t
print 'So, these are orthogonal.'
print '------------------------------------------'
print 'If inner product is defined as:'
print '(x1,x2)|(y1,y2) = x1y1- x2y1 - x1y2 + 4x2y2'
print 'Then, (x,y)|(-y,x) = -x*y+y**2-x**2+4*x*y = 0 if,'
print 'y = 1/2(-3 + sqrt(13))*x'
print 'or'
print 'y = 1/2(-3 - sqrt(13))*x'
print 'Hence,'
if y == (1./2*(-3 + sqrt(13))*x) or (1./2*(-3 - sqrt(13))*x):
print a
print 'is orthogonal to'
print b
else:
print a
print 'is not orthogonal to'
print b
from numpy import array,random,transpose,linalg,sqrt
b1 = array([3, 0, 4])
b2 = array([-1 ,0 ,7])
b3 = array([2 ,9 ,11])
print 'b1 = ',b1
print 'b2 = ',b2
print 'b3 = ',b3
print 'Applying the Gram-Schmidt process to b1,b2,b3:'
a1 = b1
print 'a1 = ',a1
a2 = b2-(transpose((b2*transpose(b1)))/25*b1)
print 'a2 = ',a2
a3 = b3-(transpose(b3*transpose(b1))/25*b1) - (transpose(b3*transpose(a2))/25*a2)
print 'a3 = ',a3
print '{a1,a2,a3} are mutually orthogonal and hence forms orthogonal basis for R**3'
print 'Any arbitrary vector {x1,x2,x3} in R**3 can be expressed as:'
print 'y = {x1,x2,x3} = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3'
x1 = 1#
x2 = 2#
x3 = 3#
y = (3*x1 + 4*x3)/25*a1 + (-4*x1 + 3*x3)/25*a2 + x2/9*a3#
print 'x1 = ',x1
print 'x2 = ',x2
print 'x3 = ',x3
print 'y = ',y
print 'i.e. y = [x1 x2 x3], according to above equation.'
print 'Hence, we get the orthonormal basis as:'
print ',',1./5*a1
print ',',1./5*a2
print 1/9.*a3
from numpy import array,random,transpose,linalg,sqrt
A = random.rand(2,2)
A[0,:] = A[0,:] + 1# #so b1 is not equal to zero
a = A[0,0]
b = A[0,1]
c = A[1,0]
d = A[1,1]
b1 = A[0,:]
b2 = A[1,:]
print 'A = ',A
print 'b1 = ',b1
print 'b2 = ',b2
print 'Applying the orthogonalization process to b1,b2:'
a1 = [a, b]
a2 = (linalg.det(A)/(a**2 + b**2))*[-transpose(b), transpose(a)]
print a1,'a1 = '
print a2,'a2 = '
print 'a2 is not equal to zero if and only if b1 and b2 are linearly independent.'
print 'That is, if determinant of A is non-zero.'
from numpy import array,random,transpose,linalg,sqrt
v = array([-10 ,2 ,8])
u = array([3, 12, -1])
print 'v = ',v
print 'u = ',u
print 'Orthogonal projection of v1 on subspace W spanned by v2 is given by:'
a = (transpose(u*transpose(v)))/(u[0]**2 + u[1]**2 + u[2]**2) * u
print a
print 'Orthogonal projection of R**3 on W is the linear transformation E given by:'
print '(x1,x2,x3) -> (3*x1 + 12*x2 - x3)/%d * (3 12 -1)',(u[0]**2 + u[1]**2 + u[2]**2)
print 'Rank(E) = 1'
print 'Nullity(E) = 2'
from mpmath import quad,cos,sin,pi,sqrt
#part c
print 'f = (sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2'
print 'Integration (f dt) in limits 0 to 1 = ',
x0 = 0#
x1 = 1#
X = quad(lambda t:(sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2,[x0,x1])
print X
from numpy import vstack,array,transpose,conj
#Equation given in example 14 is used.
def transform(x,y,z):
x1 = 3*x#
x2 = 12*y#
x3 = -z#
m = [x1 ,x2, x3]
return m
print 'Matrix of projection E in orthonormal basis is:'
t1 = transform(3,3,3)#
t2 = transform(12,12,12)#
t3 = transform(-1,-1,-1)#
A = vstack([t1,t2,t3])#[t1# t2# t3]#
print 'A = 1/154 * ',A
A1 = transpose(conj(A))
print 'A* = ',A1
print 'Since, E = E* and A = A*, then A is also the matrix of E*'
a1 = [154, 0, 0]#
a2 = [145 ,-36, 3]#
a3 = [-36 ,10 ,12]#
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
print '{a1,a2,a3} is the basis.'
Ea1 = [9 ,36 ,-3]#
Ea2 = [0 ,0, 0]#
Ea3 = [0 ,0 ,0]#
print 'Ea1 = ',Ea1
print 'Ea2 = ',Ea2
print 'Ea3 = ',Ea3
B = array([[-1, 0, 0],[-1, 0 ,0],[0, 0, 0]])
print 'Matrix B of E in the basis is:'
print 'B = \n',B
B1 = transpose(conj(B))
print 'B* = \n',B1
print 'Since, B is not equal to B*, B is not the matrix of E*'