# Chapter 1 - Linear Equations¶

## Page 8 Example 1.5¶

In [1]:
from numpy import array
a = array([[2, -1, 3, 2],[1, 4, 0 ,-1],[2, 6, -1, 5]])
print 'a=\n',a
print 'Applying row transformations:'
print 'R1 = R1-2*R2'
a[0,:] = a[0,:] - 2*a[1,:]#
print 'a = \n',a
print 'R3 = R3-2*R2'
a[2,:] = a[2,:] - 2*a[1,:]
print 'a = \n',a
print 'R3 = R3/-2'
a[2,:] = -1.0/2*a[2,:]#
print 'a = \n',a
print 'R2 = R2-4*R3'
a[1,:] = a[1,:] - 4*a[2,:]
print 'a = \n',a
print 'R1 = R1+9*R3'
a[0,:] = a[0,:] + 9*a[2,:]#
print 'a = \n',a
print 'R1 = R1*2/15'
a[0,:] = a[0,:] * 2/15
print 'a = \n',a
print 'R2 = R2+2*R1'
a[1,:] = a[1,:] + 2*a[0,:]
print 'a = \n',a
print 'R3 = R3-R1/2'
a[2,:] = a[2,:] - 1.0/2*a[0,:]
print 'a = \n',a
print 'We get the system of equations as:'
print '2*x1 - x2 + 3*x3 + 2*x4 = 0'
print 'x1 + 4*x2 - x4 = 0'
print '2*x1 + 6* x2 - x3 + 5*x4 = 0'
print 'and'
print 'x2 - 5/3*x4 = 0','x1 + 17/3*x4 = 0','x3 - 11/3*x4 = 0'
print 'now by assigning any rational value c to x4 in system second, the solution is evaluated as:'
print '(-17/3*c,5/3,11/3*c,c)'

a=
[[ 2 -1  3  2]
[ 1  4  0 -1]
[ 2  6 -1  5]]
Applying row transformations:
R1 = R1-2*R2
a =
[[ 0 -9  3  4]
[ 1  4  0 -1]
[ 2  6 -1  5]]
R3 = R3-2*R2
a =
[[ 0 -9  3  4]
[ 1  4  0 -1]
[ 0 -2 -1  7]]
R3 = R3/-2
a =
[[ 0 -9  3  4]
[ 1  4  0 -1]
[ 0  1  0 -3]]
R2 = R2-4*R3
a =
[[ 0 -9  3  4]
[ 1  0  0 11]
[ 0  1  0 -3]]
R1 = R1+9*R3
a =
[[  0   0   3 -23]
[  1   0   0  11]
[  0   1   0  -3]]
R1 = R1*2/15
a =
[[ 0  0  0 -4]
[ 1  0  0 11]
[ 0  1  0 -3]]
R2 = R2+2*R1
a =
[[ 0  0  0 -4]
[ 1  0  0  3]
[ 0  1  0 -3]]
R3 = R3-R1/2
a =
[[ 0  0  0 -4]
[ 1  0  0  3]
[ 0  1  0 -1]]
We get the system of equations as:
2*x1 - x2 + 3*x3 + 2*x4 = 0
x1 + 4*x2 - x4 = 0
2*x1 + 6* x2 - x3 + 5*x4 = 0
and
x2 - 5/3*x4 = 0 x1 + 17/3*x4 = 0 x3 - 11/3*x4 = 0
now by assigning any rational value c to x4 in system second, the solution is evaluated as:
(-17/3*c,5/3,11/3*c,c)


## Page 9 Example 1.6¶

In [2]:
from numpy import array
a=array([[-1, 1J],[-1J, 3],[1 ,2]])
print 'a = \n',a
print 'Applying row transformations:'
print 'R1 = R1+R3 and R2 = R2 + i *R3'
a[0,:] = a[0,:] +a[2,:]
a[1,:] = a[1,:] + 1J * a[2,:]
print 'a = \n',a
print 'R1 = R1 * (1/2+i)'
a[0,:] = 1.0/(2 + 1J) * a[0,:]
print 'a = \n',a
print 'R2 = R2-R1*(3+2i) and R3 = R3 - 2 *R1'
a[1,:] = (a[1,:] - (3 + 2 * 1J) * a[0,:])
a[2,:] = (a[2,:] - 2 * a[0,:])
print 'a = \n',a
print 'Thus the system of equations is:'
print 'x1 + 2*x2 = 0','-i*x1 + 3*x2 = 0','-x1+i*x2 = 0'
print 'It has only trivial solution x1 = x2 = 0'

a =
[[-1.+0.j  0.+1.j]
[ 0.-1.j  3.+0.j]
[ 1.+0.j  2.+0.j]]
Applying row transformations:
R1 = R1+R3 and R2 = R2 + i *R3
a =
[[ 0.+0.j  2.+1.j]
[ 0.+0.j  3.+2.j]
[ 1.+0.j  2.+0.j]]
R1 = R1 * (1/2+i)
a =
[[ 0.+0.j  1.+0.j]
[ 0.+0.j  3.+2.j]
[ 1.+0.j  2.+0.j]]
R2 = R2-R1*(3+2i) and R3 = R3 - 2 *R1
a =
[[ 0.+0.j  1.+0.j]
[ 0.+0.j  0.+0.j]
[ 1.+0.j  0.+0.j]]
Thus the system of equations is:
x1 + 2*x2 = 0 -i*x1 + 3*x2 = 0 -x1+i*x2 = 0
It has only trivial solution x1 = x2 = 0


## Page 9 Example 1.7¶

In [3]:
from numpy import random, identity
i=2;
while i<=2:
n = random.randint(9)
i=n
print identity(n)
print 'This is an Identity matrix of order %d * %d'%(n,n)
print 'And It is a row reduced matrix.'

[[ 1.  0.  0.  0.  0.  0.  0.]
[ 0.  1.  0.  0.  0.  0.  0.]
[ 0.  0.  1.  0.  0.  0.  0.]
[ 0.  0.  0.  1.  0.  0.  0.]
[ 0.  0.  0.  0.  1.  0.  0.]
[ 0.  0.  0.  0.  0.  1.  0.]
[ 0.  0.  0.  0.  0.  0.  1.]]
This is an Identity matrix of order 7 * 7
And It is a row reduced matrix.


## Page 12 Example 1.8¶

In [4]:
from numpy import random,identity, zeros,array
n = random.randint(9)
print identity(n)
print 'This is an Identity matrix of order %d * %d'%(n,n)
print 'And It is a row reduced matrix.'
m = random.randint(0,9)
n = random.randint(9)
print zeros([m,n])
print 'This is an Zero matrix of order %d * %d'%(m,n)
print 'And It is also a row reduced matrix.'
a = array([[0, 1, -3, 0, 1.0/2],[0, 0, 0, 1, 2],[0, 0 ,0 ,0 ,0]])
print 'a = \n',a
print 'This is a non-trivial row reduced matrix.'

[[ 1.]]
This is an Identity matrix of order 1 * 1
And It is a row reduced matrix.
[[ 0.  0.  0.  0.]]
This is an Zero matrix of order 1 * 4
And It is also a row reduced matrix.
a =
[[ 0.   1.  -3.   0.   0.5]
[ 0.   0.   0.   1.   2. ]
[ 0.   0.   0.   0.   0. ]]
This is a non-trivial row reduced matrix.


## Page 14 Example 1.9¶

In [5]:
from numpy import array
A = array([[1, -2, 1],[2, 1, 1],[0, 5, -1]])
print 'A = \n',A
print 'Applying row transformations:'
print 'R2 = R2 - 2*R1'
A[1,:] = A[1,:] - 2*A[0,:]
print 'A = \n',A
print 'R3 = R3 - R2'
A[2,:] = A[2,:] - A[1,:]
print 'A = \n',A
print 'R2 = 1/5*R2'
A[1,:] = 1.0/5*A[1,:]
print 'A = \n',A
print 'R1 = R1 - 2*R2'
A[0,:] = A[0,:] + 2*A[1,:]
print 'A = \n',A
print 'The condition that the system have a solution is:'
print '2*y1 - y2 + y3 = 0'
print 'where, y1,y2,y3 are some scalars'
print 'If the condition is satisfied then solutions are obtained by assigning a value c to x3'
print 'Solutions are:'
print 'x2 = 1/5*c + 1/5*(y2 - 2*y1)','x1 = -3/5*c + 1/5*(y1 + 2*y2)'

A =
[[ 1 -2  1]
[ 2  1  1]
[ 0  5 -1]]
Applying row transformations:
R2 = R2 - 2*R1
A =
[[ 1 -2  1]
[ 0  5 -1]
[ 0  5 -1]]
R3 = R3 - R2
A =
[[ 1 -2  1]
[ 0  5 -1]
[ 0  0  0]]
R2 = 1/5*R2
A =
[[ 1 -2  1]
[ 0  1  0]
[ 0  0  0]]
R1 = R1 - 2*R2
A =
[[1 0 1]
[0 1 0]
[0 0 0]]
The condition that the system have a solution is:
2*y1 - y2 + y3 = 0
where, y1,y2,y3 are some scalars
If the condition is satisfied then solutions are obtained by assigning a value c to x3
Solutions are:
x2 = 1/5*c + 1/5*(y2 - 2*y1) x1 = -3/5*c + 1/5*(y1 + 2*y2)


## Page 17 Example 1.10¶

In [6]:
from numpy import array
#Part a
a = array([[1, 0],[-3, 1]])
b = array([[5, -1, 2],[15, 4, 8]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)

print '-----------------------------------------------------------------'
#Part b
a = array([[1, 0],[-2, 3],[5 ,4],[0, 1]])
b = array([[0, 6, 1],[3 ,8 ,-2]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)
print '-----------------------------------------------------------------'
#Part c
a = array([[2, 1],[5, 4]])
b = array([[1],[6]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)
print '-----------------------------------------------------------------'
#Part d
a = array([[-1],[3]])
b = array([[2, 4]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)
print '-----------------------------------------------------------------'
#Part e
a = array([[2, 4]])
b = array([[-1],[3]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)
print '-----------------------------------------------------------------'
#Part f
a = array([[0, 1 ,0],[0, 0, 0],[0, 0, 0]])
b = array([[1, -5, 2],[2, 3, 4],[9 ,-1, 3]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)
print '-----------------------------------------------------------------'
#Part g
a = array([[1, -5, 2],[2, 3, 4],[9, -1, 3]])
b = array([[0, 1, 0],[0 ,0 ,0],[0, 0, 0]])
print 'a=\n',a
print 'b=\n',b
print 'ab = \n',a.dot(b)

a=
[[ 1  0]
[-3  1]]
b=
[[ 5 -1  2]
[15  4  8]]
ab =
[[ 5 -1  2]
[ 0  7  2]]
-----------------------------------------------------------------
a=
[[ 1  0]
[-2  3]
[ 5  4]
[ 0  1]]
b=
[[ 0  6  1]
[ 3  8 -2]]
ab =
[[ 0  6  1]
[ 9 12 -8]
[12 62 -3]
[ 3  8 -2]]
-----------------------------------------------------------------
a=
[[2 1]
[5 4]]
b=
[[1]
[6]]
ab =
[[ 8]
[29]]
-----------------------------------------------------------------
a=
[[-1]
[ 3]]
b=
[[2 4]]
ab =
[[-2 -4]
[ 6 12]]
-----------------------------------------------------------------
a=
[[2 4]]
b=
[[-1]
[ 3]]
ab =
[[10]]
-----------------------------------------------------------------
a=
[[0 1 0]
[0 0 0]
[0 0 0]]
b=
[[ 1 -5  2]
[ 2  3  4]
[ 9 -1  3]]
ab =
[[2 3 4]
[0 0 0]
[0 0 0]]
-----------------------------------------------------------------
a=
[[ 1 -5  2]
[ 2  3  4]
[ 9 -1  3]]
b=
[[0 1 0]
[0 0 0]
[0 0 0]]
ab =
[[0 1 0]
[0 2 0]
[0 9 0]]


## Page 22 Example 1.14¶

In [7]:
from numpy import array,linalg
a = array([[0, 1],[1, 0]])
print 'a = \n',a
print 'inverse a = \n',linalg.inv(a)

a =
[[0 1]
[1 0]]
inverse a =
[[ 0.  1.]
[ 1.  0.]]


## Page 25 Example 1.15¶

In [8]:
from numpy import array,linalg
a = array([[2, -1],[1 ,3]])
b = array([[2, -1],[1 ,3]]) #Temporary variable to store a
print 'a = \n',a
print 'Applying row tranformations'
print 'Interchange R1 and R2'
a[0,:] = a[1,:]
a[1,:] = b[0,:]
print 'a = \n',a
print 'R2 = R2 - 2 * R1'
a[1,:] = a[1,:] - 2 * a[0,:]
print 'a =\n ',a
print 'R2 = R2 *1/(-7)'
a[1,:] = (-1.0/7) * a[1,:]
print 'a = \n',a
print 'R1 = R1 - 3 * R2'
a[0,:] = a[0,:] - 3 * a[1,:]
print 'a = \n',a
print 'Since a  has become an identity matrix. So, a is invertible'
print 'inverse of a = '
print linalg.inv(b)# #a was stored in b

a =
[[ 2 -1]
[ 1  3]]
Applying row tranformations
Interchange R1 and R2
a =
[[ 1  3]
[ 2 -1]]
R2 = R2 - 2 * R1
a =
[[ 1  3]
[ 0 -7]]
R2 = R2 *1/(-7)
a =
[[1 3]
[0 1]]
R1 = R1 - 3 * R2
a =
[[1 0]
[0 1]]
Since a  has become an identity matrix. So, a is invertible
inverse of a =
[[ 0.42857143  0.14285714]
[-0.14285714  0.28571429]]


## Page 25 Example 1.16¶

In [9]:
from numpy import array,identity,matrix
a = array([[1 ,1./2, 1.0/3],[1.0/2 ,1.0/3, 1.0/4],[1.0/3, 1.0/4, 1.0/5]])
print 'a = \n',a
b = identity(3)
print 'b = \n',b
print 'Applying row transformations on a and b simultaneously,'
print 'R2 = R2 - 1/2 * R1 and R3 = R3 - 1/3*R1'
a[1,:] = a[1,:] - 1.0/2 * a[0,:]
a[2,:] = a[2,:] - 1.0/3 * a[0,:]
b[1,:] = b[1,:] - 1.0/2 * b[0,:]
b[2,:] = b[2,:] - 1.0/3 * b[0,:]
print 'a = \n',a
print 'b = \n',b
print 'R3 = R3 - R2'
a[2,:] = a[2,:] -  a[1,:]#
b[2,:] = b[2,:] -  b[1,:]
print 'a = \n',a
print 'b = \n',b
print 'R2 = R2 * 12 and R3 = R3 * 180'
a[1,:] = a[1,:] *12#
a[2,:] = a[2,:] * 180#
b[1,:] = b[1,:] * 12#
b[2,:] = b[2,:] * 180#
print 'a = \n',a
print 'b = \n',b
print 'R2 = R2 - R3 and R1 = R1 - 1/3*R3'
a[1,:] = a[1,:] - a[2,:]#
a[0,:] = a[0,:] - 1./3 * a[2,:]#
b[1,:] = b[1,:] - b[2,:]#
b[0,:] = b[0,:] - 1./3 * b[2,:]#
print 'a = \n',a
print 'b = \n',b
print 'R1 = R1 - 1/2 * R2'
a[0,:] = a[0,:] -  1./2 * a[1,:]#
b[0,:] = b[0,:] -  1./2 * b[1,:]#
print 'a = \n',matrix.round(a)
print 'b = \n',b
print 'Since, a = identity matrix of order 3*3. So, b is inverse of a'
print 'inverse(a) = \n',b

a =
[[ 1.          0.5         0.33333333]
[ 0.5         0.33333333  0.25      ]
[ 0.33333333  0.25        0.2       ]]
b =
[[ 1.  0.  0.]
[ 0.  1.  0.]
[ 0.  0.  1.]]
Applying row transformations on a and b simultaneously,
R2 = R2 - 1/2 * R1 and R3 = R3 - 1/3*R1
a =
[[ 1.          0.5         0.33333333]
[ 0.          0.08333333  0.08333333]
[ 0.          0.08333333  0.08888889]]
b =
[[ 1.          0.          0.        ]
[-0.5         1.          0.        ]
[-0.33333333  0.          1.        ]]
R3 = R3 - R2
a =
[[  1.00000000e+00   5.00000000e-01   3.33333333e-01]
[  0.00000000e+00   8.33333333e-02   8.33333333e-02]
[  0.00000000e+00   2.77555756e-17   5.55555556e-03]]
b =
[[ 1.          0.          0.        ]
[-0.5         1.          0.        ]
[ 0.16666667 -1.          1.        ]]
R2 = R2 * 12 and R3 = R3 * 180
a =
[[  1.00000000e+00   5.00000000e-01   3.33333333e-01]
[  0.00000000e+00   1.00000000e+00   1.00000000e+00]
[  0.00000000e+00   4.99600361e-15   1.00000000e+00]]
b =
[[   1.    0.    0.]
[  -6.   12.    0.]
[  30. -180.  180.]]
R2 = R2 - R3 and R1 = R1 - 1/3*R3
a =
[[  1.00000000e+00   5.00000000e-01  -4.44089210e-16]
[  0.00000000e+00   1.00000000e+00  -1.33226763e-15]
[  0.00000000e+00   4.99600361e-15   1.00000000e+00]]
b =
[[  -9.   60.  -60.]
[ -36.  192. -180.]
[  30. -180.  180.]]
R1 = R1 - 1/2 * R2
a =
[[ 1.  0.  0.]
[ 0.  1. -0.]
[ 0.  0.  1.]]
b =
[[   9.  -36.   30.]
[ -36.  192. -180.]
[  30. -180.  180.]]
Since, a = identity matrix of order 3*3. So, b is inverse of a
inverse(a) =
[[   9.  -36.   30.]
[ -36.  192. -180.]
[  30. -180.  180.]]