# Chapter 6 - Elementary canonical forms¶

## Page 184 Example 6.1¶

In [1]:
from sympy import Symbol,Matrix,eye
print 'Standard ordered matrix for Linear operator T on R**2 is:'
A = Matrix(([0, -1],[1 ,0]))
print 'A = \n',A
print 'The characteristic polynomial for T or A is:',
x = Symbol("x")
p = (x*eye(2)-A)
print p
print 'Since this polynomial has no real roots,T has no characteristic values.'

Standard ordered matrix for Linear operator T on R**2 is:
A =
Matrix([[0, -1], [1, 0]])
The characteristic polynomial for T or A is: Matrix([[x, 1], [-1, x]])
Since this polynomial has no real roots,T has no characteristic values.


## Page 184 Example 6.2¶

In [27]:
from sympy import Symbol,Matrix,eye,solve
A = Matrix(([3, 1, -1],[ 2, 2, -1],[2, 2, 0]))
print 'A = \n',A
print 'Characteristic polynomial for A is:',
x=Symbol('x')
p = A.charpoly(x)#
print p.as_expr()
print 'or'
print '(x-1)(x-2)**2'

r = solve(p.as_expr())#
[m,n] = A.shape
print 'The characteristic values of A are:'
print r  #print round(r)
B = A-eye(m)
print 'Now, A-I = \n',B

print 'rank of A - I= ',B.rank()
print 'So, nullity of T-I = 1'
a1 = [1 ,0 ,2]#
print 'The vector that spans the null space of T-I = ',a1
B = A-2*eye(m)
print 'Now,A-2I = \n',B
print 'rank of A - 2I= ',B.rank()
print 'T*alpha = 2*alpha if alpha is a scalar multiple of a2'
a2 = [1 ,1 ,2]
print 'a2 = ',a2

A =
Matrix([[3, 1, -1], [2, 2, -1], [2, 2, 0]])
Characteristic polynomial for A is: x**3 - 5*x**2 + 8*x - 4
or
(x-1)(x-2)**2
The characteristic values of A are:
[1, 2]
Now, A-I =
Matrix([[2, 1, -1], [2, 1, -1], [2, 2, -1]])
rank of A - I=  2
So, nullity of T-I = 1
The vector that spans the null space of T-I =  [1, 0, 2]
Now,A-2I =
Matrix([[1, 1, -1], [2, 0, -1], [2, 2, -2]])
rank of A - 2I=  2
T*alpha = 2*alpha if alpha is a scalar multiple of a2
a2 =  [1, 1, 2]


## Page 187 Example 6.3¶

In [1]:
from numpy import array,transpose,vstack,rank
from sympy import Symbol,Matrix,eye
print 'Standard ordered matrix for Linear operator T on R**3 is:'
A = Matrix(([5, -6, -6],[ -1, 4, 2],[ 3, -6, -4]))
print 'A = \n',A
print 'xI - A = '
B = eye(3)
x = Symbol('x')
P = x*B - A#
print P

print 'Applying row and column transformations:'
print 'C2 = C2 - C3'
P[:,1] = P[:,1] - P[:,2]
print '=>'
print P
print 'Taking (x-2) common from C2'
c = x-2#
P[:,1] = P[:,1] / (x-2)
print '=>'
print ' * ', c
print P
print 'R3 = R3 + R2'
P[2,:] = P[2,:] + P[1,:]
print '=>'
print ' * ', c
print P
P = Matrix(([P[0,0], P[0,2]],[P[2,0], P[2,2]]))
print '=>'
print ' * ', c
print P
print '=>'
print ' * ',c
print P.det()
print 'This is the characteristic polynomial'

print 'Now, A - I = ',A-B
print 'And, A- 2I = ',A-2*B
print 'rank(A-I) = ',rank(A-B)

print 'rank(A-2I) = ',rank(A-2*B)
print 'W1,W2 be the spaces of characteristic vectors associated with values 1,2'
print 'So by theorem 2, T is diagonalizable'
a1 = array([[3, -1 ,3]])
a2 = array([[2, 1, 0]])
a3 = array([[2, 0, 1]])
print 'Null space of (T- I) i.e basis of W1 is spanned by a1 = ',a1
print 'Null space of (T- 2I) i.e. basis of W2 is spanned by vectors x1,x2,x3 such that x1 = 2x1 + 2x3'
print 'One example :'
print 'a2 = ',a2
print 'a3 = ',a3
print 'The diagonal matrix is:'
D = array([[1 ,0 ,0 ],[0, 2, 0],[0, 0, 2]])
print 'D = ',D
print 'The standard basis matrix is denoted as:'
P = transpose(vstack([a1,a2,a3]))
print 'P = ',P
print 'AP = ',A*P
print 'PD = ',P*D
print 'That is, AP = PD'
print '=>  inverse(P)*A*P = D'

Standard ordered matrix for Linear operator T on R**3 is:
A =
Matrix([[5, -6, -6], [-1, 4, 2], [3, -6, -4]])
xI - A =
Matrix([[x - 5, 6, 6], [1, x - 4, -2], [-3, 6, x + 4]])
Applying row and column transformations:
C2 = C2 - C3
=>
Matrix([[x - 5, 0, 6], [1, x - 2, -2], [-3, -x + 2, x + 4]])
Taking (x-2) common from C2
=>
*  x - 2
Matrix([[x - 5, 0, 6], [1, 1, -2], [-3, (-x + 2)/(x - 2), x + 4]])
R3 = R3 + R2
=>
*  x - 2
Matrix([[x - 5, 0, 6], [1, 1, -2], [-2, (-x + 2)/(x - 2) + 1, x + 2]])
=>
*  x - 2
Matrix([[x - 5, 6], [-2, x + 2]])
=>
*  x - 2
x**2 - 3*x + 2
This is the characteristic polynomial
Now, A - I =  Matrix([[4, -6, -6], [-1, 3, 2], [3, -6, -5]])
And, A- 2I =  Matrix([[3, -6, -6], [-1, 2, 2], [3, -6, -6]])
rank(A-I) =  2
rank(A-2I) =  2
W1,W2 be the spaces of characteristic vectors associated with values 1,2
So by theorem 2, T is diagonalizable
Null space of (T- I) i.e basis of W1 is spanned by a1 =  [[ 3 -1  3]]
Null space of (T- 2I) i.e. basis of W2 is spanned by vectors x1,x2,x3 such that x1 = 2x1 + 2x3
One example :
a2 =  [[2 1 0]]
a3 =  [[2 0 1]]
The diagonal matrix is:
D =  [[1 0 0]
[0 2 0]
[0 0 2]]
The standard basis matrix is denoted as:
P =  [[ 3  2  2]
[-1  1  0]
[ 3  0  1]]
AP =  Matrix([[3, 4, 4], [-1, 2, 0], [3, 0, 2]])
PD =  [[3 0 0]
[0 2 0]
[0 0 2]]
That is, AP = PD
=>  inverse(P)*A*P = D


## Page 193 Example 6.4¶

In [2]:
from numpy import array,transpose,vstack,rank
from sympy import Symbol,Matrix,eye

x = Symbol("x")
A = array([[5, -6, -6],[ -1, 4 ,2],[ 3, -6, -4]])   #Matrix given in Example 3
print 'A = \n',A
f = (x-1)*(x-2)**2#
print 'Characteristic polynomial of A is:'
print 'f = (x-1)(x-2)**2'
print 'i.e., f = ',f
p = (x-1)*(x-2)#
print '(A-I)(A-2I) = ',(A-eye(3))*(A-2 * eye(3))
print 'Since, (A-I)(A-2I) = 0. So, Minimal polynomial for above is:'
print 'p = ',p
print '---------------------------------------'

A = array([[3, 1 ,-1],[ 2, 2 ,-1],[2, 2, 0]])    #Matrix given in Example 2
print 'A = \n',A
f = (x-1)*(x-2)**2#
print 'Characteristic polynomial of A is:'
print 'f = (x-1)(x-2)**2'
print 'i.e., f = ',f
print '(A-I)(A-2I) = ',(A-eye(3))*(A-2 * eye(3))
print 'Since, (A-I)(A-2I) is not equal to 0. T is not diagonalizable. So, Minimal polynomial cannot be p.'
print '---------------------------------------'
A = array([[0, -1],[1, 0]])
print 'A = \n',A
f = x**2 + 1#
print 'Characteristic polynomial of A is:'
print 'f = ',f
print 'A**2 + I = ',A**2 + eye(2)
print 'Since, A**2 + I = 0, so minimal polynomial is'
p = x**2 + 1
print 'p = ',p

A =
[[ 5 -6 -6]
[-1  4  2]
[ 3 -6 -4]]
Characteristic polynomial of A is:
f = (x-1)(x-2)**2
i.e., f =  (x - 2)**2*(x - 1)
(A-I)(A-2I) =  Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
Since, (A-I)(A-2I) = 0. So, Minimal polynomial for above is:
p =  (x - 2)*(x - 1)
---------------------------------------
A =
[[ 3  1 -1]
[ 2  2 -1]
[ 2  2  0]]
Characteristic polynomial of A is:
f = (x-1)(x-2)**2
i.e., f =  (x - 2)**2*(x - 1)
(A-I)(A-2I) =  Matrix([[2, 0, -1], [2, 0, -1], [4, 0, -2]])
Since, (A-I)(A-2I) is not equal to 0. T is not diagonalizable. So, Minimal polynomial cannot be p.
---------------------------------------
A =
[[ 0 -1]
[ 1  0]]
Characteristic polynomial of A is:
f =  x**2 + 1
A**2 + I =  Matrix([[1, 1], [1, 1]])
Since, A**2 + I = 0, so minimal polynomial is
p =  x**2 + 1


## Page 197 Example 6.5¶

In [4]:
from numpy import array,transpose,vstack,rank
from sympy import Symbol,Matrix,eye,solve
A = array([[0, 1, 0, 1],[1, 0 ,1 ,0],[0, 1, 0, 1],[1, 0, 1, 0]])
print 'A = \n',A
print 'Computing powers on A:'
print 'A**2 = \n',A*A
print 'A**3 = \n',A*A*A
def p(x):
pp = x**3 - 4*x
return pp
print 'if p = x**3 - 4x, then'
print 'p(A) = ',p(A)
x = Symbol("x")
f = x**3 - 4*x
print 'Minimal polynomial for A is: ',f
print 'Characteristic values for A are:',solve(f,x)
print 'Rank(A) = ',rank(A)
print 'So, from theorem 2, characteristic polynomial for A is:',Matrix(A).charpoly(x).as_expr()

 A =
[[0 1 0 1]
[1 0 1 0]
[0 1 0 1]
[1 0 1 0]]
Computing powers on A:
A**2 =
[[0 1 0 1]
[1 0 1 0]
[0 1 0 1]
[1 0 1 0]]
A**3 =
[[0 1 0 1]
[1 0 1 0]
[0 1 0 1]
[1 0 1 0]]
if p = x**3 - 4x, then
p(A) =  [[ 0 -3  0 -3]
[-3  0 -3  0]
[ 0 -3  0 -3]
[-3  0 -3  0]]
Minimal polynomial for A is:  x**3 - 4*x
Characteristic values for A are: [-2, 0, 2]
Rank(A) =  2
So, from theorem 2, characteristic polynomial for A is: x**4 - 4*x**2


## Page 210 Example 6.12¶

In [2]:
from numpy import array,transpose,random,equal

A = random.rand(3,3)
for i in range(0,3):
for j in range(0,3):
A[i,j]=round(A[i,j]*10)

print 'A = \n',A
print 'A transpose is:\n',
print "A' = \n",Adash
print "Since, A' = A, A is a symmetric matrix."
else:
print "Since, A' is not equal to A, A is not a symmetric matrix."

print "Since, A' = -A, A is a skew-symmetric matrix."
else:
print "Since, A' is not equal to -A, A is not a skew-symmetric matrix."

A1 = 1./2*(A + Adash)
A2 = 1./2*(A - Adash)
print 'A can be expressed as sum of A1 and A2'
print 'i.e., A = A1 + A2'
print 'A1 = \n',A1
print 'A2 = \n',A2
print 'A1 + A2 = \n',A1 + A2

A =
[[ 9.  3.  3.]
[ 7.  4.  4.]
[ 1.  1.  2.]]
A transpose is:
A' =
[[ 9.  7.  1.]
[ 3.  4.  1.]
[ 3.  4.  2.]]
Since, A' is not equal to A, A is not a symmetric matrix.
Since, A' is not equal to -A, A is not a skew-symmetric matrix.
A can be expressed as sum of A1 and A2
i.e., A = A1 + A2
A1 =
[[ 9.   5.   2. ]
[ 5.   4.   2.5]
[ 2.   2.5  2. ]]
A2 =
[[ 0.  -2.   1. ]
[ 2.   0.   1.5]
[-1.  -1.5  0. ]]
A1 + A2 =
[[ 9.  3.  3.]
[ 7.  4.  4.]
[ 1.  1.  2.]]