-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.clj
44 lines (34 loc) · 864 Bytes
/
matrix.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(ns matrix
"Clojure implementation of matrix math, mostly to prove to myself that I
understand how matrix multiplication works.")
(def three-by-two
[[1 2]
[3 4]
[5 6]])
(def two-by-four
[[ 7 8 9 10]
[11 12 13 14]])
(def result
[[ 29 32 35 38]
[ 65 72 79 86]
[101 112 123 134]])
(defn transpose
"Transpose a matrix."
[matrix]
(apply (partial mapv vector) matrix))
(defn multiply
"Multiply two matricies."
[matrix-x matrix-y]
(let [matrix-y' (transpose matrix-y)]
(loop [[x & xs] matrix-x
result []]
(if (seq x)
(recur xs
(->> matrix-y'
(map (partial map * x))
(mapv (partial apply +))
(conj result)))
result))))
(multiply three-by-two two-by-four)
(= result
(multiply three-by-two two-by-four))