Chapter2 Vector Spaces

Ex:2.1.1 Pg: 70

In [1]:
print 'Consider all vectors in R**2 whose components are positive or zero'
print 'The subset is first Quadrant of x-y plane,the co-ordinates satisfy x>=0 and y>=0.It is not a subspace.'
v=[1,1]
print 'If the Vector=',v
print 'Taking a scalar,c=-1'
c=-1# #scalar
print 'c*v=',[c*vv for vv in v]
print 'It lies in third Quadrant instead of first,Hence violating the rule(ii).'
Consider all vectors in R**2 whose components are positive or zero
The subset is first Quadrant of x-y plane,the co-ordinates satisfy x>=0 and y>=0.It is not a subspace.
If the Vector= [1, 1]
Taking a scalar,c=-1
c*v= [-1, -1]
It lies in third Quadrant instead of first,Hence violating the rule(ii).

Ex:2.3.2 Pg: 92

In [4]:
from numpy import mat
A=mat([[1, 3 ,3 ,2],[2, 6, 9, 5],[-1, -3, 3, 0]])
print 'Given matrix:'
print A
B=A
print 'C2->C2-3*C1'
A[:,1]=A[:,1]-3*A[:,0]
print A
print 'Here,C2=3*C1,Therefore the columns are linearly dependent.'
print 'R3->R3-2*R2+5*R1'
B[2,:]=B[2,:]-2*B[1,:]+5*B[0,:]
print B
print 'Here R3=R3-2*R2+5*R1,therefore the rows are linearly dependent.'
Given matrix:
[[ 1  3  3  2]
 [ 2  6  9  5]
 [-1 -3  3  0]]
C2->C2-3*C1
[[ 1  0  3  2]
 [ 2  0  9  5]
 [-1  0  3  0]]
Here,C2=3*C1,Therefore the columns are linearly dependent.
R3->R3-2*R2+5*R1
[[1 0 3 2]
 [2 0 9 5]
 [0 0 0 0]]
Here R3=R3-2*R2+5*R1,therefore the rows are linearly dependent.

Ex:2.3.3 Pg:

In [5]:
from numpy import mat
A=mat([[3, 4 ,2],[0, 1 ,5],[0, 0, 2]])
print 'A=\n',A
print 'The columns of the triangular matrix are linearly independent,it has no zeros on the diagonal'
A=
[[3 4 2]
 [0 1 5]
 [0 0 2]]
The columns of the triangular matrix are linearly independent,it has no zeros on the diagonal

Ex:2.3.4 Pg: 93

In [2]:
from numpy import eye
print 'The columns of the nxn identity matrix are independent.'
n=4 # size for identity matrix
I=eye(n)
print 'I=\n',I
The columns of the nxn identity matrix are independent.
I=
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

Ex:2.3.5 Pg: 93

In [3]:
from scipy.linalg import lu
from numpy import mat
print 'Three columns in R2 cannot be independent.'
A=mat([[1, 2, 1],[1, 2, 3]])
print 'Given matrix:\n',A
L=lu(A)[1]
U=lu(A)[2]
print 'U=\n',U
print 'If c3 is 1 ,then back-substitution Uc=0 gives c2=-1,c1=1,With these three weights,the first column minus the second plus the third equals zero ,therefore linearly dependent.'
Three columns in R2 cannot be independent.
Given matrix:
[[1 2 1]
 [1 2 3]]
U=
[[ 1.  2.  1.]
 [ 0.  0.  2.]]
If c3 is 1 ,then back-substitution Uc=0 gives c2=-1,c1=1,With these three weights,the first column minus the second plus the third equals zero ,therefore linearly dependent.

Ex:2.3.9 Pg: 96

In [11]:
from numpy import mat
print 'These four columns span the column space U,but they are not independent.'
U=mat([[1, 3 ,3, 2],[0, 0 ,3 ,1],[0, 0, 0, 0]])
print 'U=\n',U
print 'The columns that contains pivots (here 1st & 3rd) are a basis for the column space. These columns are independent, and it is easy to see that they span the space.In fact,the column space of U is just the x-y plane withinn R3. C(U) is not the same as the column space C(A) before elimination-but the number of independent columns did not change.'
These four columns span the column space U,but they are not independent.
U=
[[1 3 3 2]
 [0 0 3 1]
 [0 0 0 0]]
The columns that contains pivots (here 1st & 3rd) are a basis for the column space. These columns are independent, and it is easy to see that they span the space.In fact,the column space of U is just the x-y plane withinn R3. C(U) is not the same as the column space C(A) before elimination-but the number of independent columns did not change.

Ex:2.4.1 Pg: 107

In [4]:
from numpy import mat,shape
from sympy import Matrix
from scipy import linalg, matrix, compress,transpose
A=mat([[1 ,2],[3, 6]])
print 'A=\n',A
m=shape(A)[0]
n=shape(A)[1]
print 'm=',m
print 'n=',n
v=Matrix(A).rref()[0]
pivot=Matrix(A).rref()[1]
r=len(pivot)
print 'rank=',r
cs=A[:,r-1]
print 'Column space=',cs


def kernel(A, eps=1e-15):
    u, s, vh = linalg.svd(A)
    null_mask = (s <= eps)
    null_space = compress(null_mask, vh, axis=0)
    return transpose(null_space)
A=mat([[1 ,2],[3, 6]])

ns=kernel(A)
print 'Null space=\n',ns
v=mat(v)
rs=transpose(v[range(0,r),:])
print 'Row space=\n',rs
lns=kernel(transpose(A))
print 'Left null sapce=\n',lns
A=
[[1 2]
 [3 6]]
m= 2
n= 2
rank= 1
Column space= [[1]
 [3]]
Null space=
[[-0.89442719]
 [ 0.4472136 ]]
Row space=
[[1]
 [2]]
Left null sapce=
[[-0.9486833 ]
 [ 0.31622777]]

Ex:2.4.2 Pg: 108

In [5]:
from numpy import mat,shape,rank,transpose
A=mat([[4, 0, 0],[0, 5, 0]])
print 'A=\n',A
m=shape(A)[0]
n=shape(A)[1]
print 'm=',m
print 'n=',n
r=rank(A)
print 'rank=',r
print 'since m=r=2 ,there exists a right inverse .'
C=transpose(A)*(A*transpose(A))**-1
print 'Best right inverse=\n',C
A=
[[4 0 0]
 [0 5 0]]
m= 2
n= 3
rank= 2
since m=r=2 ,there exists a right inverse .
Best right inverse=
[[ 0.25  0.  ]
 [ 0.    0.2 ]
 [ 0.    0.  ]]

Ex:2.5.1 Pg: 121

In [6]:
from numpy import mat,transpose,diag,zeros,random,vstack,hstack,linalg
print 'Applying current law A''y=f at nodes 1,2,3:'
A=mat([[-1, 1 ,0],[0, -1, 1],[ -1, 0 ,1],[0, 0 ,-1],[-1, 0, 0]])
print "A' = \n",transpose(A)
C=diag(random.rand(5))# #Taking some values for the resistances.\
b=zeros([5,1])
b[2,0]=random.rand(1)[0]##Taking some value of the battery.
f=zeros([3,1])
f[1,0]=random.rand(1)[0]##Taking some value of the current source.
B=vstack([b,f])#[b]#f]
print B
print 'The other equation is inv(C)y+Ax=b.The block form of the two equations is:'
#C=[C**-1 A],[np.transpose(A),np.zeros([3,3])]
C1=hstack([linalg.inv(C),A])
C2=hstack([transpose(A),zeros([3,3])])
C=vstack([C1,C2])
print C
X=mat([['y1'],['y2'],['y3'],['y4'],['y5'],['x1'],['x2'],['x3']])
print "X=\n",X
X=linalg.solve(C,B)
print 'X=',X
Applying current law Ay=f at nodes 1,2,3:
A' = 
[[-1  0 -1  0 -1]
 [ 1 -1  0  0  0]
 [ 0  1  1 -1  0]]
[[ 0.        ]
 [ 0.        ]
 [ 0.59010697]
 [ 0.        ]
 [ 0.        ]
 [ 0.        ]
 [ 0.55757785]
 [ 0.        ]]
The other equation is inv(C)y+Ax=b.The block form of the two equations is:
[[  1.37051236   0.           0.           0.           0.          -1.
    1.           0.        ]
 [  0.           1.54412147   0.           0.           0.           0.
   -1.           1.        ]
 [  0.           0.           9.1949881    0.           0.          -1.
    0.           1.        ]
 [  0.           0.           0.          13.91590014   0.           0.
    0.          -1.        ]
 [  0.           0.           0.           0.           2.46587381  -1.
    0.           0.        ]
 [ -1.           0.          -1.           0.          -1.           0.
    0.           0.        ]
 [  1.          -1.           0.           0.           0.           0.
    0.           0.        ]
 [  0.           1.           1.          -1.           0.           0.
    0.           0.        ]]
X=
[['y1']
 ['y2']
 ['y3']
 ['y4']
 ['y5']
 ['x1']
 ['x2']
 ['x3']]
X= [[ 0.3717052 ]
 [-0.18587265]
 [ 0.08836592]
 [-0.09750673]
 [-0.46007112]
 [-1.13447733]
 [-1.6439039 ]
 [-1.35689395]]