Matrix Arithmetic with Python

Obioku Obotette
Oct 22, 2020

#list for reading numbers
nums = []
index = 0
#open file
#read line by line
#then split to append to list
with open(“COSC450_P2_Data.txt”,”r”) as data:
for x in data:
array = x.split()
for i in array:
nums.append(int(i))

data.close()

import numpy as n
#create matrix
length = len(nums)//5
matrix1 = n.asarray(nums).reshape(5,length)
matrix2 = n.asarray(nums).reshape(length,5)
result = n.dot(matrix1, matrix2)

#prepare output file
output = open(“COSC450_P2_Output.txt”,”w+”)
output.write(‘Complete set of numbers\n’)
output.write(str(nums))
output.write(“\n\nFirst Matrix\n”)
output.write(str(matrix1))
output.write(“\n\nSecond Matrix\n”)
output.write(str(matrix2))
output.write(“\n\nResults after multiplication\n”)
output.write(str(result))
output.write(“\n\nSorted Results\n”)
output.write(str(n.sort(result, axis=None).reshape(5,5)))

output.close()

--

--

Obioku Obotette
0 Followers

Trying to introduce myself in a new forum with hopeful expectations!