# Chapter 1 - Matrix notation & matrix multiplication¶

## Ex:1.3.1 Pg:20¶

In [1]:
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'

x=
[[u]
[v]
[w]]
R2=R2-R1,R3=R3-4*R1
[[1 1 1]
[0 0 3]
[0 2 4]]
R2<->R3
[[1 1 1]
[0 2 4]
[0 0 3]]
The system is now triangular and the equations can be solved by Back substitution


## Ex:1.3.2 Pg:21¶

In [2]:
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'

a=
[[1 1 1]
[2 2 5]
[4 4 8]]
x=
[[u]
[v]
[w]]
R2=R2-2*R1,R3=R3-4*R1
[[1 1 1]
[0 0 3]
[0 0 4]]
No exchange of equations can avoid zero in the second pivot positon ,therefore the equations are unsolvable


# Ex:1.4.1 Pg:21¶

In [3]:
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)

A =
[[2 3]
[4 0]]
B =
[[ 1  2  0]
[ 5 -1  0]]
AB=
[[17  1  0]
[ 4  8  0]]


## Ex:1.4.2 Pg:22¶

In [37]:
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)

A=
[[2 3]
[7 8]]
P(Row exchange matrix)=
[[0 1]
[1 0]]
PA=
[[7 8]
[2 3]]


## Ex:1.4.3 Pg:24¶

In [4]:
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)

A=
[[1 2]
[3 4]]
I=
[[ 1.  0.]
[ 0.  1.]]
IA=
[[ 1.  2.]
[ 3.  4.]]


## Ex:1.4.4 Pg:25¶

In [39]:
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'

E=
[[ 1.  0.  0.]
[-2.  1.  0.]
[ 0.  0.  1.]]
F=
[[ 1.  0.  0.]
[ 0.  1.  0.]
[ 1.  0.  1.]]
EF=
[[ 1.  0.  0.]
[-2.  1.  0.]
[ 1.  0.  1.]]
FE=
[[ 1.  0.  0.]
[-2.  1.  0.]
[ 1.  0.  1.]]
Here,EF=FE,so this shows that these two matrices commute


## Ex:1.4.5 Pg:25¶

In [5]:
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.'

E=
[[ 1.  0.  0.]
[-2.  1.  0.]
[ 0.  0.  1.]]
F=
[[ 1.  0.  0.]
[ 0.  1.  0.]
[ 1.  0.  1.]]
G=
[[ 1.  0.  0.]
[ 0.  1.  0.]
[ 0.  1.  1.]]
GE= [[ 1.  0.  0.]
[-2.  1.  0.]
[-2.  1.  1.]]
EG=
[[ 1.  0.  0.]
[-2.  1.  0.]
[ 0.  1.  1.]]
Here EG is not equal to GE,Therefore these two matrices do not commute and shows that most matrices do not commute.
GFE=
[[ 1.  0.  0.]
[-2.  1.  0.]
[-1.  1.  1.]]
EFG=
[[ 1.  0.  0.]
[-2.  1.  0.]
[ 1.  1.  1.]]
The product GFE is the true order of elimation.It is the matrix that takes the original A to the upper triangular U.


## Ex:1.5.1 Pg: 34¶

In [6]:
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)

A= [[1 2]
[3 8]]
L= [[ 1.          0.        ]
[ 0.33333333  1.        ]]
U= [[ 3.          8.        ]
[ 0.         -0.66666667]]
LU=
[[ 3.  8.]
[ 1.  2.]]


## Ex:1.5.2 Pg: 34¶

In [42]:
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)'

A= [[0 2]
[3 4]]
Here this cannot be factored into A=LU,(Needs a row exchange)


## Ex:1.5.3 Pg: 34¶

In [8]:
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'

Given Matrix:
A= [[1, 1, 1], [1, 2, 2], [1, 2, 3]]

L= [[ 1.  0.  0.]
[ 1.  1.  0.]
[ 1.  1.  1.]]

U= [[ 1.  1.  1.]
[ 0.  1.  1.]
[ 0.  0.  1.]]

LU=
[[ 1.  1.  1.]
[ 1.  2.  2.]
[ 1.  2.  3.]]
Here LU=A,from A to U there are subtraction of rows.Frow U to A there are additions of rows


## Ex:1.5.4 Pg: 34¶

In [9]:
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'

L=
[[ 1.          0.          0.        ]
[ 0.74342501  1.          0.        ]
[ 0.97556591  0.5785538   1.        ]]
U=
[[ 1.  0.  0.]
[ 0.  1.  0.]
[ 0.  0.  1.]]
E=
[[ 1.          0.          0.        ]
[-0.74342501  1.          0.        ]
[ 0.          0.          1.        ]]
F=
[[ 1.          0.          0.        ]
[ 0.          1.          0.        ]
[-0.97556591  0.          1.        ]]
G=
[[ 1.         0.         0.       ]
[ 0.         1.         0.       ]
[ 0.        -0.5785538  1.       ]]
A=inv(E)*inv(F)*inv(G)*U
A=
[[ 1.          0.          0.        ]
[ 0.74342501  1.          0.        ]
[ 0.97556591  0.5785538   1.        ]]
When U is identity matrix then L is same as A


## Ex:1.5.5 Pg: 39¶

In [10]:
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.'

A=
[[ 1 -1  0  0]
[-1  2 -1  0]
[ 0 -1  2 -1]
[ 0  0 -1  2]]
U=
[[ 1. -1.  0.  0.]
[ 0.  1. -1.  0.]
[ 0.  0.  1. -1.]
[ 0.  0.  0.  1.]]
L=
[[ 1.  0.  0.  0.]
[-1.  1.  0.  0.]
[ 0. -1.  1.  0.]
[ 0.  0. -1.  1.]]
This shows how a matrix A with 3 diagnols has factors L and U with two diagnols.


## Ex:1.5.6 Pg: 36¶

In [11]:
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

a=
[[ 1 -1  0  0]
[-1  2 -1  0]
[ 0 -1  2 -1]
[ 0  0 -1  2]]
b=
[[1]
[1]
[1]
[1]]
Given Equation ,ax=b
U=
[[ 1. -1.  0.  0.]
[ 0.  1. -1.  0.]
[ 0.  0.  1. -1.]
[ 0.  0.  0.  1.]]
L=
[[ 1.  0.  0.  0.]
[-1.  1.  0.  0.]
[ 0. -1.  1.  0.]
[ 0.  0. -1.  1.]]
Augmented Matrix of L and b=
[[ 1.  0.  0.  0.  1. -1.  0.  0.]
[-1.  1.  0.  0.  0.  1. -1.  0.]
[ 0. -1.  1.  0.  0.  0.  1. -1.]
[ 0.  0. -1.  1.  0.  0.  0.  1.]]
c=
[[ 1.]
[ 2.]
[ 2.]
[ 2.]]
Augmented matrix of U and c=
[[ 1. -1.  0.  0.  1.]
[ 0.  1. -1.  0.  2.]
[ 0.  0.  1. -1.  2.]
[ 0.  0.  0.  1.  2.]]
x=
[[ 0.]
[ 6.]
[ 4.]
[ 2.]]


## Ex:1.5.7 Pg: 39¶

In [12]:
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)

A=
[[1 1 1]
[1 1 3]
[2 5 8]]
L=
[[ 1.   0.   0. ]
[ 0.5  1.   0. ]
[ 0.5  1.   1. ]]
U=
[[ 2.   5.   8. ]
[ 0.  -1.5 -1. ]
[ 0.   0.  -2. ]]
P=
[[ 0.  0.  1.]
[ 0.  1.  0.]
[ 1.  0.  0.]]
PA=
[[ 2.  5.  8.]
[ 1.  1.  3.]
[ 1.  1.  1.]]
LU=
[[ 2.   0.   0. ]
[ 0.  -1.5 -0. ]
[ 0.   0.  -2. ]]


## Ex:1.6.1 Pg: 47¶

In [13]:
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)]

Given matrix:
[[ 2  1  1]
[ 4 -6  0]
[-2  7  2]]
Augmented matrix :
[[ 2.  1.  1.  1.  0.  0.]
[ 4. -6.  0.  0.  1.  0.]
[-2.  7.  2.  0.  0.  1.]]
R2=R2-2*R1,R3=R3-(-2)*R1
[[ 2.  1.  1.  1.  0.  0.]
[ 8. -4.  2.  2.  1.  0.]
[ 0.  8.  3.  1.  0.  1.]]
R3=R3-(-1)*R2
a=
[[ 2.  1.  1.  1.  0.  0.]
[ 8. -4.  2.  2.  1.  0.]
[ 8.  4.  5.  3.  1.  1.]]
[U inv(L)] :
[[ 2.  1.  1.  1.  0.  0.]
[ 8. -4.  2.  2.  1.  0.]
[ 8.  4.  5.  3.  1.  1.]]
R2=R2-(-2)*R3,R1=R1-R3
[[ 10.   5.   6.   4.   1.   1.]
[ 24.   4.  12.   8.   3.   2.]
[  8.   4.   5.   3.   1.   1.]]
R1=R1-(-1/8)*R2)
[[ 13.      5.5     7.5     5.      1.375   1.25 ]
[ 24.      4.     12.      8.      3.      2.   ]
[  8.      4.      5.      3.      1.      1.   ]]
[I inv(A)]:
[[ 1.          0.42307692  0.57692308  0.38461538  0.10576923  0.09615385]
[ 6.          1.          3.          2.          0.75        0.5       ]
[ 1.6         0.8         1.          0.6         0.2         0.2       ]]
inv(A):
[[ 0.38461538  0.10576923  0.09615385]
[ 2.          0.75        0.5       ]
[ 0.6         0.2         0.2       ]]


## Ex:1.6.2 Pg:51¶

In [14]:
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.'

R= [[1 2]]
Transpose of the given matrix is : [[1]
[2]]
The product of R & transpose(R)is : [[5]]
The product of transpose(R)& R is : [[1 2]
[2 4]]
Rt*R and R*Rt are not likely to be equal even if m==n.