Chapter 2 - Vector spaces

Page 37 Example 2.8

In [1]:
a1 = [1, 2 ,0 ,3, 0]#
a2 =[0, 0 ,1 ,4 ,0]#
a3 = [0 ,0 ,0 ,0, 1]#
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
print 'By theorem 3, vector a is in subspace W of F**5 spanned by a1, a2, a3'
print 'if and only if there exist scalars c1, c2, c3 such that'
print 'a= c1a1 + c2a2 + c3a3'
print 'So, a = (c1,2*c1,c2,3c1+4c2,c3)'
c1 = -3#
c2 = 1#
c3 = 2#
a = c1*a1 + c2*a2 + c3*a3#
print 'c1 = ',c1
print 'c2 = ',c2
print 'c3 = ',c3
print 'Therefore, a = ',a
print 'This shows, a is in W'
print 'And (2,4,6,7,8) is not in W as there is no value of c1 c2 c3 that satisfies the equation'
a1 =  [1, 2, 0, 3, 0]
a2 =  [0, 0, 1, 4, 0]
a3 =  [0, 0, 0, 0, 1]
By theorem 3, vector a is in subspace W of F**5 spanned by a1, a2, a3
if and only if there exist scalars c1, c2, c3 such that
a= c1a1 + c2a2 + c3a3
So, a = (c1,2*c1,c2,3c1+4c2,c3)
c1 =  -3
c2 =  1
c3 =  2
Therefore, a =  [0, 0, 1, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
This shows, a is in W
And (2,4,6,7,8) is not in W as there is no value of c1 c2 c3 that satisfies the equation

Page 38 Example 2.10

In [2]:
from numpy import array

A = array([[1, 2, 0 ,3 ,0],[0, 0, 1, 4, 0],[0, 0, 0, 0, 1]])
print 'A = \n',A
print 'The subspace of F**5 spanned by a1 a2 a3(row vectors of A) is called row space of A.'
a1 = A[0,:]
a2 = A[1,:]
a3 = A[2,:]
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
print 'And, it is also the row space of B.'
B = array([[1, 2, 0, 3, 0],[0, 0, 1, 4, 0],[0, 0, 0, 0, 1],[-4, -8, 1 ,-8, 0]])
print 'B = \n',B
A = 
[[1 2 0 3 0]
 [0 0 1 4 0]
 [0 0 0 0 1]]
The subspace of F**5 spanned by a1 a2 a3(row vectors of A) is called row space of A.
a1 =  [1 2 0 3 0]
a2 =  [0 0 1 4 0]
a3 =  [0 0 0 0 1]
And, it is also the row space of B.
B = 
[[ 1  2  0  3  0]
 [ 0  0  1  4  0]
 [ 0  0  0  0  1]
 [-4 -8  1 -8  0]]

Page 39 Example 2.11

In [3]:
from sympy import Symbol
from numpy import random
print 'V is the space of all polynomial functions over F.'
print 'S contains the functions as:'
x = Symbol("x")
#n = round(rand()*10)#
n=random.randint(0,19)
print 'n = ',n
for i in range (0,n):
    f = x**i#
    print 'f%d(x) = '%(i,),f
    

print 'Then, V is the subspace spanned by set S.'
V is the space of all polynomial functions over F.
S contains the functions as:
n =  5
f0(x) =  1
f1(x) =  x
f2(x) =  x**2
f3(x) =  x**3
f4(x) =  x**4
Then, V is the subspace spanned by set S.

Page 41 Example 2.12

In [4]:
from numpy import array
a1 = array([3 ,0, -3])
a2 = array([-1 ,1 ,2])
a3 = array([4 ,2, -2])
a4 = array([2 ,1, 1])
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
print 'a4 = ',a4
t = 2 * a1 + 2 * a2 - a3 + 0 * a4
print ' Since, 2 * a1 + 2 * a2 - a3 + 0 * a4 = ',t,'= 0'
print 'a1,a2,a3,a4 are linearly independent'
e1 = [1, 0, 0]#
e2 = [0 ,1 ,0]#
e3 = [0 ,0, 1]#
print  'Now, e1 = ',e1
print  'e2 = ',e2
print  'e3 = ',e3
print 'Also, e1,e2,e3 are linearly independent.'
a1 =  [ 3  0 -3]
a2 =  [-1  1  2]
a3 =  [ 4  2 -2]
a4 =  [2 1 1]
 Since, 2 * a1 + 2 * a2 - a3 + 0 * a4 =  [0 0 0] = 0
a1,a2,a3,a4 are linearly independent
Now, e1 =  [1, 0, 0]
e2 =  [0, 1, 0]
e3 =  [0, 0, 1]
Also, e1,e2,e3 are linearly independent.

Page 41 Example 2.13

In [5]:
from numpy import array,random,identity
print 'S is the subset of F**n consisting of n vectors.'
#n = round(rand() *10 + 1)#\
n=random.randint(0,19)
print 'n = ',n
I = identity(n)
for i in range(0,n):
    e = I[i,:]
    print 'e%d = '%(i+1)
    print e

print 'x1,x2,x3...xn are the scalars in F'
print 'Putting a = x1*e1 + x2*e2 + x3*e3 + .... + xn*en'
print 'So, a = (x1,x2,x3,...,xn)'
print 'Therefore, e1,e2..,en span F**n'
print 'a = 0 if x1 = x2 = x3 = .. = xn = 0'
print 'So,e1,e2,e3,..,en are linearly independent.'
print 'The set S = {e1,e2,..,en} is called standard basis of F**n'
S is the subset of F**n consisting of n vectors.
n =  1
e1 = 
[ 1.]
x1,x2,x3...xn are the scalars in F
Putting a = x1*e1 + x2*e2 + x3*e3 + .... + xn*en
So, a = (x1,x2,x3,...,xn)
Therefore, e1,e2..,en span F**n
a = 0 if x1 = x2 = x3 = .. = xn = 0
So,e1,e2,e3,..,en are linearly independent.
The set S = {e1,e2,..,en} is called standard basis of F**n

Page 54 Example 2.20

In [6]:
from numpy import array,transpose,linalg
P = array([[-1, 4, 5],[ 0, 2, -3],[ 0, 0, 8]])
print 'P = \n',P
print '\ninverse(P) = \n',linalg.inv(P)
a1 = P[:,0]
a2 = P[:,1]
a3 = P[:,2]
print 'The vectors forming basis of F**3 are a1'', a2'', a3'''
print "a1' = \n",transpose(a1)
print "\na2' = \n",transpose(a2)
print "\na3' = \n",transpose(a3)
print 'The coordinates x1'',x2'',x3'' of vector a = [x1,x2,x3] is given by inverse(P)*[x1# x2# x3]'
t = -10*a1 - 1./2*a2 - a3#
print 'And, -10*a1'' - 1/2*a2'' - a3'' = ',t
P = 
[[-1  4  5]
 [ 0  2 -3]
 [ 0  0  8]]

inverse(P) = 
[[-1.      2.      1.375 ]
 [ 0.      0.5     0.1875]
 [ 0.      0.      0.125 ]]
The vectors forming basis of F**3 are a1, a2, a3
a1' = 
[-1  0  0]

a2' = 
[4 2 0]

a3' = 
[ 5 -3  8]
The coordinates x1,x2,x3 of vector a = [x1,x2,x3] is given by inverse(P)*[x1# x2# x3]
And, -10*a1 - 1/2*a2 - a3 =  [ 3.  2. -8.]

Page 60 Example 2.21

In [7]:
from numpy import array,identity,rank,vstack
a1 = [1 ,2 ,2, 1]#
a2 = [0 ,2 ,0 ,1]#
a3 = [-2, 0, -4, 3]#
print 'Given row vectors are:'
print 'a1 = ',a1
print 'a2 = ',a2
print 'a3 = ',a3
print 'The matrix A from these vectors will be:'
#A = [a1],[a2], [a3]]
A=array([a1,a2,a3])
print 'A = \n',A

print 'Finding Row reduced echelon matrix of A that is given by R'
print 'And applying same operations on identity matrix Q such that R = QA'
Q = identity(3)
print 'Q = \n',Q
T = A#              #Temporary matrix to store A
print 'Applying row transformations on A and Q,we get'
print 'R1 = R1-R2'
A[0,:] = A[0,:] - A[1,:]
Q[0,:] = Q[0,:] - Q[1,:]
print 'A = \n',A
print 'Q = \n',Q
print 'R3 = R3 + 2*R1'
A[2,:] = A[2,:] + 2*A[0,:]
Q[2,:] = Q[2,:] + 2*Q[0,:]
print 'A = \n',A
print 'Q = \n',Q
print 'R3 = R3/3'
A[2,:] = 1./3*A[2,:]
Q[2,:] = 1./3*Q[2,:]
print 'A = \n',A
print 'Q = \n',Q
print 'R2 = R2/2'
A[1,:] = 1./2*A[1,:]
Q[1,:] = 10/2*Q[1,:]
print 'A = \n',A
print 'Q = \n',Q
print 'R2 = R2 - 1/2*R3'
A[1,:] = A[1,:] - 1./2*A[2,:]
Q[1,:] = Q[1,:] - 1./2*Q[2,:]
print 'A = \n',A
print 'Q = \n',Q
R = A#
A = T#
print 'Row reduced echelon matrix:'
print 'A = \n',A
print 'Q = \n',Q
#part a
print 'rank of R = ',rank(R)

print 'Since, Rank of R is 3, so a1, a2, a3 are independent'
#part b
print 'Now, basis for W can be given by row vectors of R i.e. p1,p2,p3'
print 'b is any vector in W. b = [b1 b2 b3 b4]'
print 'Span of vectors p1,p2,p3 consist of vector b with b3 = 2*b1'
print 'So,b = b1p1 + b2p2 + b4p3'
print 'And,[p1 p2 p3] = R = Q*A'
print 'So, b = [b1 b2 b3]* Q * A'
print 'hence, b = x1a1 + x2a2 + x3a3 where x1 = [b1 b2 b4] * Q(1) and so on' # #Equation 1
#part c
print 'Now, given 3 vectors a1'' a2'' a3'':'
c1 = [1, 0, 2, 0]#
c2 = [0 ,2 ,0, 1]#
c3 = [0 ,0 ,0 ,3]#
print 'a1'' = ',c1
print 'a2'' = ',c2
print 'a3'' = ',c3
print 'Since a1'' a2'' a3'' are all of the form (y1 y2 y3 y4) with y3 = 2*y1, hence they are in W.'
print 'So, they are independent.'
#part d
c = array([c1,c2,c3])
P = identity(3)
for i in range(0,3):
    b1 = c[i,0]
    b2 = c[i,1]
    b4 = c[i,3]
    x1 = array([b1, b2, b4]) * Q[:,0]
    x2 = array([b1, b2, b4])*Q[:,1]
    x3 = array([b1, b2, b4])*Q[:,2]
    

print 'Required matrix P such that X = PX'' is:'
P=vstack([x1,x2,x3])
print 'P = \n',P
#print x1
Given row vectors are:
a1 =  [1, 2, 2, 1]
a2 =  [0, 2, 0, 1]
a3 =  [-2, 0, -4, 3]
The matrix A from these vectors will be:
A = 
[[ 1  2  2  1]
 [ 0  2  0  1]
 [-2  0 -4  3]]
Finding Row reduced echelon matrix of A that is given by R
And applying same operations on identity matrix Q such that R = QA
Q = 
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
Applying row transformations on A and Q,we get
R1 = R1-R2
A = 
[[ 1  0  2  0]
 [ 0  2  0  1]
 [-2  0 -4  3]]
Q = 
[[ 1. -1.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
R3 = R3 + 2*R1
A = 
[[1 0 2 0]
 [0 2 0 1]
 [0 0 0 3]]
Q = 
[[ 1. -1.  0.]
 [ 0.  1.  0.]
 [ 2. -2.  1.]]
R3 = R3/3
A = 
[[1 0 2 0]
 [0 2 0 1]
 [0 0 0 1]]
Q = 
[[ 1.         -1.          0.        ]
 [ 0.          1.          0.        ]
 [ 0.66666667 -0.66666667  0.33333333]]
R2 = R2/2
A = 
[[1 0 2 0]
 [0 1 0 0]
 [0 0 0 1]]
Q = 
[[ 1.         -1.          0.        ]
 [ 0.          5.          0.        ]
 [ 0.66666667 -0.66666667  0.33333333]]
R2 = R2 - 1/2*R3
A = 
[[1 0 2 0]
 [0 1 0 0]
 [0 0 0 1]]
Q = 
[[ 1.         -1.          0.        ]
 [-0.33333333  5.33333333 -0.16666667]
 [ 0.66666667 -0.66666667  0.33333333]]
Row reduced echelon matrix:
A = 
[[1 0 2 0]
 [0 1 0 0]
 [0 0 0 1]]
Q = 
[[ 1.         -1.          0.        ]
 [-0.33333333  5.33333333 -0.16666667]
 [ 0.66666667 -0.66666667  0.33333333]]
rank of R =  2
Since, Rank of R is 3, so a1, a2, a3 are independent
Now, basis for W can be given by row vectors of R i.e. p1,p2,p3
b is any vector in W. b = [b1 b2 b3 b4]
Span of vectors p1,p2,p3 consist of vector b with b3 = 2*b1
So,b = b1p1 + b2p2 + b4p3
And,[p1 p2 p3] = R = Q*A
So, b = [b1 b2 b3]* Q * A
hence, b = x1a1 + x2a2 + x3a3 where x1 = [b1 b2 b4] * Q(1) and so on
Now, given 3 vectors a1 a2 a3:
a1 =  [1, 0, 2, 0]
a2 =  [0, 2, 0, 1]
a3 =  [0, 0, 0, 3]
Since a1 a2 a3 are all of the form (y1 y2 y3 y4) with y3 = 2*y1, hence they are in W.
So, they are independent.
Required matrix P such that X = PX is:
P = 
[[ 0. -0.  2.]
 [-0.  0. -2.]
 [ 0. -0.  1.]]

Page 63 Example 2.22

In [8]:
from numpy import array,identity
A = array([[1, 2, 0, 3, 0],[1, 2, -1, -1, 0],[0 ,0 ,1 ,4 ,0],[2, 4 ,1 ,10, 1],[0 ,0 ,0 ,0 ,1]])
print 'A = \n',A
#part a
T = A#                  #Temporary storing A in T
print 'Taking an identity matrix P:'
P = identity(5)
print 'P = \n',P
print 'Applying row transformations on P and A to get a row reduced echelon matrix R:'
print 'R2 = R2 - R1 and R4 = R4 - 2* R1'
A[1,:] = A[1,:] - A[0,:]
P[1,:] = P[1,:] - P[0,:]
A[3,:] = A[3,:] - 2 * A[0,:]
P[3,:] = P[3,:] - 2 * P[0,:]
print 'A = \n',A
print 'P = \n',P
print 'R2 = -R2 , R3 = R3 - R1 + R2 and R4  = R4 - R1 + R2'
A[1,:] = -A[1,:]
P[1,:] = -P[1,:]
A[2,:] = A[2,:] - A[1,:]
P[2,:] = P[2,:] - P[1,:]
A[3,:] = A[3,:] - A[1,:]
P[3:] = P[3,:] - P[1,:]
print 'A = \n',A
print 'P = \n',P
print 'Mutually interchanging R3, R4 and R5'
x = A[2,:]
A[2,:] = A[4,:]
y = A[3,:]
A[3,:] = x#
A[4,:] = y - A[2,:]
x = P[2,:]
P[2,:] = P[4,:]
y = P[3,:]
P[3,:] = x#
P[4,:] = y - P[2,:]
R = A#
A = T#
print 'Row reduced echelon matrix R = \n',R
print 'Invertible Matrix P = \n',P
print 'Invertible matrix P is not unique. There can be many that depends on operations used to reduce A'
print '-----------------------------------------'
#part b
print 'For the basis of row space W of A, we can take the non-zero rows of R'
print 'It can be given by p1, p2, p3'
p1 = R[0,:]
p2 = R[1,:]
p3 = R[2,:]
print 'p1 = ',p1
print 'p2 = ',p2
print 'p3 = ',p3
print '-----------------------------------------'
#part c
print 'The row space W consists of vectors of the form:'
print 'b = c1p1 + c2p2 + c3p3'
print 'i.e. b = (c1,2*c1,c2,3*c1+4*c2,c3) where, c1 c2 c3 are scalars.'
print 'So, if b2 = 2*b1 and b4 = 3*b1 + 4*b3  =>  (b1,b2,b3,b4,b5) = b1p1 + b3p2 + b5p3'
print 'then,(b1,b2,b3,b4,b5) is in W'
print '-----------------------------------------'
#part d
print 'The coordinate matrix of the vector (b1,2*b1,b2,3*b1+4*b2,b3) in the basis (p1,p2,p3) is column matrix of b1,b2,b3 such that:'
print '  b1'
print '  b2'
print '  b3'
print '-----------------------------------------'
#part e
print 'Now, to write each vector in W as a linear combination of rows of A:'
print 'Let b = (b1,b2,b3,b4,b5) and if b is in W, then'
print 'we know,b = (b1,2*b1,b3,3*b1 + 4*b3,b5)  =>  [b1,b3,b5,0,0]*R'
print '=> b = [b1,b3,b5,0,0] * P*A  =>  b = [b1+b3,-b3,0,0,b5] * A'
print 'if b = (-5,-10,1,-11,20)'
b1 = -5#
b2 = -10#
b3 = 1#
b4 = -11#
b5 = 20#
x = [b1 + b3,-b3,0,0,b5]#
print 'b = (',x,')','*[',A,']'
print '-----------------------------------------'
#part f
print 'The equations in system RX = 0 are given by R * [x1 x2 x3 x4 x5]'
print 'i.e., x1 + 2*x2 + 3*x4'
print 'x3 + 4*x4'
print 'x5'
print 'so, V consists of all columns of the form'
print '[','X='
print '  -2*x2 - 3*x4'
print '  x2'
print '  -4*x4'
print '  x4'
print '  0'
print 'where x2 and x4  are arbitrary',']'
print '-----------------------------------------'
#part g
print 'Let x2 = 1,x4 = 0 then the given column forms a basis of V'

x2 = 1#
x4 = 0#
print [[-2*x2-3*x4],[ x2],[ -4*x4],[ x4],[ 0]]
print 'Similarly,if x2 = 0,x4 = 1 then the given column forms a basis of V'
x2 = 0#
x4 = 1#
print [[-2*x2-3*x4],[ x2],[ -4*x4],[ x4],[ 0]]
print '-----------------------------------------'
#part h
print 'The equation AX = Y has solutions X if and only if'
print '-y1 + y2 + y3 = 0'
print '-3*y1 + y2 + y4 -y5 = 0'
print 'where, Y = (y1 y2 y3 y4 y5)'
A = 
[[ 1  2  0  3  0]
 [ 1  2 -1 -1  0]
 [ 0  0  1  4  0]
 [ 2  4  1 10  1]
 [ 0  0  0  0  1]]
Taking an identity matrix P:
P = 
[[ 1.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.]
 [ 0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  1.]]
Applying row transformations on P and A to get a row reduced echelon matrix R:
R2 = R2 - R1 and R4 = R4 - 2* R1
A = 
[[ 1  2  0  3  0]
 [ 0  0 -1 -4  0]
 [ 0  0  1  4  0]
 [ 0  0  1  4  1]
 [ 0  0  0  0  1]]
P = 
[[ 1.  0.  0.  0.  0.]
 [-1.  1.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.]
 [-2.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  1.]]
R2 = -R2 , R3 = R3 - R1 + R2 and R4  = R4 - R1 + R2
A = 
[[1 2 0 3 0]
 [0 0 1 4 0]
 [0 0 0 0 0]
 [0 0 0 0 1]
 [0 0 0 0 1]]
P = 
[[ 1.  0.  0.  0.  0.]
 [ 1. -1. -0. -0. -0.]
 [-1.  1.  1.  0.  0.]
 [-3.  1.  0.  1.  0.]
 [-3.  1.  0.  1.  0.]]
Mutually interchanging R3, R4 and R5
Row reduced echelon matrix R = 
[[1 2 0 3 0]
 [0 0 1 4 0]
 [0 0 0 0 1]
 [0 0 0 0 1]
 [0 0 0 0 0]]
Invertible Matrix P = 
[[ 1.  0.  0.  0.  0.]
 [ 1. -1. -0. -0. -0.]
 [-3.  1.  0.  1.  0.]
 [-3.  1.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.]]
Invertible matrix P is not unique. There can be many that depends on operations used to reduce A
-----------------------------------------
For the basis of row space W of A, we can take the non-zero rows of R
It can be given by p1, p2, p3
p1 =  [1 2 0 3 0]
p2 =  [0 0 1 4 0]
p3 =  [0 0 0 0 1]
-----------------------------------------
The row space W consists of vectors of the form:
b = c1p1 + c2p2 + c3p3
i.e. b = (c1,2*c1,c2,3*c1+4*c2,c3) where, c1 c2 c3 are scalars.
So, if b2 = 2*b1 and b4 = 3*b1 + 4*b3  =>  (b1,b2,b3,b4,b5) = b1p1 + b3p2 + b5p3
then,(b1,b2,b3,b4,b5) is in W
-----------------------------------------
The coordinate matrix of the vector (b1,2*b1,b2,3*b1+4*b2,b3) in the basis (p1,p2,p3) is column matrix of b1,b2,b3 such that:
  b1
  b2
  b3
-----------------------------------------
Now, to write each vector in W as a linear combination of rows of A:
Let b = (b1,b2,b3,b4,b5) and if b is in W, then
we know,b = (b1,2*b1,b3,3*b1 + 4*b3,b5)  =>  [b1,b3,b5,0,0]*R
=> b = [b1,b3,b5,0,0] * P*A  =>  b = [b1+b3,-b3,0,0,b5] * A
if b = (-5,-10,1,-11,20)
b = ( [-4, -1, 0, 0, 20] ) *[ [[1 2 0 3 0]
 [0 0 1 4 0]
 [0 0 0 0 1]
 [0 0 0 0 1]
 [0 0 0 0 0]] ]
-----------------------------------------
The equations in system RX = 0 are given by R * [x1 x2 x3 x4 x5]
i.e., x1 + 2*x2 + 3*x4
x3 + 4*x4
x5
so, V consists of all columns of the form
[ X=
  -2*x2 - 3*x4
  x2
  -4*x4
  x4
  0
where x2 and x4  are arbitrary ]
-----------------------------------------
Let x2 = 1,x4 = 0 then the given column forms a basis of V
[[-2], [1], [0], [0], [0]]
Similarly,if x2 = 0,x4 = 1 then the given column forms a basis of V
[[-3], [0], [-4], [1], [0]]
-----------------------------------------
The equation AX = Y has solutions X if and only if
-y1 + y2 + y3 = 0
-3*y1 + y2 + y4 -y5 = 0
where, Y = (y1 y2 y3 y4 y5)