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

Using language F# . Full and complete answer in order to get full credit please.

ID: 3604154 • Letter: U

Question

Using language F# . Full and complete answer in order to get full credit please. Do not copy- paste from a diferent site. Thank you.

1)Given vectors u = (u1, u2,..., un) and v = (v1, v2,..., vn), the inner product of u and v is defined to be u1*v1 + u2*v2 + ... + un*vn. Write a curried F# function inner that takes two vectors represented as int lists and returns their inner product:

(Assume that the two lists have the same length.)

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

Write an uncurried F# function to do matrix multiplication:

Assume that the dimensions of the matrices are appropriate.

Hint: Use transpose (from Homework 2), inner, and List.map.

Explanation / Answer

1. Code for sum pf cartesian product of two similar lists

2.Matrix multiplication code block .

type Row = float[]
type Column = float[]
type Matrix2D =
{ Matrix: float[,] }
member self.Item with get (x,y) = self.Matrix.[x,y]
member self.GetRow(r): Row = self.Matrix.[r..r,*] |> Seq.cast |> Seq.toArray
member self.GetColumn(c): Column = self.Matrix.[*,c..c] |> Seq.cast |> Seq.toArray
member self.NRows = Array2D.length1 self.Matrix
member self.NColumns = Array2D.length2 self.Matrix
member self.HasSameSizeAs(x: Matrix2D) = self.NRows = x.NRows && self.NColumns = x.NColumns
/// Call a function on all elements of the matrix
member self.Map f = { self with Matrix = self.Matrix |> Array2D.map f }
member self.Map2(f, x: Matrix2D) =
// Make sure both matrices have the same size
if not (self.HasSameSizeAs(x)) then
raise (InvalidOperationException())
// Map both of them
let temp r c = f (self.[r,c]) (x.[r,c])
{ Matrix = Array2D.init self.NRows x.NColumns temp }
/// Adds 2 matrices
static member (+) (x: Matrix2D,y) =
x.Map2((+), y)
/// Subtracts a matrix from the other
static member (-) (x: Matrix2D,y) =
x.Map2((-), y)
/// Negates the matrix
static member (~-) (x: Matrix2D) =
// Negate all elements of the matrix
x.Map((~-))
/// Scalar multiplication of a matrix be a number
static member (*) (x: Matrix2D, s: float) =
// Multiply each element by s
x.Map((*) s)

module Matrix2 =
/// Creates a matrix
let create (matrix) = { Matrix = array2D matrix }
/// Get an element from the matrix
let get x y m = m.Matrix.[x,y]
let map2 f (x: Matrix2D) y = x.Map2(f, y)
let matrix2 = Matrix2.create
type Matrix2D with
/// Multiplies 2 matrices together
static member (*) (x: Matrix2D, y: Matrix2D) =
// Make sure they have the correct sizes
if not (x.NColumns = y.NRows) then
raise (System.InvalidOperationException())
// Initialize a new Array2D With given function used to calculate the value for each index
{ Matrix = Array2D.init x.NColumns y.NRows (fun x1 y1 ->
y.GetColumn(y1)
// Multiply each element from the row of the 1st and to its corresponding element from the column of the 2nd matrix
|> Array.map2 (fun r c -> r*c) (x.GetRow(x1))
// Add the results to form the complete dot product of the row and column vectors
|> Array.fold (+) 0.0)}

// Test code:
let A =
matrix2
[[ 1.0; 2.0; 3.0];
[-3.0; -1.0; 1.0];
[ 1.0; 2.0; 5.0]];

let B =
matrix2
[[3.0; 1.0; 0.0];
[0.0; 4.0; 5.0];
[5.0; 6.0; 7.0]];
// C is the expected result of A*B
let C =
matrix2
[[18.0; 27.0; 31.0];
[-4.0; -1.0; 2.0];
[28.0; 39.0; 45.0]];

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote