from numpy import random
A = random.rand(2,2)
for x in range(0,2):
for y in range(0,2):
A[x,y]=round(A[x,y]*10)
print 'A = \n',A
D1 = A[0,0]*A[1,1]
D2 = - A[0,1]*A[1,0]
print 'D1(A) = ',D1
print 'D2(A) = ',D2
print 'D(A) = D1(A) + D2(A) = ',D1 + D2
print 'That is, D is a 2-linear function.'
from numpy import array,random,identity
from sympy import Symbol,Matrix
x = Symbol("x")
A = Matrix(([x, 0, -x**2],[0, 1, 0],[1, 0, x**3]))
print 'A = \n',A
print 'e1,e2,e3 are the rows of 3*3 identity matrix, then'
T = identity(3)
e1 = T[0,:]
e2 = T[1,:]
e3 = T[2,:]
print 'e1 = ',e1
print 'e2 = ',e2
print 'e3 = ',e3
print 'D(A) = D(x*e1 - x**2*e3, e2, e1 + x**3*e3)'
print 'Since, D is linear as a function of each row,'
print 'D(A) = x*D(e1,e2,e1 + x**3*e3) - x**2*D(e3,e2,e1 + x**3*e3)'
print 'D(A) = x*D(e1,e2,e1) + x**4*D(e1,e2,e3) - x**2*D(e3,e2,e1) - x**5*D(e3,e2,e3)'
print 'As D is alternating, So'
print 'D(A) = (x**4 + x**2)*D(e1,e2,e3)'
from sympy import Symbol, Matrix
#part a
x = Symbol('x')
A = Matrix(([x-1, x**2, x**3],[0, x-2, 1],[0, 0, x-3]))
print 'A = \n',A
print '\nE(A) = ',A.det()
print '--------------------------------------'
#part b
A = Matrix(([0 ,1, 0],[0, 0, 1],[1 ,0, 0]))
print 'A = \n',A
print '\nE(A) = ',A.det()
from numpy import array,linalg
print 'Given Matrix:'
A = array([[1, -1, 2, 3],[ 2, 2, 0, 2],[ 4, 1 ,-1, -1],[1, 2, 3, 0]])
print 'A = \n',A
print 'After, Subtracting muliples of row 1 from rows 2 3 4'
print 'R2 = R2 - 2*R1'
A[1,:] = A[1,:] - 2 * A[0,:]
print 'R3 = R3 - 4*R1'
A[2,:] = A[2,:] - 4 * A[0,:]
print 'R4 = R4 - R1'
A[3,:] = A[3,:] - A[0,:]
print 'A = \n',A
T = A# #Temporary matrix to store A
print 'We obtain the same determinant as before.'
print 'Now, applying some more row transformations as:'
print 'R3 = R3 - 5/4 * R2'
T[2,:] = T[2,:] - 5./4 * T[1,:]
print 'R4 = R4 - 3/4 * R2'
T[3,:] = T[3,:] - 3./4 * T[1,:]
B = T#
print 'We get B as:'
print 'B = \n',B
print 'Now,determinant of A and B will be same'
print 'det A = det B = ',linalg.det(B)
from sympy import Symbol, Matrix
x = Symbol("x")
A = Matrix(([x**2+x, x+1],[x-1, 1]))
B = Matrix(([x**2-1, x+2],[x**2-2*x+3, x]))
print 'A = \n',A
print 'B = \n',B
print 'det A = ',A.det()
print 'det B = ',B.det()
print 'Thus, A is not invertible over K whereas B is invertible'
print 'adj A = ',(A**-1)*A.det()
print 'adj B = ',(B**-1)*B.det()
print '(adj A)A = (x+1)I'
print '(adj B)B = -6I'
print 'B inverse = ',B**-1
from numpy import array,linalg,identity
A = array([[1, 2],[3, 4]])
print 'A = \n',A
d = linalg.det(A)#
print 'det A = ','Determinant of A is:',d
ad = (d* identity(2)) / A
print 'Adjoint of A is:',ad
print 'Thus, A is not invertible as a matrix over the ring of integers.'
print 'But, A can be regarded as a matrix over field of rational numbers.'
In = linalg.inv(A)#
#The A inverse matrix given in book has a wrong entry of 1/2. It should be -1/2.
print 'Then, A is invertible and Inverse of A is:',In