Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

F# Question. Given an m -by- n matrix A and an n -by- k matrix B , the product o

ID: 3848536 • Letter: F

Question

F# Question.

Given an m-by-n matrix A and an n-by-k matrix B, the product of A and B is an m-by-k matrix whose entry in position (i,j) is the inner product of row i of A with column j of B. For example,

Write an uncurried F# function to do matrix multiplication (assuming the dimensions of two given matrices are appropriate):

> multiply ([[1;2;3];[4;5;6]], [[0;1];[3;2];[1;2]]);;

val it : int list list = [[9; 11]; [21; 26]]

Hint: Use transpose (from previous assignment), inner, and List.map.

36 25 14

Explanation / Answer

1) f# code:

let multiply (xs, ys) =
let rec matrix_mult xs ys =
match xs with
| [] -> []
| x::xs -> List.map (fun f -> inner f x) ys :: matrix_mult xs ys
matrix_mult xs (transpose ys)

let x = multiply ([[1;2;3];[4;5;6]], [[0;1];[3;2];[1;2]]);;