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?
Multiplication of Matrix is one of the tasks that takes time to compute. Note that the...
Multiplication of Matrix is one of the tasks that takes time to compute. Note that the time complexity of multiplying two square matrix is o(n^3) using the nested loops method, and Strassen algorithm improves it to O(n^2.8074). Multi-threading can be used to further enhance this. Using the matrices below, write (a) a serial program (the normal nested loops method) to perform the multiplication. (b) a multithreaded program using pthreads to perform this computation. (c) compare the execution times of (a)...
The following algorithm multiplies two square matrices using a straightforward implementation of the definition of matrix...
The following algorithm multiplies two square matrices using a straightforward implementation of the definition of matrix multiplication. (If you are unfamiliar with matrices, think of them as 2-dimensional arrays, and don’t worry, you don’t need to know how to do matrix multiplication to solve this problem) 1 2 3 4 5 6 7 8 9 10 ALGORITHM MatrixMultiplication(A[1..n, 1..n], B[1..n, 1..n]) // Multiply two square matrices of order n using the definition-based algorithm //Input: twon×nmatricesAandB // Output: matrix C =...
1.       Test the execution time of the program in Code 3. Make a screen capture which...
1.       Test the execution time of the program in Code 3. Make a screen capture which shows the execution time that has the unit of machine time unit. Code3 SOURCE.CPP # include<fstream> # include "List.h" # include <iostream> using namespace std; int main() { List temps; int oneTemp; ifstream inData; ofstream outData; inData.open("temp.dat"); if (!inData) {   outData<<"Can't open file temp.dat" << endl;   return 1; } inData >> oneTemp; while (inData && !temps.IsFull()) {   if (!temps.IsPresent(oneTemp))    temps.Insert(oneTemp);   inData >> oneTemp; }...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT