Professional Documents
Culture Documents
Management Nagpur}
Department of Mathematics
Expriment Number 5
Section-N3
Roll No-65
Matrix Algebra
Define a Matrix
Defining 3 by 3 matrix
In [1]: A=matrix([
[1,2,3],
[4,5,6],
[7,8,9]
])
show('A=',A)
1 2 3
⎛ ⎞
A= ⎜ 4 5 6⎟
⎝ ⎠
7 8 9
In [2]: show(matrix(3,3,[1,2,3,4,5,6,7,8,9]))
1 2 3
⎛ ⎞
⎜4 5 6⎟
⎝ ⎠
7 8 9
1 2
⎛ ⎞
⎜3 4⎟
⎝ ⎠
5 6
1.00000000000000 2.00000000000000
⎛ ⎞
⎜ 3.00000000000000 4.00000000000000 ⎟
⎝ ⎠
5.00000000000000 6.00000000000000
1.0 2.0
⎛ ⎞
⎜ 3.0 4.0 ⎟
⎝ ⎠
5.0 6.0
1 2
⎛ ⎞
⎜3 4⎟
⎝ ⎠
5 6
Matrix Operations
In [7]: 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.transpose())
9 7 11 7 3 1
P+Q =( ) P-Q =( )
12 13 7 6 1 −1
48 78
P Q =( )
38 81
In [8]: R=matrix(2,2,[-8,5,1,-7])
−8 5 −56313 94005
R= ( ) R^5 =( )
1 −7 18801 −37512
Out[9]: True
Out[10]: 51
Out[11]: [-8 1]
[ 5 -7]
R.adjoint()
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/repl/ipython_ke
rnel/__main__.py:2: DeprecationWarning: adjoint is deprecated. Please us
e adjugate instead.
[-1 -8]
[-1 -8]
In [14]: R.adjugate()==(R.det())*(R.inverse())
Out[14]: True
[ 3 -21]
In [16]: R.trace()
Out[16]: -15
In [17]: var('x,y,z')
S=matrix(3,3,[1,z,-y,-z,1,x,y,-z,1])
show('S=',S)
show('|S|=',S.det())
1 z −y
⎛ ⎞
S= ⎜ −z 1 x⎟
⎝ ⎠
y −z 1
In [18]: 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()
x+y x x
⎛ ⎞
S= ⎜ 5x+4y 4x 2x⎟
⎝ ⎠
10 x + 8 y 8x 3x
2 2
|S|=(5 x + 4 y)x − 4 (x + y)x
Out[18]: x^3
In [19]: show(S.inverse())
2 ⎜ − ⎟
(5 x+4 y)x x+y (5 x+4 y)
⎜ ⎝ ( −4 x)(x+y) ⎠ (
⎜ 1 (5 x+4 y)x x+y x x+y
⎜ − + −
x+y (5 x+4 y)x x (5 x+4 y)x
⎜ ( −4 x)(x+y)
2
( −4 x)(x+y)
⎜ x+y x+y
⎜ (5 x+4 y)x
⎜ 2 ( −2 x)
⎜ 5 x+4 y 1 x+y
⎜ − −
(5 x+4 y)x (5 x+4 y)x (5 x+4 y)x
⎜ ( −4 x)(x+y) −4 x ( −4 x)x (
⎜ x+y x+y x+y
⎝ 2
0
x
In [20]: show(diff(S,x))
show(diff(S,y))
1 1 1
⎛ ⎞
⎜ 5 4 2⎟
⎝ ⎠
10 8 3
1 0 0
⎛ ⎞
⎜4 0 0⎟
⎝ ⎠
8 0 0
In [21]: show('A=',A)
1 2 3
⎛ ⎞
A= ⎜ 4 5 6⎟
⎝ ⎠
7 8 9
Out[22]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Out[23]: [-3, -6, -3, -6, -12, -6, -3, -6, -3]
Out[24]: [0]
Out[25]: True
In [26]: A.det()
Out[26]: 0
Exercise 1
In [75]: A=matrix(3,3,
[1,2,3,1,3,3,1,2,4])
B=matrix(3,3,
[5,2,3,7,3,4,9,4,5])
show('A+B=', A+B,'A-B=',A-B)
show('AB=',A*B)
6 4 6 −4 0 0
⎛ ⎞ ⎛ ⎞
A+B= ⎜ 8 6 7 ⎟ A-B= ⎜ −6 0 −1 ⎟
⎝ ⎠ ⎝ ⎠
10 6 9 −8 −2 −1
46 20 26
⎛ ⎞
AB= ⎜ 53 23 30 ⎟
⎝ ⎠
55 24 31
In [76]: R=matrix(3,3,
[1,2,3,1,3,3,1,2,4])
show('R^6=',R^6)
In [77]: A.det()
B.det()
Out[77]: 0
In [78]: A.transpose()
B.transpose()
Out[78]: [5 7 9]
[2 3 4]
[3 4 5]
In [79]: A.inverse()
Out[79]: [ 6 -2 -3]
[-1 1 0]
[-1 0 1]
In [80]: A.adjoint()
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/repl/ipython_ke
rnel/__main__.py:1: DeprecationWarning: adjoint is deprecated. Please us
e adjugate instead.
Out[80]: [ 6 -2 -3]
[-1 1 0]
[-1 0 1]
In [81]: B.adjoint()
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/repl/ipython_ke
rnel/__main__.py:1: DeprecationWarning: adjoint is deprecated. Please us
e adjugate instead.
[ 1 -2 1]
[ 1 -2 1]
In [82]: A.minors(1)
Out[82]: [1, 2, 3, 1, 3, 3, 1, 2, 4]
In [83]: A.minors(2)
In [84]: A.minors(3)
Out[84]: [1]
In [85]: B.minors(1)
Out[85]: [5, 2, 3, 7, 3, 4, 9, 4, 5]
In [86]: B.minors(2)
In [87]: B.minors(3)
Out[87]: [0]
In [28]: X=ones_matrix(5,6)
show('X=',X)
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 [29]: X[2,3]=100
Out[29]: [ 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 [30]: X[2:2]=666
Out[30]: [ 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 [31]: Y=ones_matrix(5,6)
show('Y=',Y)
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 [32]: Y[2:3]=1
Out[32]: [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 [33]: Y[2,4]=100
Out[33]: [ 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 [34]: zero_matrix(3,2)
Out[34]: [0 0]
[0 0]
[0 0]
In [35]: identity_matrix(2)
Out[35]: [1 0]
[0 1]
[53 50 54]
In [40]: T=matrix(3,3,[1,2,3,4,5,6,7,8,9])
show(T)
1 2 3
⎛ ⎞
⎜4 5 6⎟
⎝ ⎠
7 8 9
Out[41]: 9
Out[42]: [2]
[5]
[8]
2x − y + z = 4; 3x − y + z = 6; 4x − y + 2z = 7; −x + y − z = 9 3.
3x + y + z = 2; x − 3y + 2z = 1; 7x − y + 4z = 5
B=vector([2,3,0])
1 −2 3 2
⎛ ⎞ ⎛ ⎞
A= ⎜ 2 0 −3 ⎟ B= ⎜ 3 ⎟
⎝ ⎠ ⎝ ⎠
1 1 1 0
In [45]: C=A.augment(B)
show('C=',C)
1 −2 3 2
⎛ ⎞
C= ⎜ 2 0 −3 3⎟
⎝ ⎠
1 1 1 0
In [46]: rank(A)==rank(C)
Out[46]: True
Out[47]: []
In [48]: A=matrix(4,3,[2,-1,1,3,-1,1,4,-1,2,-1,1,-1])
B=vector([4,6,7,9])
2 −1 1 4
⎛ ⎞ ⎛ ⎞
⎜ 3 −1 1⎟ ⎜6⎟
A= ⎜ ⎟ B= ⎜ ⎟
⎜ 4 −1 2⎟ ⎜7⎟
⎝ ⎠ ⎝ ⎠
−1 1 −1 9
In [49]: C=A.augment(B)
show('C=',C)
2 −1 1 4
⎛ ⎞
⎜ 3 −1 1 6⎟
C= ⎜ ⎟
⎜ 4 −1 2 7⎟
⎝ ⎠
−1 1 −1 9
In [50]: rank(A)==rank(C)
Out[50]: False
In [51]: show(C.echelon_form())
1 0 0 2
⎛ ⎞
⎜0 1 0 10 ⎟
⎜ ⎟
⎜0 0 1 10 ⎟
⎝ ⎠
0 0 0 11
In [53]: U=matrix(3,4,[1,-1,2,4,-3,3,2,1,-1,-2,6,9])
In [54]: U.transpose().echelon_form()
Out[54]: [1 0 2]
[0 1 1]
[0 0 3]
[0 0 0]
In [55]: U=matrix(3,4,[2,6,0,-11,6,20,-6,0,6,-18,-1,-3])
In [56]: show(U.transpose().echelon_form())
1 0 0
⎛ ⎞
⎜0 2 0⎟
⎜ ⎟
⎜0 0 1⎟
⎝ ⎠
0 0 0
(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 [57]: A=matrix(3,3,[-2,1,1,-6,1,3,-12,-2,8])
p(x)=A.characteristic_polynomial ( x )
show('p(x)=',p(x))
3 2
p(x)=x −7x + 14 x − 8
In [58]: p(x).roots()
E= A . eigenvalues ()
show(E)
[4, 2, 1]
In [59]: V= A . eigenvectors_right ()
show('V=',V)
3 5
V= [(4, [(1, 2, 4)] , 1) , (2, [(1, , )] , 1) , (1, [(1, 1, 2)] , 1)]
2 2
In [60]: sum(E)==A.trace()
Out[60]: True
In [61]: product(E)==det(A)
Out[61]: True
In [62]: det(A)
Out[62]: 8
D,M=A.eigenmatrix_right() # D= Diagonal matrix and M= Modal matrix (i.e. matrix obtained from
eigenvectors) show('D=',D, 'M=',M)
show('B=',B)
cos(x) sin(x)
B= ( )
− sin(x) cos(x)
D,M=B.eigenmatrix_right() # D= Diagonal matrix and M= Modal matrix (i.e. matrix obtained from
eigenvectors) show('D=',D, 'M=',M)
A = (2 −1 1 − 1 2 −1 1 −1 2)
In [64]: 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)
4 3 2
p(x)=x − 7x − 4x + 10x + 4
In [65]: Coff=p.coefficients()
show(Coff)
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.
In [67]: A=matrix(3,3,[2,-2,2,1,1,1,1,3,-1])
p=A.characteristic_polynomial ( x )
show('p(x)=',p)
3 2
p(x)=x − 2x − 4x + 8
In [68]: p(x).roots()
In [69]: E=A.eigenvalues()
show(E)
[−2, 2, 2]
In [70]: v=A.eigenvectors_right()
show('v=',v)
1 7
v= [(−2, [(1, , − )] , 1) , (2, [(0, 1, 1)] , 2)]
4 4
In [71]: sum(E)==A.trace
Out[71]: False
In [72]: product(E)==det(A)
Out[72]: True
In [73]: det(A)
Out[73]: -8
In [74]: D,M=A.eigenmatrix_right()
show('D=',D,'M=',M)
1 0 0
−2 0 0 ⎛ ⎞
⎛ ⎞
1
D= ⎜ 0 2 0 ⎟ M= ⎜
⎜ 4
1 0⎟
⎟
⎝ ⎠ 7
0 0 2 ⎝ ⎠
− 1 0
4
In [ ]: