Chapter 4 Determinants

Ex:4.3.1 Pg:210

In [1]:
from numpy.linalg import det
from numpy.random import rand
n= 4 # the value of n
a=rand(n,n)
determinant = det(a)
print 'Determinant=',determinant
Determinant= -0.0288448892628

Ex:4.3.3 Pg: 214

In [2]:
from numpy import mat,shape, transpose as tp
from numpy.linalg import det
A=[[2, -1, 0, 0],[-1, 2, -1, 0],[0, -1 ,2 ,-1],[0, 0 ,-1 ,2]]
A=mat(A)
print 'A=\n',A
m,n=shape(A)

a=A[1,:]

c=[];
for L in range(0,4):
    
    for i in range(0,4):
        l=[]
        for j in range(0,4):
            if i!=j:
                l.append(j)
            
        B=A[1:4,l]
        
    
    c1l=(-1)**(1+L+1)*det(B);
    c=c+[c1l]

d=a*tp(mat(c))+1;
print d
A=
[[ 2 -1  0  0]
 [-1  2 -1  0]
 [ 0 -1  2 -1]
 [ 0  0 -1  2]]
[[ 5.]]

Ex:4.4.1 Pg:282

In [10]:
# inverse of a sum matrix is a difference matrix
from numpy import mat,linalg,dot
A=mat([[1, 1 ,1],[0, 1, 1],[0, 0, 1]])
adjA = linalg.det(A)*linalg.inv(A)*linalg.det(A)
invA=(adjA/det(A))
print 'Adjoint of A:\n',adjA
print 'inv(A):\n',invA
Adjoint of A:
[[ 1. -1.  0.]
 [ 0.  1. -1.]
 [ 0.  0.  1.]]
inv(A):
[[ 1. -1.  0.]
 [ 0.  1. -1.]
 [ 0.  0.  1.]]

Ex:4.4.2 Pg: 222

In [56]:
from numpy import mat
from numpy.linalg import det
#x1+3x2=0
#2x1+4x2=6
A=mat([[1, 3],[2, 4]])
b=mat([[0],[6]])
X1=mat([[0, 3],[6, 4]])
X2=mat([[1, 0],[2, 6]])
print 'x1=',(det(X1)/det(A))
print 'x2=',(det(X2)/det(A))
x1= 9.0
x2= -3.0