0% found this document useful (0 votes)
3 views

Lab1.ipynb - Colab

The document is a Jupyter notebook containing Python code that demonstrates various matrix operations using NumPy, including addition, subtraction, scalar multiplication, dot product, determinant calculation, and matrix inversion. It also includes solving systems of equations using symbolic mathematics with SymPy. The notebook provides outputs for each operation, showcasing the results of the computations performed.

Uploaded by

Haniya Shahbaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab1.ipynb - Colab

The document is a Jupyter notebook containing Python code that demonstrates various matrix operations using NumPy, including addition, subtraction, scalar multiplication, dot product, determinant calculation, and matrix inversion. It also includes solving systems of equations using symbolic mathematics with SymPy. The notebook provides outputs for each operation, showcasing the results of the computations performed.

Uploaded by

Haniya Shahbaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

11/15/24, 12:56 PM Lab1.

ipynb - Colab

a=3
b=4
c=a+b
print(c)

import numpy as np
A=np.array([[2,3],[4,5]])
B=np.array([[1,0],[0,1]])
print(A)
print(B)

[[2 3]
[4 5]]
[[1 0]
[0 1]]

print(A+B)

[[3 3]
[4 6]]

print(A-B)

[[1 3]
[4 4]]

scalar=3
print(scalar*A)

[[ 6 9]
[12 15]]

np.dot(A,B)

array([[2, 3],
[4, 5]])

np.linalg.det(A)

-2.0

D=np.linalg.det(A)

if D==0:
print("Not Possible")
else:
I=np.linalg.inv(A)
print(I)

[[-2.5 1.5]
[ 2. -1. ]]

A.T

array([[2, 4],
[3, 5]])

A=np.array([[4,-1],[2,3]])
B=np.array([[0,5],[-3,2]])
print(A)
print(B)

[[ 4 -1]
[ 2 3]]
[[ 0 5]
[-3 2]]

https://colab.research.google.com/drive/10CrN6Cf5tJRWkCKkj8NZk13qesgK4e02#scrollTo=Vk8bXrpYvydE 1/5
11/15/24, 12:56 PM Lab1.ipynb - Colab
print(A+B)

[[ 4 4]
[-1 5]]

print(A-B)

[[ 4 -6]
[ 5 1]]

scalar=3
print(scalar*A)

[[12 -3]
[ 6 9]]

np.dot(A,B)

array([[ 3, 18],
[-9, 16]])

A.T

array([[ 4, 2],
[-1, 3]])

np.linalg.det(A)

14.000000000000004

D=np.linalg.det(A)

if D==0:
print("Not Possible")
else:
I=np.linalg.inv(A)
print(A)

[[ 4 -1]
[ 2 3]]

A=np.array([[4,-1],[2,3]])
B=np.array([[0,5],[-3,2]])
C=np.array([[1,-2],[3,4]])
print(A)
print(B)
print(C)

[[ 4 -1]
[ 2 3]]
[[ 0 5]
[-3 2]]
[[ 1 -2]
[ 3 4]]

print(2*A+3*B)

[[ 8 13]
[-5 12]]

print((2*A+3*B)*C)

[[ 8 -26]
[-15 48]]

D=(2*A+3*B)*C
print(D.T)

[[ 8 -15]
[-26 48]]

D=np.array([[3,6],[-2,5]])
E=np.array([[4,-3],[1,2]])
print(D)
print(E)

https://colab.research.google.com/drive/10CrN6Cf5tJRWkCKkj8NZk13qesgK4e02#scrollTo=Vk8bXrpYvydE 2/5
11/15/24, 12:56 PM Lab1.ipynb - Colab

[[ 3 6]
[-2 5]]
[[ 4 -3]
[ 1 2]]

print(np.linalg.det(D))
print(np.linalg.det(E))

27.0
11.000000000000002

np.linalg.inv(D)

array([[ 0.18518519, -0.22222222],


[ 0.07407407, 0.11111111]])

Z=np.linalg.det(D)

if Z==0:
print("Not Possible")
else:
I=np.linalg.inv(D)
print(I)

[[ 0.18518519 -0.22222222]
[ 0.07407407 0.11111111]]

X=np.dot(I,E)
print(X)

[[ 0.51851852 -1. ]
[ 0.40740741 0. ]]

np.linalg.det(X)

0.4074074074074074

F=np.array([[7,2],[-3,5]])
G=np.array([[4,1],[0,-2]])
H=np.array([[-1,3],[6,-4]])
print(F)
print(G)
print(H)

[[ 7 2]
[-3 5]]
[[ 4 1]
[ 0 -2]]
[[-1 3]
[ 6 -4]]

from sympy import symbols, Eq, solve


x=symbols("x")
y=symbols("y")
equation1=Eq(x+2,6)
equation2=Eq(y-1,4)
S=solve((equation1,equation2),(x,y))
print(S)

{x: 4, y: 5}

equation1=Eq(x+3,7)
equation2=Eq(2+y,6)
equation3=Eq(-1+x,3)
equation4=Eq(y+5,9)
S=solve((equation1,equation2,equation3,equation4),(x,y))
print(S)

{x: 4, y: 4}

equation1=Eq(1*x+x*1,4)
equation2=Eq(1*3+x*y,7)
equation3=Eq(2*x+y*1,6)
equation4=Eq(2*3+y*y,10)

https://colab.research.google.com/drive/10CrN6Cf5tJRWkCKkj8NZk13qesgK4e02#scrollTo=Vk8bXrpYvydE 3/5
11/15/24, 12:56 PM Lab1.ipynb - Colab
S=solve((equation1,equation2,equation3,equation4),(x,y))
print(S)

[(2, 2)]

import numpy as np
A=np.array([[2,3],[1,-4]])
B=np.array([5,-2])
solution=np.linalg.solve(A,B)
x,y=solution
print("The value of x is:",x)
print("The value of y is:",y)

The value of x is: 1.2727272727272727


The value of y is: 0.8181818181818182

import numpy as np
F=np.array([[7,2],[-3,5]])
G=np.array([[4,1],[0,-2]])
H=np.array([[-1,3],[6,-4]])
print(F)
print(G)
print(H)

[[ 7 2]
[-3 5]]
[[ 4 1]
[ 0 -2]]
[[-1 3]
[ 6 -4]]

A=F*G+3*H
print(A)

[[ 25 11]
[ 18 -22]]

B=A.T
print(B)

[[ 25 18]
[ 11 -22]]

C=np.linalg.det(B)
print(C)

-747.9999999999997
-747.9999999999997

D=np.linalg.det(B)

if D==0:
print("Not Possible")
else:
I=np.linalg.inv(B)
print(I)

[[ 0.02941176 0.02406417]
[ 0.01470588 -0.03342246]]

J=np.array([[2,-1],[0,3]])
K=np.array([[1,2],[-3,4]])
L=np.array([[3,0],[1,-2]])
print(J)
print(K)
print(L)

[[ 2 -1]
[ 0 3]]
[[ 1 2]
[-3 4]]
[[ 3 0]
[ 1 -2]]

A=(4*J-2*K)
print(A)

https://colab.research.google.com/drive/10CrN6Cf5tJRWkCKkj8NZk13qesgK4e02#scrollTo=Vk8bXrpYvydE 4/5
11/15/24, 12:56 PM Lab1.ipynb - Colab

[[ 6 -8]
[ 6 4]]

B=(A*L)
print(B)

[[18 0]
[ 6 -8]]

S=K.T
print(S)

[[ 1 -3]
[ 2 4]]

Z=(B+S)
print(Z)

[[19 -3]
[ 8 -4]]

np.linalg.det(Z)

-51.999999999999986

https://colab.research.google.com/drive/10CrN6Cf5tJRWkCKkj8NZk13qesgK4e02#scrollTo=Vk8bXrpYvydE 5/5

You might also like