MATRIX BASIC PROGRAMS (PYTHON)

 


# MATRIX input.
row = int(input("Enter the number of rows:"))
column = int(input("Enter the number of columns:"))


# Addition of two MATRICES.

X=[[1,2,3],[3,4,5],[6,7,8]]
Y=[[3,4,5],[6,4,5],[5,9,8]]
Z=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(X)): #len[X] is length of rows i.e no of small brackets.
for j in range(len(X[0])): #X[0] gives idea of number of columns.
Z[i][j]=X[i][j]+Y[i][j]
for r in Z:
print(r)


# Subtraction of two MATRICES.

X=[[2,3,4],
[4,5,6],
[7,6,7]]
Y=[[3,4,5],
[6,7,8],
[2,1,3]]
Z=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
Z[i][j] = X[i][j] - Y[i][j]
for r in Z:
print(r)

# Multiplication of two MATRICES.

X=[[1,2,3],
[3,4,5],
[6,7,8]]
Y=[[3,4,5],
[6,4,5],
[5,9,8]]
Z=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
Z[i][j]+=X[i][k]*Y[k][j]
for r in Z:
print(r)


# Transpose of a MATRIX.

X=[[1,2,3],
[3,4,5],
[6,7,8]]
Z=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
Z[j][i]=X[i][j]
for r in Z:
print(r)




#MATRIX programs using numpy.

import numpy
#Initialization.
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])

# add() Addition of two MATRICES.
print (numpy.add(x,y))
print ("Addition of two matrices: ")


# subtract() Subtraction of two MATRICES.
print ("Subtraction of two matrices : ")
print (numpy.subtract(x,y))


# divide() Division of two MATRICES.
print ("Matrix Division : ")
print (numpy.divide(x,y))


# multiply() Multiplication of two MATRICES.
print ("Multiplication of two matrices: ")
print (numpy.multiply(x,y))

# product() Product of two MATRICES.
print ("The product of two matrices : ")
print (numpy.dot(x,y))


# sqrt() Square root of two MATRICES.
print ("square root is : ")
print (numpy.sqrt(x))


# sum() Summation of elements.
print ("The summation of elements : ")
print (numpy.sum(y))

# Column wise summation.
print ("The column wise summation : ")
print (numpy.sum(y,axis=0))

# Row wise summation.
print ("The row wise summation: ")
print (numpy.sum(y,axis=1))

# using "T" for transpose of the MATRIX.
print ("Matrix transposition : ")
print (x.T)
 #python #programming #coding #programmer #snake #machinelearning #code #coder #ballpython #pythonprogramming #computerscience #datascience #reptile #snakes #reptiles #snakesofinstagram #reptilesofinstagram #technology #pythonsofinstagram #artificialintelligence #tech#directories#electronics#engineering#matrix

Comments

Popular posts from this blog

PIR SENSOR (PASSIVE INFRARED SENSOR)