You are on page 1of 1

Shri Ramdeobaba College of Engineering and

Management Nagpur}

Department of Mathematics

Computational Mathematics Lab

Expriment Number 5

Aim : Basic of Linear Algebra

Name- Tarika Jaikalyani

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

In [3]: show(matrix(ZZ, 3,2, [1,2,3,4,5,6])) # ZZ (Integer Field) defines all en


tries in matrix is an integer

1 2
⎛ ⎞
⎜3 4⎟
⎝ ⎠
5 6

In [4]: show(matrix(RR, 3,2, [1,2,3,4,5,6])) # RR (Real Field) defines all entri


es in matrix is a real number

1.00000000000000 2.00000000000000
⎛ ⎞

⎜ 3.00000000000000 4.00000000000000 ⎟
⎝ ⎠
5.00000000000000 6.00000000000000

In [5]: show(matrix(CDF, 3,2, [1,2,3,4,5,6])) # CDF (Complex Dence Field) define


s all entries in matrix is a complex number

1.0 2.0
⎛ ⎞

⎜ 3.0 4.0 ⎟
⎝ ⎠
5.0 6.0

In [6]: show(matrix(QQ, 3,2, [1,2,3,4,5,6])) # QQ (Rational Field) defines all e


ntries in matrix is a rational number

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 , 'P-Q =', P-Q)

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])

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

−8 5 −56313 94005
R= ( ) R^5 =( )
1 −7 18801 −37512

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

Out[9]: True

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

Out[10]: 51

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

Out[11]: [-8 1]

[ 5 -7]

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

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.

See http://trac.sagemath.org/10501 for details.


from sage.repl.ipython_kernel.kernel import SageKernel

Out[12]: [-7 -5]

[-1 -8]

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

Out[13]: [-7 -5]

[-1 -8]

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

Out[14]: True

In [15]: 3*R # Scalar Multiplication

Out[15]: [-24 15]

[ 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

|S|=(xz + y)y − (yz − z)z + xz + 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())

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


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

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

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

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

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

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

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

Out[24]: [0]

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

Out[25]: True

In [26]: A.det()

Out[26]: 0

Exercise 1

1. Find x, y, z and t if 2 ( x z y t ) + 3( 1 −1 0 0 ) = 3( 3 5 4 6)

2.Let A = ( 1 2 3 1 3 3 1 2 4 ) and B = ( 5 2 3 7 3 4 9 4 5 ) .


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

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)

13201 30912 46368


⎛ ⎞
R^6= ⎜ 15456 36193 54288 ⎟
⎝ ⎠
15456 36192 54289

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.

See http://trac.sagemath.org/10501 for details.


from ipykernel.kernelapp import IPKernelApp

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.

See http://trac.sagemath.org/10501 for details.


from ipykernel.kernelapp import IPKernelApp

Out[81]: [-1 2 -1]

[ 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)

Out[83]: [1, 0, -3, 0, 1, 2, -1, 1, 6]

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)

Out[86]: [1, -1, -1, 2, -2, -2, 1, -1, -1]

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]

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

Out[36]: [52 53 54]

[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

In [41]: 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[41]: 9

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

Out[42]: [2]

[5]

[8]

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

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 = 9 3.

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

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

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

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

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

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

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

In [47]: 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[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])

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

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 [52]: solve([3*x+y+z == 2, x-3*y+2*z == 1,7*x-y+4*z == 5], x, y, z)

Out[52]: [[x == -1/2*r1 + 7/10, y == 1/2*r1 - 1/10, z == r1]]

Check if the following set of vectors are linearly dependent?

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

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

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 [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)

In [63]: B=matrix(2,2,[cos(x) , sin(x), -sin(x) , cos(x)])

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)

Verify the Cayley-Hamilton theorem for A.

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)

[4, 10, −4, −7, 1]

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

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

−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.

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

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

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 [ ]:

You might also like