from sympy.abc import u,v,w
from numpy import array
a =array([[1 ,1 ,1],[2, 2, 5],[4, 6, 8]])
x=[[u],[v],[w]]
x=array(x)
print 'x=\n',x
print 'R2=R2-R1,R3=R3-4*R1'
a[1,:]=a[1,:]-2*a[0,:]
a[2,:]=a[2,:]-4*a[0,:]
print a
print 'R2<->R3'
def swap_rows(arr, frm, to):
arr[[frm, to],:] = arr[[to, frm],:]
swap_rows(a,1,2)
print a
print 'The system is now triangular and the equations can be solved by Back substitution'
from sympy.abc import u,v,w
from numpy import array
a =array([[1, 1, 1],[2, 2, 5],[4, 4, 8]])
print 'a=\n',a
x=[[u],[v],[w]]
x=array(x)
print 'x=\n',x
print 'R2=R2-2*R1,R3=R3-4*R1'
a[1,:]=a[1,:]-2*a[0,:]
a[2,:]=a[2,:]-4*a[0,:]
print a
print 'No exchange of equations can avoid zero in the second pivot positon ,therefore the equations are unsolvable'
from numpy import array
A=array([[2, 3],[4, 0]])
print 'A = \n',A
B=array([[1, 2, 0],[5, -1, 0]])
print 'B = \n',B
print 'AB=\n',A.dot(B)
from numpy import array
A=array([[2, 3],[7, 8]])
print 'A=\n',A
P=array([[0, 1],[1, 0]])
print 'P(Row exchange matrix)=\n',P
print 'PA=\n',P.dot(A)
from numpy import array,identity
A=array([[1, 2],[3, 4]])
print 'A=\n',A
I=identity(2)
print 'I=\n',I
print 'IA=\n',I.dot(A)
from numpy import array,identity
E=identity(3)
E[1,:]=E[1,:]-2*E[0,:]
print 'E=\n',E
F=identity(3)
F[2,:]=F[2,:]+F[0,:]
print 'F=\n',F
print 'EF=\n',E.dot(F)
print 'FE=\n',F.dot(E)
print 'Here,EF=FE,so this shows that these two matrices commute'
from numpy import array,identity
E=identity(3)
E[1,:]=E[1,:]-2*E[0,:]
print 'E=\n',E
F=identity(3)
F[2,:]=F[2,:]+F[0,:]
print 'F=\n',F
G=identity(3)
G[2,:]=G[2,:]+G[1,:]
print 'G=\n',G
print 'GE=',G.dot(E)
print 'EG=\n',E.dot(G)
print 'Here EG is not equal to GE,Therefore these two matrices do not commute and shows that most matrices do not commute.'
print 'GFE=\n',G.dot(F.dot(E))
print 'EFG=\n',E.dot(F.dot(G))
print 'The product GFE is the true order of elimation.It is the matrix that takes the original A to the upper triangular U.'
from numpy import mat,dot
from scipy.linalg import lu
A=mat([[1, 2],[3, 8]])
print 'A=',A
L=lu(A)[1]
U=lu(A)[2]
print 'L=',L
print 'U=',U
L=mat(L);U=mat(U)
print 'LU=\n',dot(L,U)
from numpy import mat
A=[[0, 2],[3, 4]]
print 'A=',mat(A)
print 'Here this cannot be factored into A=LU,(Needs a row exchange)'
from numpy import mat
from scipy.linalg import lu
print 'Given Matrix:'
A=[[1, 1 ,1],[1, 2, 2],[1, 2, 3]]
print 'A=',A
L=lu(A)[1]
U=lu(A)[2]
print '\nL=',L
print '\nU=',U
print '\nLU=\n',mat(L)*mat(U)
print 'Here LU=A,from A to U there are subtraction of rows.Frow U to A there are additions of rows'
from numpy.random import random
from numpy import mat,eye
a=random()
b=random()
c=random()
L=mat([[1, 0, 0],[a, 1, 0],[b, c, 1]])
print 'L=\n',L
U=eye(3)
print 'U=\n',U
E=mat([[1, 0, 0],[-a, 1, 0],[0, 0, 1]])
print 'E=\n',E
F=mat([[1, 0, 0],[0, 1, 0],[-b, 0, 1]])
print 'F=\n',F
G=mat([[1, 0, 0],[0, 1, 0],[0, -c, 1]])
print 'G=\n',G
print 'A=inv(E)*inv(F)*inv(G)*U'
A=(E**-1)*(F**-1)*(G**-1)*U#
print 'A=\n',A
print 'When U is identity matrix then L is same as A'
from numpy import mat
from scipy.linalg import lu
A=mat([[1, -1, 0, 0],[-1, 2 ,-1, 0],[0, -1, 2 ,-1],[0, 0 ,-1 ,2]])
print 'A=\n',A
L=lu(A)[1]
U=lu(A)[2]
print 'U=\n',U
print 'L=\n',L
print 'This shows how a matrix A with 3 diagnols has factors L and U with two diagnols.'
from numpy import mat,hstack,zeros,arange
from scipy.linalg import lu
a=mat([[1, -1, 0 ,0],[-1, 2, -1, 0],[0, -1, 2, -1],[0, 0, -1, 2]])
print 'a=\n',a
b=mat([[1],[1],[1],[1]])
print 'b=\n',b
print 'Given Equation ,ax=b'
L=lu(a)[1]
U=lu(a)[2]
print 'U=\n',U
print 'L=\n',L
print 'Augmented Matrix of L and b='
A=hstack([L,U])
print A
c=zeros([4,1])#
n=4
#Algorithm Finding the value of c
c[0]=A[0,n]/A[0,0]
for i in range(1,n):
sumk=0#
for k in range(0,n-1):
sumk=sumk+A[i,k]*c[k]
c[i]=(A[i,n+1]-sumk)/A[i,i]
print 'c=\n',c
x=zeros([4,1])
print 'Augmented matrix of U and c='
B=hstack([U,c])
print B
#Algorithm for finding value of x
x[n-1]=B[n-1,n]/B[n-1,n-1]
for i in arange(n-1-1,-1+1,-1):
sumk=0#
for k in range(i+1-1,n):
sumk=sumk+B[i,k]*x[k]
x[i]=(B[i,n+1-1]-sumk)/B[i,i]
print 'x=\n',x
from numpy import mat
from scipy.linalg import lu
A=mat([[1, 1, 1],[1, 1, 3],[2, 5, 8]])
print 'A=\n',A
P=lu(A)[0]
L=lu(A)[1]
U=lu(A)[2]
print 'L=\n',L
print 'U=\n',U
print 'P=\n',P
print 'PA=\n',(P*A)
print 'LU=\n',(L*U)
from numpy import mat,shape,nditer,hstack,eye,nditer
print 'Given matrix:'
A=mat([[2, 1, 1],[4, -6, 0],[-2, 7, 2]])
print A
m=int(shape(A)[0])
n=int(shape(A)[1])
print 'Augmented matrix :'
a=hstack([A,eye(n)])
print a
print 'R2=R2-2*R1,R3=R3-(-2)*R1'
def fun1(a,x,y,n):
import numpy as np
z=[]
for xx,yy in nditer([a[x-1],a[y-1]]):
z.append(xx-n*yy)
a[x-1]=z
return a
a=fun1(a,2,1,-2)
a=fun1(a,3,1,-1)
print a
print 'R3=R3-(-1)*R2'
a=fun1(a,3,2,-1) # a(3,:)-(-1)*a(2,:)#
print 'a=\n',a
print '[U inv(L)] :\n',a
print 'R2=R2-(-2)*R3,R1=R1-R3'
a=fun1(a,2,3,-2)
a=fun1(a,1,3,-1)
print a
print 'R1=R1-(-1/8)*R2)'
a=fun1(a,1,2,-1./8)
print a
a[0]=a[0]/a[0,0]
a[1]=a[1]/a[1,1]
print '[I inv(A)]:'
a[2]=a[2]/a[2,2]
print a
print 'inv(A):'
print a[:,range(3,6)]
from numpy import mat,transpose
R=mat([1, 2])
print 'R=',R
Rt=transpose(R)
print 'Transpose of the given matrix is :',Rt
print 'The product of R & transpose(R)is :',(R*Rt)
print 'The product of transpose(R)& R is :',(Rt*R)
print 'Rt*R and R*Rt are not likely to be equal even if m==n.'