You are on page 1of 14

3/24/22, 7:54 PM exp 6

SHRI RAMDEOBABA COLLEGE OF ENGINEERING


AND MANAGEMENT

Department of Mathematics

Computational Mathematics Lab

Experiment No. 5

NAME: VEDANT PULLIWAR

ROLL NO : 85

BATCH: M3

Aim : Basic of Linear Algebra

DEFINE A MATRIX
In [51]: # Defining 3 by 3 matrix

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

show('A=',A)

Out[51]:
1 2 3
⎛ ⎞

A= ⎜ 4 5 6⎟
⎝ ⎠
7 8 9

In [0]:

In [7]: show(matrix(2,3,[1,2,3,5,6,7]))

Out[7]:
1 2 3
( )
5 6 7

In [9]: show(matrix(3,3,[1,2,3,4,5,6,7,8,9]))

Out[9]:
1 2 3
⎛ ⎞

⎜4 5 6⎟
⎝ ⎠
7 8 9

In [12]: show(matrix(ZZ,3,2,[-1,2,3,4,5,0])) # ZZ (integrr field)

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 1/14


3/24/22, 7:54 PM exp 6

Out[12]: ⎛
−1 2

⎜ 3 4⎟
⎝ ⎠
5 0

In [13]: show(matrix(RR,3,2,[1,2,3,4,5,6])) # RR (real field)

Out[13]:
1.00000000000000 2.00000000000000
⎛ ⎞

⎜ 3.00000000000000 4.00000000000000 ⎟
⎝ ⎠
5.00000000000000 6.00000000000000

In [14]: show(matrix(RR,3,2,[1/2,2,3,4,5/6,6])) # RR (real field)

Out[14]:
0.500000000000000 2.00000000000000
⎛ ⎞

⎜ 3.00000000000000 4.00000000000000 ⎟
⎝ ⎠
0.833333333333333 6.00000000000000

In [15]: show(matrix(CDF,3,2,[1+i,2,3,4,5-i,6+2*i])) # CDF (complex dence field)

Out[15]:
1.0 + 1.0i 2.0
⎛ ⎞

⎜ 3.0 4.0 ⎟
⎝ ⎠
5.0 − 1.0i 6.0 + 2.0i

In [16]: show(matrix(QQ,3,2,[1,5/2,22/7,4,5,6/7])) # QQ (rational field)

Out[16]: 5
1
⎛ 2


22 ⎟


4 ⎟

7
⎜ ⎟
6
⎝ 5 ⎠
7

In [18]: show(matrix(QQ,3,2,[1,5/2,22/7,4,sqrt(9),6/7])) # QQ (rational feild)

Out[18]: 5
⎛ 1 ⎞
2


22 ⎟


4 ⎟

⎜ 7 ⎟
6
⎝ 3 ⎠
7

MATRIX OPERATIONS
In [21]: P=matrix(2,3,[8,5,6,9,7,3])

Q=matrix(2,3,[1,2,5,3,6,4])

show('P+Q =',P+Q, 'P-Q =',P-Q)

show('P Q^T =',P*Q.transpose())

Out[21]:
9 7 11 7 3 1
P+Q =( ) P-Q =( )
12 13 7 6 1 −1

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 2/14


3/24/22, 7:54 PM exp 6

48 78
Out[21]: P Q^T =( )
38 81

In [25]: T=transpose(Q)

show(T)

Out[25]:
1 3
⎛ ⎞

⎜2 6⎟
⎝ ⎠
5 4

In [50]: Q=matrix(2,3,[8,5,6,9,7,3])

P=matrix(2,3,[1,2,5,3,6,4])

show('Q P =',Q*P.transpose())

Out[50]:
48 78
Q P =( )
38 81

In [27]: R=matrix(2,2,[-8,5,1,-7])

show('R=',R, 'R^5 = ', R^5)

Out[27]:
−8 5 −56313 94005
R= ( ) R^5 =( )
1 −7 18801 −37512

In [28]: R^2 == R*R

Out[28]: True

In [29]: R.det() # To find determinant of R

Out[29]: 51

In [30]: R.transpose() # To find transpose of R

Out[30]: [-8 1]

[ 5 -7]

In [31]: R.inverse() # To find inverse of R

Out[31]: [-7/51 -5/51]

[-1/51 -8/51]

In [32]: R.adjoint()

Out[32]: [-7 -5]

[-1 -8]

In [33]: R.adjugate() # To find adjoint of R

Out[33]: [-7 -5]

[-1 -8]

In [35]: R.adjugate()==(R.det())*(R.inverse())

Out[35]: True

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 3/14


3/24/22, 7:54 PM exp 6

In [36]: 3*R # Scalar Multiplication

Out[36]: [-24 15]

[ 3 -21]

In [37]: R.trace()

Out[37]: -15

In [38]: var('x,y,z')

S=matrix(3,3,[1,z,-y,-z,1,x,y,-z,1])

show('S=',S)

show('|S|=',S.det())

Out[38]:
1 z −y
⎛ ⎞

S= ⎜ −z 1 x⎟
⎝ ⎠
y −z 1

Out[38]:
|S|=(xz + y)y − (yz − z)z + xz + 1

In [39]: var('x,y,z')

S=matrix(3,3,[x+y,x,x,5*x+4*y,4*x,2*x,10*x+8*y,8*x,3*x])

show('S=',S)

show('|S|=',S.det())

T=S.det()

T.full_simplify()

Out[39]:
x + y x x
⎛ ⎞

S= ⎜ 5x + 4y 4x 2x⎟

⎝ ⎠
10 x + 8 y 8x 3x

Out[39]: 2 2
|S|=(5 x + 4 y)x − 4 (x + y)x

Out[39]: x^3

In [40]: show(S.inverse())

Out[40]: (5 x+4 y)x (5 x+4 y)x

⎛ ( −2 x)x ⎞ ( −2 x
⎛ x+y
x
x+y

2 ⎜ − ⎟
(5 x+4 y)x x+y (5 x+4 y)x

⎝ ( −4 x)(x+y) ⎠ ( −4 x)
(5 x+4 y)x x+y

1 x+y
x

x+y − (5 x+4 y)x
2 x
+
(5 x+4 y)x

x

( −4 x)(x+y) ( −4 x)(x+y)
x+y x+y


(5 x+4 y)x
(5 x+4

2 ( −2 x)
5 x+4 y

1
x+y
x+


− −
(5 x+4 y)x (5 x+4 y)x (5 x+4 y)x (5 x+4

( −4 x)(x+y) −4 x ( −4 x)x (
⎜ x+y x+y x+y x+y

2

0
x

In [41]: show(diff(S,x))

show(diff(S,y))

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 4/14


3/24/22, 7:54 PM exp 6

Out[41]: ⎛
1 1 1

⎜ 5 4 2⎟
⎝ ⎠
10 8 3

Out[41]:
1 0 0
⎛ ⎞

⎜4 0 0⎟
⎝ ⎠
8 0 0

In [52]: show('A=',A)

Out[52]:
1 2 3
⎛ ⎞

A= ⎜ 4 5 6⎟
⎝ ⎠
7 8 9

In [54]: A.minors(1) # All minors of order 1

Out[54]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In [55]: A.minors(2) # All minors of order 2

Out[55]: [-3, -6, -3, -6, -12, -6, -3, -6, -3]

In [56]: A.minors(3) # All minors of order 3

Out[56]: [0]

In [57]: A.minors(3)== [A.det()]

Out[57]: True

In [58]: A.det()

Out[58]: 0

Exercise 1

1. Find x, y, z and t if $2

(x z y t)

+ 3 \begin{pmatrix} 1
& -1 \ 0 & 0 \end{pmatrix} =3

(3 5 4 6)

2. Let A = (1 2 3 1 3 3 1 2 and4)

B = (5 2 3 7 3 4 9 4 5 ) . Determine (i)

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 5/14


3/24/22, 7:54 PM exp 6

determinent ,

adjoint , inverse and all minors of order 1,2 & 3. (ii) A+B , A-B ,
AB, A . 6

In [187… x,y,z,t = var('x, y, z, t')

show(*solve([2*x+3 == 9, 2*z-3 == 15, 2*y== 12, 2*t==18], x, y, z, t))

Out[187]:
[x = 3, y = 6, z = 9, t = 9]

In [177… A=matrix([[1,2,3],[1,3,3],[1,2,4]])

B=matrix([[5,2,3],[7,3,4,],[9,4,5]])

show('(i) A =',A)

show('Determinent =',A.det())

show('adjoint =',A.adjugate())

show('inverse =',A.inverse())

show('minor of 1 =',A.minors(1))

show('minor of 2 =',A.minors(2))

show('minor of 3 =',A.minors(3))

Out[177]:
1 2 3
⎛ ⎞

(i) A =⎜1 3 3⎟
⎝ ⎠
1 2 4

Out[177]:
Determinent =1

Out[177]:
6 −2 −3
⎛ ⎞

adjoint = ⎜ −1 1 0⎟
⎝ ⎠
−1 0 1

Out[177]:
6 −2 −3
⎛ ⎞

inverse = ⎜ −1 1 0⎟
⎝ ⎠
−1 0 1

Out[177]:
minor of 1 = [1, 2, 3, 1, 3, 3, 1, 2, 4]

Out[177]:
minor of 2 = [1, 0, −3, 0, 1, 2, −1, 1, 6]

Out[177]:
minor of 3 = [1]

In [171… show('(ii) A+B =' ,A+B)

show('A-B =',A-B)

show('AB =',A*B)

show('A^6 =' ,A^6)

Out[171]:
6 4 6
⎛ ⎞
(ii) A+B =⎜ 8 6 7⎟
⎝ ⎠
10 6 9

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 6/14


3/24/22, 7:54 PM exp 6

Out[171]: ⎛
−4 0 0

A-B = ⎜ −6 0 −1 ⎟
⎝ ⎠
−8 −2 −1

Out[171]:
46 20 26
⎛ ⎞

AB = ⎜ 53 23 30 ⎟
⎝ ⎠
55 24 31

Out[171]:
13201 30912 46368
⎛ ⎞

A^6 = ⎜ 15456 36193 54288 ⎟


⎝ ⎠
15456 36192 54289

In [146… X=ones_matrix(5,6)

show ('X=',X)

Out[146]:
1 1 1 1 1 1
⎛ ⎞


1 1 1 1 1 1 ⎟



X=

1 1 1 1 1 1 ⎟


⎜1 1 1 1 1 1⎟
⎝ ⎠
1 1 1 1 1 1

In [134… X[2,3]=100

Out[134]: [ 1 1 1 1 1 1]

[ 1 1 1 1 1 1]

[ 1 1 1 100 1 1]

[ 1 1 1 1 1 1]

[ 1 1 1 1 1 1]

In [145… X[2:4]=222

Out[145]: [ 1 1 1 1 1 1]

[ 1 1 1 1 1 1]

[222 222 222 222 222 222]

[222 222 222 222 222 222]

[ 1 1 1 1 1 1]

In [131… X[3:4]=666

Out[131]: [ 1 1 1 1 1 1]

[ 1 1 1 1 1 1]

[222 222 222 222 222 222]

[666 666 666 666 666 666]

[ 1 1 1 1 1 1]

In [125… Y=ones_matrix(5,6)

show ('Y=',Y)

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 7/14


3/24/22, 7:54 PM exp 6

Out[125]: ⎛
1 1 1 1 1 1


1 1 1 1 1 1 ⎟



Y=

1 1 1 1 1 1 ⎟


⎜ ⎟
1 1 1 1 1 1
⎝ ⎠
1 1 1 1 1 1

In [126… Y[2:3]=1

Out[126]: [1 1 1 1 1 1]

[1 1 1 1 1 1]

[1 1 1 1 1 1]

[1 1 1 1 1 1]

[1 1 1 1 1 1]

In [127… Y[2,4]=100

In [67]: Y

Out[67]: [ 1 1 1 1 1 1]

[ 1 1 1 1 1 1]

[ 1 1 1 1 100 1]

[ 1 1 1 1 1 1]

[ 1 1 1 1 1 1]

In [68]: zero_matrix(3,2)

Out[68]: [0 0]

[0 0]

[0 0]

In [69]: identity_matrix(2)

Out[69]: [1 0]

[0 1]

In [70]: random_matrix(ZZ,2,3, x=50 , y=60)

Out[70]: [57 50 53]

[55 50 59]

In [71]: T=matrix(3,3,[1,2,3,4,5,6,7,8,9])

show(T)

Out[71]:
1 2 3
⎛ ⎞

⎜4 5 6⎟
⎝ ⎠
7 8 9

In [72]: T[0] # First Row of T

T[1] # Second row of T

T[2] # Third row of T

T[0,1] # First row second column element

T[2,2] # Third row third column element

Out[72]: 9

In [73]: T[:,1] # Second column of T

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 8/14


3/24/22, 7:54 PM exp 6

Out[73]: [2]

[5]

[8]

In [74]: T[0,1]=100 # Relace first row second column element by 100

Out[74]: [ 1 100 3]

[ 4 5 6]

[ 7 8 9]

System of equations

Solve the following system of equations

1. x − 2y + 3z = 2; 2x − 3z = 3; x + y + z = 0

2.
2x − y + z = 4; 3x − y + z = 6; 4x − y + 2z = 7; −x + y − z =

3. 3x + y + z = 2; x − 3y + 2z = 1; 7x − y + 4z = 5

solution 1:

In [77]: x, y, z = var('x, y, z')

show(*solve([x-2*y+3*z == 2, 2*x-3*z == 3,x+y+z == 0], x, y, z))

Out[77]:
21 16 5
[x = ( ) , y = (− ) , z = (− )]
19 19 19

solution 2:

In [210… x, y, z = var('x, y, z')

show(*solve([2*x-y+z == 4, 3*x-y+z == 6,4*x-y+2*z == 7,], x, y, z))

Out[210]:
[x = 2, y = (−1) , z = (−1)]

solution 3:

In [209… x, y, z = var('x, y, z')

show(*solve([3*x+y+z == 2, x-3*y+2*z == 1,7*x-y+4*z == 5], x, y, z))

Out[209]:
1 7 1 1
[x = − r3 + ,y = r3 − , z = r3 ]
2 10 2 10

In [205… A=matrix([[1,-2,3], [2,0,-3] ,[1,1,1]])

B=vector([2,3,0])

show('A= ', A , 'B= ', B.column())

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 9/14


3/24/22, 7:54 PM exp 6

Out[205]: ⎛
1 −2 3
⎞ ⎛
2

A= ⎜ 2 0 −3 ⎟ B= ⎜ 3 ⎟
⎝ ⎠ ⎝ ⎠
1 1 1 0

In [79]: C=A.augment(B)

show('C=',C)

Out[79]:
1 −2 3 2
⎛ ⎞

C= ⎜ 2 0 −3 3⎟
⎝ ⎠
1 1 1 0

In [80]: rank(A)==rank(C)

Out[80]: True

In [81]: solve([2*x-y+z == 4, 3*x-y+z == 6,4*x-y+2*z == 7 , -x+y-z==9], x, y, z)

Out[81]: []

In [82]: A=matrix(4,3,[2,-1,1,3,-1,1,4,-1,2,-1,1,-1])

B=vector([4,6,7,9])

show('A= ', A , 'B= ', B.column())

Out[82]:
2 −1 1 4
⎛ ⎞ ⎛ ⎞


3 −1 1 ⎟

6 ⎟

A= ⎜

B= ⎜

⎜ 4 −1 2⎟ ⎜7⎟

⎝ ⎠ ⎝ ⎠
−1 1 −1 9

In [83]: C=A.augment(B)

show('C=',C)

Out[83]:
2 −1 1 4
⎛ ⎞


3 −1 1 6 ⎟

C= ⎜

⎜ 4 −1 2 7⎟
⎝ ⎠
−1 1 −1 9

In [84]: rank(A)==rank(C)

Out[84]: False

In [85]: show(C.echelon_form())

Out[85]:
1 0 0 2
⎛ ⎞


0 1 0 10 ⎟


⎜0 0 1 10 ⎟
⎝ ⎠
0 0 0 11

In [87]: show(solve([3*x+y+z == 2, x-3*y+2*z == 1,7*x-y+4*z == 5], x, y, z))

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 10/14


3/24/22, 7:54 PM exp 6

1 7 1 1
Out[87]: [[x = − r2 + ,y = r2 − , z = r2 ]]
2 10 2 10

Check if the following set of vectors are linearly


dependent
(1, −1, 2, 4), (−3, 3, 2, 1), (−1, −2, 6, 9)

In [88]: U=matrix(3,4,[1,-1,2,4,-3,3,2,1,-1,-2,6,9])

In [89]: U.transpose().echelon_form()

Out[89]: [1 0 2]

[0 1 1]

[0 0 3]

[0 0 0]

In [90]: U=matrix(3,4,[2,6,0,-11,6,20,-6,0,6,-18,-1,-3])

In [91]: show(U.transpose().echelon_form())

Out[91]:
1 0 0
⎛ ⎞


0 2 0 ⎟


⎜0 0 1⎟
⎝ ⎠
0 0 0

Eigenvalues and Eigenvectors

Ex. Consider the matrix A =


( −2 1 1  − 6 1 3  − 12 −2 8)

(i) Find the characteristics polynolmial, p(x), of A. (ii) Find the


roots of p(x), show that

eigenvalues of A are roots of p(x). (iii) Find the eigenvectors of


A. (iv) Show that sum of the

eigenvalues of A is trace of A. (iv) Show that the product of


eigenvalues of A is the determinant

of A
In [92]: A=matrix(3,3,[-2,1,1,-6,1,3,-12,-2,8])

p(x)=A.characteristic_polynomial ( x )

show('p(x)=',p(x))

Out[92]: 3 2
p(x)=x − 7x + 14 x − 8

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 11/14


3/24/22, 7:54 PM exp 6

In [93]: p(x).roots()

Out[93]: [(2, 1), (4, 1), (1, 1)]

In [94]: E= A . eigenvalues ()

show(E)

Out[94]:
[4, 2, 1]

In [95]: V= A . eigenvectors_right ()

show('V=',V)

Out[95]:
3 5
V= [(4, [(1, 2, 4)] , 1) , (2, [(1, , )] , 1) , (1, [(1, 1, 2)] , 1)]
2 2

In [96]: sum(E)==A.trace()

Out[96]: True

In [97]: product(E)==det(A)

Out[97]: True

In [98]: det(A)

Out[98]: 8

In [99]: D,M=A.eigenmatrix_right() # D= Diagonal matrix and M= Modal matrix (i.e. matrix obt
show('D=',D, 'M=',M)

Out[99]:
1 1 1
4 0 0 ⎛ ⎞
⎛ ⎞
3

2 1 ⎟

D= ⎜ 0 2 0 ⎟ M= ⎜ 2 ⎟
⎝ ⎠ 5
0 0 1 ⎝4 2⎠
2

In [100… B=matrix(2,2,[cos(x) , sin(x), -sin(x) , cos(x)])

show('B=',B)

Out[100]:
cos(x) sin(x)
B= ( )
− sin(x) cos(x)

In [101… D,M=B.eigenmatrix_right() # D= Diagonal matrix and M= Modal matrix (i.e. matrix obt
show('D=',D, 'M=',M)

Out[101]:
cos(x) − i sin(x) 0 1 1
D= ( ) M= ( )
0 cos(x) + i sin(x) −i i

Verify the Cayley-Hamilton theorem for A.


A = (2 −1 1  − 1 2 −1 1 −1 2)

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 12/14


3/24/22, 7:54 PM exp 6

In [102… A=matrix(4,4,[1,0,-1,1,2,1,2,2,3,2,1,3,5,2,3,4])

p=A.characteristic_polynomial ( x )

show('p(x)=',p)

Out[102]: 4 3 2
p(x)=x − 7x − 4x + 10x + 4

In [103… Coff=p.coefficients()

show(Coff)

Out[103]:
[4, 10, −4, −7, 1]

In [104… show('p(A)=',sum ([ Coff[k]* A ^(k) for k in range (4) ]))

print('Therefore A satisfies characteristic equation p(x)=0')

Out[104]:
−110 −42 −42 −96
⎛ ⎞


−1124 −449 −432 −990 ⎟

p(A)= ⎜

⎜ −1386 −552 −534 −1220 ⎟


⎝ ⎠
−2076 −828 −796 −1828

Therefore A satisfies characteristic equation p(x)=0

Exercise 2

Ex.2.1) Consider the matrix A = ( 2 −2 2 1 1 1 1 3 −1 )

(i) Find the characteristics polynolmial, p(x), of A. (ii) Find the roots of p(x),
show that

eigenvalues of A are roots of p(x). (iii) Find the eigenvectors of A. (iv) Show
that sum of the

eigenvalues of A is trace of A. (iv) Show that the product of eigenvalues of


A is the determinant

of A.

Ex. 2.2) Show that the matrix ( cos t sin t  − sin t cos t )

satisfies its characteristic equation.

SOLUTION 1:

In [192… A=matrix(3,3,[2,-2,2,1,1,1,1,3,-1])

p=A.characteristic_polynomial ( x )

show('p(x)=',p)

Out[192]: 3 2
p(x)=x − 2x − 4x + 8

In [194… p(x).roots()

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 13/14


3/24/22, 7:54 PM exp 6

Out[194]: [(-2, 1), (2, 2)]

In [195… E= A . eigenvalues ()

show(E)

Out[195]:
[−2, 2, 2]

In [196… V= A . eigenvectors_right ()

show('V=',V)

Out[196]:
1 7
V= [(−2, [(1, , − )] , 1) , (2, [(0, 1, 1)] , 2)]
4 4

In [197… sum(E)==A.trace()

Out[197]: True

In [198… product(E)==det(A)

Out[198]: True

In [199… det(A)

Out[199]: -8

SOLUTION 2:

In [188… A=matrix(2,2,[cos(t),sin(t),-sin(t),cos(t)])

p=A.characteristic_polynomial ( x )

show('p(x)=',p)

Out[188]: 2 2 2
p(x)=x − 2 cos(t)x + cos (t) + sin (t)

In [189… Coff=p.coefficients()

show(Coff)

Out[189]:
2 2
[cos (t) + sin (t) , −2 cos(t), 1]

In [191… show('p(A)=',sum ([ Coff[k]* A ^(k) for k in range (2) ]))

print('Therefore A satisfies characteristic equation p(x)=0')

Out[191]: 2 2
− cos (t) + sin (t) −2 cos(t) sin(t)
p(A)= ( )
2 2
2 cos(t) sin(t) − cos (t) + sin (t)

Therefore A satisfies characteristic equation p(x)=0

file:///C:/Users/VEDANT PULLIWAR/Downloads/exp 6 (1).html 14/14

You might also like