# Chapter 5 - Determinants¶

## Page 143 Example 5.3¶

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

A =
[[ 7.  9.]
[ 1.  8.]]
D1(A) =  56.0
D2(A) =  -9.0
D(A) = D1(A) + D2(A) =  47.0
That is, D is a 2-linear function.


## Page 145 Example 5.4¶

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

A =
Matrix([[x, 0, -x**2], [0, 1, 0], [1, 0, x**3]])
e1,e2,e3 are the rows of 3*3 identity matrix, then
e1 =  [ 1.  0.  0.]
e2 =  [ 0.  1.  0.]
e3 =  [ 0.  0.  1.]
D(A) = D(x*e1 - x**2*e3, e2, e1 + x**3*e3)
Since, D is linear as a function of each row,
D(A) = x*D(e1,e2,e1 + x**3*e3) - x**2*D(e3,e2,e1 + x**3*e3)
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)
As D is alternating, So
D(A) = (x**4 + x**2)*D(e1,e2,e3)


## Page 147 Example 5.5¶

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

A =
Matrix([[x - 1, x**2, x**3], [0, x - 2, 1], [0, 0, x - 3]])

E(A) =  x**3 - 6*x**2 + 11*x - 6
--------------------------------------
A =
Matrix([[0, 1, 0], [0, 0, 1], [1, 0, 0]])

E(A) =  1


## Page 158 Example 5.6¶

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


Given Matrix:
A =
[[ 1 -1  2  3]
[ 2  2  0  2]
[ 4  1 -1 -1]
[ 1  2  3  0]]
After, Subtracting muliples of row 1 from rows 2 3 4
R2 = R2 - 2*R1
R3 = R3 - 4*R1
R4 = R4 - R1
A =
[[  1  -1   2   3]
[  0   4  -4  -4]
[  0   5  -9 -13]
[  0   3   1  -3]]
We obtain the same determinant as before.
Now, applying some more row transformations as:
R3 = R3 - 5/4 * R2
R4 = R4 - 3/4 * R2
We get B as:
B =
[[ 1 -1  2  3]
[ 0  4 -4 -4]
[ 0  0 -4 -8]
[ 0  0  4  0]]
Now,determinant of A and B will be same
det A = det B =  128.0


## Page 160 Example 5.7¶

In [5]:
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 'B inverse = ',B**-1

A =
Matrix([[x**2 + x, x + 1], [x - 1, 1]])
B =
Matrix([[x**2 - 1, x + 2], [x**2 - 2*x + 3, x]])
det A =  x + 1
det B =  -6
Thus, A is not invertible over K whereas B is invertible
adj A =  Matrix([[(x + 1)*((x - 1)/(x*(x + 1)) + 1/(x*(x + 1))), -x - 1], [-x + 1, x*(x + 1)]])
adj B =  Matrix([[-6*(x + 2)*(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1)**2 - 6/(x**2 - 1), 6*(x + 2)*(-x**2/6 + 1/6)/(x**2 - 1)], [6*(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1), x**2 - 1]])
B inverse =  Matrix([[(x + 2)*(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1)**2 + 1/(x**2 - 1), -(x + 2)*(-x**2/6 + 1/6)/(x**2 - 1)], [-(-x**2/6 + 1/6)*(x**2 - 2*x + 3)/(x**2 - 1), -x**2/6 + 1/6]])


## Page 161 Example 5.8¶

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

A =
[[1 2]
[3 4]]
det A =  Determinant of A is: -2.0
Adjoint of A is: [[-2.  -0. ]
[-0.  -0.5]]
Thus, A is not invertible as a matrix over the ring of integers.
But, A can be regarded as a matrix over field of rational numbers.
Then, A is invertible and Inverse of A is: [[-2.   1. ]
[ 1.5 -0.5]]