Chapter 8 - Inner product spaces

Page 271 Example 8.1

In [1]:
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)
n =  4
a =  [[ 1.  5.  4.  1.]]
b =  [[ 8.  6.  6.  8.]]
Then, (a|b) = 

[[  8.  40.  32.   8.]
 [  6.  30.  24.   6.]
 [  6.  30.  24.   6.]
 [  8.  40.  32.   8.]]

Page 271 Example 8.2

In [2]:
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
a =  [[ 9.  4.]]
b =  [[ 1.  9.]]
Then, a|b =  68.0

Page 307 Example 8.28

In [3]:
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'
x1 and x2  are two real nos. i.e., x1**2 + x2**2 = 1
x1 =  0.248003219206
x2 =  0.968759208092
B = 
[[ 0.24800322  0.96875921  0.        ]
 [ 0.          1.          0.        ]
 [ 0.          0.          1.        ]]
Applying Gram-Schmidt process to B:
a1 =  [ 0.24800322  0.96875921  0.        ]
a2 =  [-0.2402554  0.0615056  0.       ]
a3 =  [0 0 1]
U = 
[[[ 0.24800322  0.96875921  0.        ]]

 [[-0.96875921  0.24800322  0.        ]]

 [[ 0.          0.          1.        ]]]
M = 
[[ 1.          0.          0.        ]
 [-3.90623642  4.03220572  0.        ]
 [ 0.          0.          1.        ]]
inverse(M) * U =  [[[ 0.24800322 -0.         -0.        ]
  [ 0.2402554   0.2402554   0.        ]
  [ 0.          0.          0.        ]]

 [[-0.96875921 -0.         -0.        ]
  [-0.9384944   0.0615056   0.        ]
  [-0.          0.          0.        ]]

 [[ 0.         -0.         -0.        ]
  [ 0.          0.          0.        ]
  [ 0.          0.          1.        ]]]
So, B = inverse(M) * U

Page 278 Example 8.9

In [4]:
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
(x,y) =  [[ 7.  4.]]
(-y,x) =  [-4.0, 7.0]
Inner product of these vectors is:
(x,y)|(-y,x) =  0.0
So, these are orthogonal.
------------------------------------------
If inner product is defined as:
(x1,x2)|(y1,y2) = x1y1- x2y1 - x1y2 + 4x2y2
Then, (x,y)|(-y,x) = -x*y+y**2-x**2+4*x*y = 0 if,
y = 1/2(-3 + sqrt(13))*x
or
y = 1/2(-3 - sqrt(13))*x
Hence,
[[ 7.  4.]]
is orthogonal to
[-4.0, 7.0]

Page 282 Example 8.12

In [5]:
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
b1 =  [3 0 4]
b2 =  [-1  0  7]
b3 =  [ 2  9 11]
Applying the Gram-Schmidt process to b1,b2,b3:
a1 =  [3 0 4]
a2 =  [2 0 3]
a3 =  [2 9 4]
{a1,a2,a3} are mutually orthogonal and hence forms orthogonal basis for R**3
Any arbitrary vector {x1,x2,x3} in R**3 can be expressed as:
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 =  [0 0 0]
i.e. y = [x1 x2 x3], according to above equation.
Hence, we get the orthonormal basis as:
, [ 0.6  0.   0.8]
, [ 0.4  0.   0.6]
[ 0.22222222  1.          0.44444444]

Page 283 Example 8.13

In [6]:
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.'
A =  [[ 1.83351494  1.26265003]
 [ 0.46651205  0.76790774]]
b1 =  [ 1.83351494  1.26265003]
b2 =  [ 0.46651205  0.76790774]
Applying the orthogonalization process to b1,b2:
[1.8335149394280341, 1.2626500316837608] a1 = 
[] a2 = 
a2 is not equal to zero if and only if b1 and b2 are linearly independent.
That is, if determinant of A is non-zero.

Page 286 Example 8.14

In [7]:
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'
v =  [-10   2   8]
u =  [ 3 12 -1]
Orthogonal projection of v1 on subspace W spanned by v2 is given by:
[-3  0  1]
Orthogonal projection of R**3 on W is the linear transformation E given by:
(x1,x2,x3) -> (3*x1 + 12*x2 - x3)/%d * (3 12 -1) 154
Rank(E) = 1
Nullity(E)  = 2

Page 288 Example 8.15

In [8]:
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
f = (sqrt(2)*cos(2*pi*t) + sqrt(2)*sin(4*pi*t))**2
Integration (f dt) in limits 0 to 1 =  2.0

Page 294 Example 8.17

In [9]:
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*'
Matrix of projection E in orthonormal basis is:
A = 1/154  *   [[  9  36  -3]
 [ 36 144 -12]
 [ -3 -12   1]]
A* =  [[  9  36  -3]
 [ 36 144 -12]
 [ -3 -12   1]]
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]
{a1,a2,a3} is the basis.
Ea1 =  [9, 36, -3]
Ea2 =  [0, 0, 0]
Ea3 =  [0, 0, 0]
Matrix B of E in the basis is:
B = 
[[-1  0  0]
 [-1  0  0]
 [ 0  0  0]]
B* = 
[[-1 -1  0]
 [ 0  0  0]
 [ 0  0  0]]
Since, B is not equal to B*, B is not the matrix of E*