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)'
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'
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.'
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.'
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)'
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)
from numpy import array,linalg
a = array([[0, 1],[1, 0]])
print 'a = \n',a
print 'inverse a = \n',linalg.inv(a)
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
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