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

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...
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?
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 Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Write a C# console program that fills the right to left diagonal of a square matrix...
Write a C# console program that fills the right to left diagonal of a square matrix with zeros, the lower-right triangle with -1s, and the upper left triangle with +1s. Let the user enter the size of the matrix up to size 21. Use a constant and error check. Output the formatted square matrix with appropriate values. Refer to the sample output below. Sample Run: *** Start of Matrix *** Enter the size of square (<= 21): 5 1 1...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without thread AND with thread. [12:18, 10/2/2020] Mohan Reddy: ou are to implement (M by N matrix) times (N by 1 vector) operation and see how multiple threads can speed up the computation. The resulting vector will be (M by 1 vector). See the following steps/requirements. 1. Accept M and N as keyboard input. 2. Generate a random (M by N matrix) and a random...
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation...
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation of the function void print_path(Matrix D[], int n, int i, int j); that takes as input the array of matrices D[1], D[2], . . . , D[n − 1] that have been computed by the algorithm, and prints the optimal path from vertex i to vertex j. Hint: for convenience you may use the notation Dr ij to denote the value D[r][i, j], i.e....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT