0

I'm asked to implement matrix multiplication. So far, the code I have: (define mult (λ (m1 m2) (if (or (empty? m1) (empty? m2)) '() (map vec-mult m1 m2))))

only generates a list of lists. Each of the inner lists must be summed up to give me the values along the diagonal to add in order to generate the elements. I'm stuck at the part of how I should do the 2D multiplication so that each row in m1 is multiplied by each row in m2 (hence the vector - matrix multiplication). I also have "transpose" and "vec-mult" implemented. Please give me a hint on how I should approach this problem. The prof wants us to use transpose and map but I just don't see how it has to be done.

Thanks

ribarcheto94
  • 436
  • 11
  • 25

2 Answers2

0

Do something with a bound variable with every element of a list:

(let ((a 5))
  (map (lambda (e) (* a e)) '(1 2 3 4)))
; ==> (5 10 15 20) 

Sum (or do anything) with a list as the arguments:

(apply + '(1 2 3)) 
; ==> 6

unzip a list:

(apply map list '((1 2 3) (4 5 6) (7 8 9) (10 11 12)))
; ==> ((1 4 7 10) (2 5 8 11) (3 6 9 12))

Flatten one level:

(apply append '((1 (2)) (3 4)))
; ==> (1 (2) 3 4)
Sylwester
  • 47,942
  • 4
  • 47
  • 79
0

Let's consider two matrices:

    [ a b ]            [ x ]
A = [ c d ]        v = [ y ]

First of all the matrix product of A and B is:

      [ ax + by ]
Ax =  [ cx + dy ]

Note that the transposing B gives:

 T   
v  = [ x y ]

Consider now the elementwise multiplication of the rows of matrix A with the transposed of v:

[a b] mult [x y]    [ ax by ]

[c d] mult [x y]  = [ cx dy ]

Note that the sum of the rows are:

ax + by 
cx + dy

which are the elements of Ax.

So to multiply A with v:

  1. Transpose v
  2. For all rows of A compute the pointwise product with v transposed.
  3. Collect the sums of the rows.

Ad 1.: Transposing: Transpose a matrix in racket (list of lists

Ad 2. Use (map * row v-transposed) to compute the pointwise product.

Ad 3. Use (apply + products) to compute the sum of the products.

Community
  • 1
  • 1
soegaard
  • 30,661
  • 4
  • 57
  • 106