package prbnmcn-linalg

  1. Overview
  2. Docs

Source file mat.ml

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
open Intf

module Make_internal
    (Repr : Basic_intf.Lang.Empty)
    (Monad : Basic_intf.Codegen_monad with type 'a m = 'a Repr.m)
    (S : Tensor with type 'a m = 'a Repr.m and type 'a k = 'a Monad.t)
    (B : Basic_intf.Lang.Bool with type 'a m = 'a Repr.m)
    (R : Basic_intf.Lang.Ring with type 'a m = 'a Repr.m)
    (R_storage : Basic_intf.Lang.Storage
                   with type 'a m = 'a Repr.m
                    and type elt = R.t)
    (I_ring : Basic_intf.Lang.Ring with type 'a m = 'a Repr.m and type t = S.pos)
    (E : Basic_intf.Lang.Exn with type 'a m = 'a Repr.m)
    (M : Basic_intf.Lang.Sequencing with type 'a m = 'a Repr.m)
    (P : Basic_intf.Lang.Product with type 'a m = 'a Repr.m) =
struct
  type 'a m = 'a Repr.m

  type base_index = S.pos

  (* An matrix index is a pair (c, r) of a column index c and a row index r. *)

  type 'a shape = 'a S.t

  let index ~c ~r = P.prod c r

  (* Could we pass this as argument to the matrix functor? *)
  (* module V = Vec.Make_internal (Repr) (B) (R) (R_storage) (S) (E) (M) (Monad) *)

  include (
    Vec.Make_internal (Repr) (Monad) (S) (B) (R) (R_storage) (E) (M) :
        module type of
          Vec.Make_internal (Repr) (Monad) (S) (B) (R) (R_storage) (E) (M)
        with type 'a shape := 'a shape
         and type 'a m := 'a m)

  open Monad.Infix

  let cols (Vec (s, _)) = S.fst s

  let rows (Vec (s, _)) = S.snd s

  let identity size =
    let shape = S.tensor size size in
    Vec
      ( shape,
        fun i ->
          B.dispatch (S.pos_equal size (P.fst i) (P.snd i)) @@ function
          | true -> R.one
          | false -> R.zero )

  let diagonal (Vec (idim, v)) =
    let shape = S.tensor idim idim in
    Vec
      ( shape,
        fun i ->
          B.dispatch (S.pos_equal idim (P.fst i) (P.snd i)) @@ function
          | true ->
              let open M in
              let* i' = P.fst i in
              v i'
          | false -> R.zero )

  let unsafe_col (Vec (s, v)) c = Vec (S.snd s, fun r -> v (P.prod c r))

  let col (Vec (s, v)) c =
    let*! _ =
      B.dispatch (S.mem (S.fst s) c) @@ function
      | false -> E.raise_ Intf.Out_of_bounds
      | true -> M.unit
    in
    Monad.return (Vec (S.snd s, fun r -> v (P.prod c r)))

  let of_col (Vec (s, v)) =
    let shape = S.tensor S.scalar s in
    let m index =
      let open M in
      let* r = P.snd index in
      v r
    in
    Vec (shape, m)

  let unsafe_row (Vec (s, v)) r = Vec (S.fst s, fun c -> v (P.prod c r))

  let row (Vec (s, v)) r =
    let*! _ =
      B.dispatch (S.mem (S.snd s) r) @@ function
      | false -> E.raise_ Intf.Out_of_bounds
      | true -> M.unit
    in
    Monad.return (Vec (S.fst s, fun c -> v (P.prod c r)))

  let of_row (Vec (s, v)) =
    let shape = S.tensor s S.scalar in
    let m index =
      let open M in
      let* r = P.fst index in
      v r
    in
    Vec (shape, m)

  let swap_rows (Vec (s, m)) r1 r2 =
    let rows_shape = S.snd s in
    let*! _ =
      B.dispatch B.(S.mem rows_shape r1 && S.mem rows_shape r2) @@ function
      | false -> E.raise_ Intf.Out_of_bounds
      | true -> M.unit
    in
    Monad.return
      (Vec
         ( s,
           fun i ->
             let open M in
             let* ri = P.snd i in
             B.dispatch (S.pos_equal rows_shape ri r1) @@ function
             | true -> m (P.prod (P.fst i) r2)
             | false -> (
                 B.dispatch (S.pos_equal rows_shape ri r2) @@ function
                 | true -> m (P.prod (P.fst i) r1)
                 | false -> m i) ))

  let swap_cols (Vec (s, m)) c1 c2 =
    let cols_shape = S.fst s in
    let*! _ =
      B.dispatch B.(S.mem cols_shape c1 && S.mem cols_shape c2) @@ function
      | false -> E.raise_ Intf.Out_of_bounds
      | true -> M.unit
    in
    Monad.return
      (Vec
         ( s,
           fun i ->
             let open M in
             let* ci = P.fst i in
             B.dispatch (S.pos_equal cols_shape ci c1) @@ function
             | true -> m (P.prod c2 (P.snd i))
             | false -> (
                 B.dispatch (S.pos_equal cols_shape ci c2) @@ function
                 | true -> m (P.prod c1 (P.snd i))
                 | false -> m i) ))

  let concat_horiz (Vec (s1, m1)) (Vec (s2, m2)) =
    let cols1 = S.fst s1 in
    let* shape = S.concat s1 s2 (S.Path.left ()) in
    let*! cols1_dim = S.dim cols1 S.Path.empty in
    let f index =
      let open M in
      let* c = P.fst index in
      let* r = P.snd index in
      B.dispatch (S.mem cols1 c) @@ function
      | true -> m1 index
      | false ->
          let* index = P.prod I_ring.(sub c cols1_dim) r in
          m2 index
    in
    Monad.return (Vec (shape, f))

  let concat_vert (Vec (s1, m1)) (Vec (s2, m2)) =
    let rows1 = S.snd s1 in
    let* shape = S.concat s1 s2 (S.Path.right ()) in
    let*! rows1_dim = S.dim rows1 S.Path.empty in
    let f index =
      let open M in
      let* c = P.fst index in
      let* r = P.snd index in
      B.dispatch (S.mem rows1 r) @@ function
      | true -> m1 index
      | false ->
          let* index = P.prod c I_ring.(sub r rows1_dim) in
          m2 index
    in
    Monad.return (Vec (shape, f))

  let mm (Vec (l_shape, _) as lhs) (Vec (r_shape, _) as rhs) =
    let*! _ =
      B.dispatch S.(equal (fst l_shape) (snd r_shape)) @@ function
      | false -> E.raise_ Intf.Dimensions_mismatch
      | true -> M.unit
    in
    let shape = S.tensor (S.fst r_shape) (S.snd l_shape) in
    Monad.return
      (Vec
         ( shape,
           fun i ->
             let open M in
             let* r = P.snd i in
             let* c = P.fst i in
             let row = unsafe_row lhs r in
             let col = unsafe_col rhs c in
             unsafe_dot (idim row) row col ))
  end
  [@@inline]

module Make : functor
  (Repr : Basic_intf.Lang.Empty)
  (Monad : Basic_intf.Codegen_monad with type 'a m = 'a Repr.m)
  (S : Tensor with type 'a m = 'a Repr.m and type 'a k = 'a Monad.t)
  (B : Basic_intf.Lang.Bool with type 'a m = 'a Repr.m)
  (R : Basic_intf.Lang.Ring with type 'a m = 'a Repr.m)
  (R_storage : Basic_intf.Lang.Storage
                 with type 'a m = 'a Repr.m
                  and type elt = R.t)
  (I_ring : Basic_intf.Lang.Ring with type 'a m = 'a Repr.m and type t = S.pos)
  (E : Basic_intf.Lang.Exn with type 'a m = 'a Repr.m)
  (M : Basic_intf.Lang.Sequencing with type 'a m = 'a Repr.m)
  (P : Basic_intf.Lang.Product with type 'a m = 'a Repr.m)
  ->
  Intf.Mat
    with type 'a k = 'a Monad.t
     and type 'a m = 'a Repr.m
     and type base_index = I_ring.t
     and type 'a shape = 'a S.t
     and type ('a, 'b) morphism = ('a, 'b) S.Morphism.t
     and type elt = R.t =
  Make_internal

(* Matrices backed by arrays *)

(* Instantiate some typical schemes *)
module Array_backed_column_major
    (Repr : Basic_intf.Lang.Empty)
    (Monad : Basic_intf.Codegen_monad with type 'a m = 'a Repr.m)
    (S : Intf.Tensor with type 'a m = 'a Repr.m)
    (B : Basic_intf.Lang.Bool with type 'a m = 'a Repr.m)
    (R : Basic_intf.Lang.Ring with type 'a m = 'a Repr.m and type t = S.pos)
    (R_ord : Basic_intf.Lang.Infix_order
               with type 'a m = 'a Repr.m
                and type t = R.t)
    (A : Basic_intf.Lang.Array with type index = R.t and type 'a m = 'a Repr.m)
    (E : Basic_intf.Lang.Exn with type 'a m = 'a Repr.m)
    (P : Basic_intf.Lang.Product with type 'a m = 'a Repr.m)
    (M : Basic_intf.Lang.Sequencing with type 'a m = 'a Repr.m) =
struct
  type index = S.pos * S.pos

  open Monad.Infix

  let of_array shape (a : A.t Repr.m) =
    let rows = S.snd shape in
    let num_rows = S.dim rows S.Path.empty in
    let*! numel = S.numel shape in
    let*! l = A.length a in
    let*! _ =
      B.dispatch R_ord.(l = numel) @@ function
      | false -> E.raise_ Intf.Dimensions_mismatch
      | true -> M.unit
    in
    let vec =
      Vec
        ( shape,
          fun i ->
            let l = R.(add (P.snd i) (mul num_rows (P.fst i))) in
            A.unsafe_get a l )
    in
    let ovec =
      OVec
        ( shape,
          fun i v ->
            let l = R.(add (P.snd i) (mul num_rows (P.fst i))) in
            A.unsafe_set a l v )
    in
    Monad.return (vec, ovec)

  let in_of_array shape a =
    let rows = S.snd shape in
    let num_rows = S.dim rows S.Path.empty in
    let*! numel = S.numel shape in
    let*! l = A.length a in
    let*! _ =
      B.dispatch R_ord.(l = numel) @@ function
      | false -> E.raise_ Intf.Dimensions_mismatch
      | true -> M.unit
    in
    let vec =
      Vec
        ( shape,
          fun i ->
            let l = R.(add (P.snd i) (mul num_rows (P.fst i))) in
            A.unsafe_get a l )
    in
    Monad.return vec

  let out_of_array shape a =
    let rows = S.snd shape in
    let num_rows = S.dim rows S.Path.empty in
    let*! numel = S.numel shape in
    let*! l = A.length a in
    let*! _ =
      B.dispatch R_ord.(l = numel) @@ function
      | false -> E.raise_ Intf.Dimensions_mismatch
      | true -> M.unit
    in
    let ovec =
      OVec
        ( shape,
          fun i v ->
            let l = R.(add (P.snd i) (mul num_rows (P.fst i))) in
            A.unsafe_set a l v )
    in
    Monad.return ovec
end
[@@inline]

(**/**)

module BL = Basic_impl.Lang

(**/**)

module Make_native
    (R : Basic_intf.Lang.Ring with type 'a m = 'a)
    (R_storage : Basic_intf.Lang.Storage with type 'a m = 'a and type elt = R.t) =
  Make (BL.Empty) (BL.Codegen) (Tensor.Int) (BL.Bool) (R) (R_storage) (BL.Int)
    (BL.Exn)
    (BL.Sequencing)
    (BL.Product)
module Float = Make_native (BL.Float) (BL.Float_storage)
module Rational = Make_native (BL.Rational) (BL.Make_storage (BL.Rational))
OCaml

Innovation. Community. Security.