Chapter 2: Determinants and Matrices

Example 2.1, page no. 55

In [6]:
import sympy

a = sympy.Symbol('a')
h = sympy.Symbol('h')
g = sympy.Symbol('g')
b = sympy.Symbol('b')
f = sympy.Symbol('f')
c = sympy.Symbol('c')
A = sympy.Matrix([[a,h,g],[h,b,f],[g,f,c]])

print "Determinant of A is: ", A.det()
Determinant of A is:  a*b*c - a*f**2 - b*g**2 - c*h**2 + 2*f*g*h

Example 2.2, page no. 55

In [4]:
import numpy

A = numpy.array([[0,1,2,3],[1,0,3,0],[2,3,0,1],[3,0,1,2]])
print "Determinant of a is:",numpy.linalg.det(A)
Determinanat of a is: 88.0

Example 2.3, page no. 56

In [2]:
import numpy
import sympy

a = sympy.Symbol('a');
b = sympy.Symbol('b');
c = sympy.Symbol('c');
A = sympy.Matrix([[a,a**2,a**3-1],[b,b**2,b**3-1],[c,c**2,c**3-1]])

print A.det()
-a**3*b**2*c + a**3*b*c**2 + a**2*b**3*c - a**2*b*c**3 + a**2*b - a**2*c - a*b**3*c**2 + a*b**2*c**3 - a*b**2 + a*c**2 + b**2*c - b*c**2

Example 2.4, page no. 57

In [6]:
import numpy

A = numpy.array([[21,17,7,10],[24,22,6,10],[6,8,2,3],[6,7,1,2]])
print "Determinanat of a is:",numpy.linalg.det(A)
Determinanat of a is: -24.0

Example 2.16, page no.60

In [1]:
import sympy

x = sympy.Symbol('x');
y = sympy.Symbol('y')
u = x**y
a = sympy.diff(u, y)
b = sympy.diff(a, x)
c = sympy.diff(b, x)
d = sympy.diff(u, x)
e = sympy.diff(d, y)
f = sympy.diff(e, x)
print "a: ", a
print "b: ", b
print "c: ", c
print "d: ", d
print "e: ", e
print "f: ", f
print "Clearly c = f"
a:  x**y*log(x)
b:  x**y*y*log(x)/x + x**y/x
c:  x**y*y**2*log(x)/x**2 - x**y*y*log(x)/x**2 + 2*x**y*y/x**2 - x**y/x**2
d:  x**y*y/x
e:  x**y*y*log(x)/x + x**y/x
f:  x**y*y**2*log(x)/x**2 - x**y*y*log(x)/x**2 + 2*x**y*y/x**2 - x**y/x**2
Clearly c = f

Example 2.17, page no. 65

In [17]:
import numpy

A = numpy.array([[1,3,0],[-1,2,1],[0,0,2]])
B = numpy.array([[2,3,4],[1,2,3],[-1,1,2]])
ma = numpy.matrix(A)
mb = numpy.matrix(B)
print "A*B="
print ma*mb
print ""
print "B*A="
print mb*ma
print "Clearly AB is not equal to BA"
A*B=
[[ 5  9 13]
 [-1  2  4]
 [-2  2  4]]

B*A=
[[-1 12 11]
 [-1  7  8]
 [-2 -1  5]]
Clearly AB is not equal to BA

Example 2.18, page no. 65

In [1]:
import numpy

A = numpy.matrix([[3,2,2],[1,3,1],[5,3,4]])
C = numpy.matrix([[3,4,2],[1,6,1],[5,6,4]])
print "AB=C−−>B=inv(A)∗C"
print ""
B = numpy.linalg.inv(A)*C 
print B
AB=C−−>B=inv(A)∗C

[[  1.00000000e+00  -1.77635684e-15  -8.88178420e-16]
 [ -2.22044605e-16   2.00000000e+00  -1.11022302e-16]
 [  1.77635684e-15   0.00000000e+00   1.00000000e+00]]

Example 2.19, page no. 66

In [48]:
import numpy

A = numpy.array([[1,3,2],[2,0,-1],[1,2,3]])
I = numpy.eye(3)
A = numpy.matrix(A)
print "Aˆ3−4∗Aˆ2−3A+11*I="
print ""
print A**3-4*A**2-3*A+11*I
Aˆ3−4∗Aˆ2−3A+11*I=

[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

Example 2.20, page no. 67

In [58]:
import numpy
0
A = numpy.matrix([[11,-25],[4, -9]])
n = int(raw_input("Enter the value of n: "))
print "Calculating A ^ n: "
print  A**n
Enter the value of n: 3
Calculating A ^ n: 
[[ 31 -75]
 [ 12 -29]]

Example 2.23, page no. 70

In [1]:
import numpy

A = numpy.matrix([[1,1,3],[1,3,-3],[-2,-4,-4]])
print "Inverse of A is: "
print numpy.linalg.inv(A)
Inverse of A is: 
[[ 3.    1.    1.5 ]
 [-1.25 -0.25 -0.75]
 [-0.25 -0.25 -0.25]]

Example 2.24.1, page no. 71

In [3]:
import numpy

A = numpy.matrix([[1,2,3],[1,4,2],[2,6,5]])
print "Rank of A is:",numpy.linalg.matrix_rank(A)
Rank of A is: 2

Example 2.24.2, page no. 71

In [4]:
import numpy

A = numpy.matrix([[0,1,-3,-1],[1,0,1,1],[3,1,0,2],[1,1,-2,0]])
print "Rank of A is:",numpy.linalg.matrix_rank(A)
Rank of A is: 2

Example 2.25, page no. 72

In [5]:
import numpy

A = numpy.matrix([[1,1,3],[1,3,-3],[-2,-4,-4]])
print "Inverse of A is: "
print numpy.linalg.inv(A)
Inverse of A is: 
[[ 3.    1.    1.5 ]
 [-1.25 -0.25 -0.75]
 [-0.25 -0.25 -0.25]]

Example 2.26, page no. 73

In [7]:
import numpy

A = numpy.matrix([[2,3,-1,-1],[1,-1,-2,-4],[3,1,3,-2],[6,3,0,-7]])
r,p = numpy.linalg.eigh ( A )
print "Rank of A is:",numpy.linalg.matrix_rank(A)
Rank of A is: 3

Example 2.28, page no. 75

In [9]:
import numpy

A = numpy.matrix([[1,1,1],[4,3,-1],[3,5,3]])
print "Inverse of A is: "
print numpy.linalg.inv(A)
Inverse of A is: 
[[ 1.4  0.2 -0.4]
 [-1.5  0.   0.5]
 [ 1.1 -0.2 -0.1]]

Example 2.31, page no. 78

In [2]:
import numpy

print  "The equations can be rewritten as AX=B where X=[ x1 ; x2 ; x3 ; x4 ] and "
A = numpy.matrix([[1,-1,1,1],[1,1,-1,1],[1,1,1,-1],[1,1,1,1]])
B = numpy.matrix([[2],[-4],[4],[0]])
print "Determinant of A="
print numpy.linalg.det(A)
print "Inverse of A ="
print numpy.linalg.inv(A)
print "X=",numpy.linalg.inv(A)*B
 The equations can be rewritten as AX=B where X=[ x1 ; x2 ; x3 ; x4 ] and 
Determinant of A=
8.0
Inverse of A =
[[ 0.5  0.5  0.5 -0.5]
 [-0.5  0.   0.   0.5]
 [ 0.  -0.5  0.   0.5]
 [ 0.   0.  -0.5  0.5]]
X= [[ 1.]
 [-1.]
 [ 2.]
 [-2.]]

Example 2.32, page no. 78

In [3]:
import numpy

print "The equations can be rewritten as AX=B where X=[x;y;z] and"
A = numpy.matrix([[5,3,7],[3,26,2],[7,2,10]])
B = numpy.matrix([[4],[9],[5]])
print "Determinant of A="
print numpy.linalg.det(A)
print "Since det(A)=0 , hence, this system of equation will have infinite solutions.. hence, the system is consistent"
The equations can be rewritten as AX=B where X=[x;y;z] and
Determinant of A=
-8.79296635503e-14
Since det(A)=0 , hence, this system of equation will have infinite solutions.. hence, the system is consistent

Example 2.34.1, page no. 80

In [4]:
import numpy

A = numpy.matrix([[1,2,3],[3,4,4],[7,10,12]])
p = numpy.linalg.matrix_rank(A)
print "Rank of A is",p
if p==3:
    print "Equations have only a trivial solution : x=y=z=0"
else:
    print "Equations have infinite no . of solutions."
Rank of A is 3
Equations have only a trivial solution : x=y=z=0

Example 2.34.2, page no. 80

In [5]:
import numpy

A = numpy.matrix([[4,2,1,3],[6,3,4,7],[2,1,0,1]])
p = numpy.linalg.matrix_rank(A)
print "Rank of A is",p
if p ==4:
    print "Equations have only a trivial solution : x=y=z=0"
else:
    print "Equations have infinite no. of solutions."
Rank of A is 2
Equations have infinite no. of solutions.

Example 2.38, page no. 83

In [2]:
import numpy

print "The given equations can be written as Y=AX where"
A = numpy.matrix([[2,1,1],[1,1,2],[1,0,-2]])
print "Determinant of A is",numpy.linalg.det ( A )
print "Since, its non−singular, hence transformation is regular"
print"Inverse of A is"
print numpy.linalg.inv ( A )
The given equations can be written as Y=AX where
Determinant of A is -1.0
Since, its non−singular, hence transformation is regular
Inverse of A is
[[ 2. -2. -1.]
 [-4.  5.  3.]
 [ 1. -1. -1.]]

Example 2.39, page no. 84

In [8]:
import numpy

A = numpy.matrix([[-2./3,1./3,2./3],[2./3,2./3,1./3],[1./3,-2./3,2./3]])
print A
print "A transpose is equal to"
print A.transpose()
print "A∗(transpose of A)="
print A*A.transpose()
print "Hence, A is orthogonal"
[[-0.66666667  0.33333333  0.66666667]
 [ 0.66666667  0.66666667  0.33333333]
 [ 0.33333333 -0.66666667  0.66666667]]
A transpose is equal to
[[-0.66666667  0.66666667  0.33333333]
 [ 0.33333333  0.66666667 -0.66666667]
 [ 0.66666667  0.33333333  0.66666667]]
A∗(transpose of A)=
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
Hence, A is orthogonal

Example 2.42, page no. 87

In [4]:
import numpy
import math

A = numpy.matrix([[5,4],[1,2]])
print "Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then"
P,R= numpy.linalg.eig(A)
U = numpy.zeros([2, 2])
print "R is normalised. let U represents unnormalised version of r"
U[0,0]= R[0,0]*math.sqrt(17)
U[0,1]= R[0,1]*math.sqrt(17)
U[0,1]= R[1,1]*math.sqrt(2)
print "Two eigen vectors are the two columns of U"
print U
Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then
R is normalised. let U represents unnormalised version of r
Two eigen vectors are the two columns of U
[[ 4.  1.]
 [ 0.  0.]]

Examle 2.43, page no. 88

In [14]:
import numpy,math

A = numpy.matrix([[1,1,3],[1,5,1],[3,1,1]])
U = numpy.zeros([3,3])
print "Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then"
R,P = numpy.linalg.eig(A)
print "R is normalised. let U represents unnormalised version of r"
print R
U[0,0] = R[0]*math.sqrt(2) 
U[0,1] = R[1]*math.sqrt(3)
U[0,2] = R[2]*math.sqrt(6)
print "Three eigen vectors are the three columns of U"
print U
Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then
R is normalised. let U represents unnormalised version of r
[-2.  3.  6.]
Three eigen vectors are the three columns of U
[[ -2.82842712   5.19615242  14.69693846]
 [  0.           0.           0.        ]
 [  0.           0.           0.        ]]

Example 2.44, page no. 89

In [15]:
import numpy,math

A = numpy.matrix([[3,1,4],[0,2,6],[0,0,5]])
U = numpy.zeros([3,3])
print "Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then"
R,P = numpy.linalg.eig(A)
print "R is normalised. let U represents unnormalised version of r"
print R
U[0,0] = R[0]*math.sqrt(1) 
U[0,1] = R[1]*math.sqrt(1)
U[0,2] = R[2]*math.sqrt(14)
print "Three eigen vectors are the three columns of U"
print U
Let Rrepresents the matrix of transformation and Prepresents a diagonalmatrix whose values are the eigenvalues of A. then
R is normalised. let U represents unnormalised version of r
[ 3.  2.  5.]
Three eigen vectors are the three columns of U
[[  3.           2.          18.70828693]
 [  0.           0.           0.        ]
 [  0.           0.           0.        ]]

Example 2.45, page no. 90

In [13]:
import numpy

x = numpy.poly([0])
A = numpy.matrix([[1,4],[2,3]])
I = numpy.eye(2)
print "Eigen values of A are"
print numpy.linalg.eig(A)
print "Let"
a = -1;
b = 5;
print "Hence, the characteristic equation is ( x−a ) ( x−b)"
print ( x - a ) *( x - b )

print "Aˆ2−4∗A−5∗ I="
print A**2-4*A-5* I
print "Inverse of A="
print numpy.linalg.inv ( A )
Eigen values of A are
(array([-1.,  5.]), matrix([[-0.89442719, -0.70710678],
        [ 0.4472136 , -0.70710678]]))
Let
Hence, the characteristic equation is ( x−a ) ( x−b)
[-8 -5]
Aˆ2−4∗A−5∗ I=
[[ 0.  0.]
 [ 0.  0.]]
Inverse of A=
[[-0.6  0.8]
 [ 0.4 -0.2]]

Example 2.46, page no. 91

In [14]:
import numpy

x = numpy.poly([0])
A = numpy.matrix([[1,1,3],[1,3,-3],[-2,-4,-4]])
print "Egenvalues of A are"
print numpy.linalg.eig(A)
print "Let"
a =4.2568381
b =0.4032794
c = -4.6601175
print "Hence, the characteristic equation is ( x−a ) ( x−b) ( x−c )"
p = (x-a)*(x-b)*(x-c)
print p
print "Inverse of A="
print numpy.linalg.inv(A)
Egenvalues of A are
(array([ 4.25683813,  0.40327935, -4.66011748]), matrix([[ 0.10296232, -0.91299477, -0.48509974],
        [-0.90473047,  0.40531299,  0.37306899],
        [ 0.41335402,  0.04649661,  0.79088417]]))
Let
Hence, the characteristic equation is ( x−a ) ( x−b) ( x−c )
[-10.99999905   8.00000095]
Inverse of A=
[[ 3.    1.    1.5 ]
 [-1.25 -0.25 -0.75]
 [-0.25 -0.25 -0.25]]

Example 2.47, page no. 91

In [2]:
import numpy

x = numpy.poly([0])
A = numpy.matrix([[2,1,1],[0,1,0],[1,1,2]])
I = numpy.eye(3)
print "Eigenvalues of A are"
print numpy.linalg.eig(A)
print "Let"
a =1
b =1
c =3
print "Hence, the characteristic  equation is (x−a)(x−b)(x−c)="
p = (x-a)*(x-b)*(x-c)
print p
print "Aˆ8−5∗Aˆ7+7∗Aˆ6−3∗Aˆ5+Aˆ4−5∗Aˆ3+8∗Aˆ2−2∗A+I ="
print A**8-5*A**7+7*A**6-3*A**5+A**4-5*A**3+8*A**2-2*A+I
Eigenvalues of A are
(array([ 3.,  1.,  1.]), matrix([[ 0.70710678, -0.70710678, -0.40824829],
        [ 0.        ,  0.        ,  0.81649658],
        [ 0.70710678,  0.70710678, -0.40824829]]))
Let
Hence, the characteristic  equation is (x−a)(x−b)(x−c)=
[ 0 -3]
Aˆ8−5∗Aˆ7+7∗Aˆ6−3∗Aˆ5+Aˆ4−5∗Aˆ3+8∗Aˆ2−2∗A+I =
[[ 8.  5.  5.]
 [ 0.  3.  0.]
 [ 5.  5.  8.]]

Example 2.48, page no. 93

In [7]:
import numpy

A = numpy.matrix([[-1,2,-2],[1,2,1],[-1,-1,0]])
print "R is matrix of transformation and D is a diagonal matrix"
[R,D]= numpy.linalg.eigh(A)
print R
print D
R is matrix of transformation and D is a diagonal matrix
[-1.65544238 -0.21075588  2.86619826]
[[-0.87936655 -0.34661859 -0.32645063]
 [ 0.11410244  0.51222983 -0.85123513]
 [-0.46227167  0.78579651  0.41088775]]

Example 2.49, page no. 93

In [17]:
import numpy,math

A = numpy.matrix([[3,1,4],[0,2,6],[0,0,5]])
P = numpy.zeros([3,3])
print "R is matrix of transformation and D is a diagonal matrix"
R,D = numpy.linalg.eig(A)
print "R is normalised, let P denotes unnormalised version of R . Then "
print R
P[0,0] = R[0]*math.sqrt(2) 
P[0,1] = R[1]*math.sqrt(3)
P[0,2] = R[2]*math.sqrt(6)
print P
print "A^4= ",A**4
R is matrix of transformation and D is a diagonal matrix
R is normalised, let P denotes unnormalised version of R . Then 
[ 3.  2.  5.]
[[  4.24264069   3.46410162  12.24744871]
 [  0.           0.           0.        ]
 [  0.           0.           0.        ]]
A^4=  [[  81   65 1502]
 [   0   16 1218]
 [   0    0  625]]

Example 2.50, page no. 94

In [9]:
import numpy

print  "3∗xˆ2+5∗yˆ2+3∗zˆ2−2∗y∗z+2∗z∗x−2∗x∗y"
print "The matrix of the given quadratic form is"
A = numpy.matrix([[3,-1,1],[-1,5,-1],[1,-1,3]])
print "Let R represents the matrix of transformation and Prepresents a diagonal matrix whose values are the eigenvalues of A. then"
[R,P] = numpy.linalg.eig(A)
print "So, canonical form is 2∗xˆ2+3∗yˆ2+6∗zˆ2"
3∗xˆ2+5∗yˆ2+3∗zˆ2−2∗y∗z+2∗z∗x−2∗x∗y
The matrix of the given quadratic form is
Let R represents the matrix of transformation and Prepresents a diagonal matrix whose values are the eigenvalues of A. then
So, canonical form is 2∗xˆ2+3∗yˆ2+6∗zˆ2

Example 2.51, page no. 95

In [14]:
import numpy

print "2∗x1∗x2+2∗x1∗x3−2∗x2∗x3"
print "The matrix of the given quadratic form is"
A = numpy.matrix([[0,1,1],[1,0,-1],[1,-1,0]])
print "Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then"
[R,P] = numpy.linalg.eig(A)
print "so, canonical form is −2∗xˆ2+yˆ2+ zˆ2"
2∗x1∗x2+2∗x1∗x3−2∗x2∗x3
The matrix of the given quadratic form is
Let R represents the matrix of transformation and P represents a diagonal matrix whose values are the eigenvalues of A. then
so, canonical form is −2∗xˆ2+yˆ2+ zˆ2

Example 2.52, page no. 96

In [32]:
import numpy

'''
A=[2+%i 3 -1+3*%i;-5 %i 4-2*%i]
'''

A = numpy.matrix([[2+1j,3,-1+3*1j],[-5,1j,4-2*1j]])
#A = A.getH()
print "A∗=", A.getH()
print "AA∗=", A*(A.getH())
print "Clearly, AA∗ is hermitian matrix"
A∗= [[ 2.-1.j -5.-0.j]
 [ 3.-0.j  0.-1.j]
 [-1.-3.j  4.+2.j]]
AA∗= [[ 24.+0.j -20.+2.j]
 [-20.-2.j  46.+0.j]]
Clearly, AA∗ is hermitian matrix

Example 2.53, page no. 97

In [35]:
import numpy 

A = numpy.matrix([[(1/2)*(1+1j),(1/2)*(-1+1j)],[(1/2)*(1+1j),(1/2)*(1-1j)]])
print "A∗=", A.getH()
print "AA∗=", A*(A.getH())
print "A∗A=", (A.getH())*A
 A∗= [[ 0.-0.j  0.-0.j]
 [-0.-0.j  0.-0.j]]
AA∗= [[ 0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j]]
A∗A= [[ 0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j]]

Example 2.54, page no. 97

In [5]:
import numpy

A = numpy.matrix([[0,1+2*1j],[-1+2*1j,0]])
I = numpy.eye(2)
print "I−A="
I-A
print "inverse of (I+A)="
print numpy.linalg.inv(I+A)
print "((I−A)(inverse(I+A)))∗((I−A)(inverse(I+A)))="
print (((I-A)*(numpy.linalg.inv(I+A))).T)*((I-A)*(numpy.linalg.inv(I+A)))
print "((I−A)(inverse(I+A)))((I−A)(inverse(I+A)))∗="
print ((I-A)*(numpy.linalg.inv(I+A)))*(((I-A)*(numpy.linalg.inv(I+A))).T)
print "Clearly, the product is an identity matrix.hence, it is a unitary matrix"
I−A=
inverse of (I+A)=
[[ 0.16666667+0.j         -0.16666667-0.33333333j]
 [ 0.16666667-0.33333333j  0.16666667+0.j        ]]
((I−A)(inverse(I+A)))∗((I−A)(inverse(I+A)))=
[[  1.11111111e-01-0.44444444j  -2.77555756e-17+0.88888889j]
 [ -2.77555756e-17+0.88888889j   1.11111111e-01+0.44444444j]]
((I−A)(inverse(I+A)))((I−A)(inverse(I+A)))∗=
[[  1.11111111e-01+0.44444444j   1.11022302e-16+0.88888889j]
 [  1.11022302e-16+0.88888889j   1.11111111e-01-0.44444444j]]
Clearly, the product is an identity matrix.hence, it is a unitary matrix