Question

In: Computer Science

Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator,...

Matrix multiplication with arrays

In this question you will verify that the numpy.ndarray matrix multiplication operator, @, does a proper matrix multiplcation. To do this, first write a function, mat_mult(a1, a2) that creates a new 2d array of zeros, and using nested for loops, fill in the elements of the matrix multiplication, and return this new array.

Then, write a second function, matrix_diff(b, c) that takes two arrays as arguments then generates and returns the following ratio:

sqrt((∑(??,?−??,?)^2)/(∑(??,?+??,?)^2))

This function gives a measure of how different two arrays are, giving 0 if the arrays are identical.

Your functions can assume that both matrix arguments are two-dimensional, but they should check that the shapes are compatible and return None if they are not.

Solutions

Expert Solution

Dear Learner,

Here is the solution to your code,

CODE:

import numpy as np

def mat_mult(matrix1,matrix2):
   result = [[0 for x in range(len(matrix1))] for y in range(len(matrix2[0]))] #initializing the result array
   for i in range(0,len(matrix1)):
       for j in range(0,len(matrix2[0])):
           for k in range(0,len(matrix2)):
               result[i][j] += matrix1[i][k] * matrix2[k][j]
   for r in result:
       print(r)

def matrix_diff(matrix1,matrix2):
   result=0
   numrows1 = len(matrix1) # number of rows in matrix 1
   numcols1 = len(matrix1[0]) # number of columns in matrix 1
   numrows2 = len(matrix2) # number of rows in matrix 2
   numcols2 = len(matrix2[0]) # number of columns in matrix 2
   if(numrows1 == numrows2) and (numcols1 == numcols2) :
       for i in range (0,numrows1):
           for j in range(0,numcols1):
               result += ((matrix1[i][j] - matrix2[i][j])**2)/((matrix1[i][j] + matrix2[i][j])**2)
   else:
       print("Error")
   print(result)
  

matrix1 = [[1,2],[3,4]]
matrix2 = [[1,2],[3,4]]
mat_mult(matrix1,matrix2) #call to multiply the 2 matrices
a = np.matrix(matrix1)
b = np.matrix(matrix2)
print(a@b) #numpy operator
matrix_diff(matrix1,matrix2) #call to the function to calculate difference

SCREENSHOT:

OUTPUT:

I hope I have answered the question the way you were expecting. If you still have any doubts or want any other explanation or modification, feel free to ask us in the comments.

Please leave a like if this was helpful.

Thanks,

Happy Studying.


Related Solutions

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?
Guided Assignment Problem 1: Multiplication Review the Three-Question Approach used to verify the Factorial recursive algorithm...
Guided Assignment Problem 1: Multiplication Review the Three-Question Approach used to verify the Factorial recursive algorithm in Ch. 3.2. Your Tasks: Now we need to develop a recursive algorithm for multiplication operation with two positive integers without using the multiplication operator. Your answer should be in pseudocode or Java code format. Then use the Three-Question Approach to verify your recursive algorithm. No coding is required for this assignment. Recursive method header given as: int multiplication (int a, int b) Hints...
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...
Prove that GL(2, Z2) is a group with matrix multiplication
Prove that GL(2, Z2) is a group with matrix multiplication
Set up the addition and multiplication tables for Z3 and Z6. Use these tables to verify...
Set up the addition and multiplication tables for Z3 and Z6. Use these tables to verify that (Z3, +), (Z3 \ {0}, ·) and (Z6, +) are groups, but (Z6 \ {0}, ·) is not a group. In which finite field does "25 divided by 5 is 14" hold?
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)...
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy /...
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy / paste code** 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...
Verify that the three eigenvectors found for the two eigenvalues of the matrix in that example...
Verify that the three eigenvectors found for the two eigenvalues of the matrix in that example are linearly independent and find the components of the vector i = ( 1 , 0 , 0 ) in the basis consisting of them. Using \begin{vmatrix}1 & 0 & 0 \\ -4 & 7 & 2 \\ 10 & -15 & -4\end{vmatrix} Which of these is the answer? (2,−5,2)(2,−5,2) (−1,3,23)(−1,3,23) (1,−3,32)(1,−3,32)
verify that multiplication of 2x2 matrices distributes from the right and from the left over addition...
verify that multiplication of 2x2 matrices distributes from the right and from the left over addition of 2x2 matrices. show all the steps. thank you
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT