0% found this document useful (0 votes)
8 views

Lab 4

Lab 4 programs

Uploaded by

akshayandani05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 4

Lab 4 programs

Uploaded by

akshayandani05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

LAB 4: Computation of basis and dimension for a vector space and graphical representation of

linear transformation

Objectives:

Use python

1. to verify the Rank nullity theorem of a given linear transformation

2. to compute the dimension of vector space

3. to represent linear transformations graphically

1 .Rank Nullity Theorem

Verify the rank-nullity theorem for the linear transformation T : R3 → R3 defined byT(x, y, z) = (x + 4y
+ 7z, 2x + 5y + 8z, 3x + 6y + 9z)

import numpy as np

from scipy . linalg import null_space

A=np.array([[1 , 2 , 3], [4 , 5 , 6], [7 , 8 , 9]])

rank=np.linalg.matrix_rank(A)

print(" Rank of the matrix ", rank)

ns=null_space(A)

print(" Null space of the matrix ", ns)

nullity=ns.shape [1]

print(" Null space of the matrix ", nullity)

if rank + nullity == A . shape [1]:

print("Rank - nullity theorem holds .")

else :

print("Rank - nullity theorem does not hold .")

output:
Rank of the matrix 2
Null space of the matrix [[-0.40824829]
[ 0.81649658]
[-0.40824829]]
Null space of the matrix 1
Rank - nullity theorem holds.

2. Dimension of Vector Space

Find the dimension of subspace spanned by the vectors(1, 2, 3)(2, 3, 1)


and (3, 1, 2)

import numpy as np
V=np.array([
[1 , 2 , 3],
[2 , 3 , 1],
[3 , 1 , 2]])
basis=np.linalg.matrix_rank(V)
dimension=V.shape[0]
print("Basis of the matrix ", basis)
print(" Dimension of the matrix ", dimension)

output:
Basis of the matrix 3
Dimension of the matrix 3

3. Extract the linearly independent rows in given matrix: Basis of Row


space

from numpy import *


import sympy as sp
A=[[1 ,-1 ,1 , 1],[2 ,-5 ,2 , 2],[3 ,-3 ,5 , 3],[4 ,-4 ,4 , 4]]
AB=array( A )
S=shape( A )
n=len( A )
for i in range( n ):
if AB[i , i]==0:
ab=copy( AB )
for k in range( i+1 , S[0]):
if ab[k , i]!=0:
ab[i ,:]=AB[k ,:]
ab[k ,:]=AB[i ,:]
AB=copy( ab )
for j in range( i+1 , n ):
Fact=AB[j , i]/AB[i , i]
for k in range(i , n ):
AB[j , k]=AB[j , k]- Fact*AB[i , k]
display("REF of given matrix : ", sp . Matrix ( AB ))
temp={(0 , 0 , 0 , 0 )}
result=[]
for idx , row in enumerate(map(tuple , AB )):
if row not in temp :
result.append( idx )
print("\n Basis are non - zero rows of A:")
display( sp . Matrix( AB[ result ]))

output:
'REF of given matrix : '

1 −1 1 1
0 −3 0 0
[ ]
0 0 2 0
0 0 0 0

Basis are non - zero rows of A:

1 −1 1 1
[0 −3 0 0]
0 0 2 0
4.Graphical representation of a transformation
4.1Horizontal stretch:
Represent the horizontal stretch transformation T : R2ßR2 geometrically
Find the image of vector (10, 0) when it is stretched horizontally by 2
units.

import numpy as np
import matplotlib . pyplot as plt
V=np.array([[10,0]])
origin=np.array([[0 , 0 , 0],[0 , 0 , 0]])
A=np.matrix([[2 , 0],[0 , 1]])
V1=np.matrix( V )
V2=A*np.transpose( V1 )
V2=np.array( V2 )
plt.quiver(*origin , V[:,0], V[:,1], color =['b'], scale =50)
plt.quiver(*origin , V2[0 ,:], V2[1 ,:], color =['r'], scale =50)
plt.show()

output:

Another example:

from math import pi,sin,cos

import matplotlib.pyplot as plt

import numpy as np

coords=np.array([[0 , 0],[0.5 , 0.5],[0.5 , 1.5],[0 , 1],[0 , 0]])

coords=coords.transpose()

coords
x=coords[0 ,:]

y=coords [1 ,:]

A=np.array([[2 , 0],[0 , 1]])

A_coords=A@coords

x_LT1=A_coords [0 ,:]

y_LT1=A_coords [1 ,:]

fig,ax=plt.subplots()

ax.plot(x ,y ,'ro ')

ax.plot(x_LT1 , y_LT1 ,'bo ')

ax.plot(x ,y ,'r', ls="--")

ax.plot( x_LT1 , y_LT1 ,'b')

ax.axvline( x=0 , color ="k", ls=":")

ax.axhline( y=0 , color ="k", ls=":")

ax.grid( True )

ax.axis([-2 ,2 ,-1 , 2])

ax.set_aspect('equal')

ax.set_title(" Horizontal Stretch ") ;

output:
4.2 Reflection:

Represent the reflection transformation T : R2 → R2 geometrically. Find the image of vector (10, 0)
when it is reflected about y axis.

import numpy as np

import matplotlib.pyplot as plt

V=np.array([[10 , 0]])

origin=np.array([[0 , 0 , 0],[0 , 0 , 0]])

A=np.matrix ([[-1 , 0],[0 , 1]])

V1=np.matrix(V)

V2=A*np.transpose( V1 )

V2=np.array ( V2 )

plt.quiver(*origin , V[:,0], V[:,1], color =['b'], scale =50)

plt.quiver (*origin , V2[0 ,:], V2[1 ,:], color =['r'], scale =50)

plt.show()

output:

Another example.
B=np.array([[-1 , 0],[0 , 1]])

B_coords=B@coords
x_LT2=B_coords[0 ,:]

y_LT2=B_coords[1 ,:]

fig,ax=plt.subplots()

ax.plot(x ,y ,'ro ')

ax.plot(x_LT2 , y_LT2 ,'bo')

ax.plot(x,y,'r', ls="--")

ax.plot( x_LT2 , y_LT2 ,'b')

ax.axvline(x=0 , color ="k", ls=":")

ax.axhline(y=0 , color ="k", ls=":")

ax.grid( True )

ax.axis([-2 ,2 ,-1 , 2])

ax.set_aspect('equal')

ax.set_title(" Reflection ") ;

output:

4.3 Rotation:

Represent the rotation transformation T : R2 → R2 geometrically. Find the image of vector (10, 0)
when it is rotated by π/2 radians.
import numpy as np

import matplotlib.pyplot as plt

V=np.array([[10 , 0]])

origin=np.array([[0 , 0 , 0],[0 , 0 , 0]])

A=np.matrix([[0 ,-1],[1 , 1]])

V1=np.matrix( V )

V2=A*np.transpose( V1 )

V2=np.array( V2 )

plt.quiver(*origin , V[:,0], V[:,1], color =['b'], scale =50)

plt.quiver(*origin , V2[0 ,:], V2[1 ,:], color =['r'], scale =50)

plt.show ()

output:

Another example

theta=pi/6

R=np.array([[cos( theta ) ,-sin( theta )],[sin( theta ) ,cos( theta )]])

R_coords=R@coords

x_LT3=R_coords[0 ,:]
y_LT3=R_coords[1 ,:]

fig,ax=plt.subplots()

ax.plot(x ,y ,'ro')

ax.plot(x_LT3 , y_LT3 ,'bo')

ax.plot(x ,y ,'r', ls="--")

ax.plot(x_LT3 , y_LT3 ,'b')

ax.axvline(x=0 , color ="k", ls=":")

ax.axhline(y=0 , color ="k", ls=":")

ax.grid( True )

ax.axis ([-2 ,2 ,-1 , 2])

ax.set_aspect ('equal')

output:

4.4 Shear Transformation

Represent the Shear transformation T : R2 → R2 geometrically. Find the image of (2, 3) under shear
transformation.
import numpy as np

import matplotlib.pyplot as plt

V=np.array([[2 , 3]])
origin=np.array([[0 , 0 , 0],[0 , 0 , 0]])

A=np.matrix([[1 , 2],[0 , 1]])

V1=np.matrix( V )

V2=A*np.transpose( V1 )

V2=np.array( V2 )

print(" Image of given vectors is:", V2)

plt.quiver (*origin , V[:,0], V[:,1], color =['b'], scale =20)

plt.quiver (*origin , V2[0 ,:], V2[1 ,:], color =['r'], scale =20)

plt.show ()

output:
Image of given vectors is: [[8]
[3]]

Another example.

S=np.array([[1 , 2],[0 , 1]])

S_coords=S@coords

x_LT4=S_coords [0 ,:]

y_LT4=S_coords [1 ,:]

fig , ax=plt.subplots()
ax.plot(x ,y ,'ro')

ax.plot(x_LT4 , y_LT4 ,'bo')

ax.plot(x ,y ,'r', ls="--")

ax.plot(x_LT4 , y_LT4 ,'b')

ax.axvline(x=0 , color ="k", ls=":")

ax.axhline(y=0 , color ="k", ls=":")

ax.grid( True )

ax.axis([-2 ,4 ,-1 , 2])

ax.set_aspect('equal')

ax.set_title(" Shear ") ;

output:

4.5 Composition

Represent the composition of two 2D transformations. Find the image of vector (10, 0) when it is
rotated by π/2 radians then stretched horizontally 2 units.

import numpy as np

import matplotlib.pyplot as plt

V=np.array([[2 , 3]])

origin=np.array([[0 , 0 , 0],[0 , 0 , 0]])

A=np.matrix([[0 , -1],[1 , 0]])

B=np.matrix([[2 , 0],[0 , 1]])


V1=np.matrix( V )

V2=A*np.transpose ( V1 )

V3=B*V2

V2=np.array( V2 )

V3=np.array( V3 )

print(" Image of given vectors is:", V3 )

plt.quiver (*origin , V[:,0], V[:,1], color =['b'], scale =20)

plt.quiver (*origin , V2[0 ,:], V2[1 ,:], color =['r'], scale =20)

plt.quiver (*origin , V3[0 ,:], V3[1 ,:], color =['g'], scale =20)

plt.title ('Blue = original , Red = Rotated ,Green = Rotated + Streached')

plt.show()
output: Image of given vectors is: [[-6]
[ 2]]

Another example.

C=np.array([[-cos( theta ) ,sin( theta )],[sin( theta ) ,cos( theta )]])

C_coords=C@coords

x_LT5=C_coords[0 ,:]

y_LT5=C_coords[1 ,:]
fig , ax=plt . subplots ()

ax.plot(x ,y ,'ro ')

ax.plot( x_LT5 , y_LT5 ,'bo')

ax.plot (x ,y ,'r', ls="--")

ax.plot ( x_LT5 , y_LT5 ,'b')

ax.axvline ( x=0 , color ="k", ls=":")

ax.axhline ( y=0 , color ="k", ls=":")

ax.grid ( True )

ax.axis ([-2 ,2 ,-1 , 2])

ax.set_aspect ('equal')

output:

You might also like