0% found this document useful (0 votes)
42 views4 pages

Matrix - Jupyter Notebook

Uploaded by

2361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Matrix - Jupyter Notebook

Uploaded by

2361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/8/23, 3:59 PM matrix - Jupyter Notebook

In [18]: #2x2 matrix


import numpy as np
from numpy import matlib

In [1]: ​
a=np.array([
[1,2],
[3,4]
])
print(a)

[[1 2]
[3 4]]

In [2]: b=np.array([
[5,6],
[7,8]
])
print(b)

[[5 6]
[7 8]]

In [3]: print(a+b)

[[ 6 8]
[10 12]]

In [4]: print(b-a)

[[4 4]
[4 4]]

In [10]: c=np.add(a,b)
d=np.subtract(b,a)
e=np.dot(a,b) #for multiplication
print(c)
print(d)
print(e)

[[ 6 8]
[10 12]]
[[4 4]
[4 4]]
[[19 22]
[43 50]]

localhost:8888/notebooks/Documents/arun/mathsprogram/matrix.ipynb# 1/4
11/8/23, 3:59 PM matrix - Jupyter Notebook

In [11]: x=np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
y=np.array([
[11,12,13],
[14,15,16],
[17,18,19]
])

In [13]: t=np.add(x,y)
u=np.subtract(y,x)
v=np.dot(x,y)
print(t)
print(u)
print(v)

[[12 14 16]
[18 20 22]
[24 26 28]]
[[10 10 10]
[10 10 10]
[10 10 10]]
[[ 90 96 102]
[216 231 246]
[342 366 390]]

In [14]: print(a.T)

[[1 3]
[2 4]]

In [19]: I=np.matlib.eye(3)
print(I)

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

In [22]: J=np.matlib.eye(3,k=-1) #values below princliple diagonal becomes 1


print(J)

[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]

In [23]: K=np.matlib.eye(3,k=1) #values above princliple diagonal becomes 1


print(K)

[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]

localhost:8888/notebooks/Documents/arun/mathsprogram/matrix.ipynb# 2/4
11/8/23, 3:59 PM matrix - Jupyter Notebook

In [24]: L=np.matlib.eye(3,k=0) # princliple diagonal becomes 1


print(L)

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

In [29]: R=np.matlib.ones((2,2))
print(R)

[[1. 1.]
[1. 1.]]

In [31]: R=np.matlib.zeros((2,2))
print(R)

[[0. 0.]
[0. 0.]]

In [33]: a=np.array([
[50,29],
[30,44]
])
det=np.linalg.det(a) #to find the determinant
print(round(det))

1330.0

In [37]: b=np.array([
[10,20,30,],
[23,45,67],
[34,67,90]
]
)
det=np.linalg.det(b) #to find the determinant
print(round(det))

100.0

In [38]: m=np.array([
[10,20,30,],
[23,45,67],
[34,67,90]
]
)
n=np.linalg.inv(m) #to find the inverse of given matrix
print(n)

[[-4.39 2.1 -0.1 ]


[ 2.08 -1.2 0.2 ]
[ 0.11 0.1 -0.1 ]]

localhost:8888/notebooks/Documents/arun/mathsprogram/matrix.ipynb# 3/4
11/8/23, 3:59 PM matrix - Jupyter Notebook

In [43]: n=np.linalg.matrix_rank(m)
print(n)
print(np.trace(m))
print(np.linalg.matrix_power(m,2))

3
145
[[ 1580 3110 4340]
[ 3543 6974 9735]
[ 4941 9725 13609]]

In [46]: a=np.array([
[1,-2,5,0],
[2,0,4,-1],
[3,1,0,7],
[0,4,-2,0]
])
det=np.linalg.det(a)
print(round(det))
print(np.trace(a))

-158.0
1

In [ ]: ​

localhost:8888/notebooks/Documents/arun/mathsprogram/matrix.ipynb# 4/4

You might also like