-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.fs
387 lines (321 loc) · 14.6 KB
/
Matrix.fs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
type 'B Tree =
| Leaf
| Node of 'B Tree * 'B * 'B Tree
with
member t.Left =
match t with
| Leaf -> Leaf
| Node(l,_, _) -> l
member t.Right =
match t with
| Leaf -> Leaf
| Node(_,_,r) -> r
member t.Key =
match t with
| Leaf -> None
| Node(_, k, _) -> Some k
type 'M Matrix =
| M of 'M Tree * int * int // normal matrix
| T of 'M Tree * int * int // carrier for a transposed matrix
// Because we carry we can transpose a matrix in constant time.
// all operations are still in the same time as for normal matrices.
with
// build a matrix from a list of list
static member build (lst : 'b list list) =
let rec row = function
| ([], _) -> Leaf
| (x::xs, submatrix : 'b Tree) -> Node(submatrix, x, row (xs, submatrix.Right))
let rec col = function
| ([], _) -> Leaf
| (x::xs, submatrix : 'b Tree ) -> row (x, col (xs, submatrix.Right))
M (col (lst, Leaf), lst.Length, lst.Head.Length)
static member Transpose = function
| M (m, h, w) -> T(m, w ,h) // transposion of a normal matrix
| T (m, h, w) -> M(m, w, h) // transposion of a transposed matrix
static member op_Equality (M1, M2) =
match M1, M2 with
// = for normal matirces
| M(m1, h1, w1), M(m2, h2, w2) ->
let rec row m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
m1.Key.Value = m2.Key.Value && row m1.Right m2.Right
let rec col m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
row m1 m2 && col m1.Left m2.Left
if h1 <> h2 || w1 <> w2 then
false
else
col m1 m2
// = for transposed matrices
| T(m1, h1, w1), T(m2, h2, w2) ->
let rec row m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
m1.Key.Value = m2.Key.Value && row m1.Right m2.Right
let rec col m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
row m1 m2 && col m1.Left m2.Left
if h1 <> h2 || w1 <> w2 then
false
else
col m1 m2
// = first case of one matrix being normal and the other being transposed
| T(m1, h1, w1), M(m2, h2, w2) ->
let rec row m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
m1.Key.Value = m2.Key.Value && row m1.Left m2.Left
let rec col m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
row m1 m2 && col m1.Left m2.Left
if h1 <> w2 || w1 <> h2 then
false
else
col m1 m2
// = second case
| M(m1, h1, w1), T(m2, h2, w2) ->
let rec row m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
m1.Key.Value = m2.Key.Value && row m1.Left m2.Left
let rec col m1 m2 =
match m1, m2 with
| Leaf, _
| _, Leaf -> true
| _ ->
row m1 m2 && col m1.Left m2.Left
if h1 <> w2 || w1 <> h2 then
false
else
col m1 m2
// not optimal has upto two of each nodes can be minimized.
static member ( + ) (M1, M2) =
match (M1, M2) with
| M(m1,h1,w1),M(m2,h2,w2) ->
// needs to build from bottom up e.i dynamic programming.
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value + m2.Key.Value, row m1.Right m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> row m1 m2 (col m1.Left m2.Left t.Left)
if h1 = h2 && w1 = w2 then
M (col m1 m2 Leaf, h1, w1)
else
failwith "Dimensions of"
| T (m1, h1, w1), T (m2, h2, w2) ->
// needs to build from bottom up e.i dynamic programming.
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value + m2.Key.Value, row m1.Right m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> row m1 m2 (col m1.Left m2.Left t.Left)
if h1 = h2 && w1 = w2 then
T (col m1 m2 Leaf, h1, w1)
else
failwith "Dimensions of"
| T (m1, h1, w1), M (m2, h2, w2) ->
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value + m2.Key.Value, row m1.Left m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| Leaf, _
| _, Leaf -> Leaf
| _ -> row m1 m2 (col m1.Right m2.Left t.Left)
if h1 = w2 && w1 = h2 then
M (col m1 m2 Leaf, h2, w2)
else
failwith "Dimensions of"
| M (m1, h1, w1), T(m2, h2, w2) ->
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value + m2.Key.Value, row m1.Right m2.Left t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| Leaf, _
| _, Leaf -> Leaf
| _ -> row m1 m2 (col m1.Left m2.Right t.Left)
if h1 = w2 && w1 = h2 then
M (col m1 m2 Leaf, h2, w2)
else
failwith "Dimensions of"
static member ( - ) (M1, M2) =
match (M1, M2) with
| M(m1,h1,w1),M(m2,h2,w2) ->
// needs to build from bottom up e.i dynamic programming.
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value - m2.Key.Value, row m1.Right m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> row m1 m2 (col m1.Left m2.Left t.Left)
if h1 = h2 && w1 = w2 then
M (col m1 m2 Leaf, h1, w1)
else
failwith "Dimensions of"
| T (m1, h1, w1), T (m2, h2, w2) ->
// needs to build from bottom up e.i dynamic programming.
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value - m2.Key.Value, row m1.Right m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> row m1 m2 (col m1.Left m2.Left t.Left)
if h1 = h2 && w1 = w2 then
T (col m1 m2 Leaf, h1, w1)
else
failwith "Dimensions of"
| T (m1, h1, w1), M (m2, h2, w2) ->
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value - m2.Key.Value, row m1.Left m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| Leaf, _
| _, Leaf -> Leaf
| _ -> row m1 m2 (col m1.Right m2.Left t.Left)
if h1 = w2 && w1 = h2 then
M (col m1 m2 Leaf, h2, w2)
else
failwith "Dimensions of"
| M (m1, h1, w1), T(m2, h2, w2) ->
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, m1.Key.Value - m2.Key.Value, row m1.Right m2.Left t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| Leaf, _
| _, Leaf -> Leaf
| _ -> row m1 m2 (col m1.Left m2.Right t.Left)
if h1 = w2 && w1 = h2 then
M (col m1 m2 Leaf, h2, w2)
else
failwith "Dimensions of"
// same as above
static member ( * ) (M1, M2) =
match M1, M2 with
| M(m1 : 'b Tree, h1, w1), M(m2, h2, w2) ->
let rec mul m1 m2 acc =
match (m1, m2) with
| (Leaf, _) -> acc
| (_, Leaf) -> acc
| _ -> mul m1.Right m2.Left (m1.Key.Value * m2.Key.Value + acc)
// needs to build from bottom up e.i dynamic programming.
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, mul m1 m2 LanguagePrimitives.GenericZero, row m1 m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> row m1 m2 (col m1.Left m2 t.Left)
if w1 = h2 then
M(col m1 m2 Leaf, h1, w2)
else
failwith "Dimensions of"
| T(m1 : 'b Tree, h1, w1), T(m2, h2, w2) ->
let rec mul m1 m2 acc =
match (m1, m2) with
| (Leaf, _) -> acc
| (_, Leaf) -> acc
| _ -> mul m1.Right m2.Left (m1.Key.Value * m2.Key.Value + acc)
// needs to build from bottom up e.i dynamic programming.
let rec row m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> Node(t, mul m1 m2 LanguagePrimitives.GenericZero, row m1 m2.Right t.Right)
let rec col m1 m2 (t : 'b Tree) =
match m1, m2 with
| (Leaf, _)
| (_, Leaf) -> Leaf
| _ -> row m1 m2 (col m1.Left m2 t.Left)
if w1 = h2 then
T(col m1 m2 Leaf, h1, w2)
else
failwith "Dimensions of"
| _ ->
// since we either has M1^T * M2 or M1 * M2^T we then only
// need to transpose one of them and change multiplication order to get one of the first two cases.
M2 * Matrix<'b>.Transpose M1
member M.Height =
match M with
| M(_, h, _) -> h
| T(_, h, _) -> h
member M.Width =
match M with
| M(_, w, _) -> w
| T(_, w, _) -> w
member M.GetTree =
match M with
| M(m, _, _) -> m
| T(m, _, _) -> m
override M.ToString() =
match M with
| M(m, _, _) ->
let rec row m =
match m with
| Leaf -> ""
| _ -> sprintf "%s %s" (m.Key.Value.ToString()) (row m.Right)
let rec col m =
match m with
| Leaf -> ""
| _ -> sprintf "%s\n%s" (row m) (col m.Left)
sprintf "\n%s" (col m)
| T(m, _, _) ->
let rec row m =
match m with
| Leaf -> ""
| _ -> sprintf "%s %s" (m.Key.Value.ToString()) (row m.Left)
let rec col m =
match m with
| Leaf -> ""
| _ -> sprintf "%s\n%s" (row m) (col m.Right)
sprintf "\n%s" (col m)
let m33 = Matrix<int>.build [[1;2;3];[4;5;6];[7;8;9]]