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