Question

In: Computer Science

build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...

build a program which performs matrix multiplication on square matrices.

use UNIX "time" to capture the time it takes to run the program with different data sizes.

Languages: Python

Task: Create matrix multiplication

Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000

Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices,
using a random number generator -- populate two of the Matrices,
Multiply the two matrices, putting the result into the third
Your routine should have not output

Externals: Your are to use UNIX "time" to time the runtime of the 5 experiments.  

Solutions

Expert Solution

#source code:

import numpy as np
import time
start=time.time()
rand_250_A_matrix=np.random.randint(1,100,size=(250,250)) #generate randomnumbers between 1 to 100 only
rand_250_B_matrix=np.random.randint(1,100,size=(250,250))
multiplication_of_250_matrix=np.matmul(rand_250_A_matrix,rand_250_B_matrix)
end=time.time()
print(multiplication_of_250_matrix)
Execution_time_of_250_sqare_matrix=end-start


start=time.time()
rand_500_A_matrix=np.random.randint(1,100,size=(500,500))
rand_500_B_matrix=np.random.randint(1,100,size=(500,500))
multiplication_of_500_matrix=np.matmul(rand_500_A_matrix,rand_500_B_matrix)
end=time.time()
print(multiplication_of_250_matrix)
Execution_time_of_500_sqare_matrix=end-start

start=time.time()
rand_1000_A_matrix=np.random.randint(1,100,size=(1000,1000))
rand_1000_B_matrix=np.random.randint(1,100,size=(1000,1000))
multiplication_of_1000_matrix=np.matmul(rand_1000_A_matrix,rand_1000_B_matrix)
end=time.time()
print(multiplication_of_1000_matrix)
Execution_time_of_1000_sqare_matrix=end-start

start=time.time()
rand_1500_A_matrix=np.random.randint(1,100,size=(1500,1500))
rand_1500_B_matrix=np.random.randint(1,100,size=(1500,1500))
multiplication_of_1500_matrix=np.matmul(rand_1500_A_matrix,rand_1500_B_matrix)
end=time.time()
print(multiplication_of_1500_matrix)
Execution_time_of_1500_sqare_matrix=end-start

start=time.time()
rand_2000_A_matrix=np.random.randint(1,100,size=(2000,2000))
rand_2000_B_matrix=np.random.randint(1,100,size=(2000,2000))
multiplication_of_2000_matrix=np.matmul(rand_2000_A_matrix,rand_2000_B_matrix)
end=time.time()
print(multiplication_of_2000_matrix)
Execution_time_of_2000_sqare_matrix=end-start


print("Execution time 250 square matrix:",Execution_time_of_250_sqare_matrix)
print("Execution time 500 square matrix:",Execution_time_of_500_sqare_matrix)
print("Execution time 1000 square matrix:",Execution_time_of_1000_sqare_matrix)
print("Execution time 1500 square matrix:",Execution_time_of_1500_sqare_matrix)
print("Execution time 2000 square matrix:",Execution_time_of_2000_sqare_matrix)

#output:

#if you have any doubt comment below...


Related Solutions

Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n...
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n x n each, find their multiplication matrix. Naive Method Following is a simple way to multiply two matrices.                void multiply(int A[][N], int B[][N], int C[][N]) {     for (int i = 0;   i < N; i++) {         for (int j = 0; j < N; j++) {             C[i][j] = 0;             for (int k = 0; k < N; k++) {                 C[i][j] += A[i][k]*B[k][j];             }...
Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices....
Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices. Provide a recursive implementation of the function void print_parenth(Matrix K[], int i, int j); that takes as input the matrix K of k values that are needed to construct the optimal parenthesization for Ai · · · Aj . Assume access to a print function that takes as input a string and prints its value. You may also assume a “+” operation for string...
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A...
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A for any square matrix A. Prove that X must be the identity matrix. Prove that for any invertible matrix A, the inverse matrix must be unique. Hint: Assume that there are two inverses and then show that they much in fact be the same matrix. Prove Theorem which shows that Gauss-Jordan Elimination produces the inverse matrix for any invertible matrix A. Your proof cannot...
How to write a method that performs matrix multiplication with three rectangle arrays along with their...
How to write a method that performs matrix multiplication with three rectangle arrays along with their dimensions as parameters using Java programming?
Consider a variant of the matrix-chain multiplication problem in which the goal is to parenthesize the sequence of matrices so as to maximize, rather than minimize, the number of scalar multiplications.
Consider a variant of the matrix-chain multiplication problem in which the goal is to parenthesize the sequence of matrices so as to maximize, rather than minimize, the number of scalar multiplications. Does this problem exhibit optimal substructure?
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Write 2 classes for each program Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix A; 1 2 3 4 5...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from...
Write a program that reads two square Matrices of size 2 *2 and calculate and print...
Write a program that reads two square Matrices of size 2 *2 and calculate and print the sum of them. Also, the signed value of the matrices are input by the user Note: 1) Use CamelCase for variables names 2) Write Comments such as your name, the class, description of the program, and description of your logic. 4) Use sematic names for your variables 5) Your program should be C program not C++.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT