I need help writing a program in python that reads from a file and performs matrix multiplication. the file has this format
1 4 2 1 1 1 1 1 1 2 2 3 3 4 4
which means :
1 #rows in A 4 #cols in A, rows in B 2 #cols in B
Matrix A contents: 1 1 1 1 Matrix B contents: 1 1 2 2 3 3 4 4
i need to be able to read in the file, and then multiply the two matrices and print out the result .. thanks
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
# function to print the matrix
def print_matrix(matrix, rows, cols):
for i in range(rows):
for j in range(cols):
print(matrix[i][j], end=' ')
print()
# open file
file_name = input("Enter the file name: ")
with open(file_name) as file:
# read data from file
data = file.readlines()
# read the columns and rows
rows_a = int(data[0])
col_a_row_b = int(data[1])
cols_b = int(data[2])
# Read contents of matrix a
matrix_a = []
for i in range(rows_a):
# read each row and append it to matrix
row = [int(x) for x in data[3 + i].split()]
matrix_a.append(row)
# Read contents of matrix b
matrix_b = []
for i in range(col_a_row_b):
# read each row and append it to matrix
row = [int(x) for x in data[3 + i + rows_a].split()]
matrix_b.append(row)
# print the matrices
print('Matrix A contents:')
print_matrix(matrix_a, rows_a, col_a_row_b)
print('Matrix B contents:')
print_matrix(matrix_b, col_a_row_b, cols_b)
# take an empty list
res = []
# perform the multiplication
for i in range(rows_a):
row = []
for j in range(cols_b):
s = 0
for k in range(col_a_row_b):
# resulted sum
s += matrix_a[i][k] * matrix_b[k][j]
row.append(s)
# append the row to result
res.append(row)
print("\nThe result is:")
print_matrix(res, rows_a, cols_b)
========


OUTPUT


I need help writing a program in python that reads from a file and performs matrix...
I need help writing python code with following instructions. You will write a program that reads a data file. The data file contains ticket IDs and ticket prices. Your job is to read these tickets (and prices). find minimum, maximum and average ticket prices and output a report file. The report file should look exactly (or better than) the one attached (output.txt). Input: A31 149.99 B31 49.99 A41 179.99 F31 169.99 A35 179.99 A44 169.99 open "input.txt" file using open()...
Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() - Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text. - Use the try/except method to create the file if it does not exist - If the file does not exist, prompt the...
I need a lot of questions for practicing writing and reading file in Python program to study for my final, but I couldn't find it anywhere. Help me, please! :( And also, I barely understand file in Python, so the questions need to be from easy and moderate, please! Thanks for helping!
Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...
Java Question: Write a multithread Java program that performs matrix multiplication. In the program, you need to calculate each element in the result matrix in a separate thread. The code should be generic with regard to matrix size. The sizes of three matrices should be stored in variable and used. The two operand matrices and the result matrix should be printed. Thanks
Please help me out with this assignment. Please read the
requirements carefully. And in the main function please cout the
matrix done by different operations! Thanks a lot!
For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...
JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...
Need help writing a Python program that reads and prints itself.
PYTHON PROGRAMMING LANGUAGE Task 5 Open a csv file for writing create 3 rows that store username, first name, last name and age data (four columns) write the data to the csv file under the correct column Task 6 Open the file in task 5) and read its contents Output the content in a tabular format of columns and values
Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...