Question: Part I . Follow the formula above, create a function dprod(matrixA,matrixB) to calculate the dot product of two matrices with loops. ( 5 points )
- Part I. Follow the formula above, create a function dprod(matrixA,matrixB) to calculate the dot product of two matrices with loops. (5 points)
- Look carefully at the formula presented above
- Calculate the elements of matrix C one by one
- For each element [,] you need to sum up the values of [,][,] where k can go from 0 to n-1
# You are not allowed to use other Python libraries than Numpy here
### import libraries you need below ### import numpy as np ###########import ends here############
# for matrix representation, we are going to use numpy array here
############ ## PART I ## ############
### define the dprod function, please replace None below with your own code def dprod(A,B): ''' parameter: A datatype: numpy array of shape (m,n) parameter: B datatype: numpy array of shape (n,p) ''' # step 1: check if the shapes of A and B meet the requirement of matrix multiplication #### START YOUR CODE HERE #### shape_A = None shape_B = None #### END YOUR CODE HERE #### assert shape_A[1]==shape_B[0], "The shapes of A and B don't meet the requirement of dot product!!" # step 2: calculate the dot product #### START YOUR CODE HERE #### m = None n = None p = None #### END YOUR CODE HERE #### ## create a placeholder matrix C to hold the dot product output ## initialize the matrix element to be all zeros, make sure the shape of C is (m,p) #### START YOUR CODE HERE #### C = None #### END YOUR CODE HERE #### ## update each of the elements in matrix C ### hint1: build your calculation logic based on the formula displayed in the description above ### hint2: loop through rows (represented by index i) in matrix A and columns (represented by index j) in matrix B ### hint3: for each pair of (i,j), C[i,j] equals the summation of value A[i,k] * B[k,j], where k can go from 0 to n-1 #### START YOUR CODE HERE #### None #### END YOUR CODE HERE #### return C
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
