You are on page 1of 11

Student Name: Pawar Sakshi Sanjay

Class: S.Y.BCS
Roll number: 5039 batch :B
Practical Number :5 .
Practical Name:-Application of Matrices.
Q 1 Create row & column vector.
(i)
from sympy import*
Matrix([[1,2,3]])
#Output: Matrix([[1, 2, 3]])

(ii)
from sympy import*
Matrix([[1],[2],[3]])
#Output :-Matrix([
[1],
[2],
[3]])

Q.2 Find i) u+v ii) u-v iii)3u iv)2u+3v


from sympy import*
u=Matrix([[2],[5],[-3]])
v=Matrix([[1],[0],[-2]])
u+v
#Output:-Matrix([
[ 3],
[ 5],
[-5]])

u-v
#Output:-Matrix([
[ 1],
[ 5],
[-1]])

3*u
#Output:-Matrix([
[ 6],
[15],
[-9]])

2*u+3*v
#Output:-Matrix([
[ 7],
[ 10],
[-12]])

Q.3 Find order of a matrix.


from sympy import*
A=Matrix([[1,2,3],[4,5,6],[7,8,9]])
A.shape
#Output:-(3, 3)

Q.4 Construct the following matrices :


1. Identity matrix of order 3 & 4
2. Zero matrix of order 2 x 3
3. Ones matrix of order 2 x 3
4. Digonal matrix
from sympy import*
eye(3)
#Output:-Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])

zeros(2,3)
#Output:-Matrix([
[0, 0, 0],
[0, 0, 0]])

ones(2,3)
#Output:-Matrix([
[1, 1, 1],
[1, 1, 1]])

diag(1,2,3)
#Output:-Matrix([
[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
Q.5 Find i) A+B ii) A-B iii) A*A iv) A*B v) A*V
vi) A**3 vii) B*A viii) B-1AB

from sympy import*


A=Matrix([[1,2,3],[4,5,6],[7,8,9]])
B=Matrix([[4,6,-3],[-4,1,6],[0,8,-9]])
V=Matrix([[1],[5],[6]])
A+B
#Output:-Matrix([
[5, 8, 0],
[0, 6, 12],
[7, 16, 0]])

A-B
#Output:-Matrix([
[-3, -4, 6],
[ 8, 4, 0],
[ 7, 0, 18]])

A*A
#Output:-Matrix([
[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])

A*B
#Output:-Matrix([
[-4, 32, -18],
[-4, 77, -36],
[-4, 122, -54]])

A*V
#Output:-Matrix([
[ 29],
[ 65],
[101]])

A**3
#Output:-Matrix([
[ 468, 576, 684],
[1062, 1305, 1548],
[1656, 2034, 2412]])
B*A
#Output:-Matrix([
[ 7, 14, 21],
[ 42, 45, 48],
[-31, -32, -33]])

B.inv()*A*B
#Output:-Matrix([
[ 4/29, -437/29, 180/29],
[-28/29, 449/29, -216/29],
[-12/29, 6/29, -18/29]])
Q.6 Find inverse of matrix.
from sympy import*
A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
B=Matrix([[1,1,1],[0,1,1],[0,0,1]])
A.inv()
#Output:-Matrix([
[ 3/4, -1/4, -1/4],
[-1/4, 3/4, -1/4],
[-1/4, -1/4, 3/4]])
B.inv()
#Output:-Matrix([
[1, -1, 0],
[0, 1, -1],
[0, 0, 1]])
Q.7 Print
i) 1st row of A & 2nd row of B
ii) 2nd column of A & 1st column of A
from sympy import*
A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
B=Matrix([[1,1,1],[0,1,1],[0,0,1]])
A.row(0)
#Output:-Matrix([[2, 1, 1]])

B.row(2)
#Output:-Matrix([[0, 0, 1]])

A.col(1)
#Output:-Matrix([
[1],
[2],
[1]])
print(A)
#Output:-Matrix([[2, 1, 1], [1, 2, 1], [1, 1, 2]])
Matrix([[2, 1, 1], [1, 2, 1], [1, 1, 2]])
#Output:-Matrix([
[2, 1, 1],
[1, 2, 1],
[1, 1, 2]])

A.col(-2)
#Output:-Matrix([
[1],
[2],
[1]])

Q.8 For matrix


i) Delete 1st row of A
ii) Delete last column of A
from sympy import*
A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
A.row_del(1)
print(A)
#Output:-Matrix([[2, 1, 1], [1, 1, 2]])

A.col_del(-1)
Print(A)
Output:-Matrix([
[2, 1],
[1, 1]])

Q.9 Insert row at 2nd place& column at 1st place


from sympy import*
A=Matrix([[2,1,1],[1,2,1],[1,1,2]])
A=A.row_insert(1,Matrix([[0,5,4]]))
Print(A)
#Output:-Matrix([
[2, 1, 1],
[0, 5, 4],
[1, 2, 1],
[1, 1, 2]])
A=A.col_insert(0,Matrix([[0],[5],[4],[6]]))
Print(A)
#Output:-Matrix([
[0, 2, 1, 1],
[5, 0, 5, 4],
[4, 1, 2, 1],
[6, 1, 1, 2]])

You might also like