Exercise 1.2.8

To compute C = AB = (m by n)(n by p), what order of the same three commands leads to columns times rows (outer products)?

 Rows times columns   Columns times rows For i = 1 to  m For...   For j = 1 to p  For...   For k = 1 to n  For...  C(i,j) = C(i,j) + A(i,k) B(k,j) C =

Answers

To get columns times rows, we do:

  • For k = 1 to n
  • For i = 1 to m
  • For j = 1 to p
  • C(i,j) = C(i,j) + A(i,k)B(k,j)
#### Test the multiplication of columns times rows 
 
#### Test cases 
a = np.array([1,2]) 
b = np.array([3,4]) 
my_outer = mat.outer_product(a, b) 
np_outer = np.outer(a,b) 
assert(np.array_equal(my_outer, np_outer)) 
 
A = np.array([[1,2],[3,4]]) 
B = np.array([[2,1],[5,6]]) 
np_prod = np.matmul(A, B) 
my_prod_outer = mat.columns_times_rows_outer(A, B) 
my_prod_rc = mat.rows_times_columns(A, B) 
my_prod_cr = mat.columns_times_rows(A, B) 
assert(np.array_equal(my_prod_outer, np_prod)) 
assert(np.array_equal(my_prod_rc, np_prod)) 
assert(np.array_equal(my_prod_cr, np_prod)) 
 
A = np.array([[1,2],[3,4]]) 
B = np.array([[2],[5]]) 
mat.columns_times_rows(A, B)
array([[12.],
       [26.]])

User profile picture
2020-03-20 00:00
Comments