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.'
```

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
```

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'
```

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
```

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()
```

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',
Adash=transpose(A)
print "A' = \n",Adash
if equal(Adash,A).all():
print "Since, A' = A, A is a symmetric matrix."
else:
print "Since, A' is not equal to A, A is not a symmetric matrix."
if equal(Adash,-A).all():
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
```