Professional Documents
Culture Documents
Department of Mathematics
Experiment No. 5
ROLL NO : 85
BATCH: M3
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
Out[12]: ⎛
−1 2
⎞
⎜ 3 4⎟
⎝ ⎠
5 0
Out[13]:
1.00000000000000 2.00000000000000
⎛ ⎞
⎜ 3.00000000000000 4.00000000000000 ⎟
⎝ ⎠
5.00000000000000 6.00000000000000
Out[14]:
0.500000000000000 2.00000000000000
⎛ ⎞
⎜ 3.00000000000000 4.00000000000000 ⎟
⎝ ⎠
0.833333333333333 6.00000000000000
Out[15]:
1.0 + 1.0i 2.0
⎛ ⎞
⎜ 3.0 4.0 ⎟
⎝ ⎠
5.0 − 1.0i 6.0 + 2.0i
Out[16]: 5
1
⎛ 2
⎞
⎜
22 ⎟
⎜
4 ⎟
7
⎜ ⎟
6
⎝ 5 ⎠
7
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])
Out[21]:
9 7 11 7 3 1
P+Q =( ) P-Q =( )
12 13 7 6 1 −1
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])
Out[27]:
−8 5 −56313 94005
R= ( ) R^5 =( )
1 −7 18801 −37512
Out[28]: True
Out[29]: 51
Out[30]: [-8 1]
[ 5 -7]
[-1/51 -8/51]
In [32]: R.adjoint()
[-1 -8]
[-1 -8]
In [35]: R.adjugate()==(R.det())*(R.inverse())
Out[35]: True
[ 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())
⎛ ( −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))
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
Out[54]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Out[55]: [-3, -6, -3, -6, -12, -6, -3, -6, -3]
Out[56]: [0]
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)
determinent ,
adjoint , inverse and all minors of order 1,2 & 3. (ii) A+B , A-B ,
AB, A . 6
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]
show('A-B =',A-B)
show('AB =',A*B)
Out[171]:
6 4 6
⎛ ⎞
(ii) A+B =⎜ 8 6 7⎟
⎝ ⎠
10 6 9
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
⎛ ⎞
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]
[ 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]
[ 1 1 1 1 1 1]
In [125… Y=ones_matrix(5,6)
show ('Y=',Y)
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]
[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
Out[72]: 9
Out[73]: [2]
[5]
[8]
Out[74]: [ 1 100 3]
[ 4 5 6]
[ 7 8 9]
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:
Out[77]:
21 16 5
[x = ( ) , y = (− ) , z = (− )]
19 19 19
solution 2:
Out[210]:
[x = 2, y = (−1) , z = (−1)]
solution 3:
Out[209]:
1 7 1 1
[x = − r3 + ,y = r3 − , z = r3 ]
2 10 2 10
B=vector([2,3,0])
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
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])
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
1 7 1 1
Out[87]: [[x = − r2 + ,y = r2 − , z = r2 ]]
2 10 2 10
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
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
In [93]: p(x).roots()
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
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
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]
Out[104]:
−110 −42 −42 −96
⎛ ⎞
⎜
−1124 −449 −432 −990 ⎟
p(A)= ⎜
⎟
Exercise 2
(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
of A.
Ex. 2.2) Show that the matrix ( cos t sin t − sin t cos t )
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()
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]
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)