-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearAlg.ml
354 lines (294 loc) · 9.74 KB
/
linearAlg.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
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
(***********************************************************************)
(* linearAlg.ml *)
(* *)
(* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, *)
(* 2011, 2012 Yaron Minsky and Contributors *)
(* *)
(* This file is part of SKS. SKS is free software; you can *)
(* redistribute it and/or modify it under the terms of the GNU General *)
(* Public License as published by the Free Software Foundation; either *)
(* version 2 of the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public License *)
(* along with this program; if not, write to the Free Software *)
(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)
(* USA or see <http://www.gnu.org/licenses/>. *)
(***********************************************************************)
open StdLabels
open MoreLabels
module Unix=UnixLabels
open Printf
open ZZp.Infix
exception Bug of string
exception LayoutMismatch
let rec riter ~f low high =
if low >= high then ()
else (
f low;
riter ~f (low + 1) high
)
let rec rfind ~f low high =
if low >= high then raise Not_found
else if f(low) then low
else rfind ~f (low + 1) high
(*********************************************************************)
(*********************************************************************)
(*********************************************************************)
module MatrixSlow =
struct
type t = { columns: int;
rows: int;
array: ZZp.zz array;
}
let columns m = m.columns
let rows m = m.rows
let dims t = (t.columns,t.rows)
let copy m = { m with array = Array.copy m.array; }
let make ~columns ~rows init =
let array = Array.create (columns * rows) init in
{ columns = columns;
rows = rows;
array = array;
}
let init ~columns ~rows ~f =
{ columns = columns;
rows = rows;
array =
Array.init (columns * rows)
~f:(fun i ->
let (i,j) = i mod columns, i / columns in
f i j)
}
let get m i j =
m.array.(i + j * m.columns)
let set m i j v =
m.array.(i + j * m.columns) <- v
let scmult_ip m sc =
for i = 0 to Array.length m.array - 1 do
m.array.(i) <- ZZp.mult m.array.(i) sc
done
let scmult m v =
{ m with
array = Array.map ~f:(fun x -> ZZp.mult v x) m.array
}
let scmult_row m j sc =
let start = j * m.columns in
for i = 0 to m.columns - 1 do
m.array.(start + i) <- ZZp.mult m.array.(start + i) sc
done
let swap_rows m j1 j2 =
let start1 = j1 * m.columns
and start2 = j2 * m.columns in
riter 0 m.columns
~f:(fun i ->
let tmp = m.array.(start1 + i) in
m.array.(start1 + i) <- m.array.(start2 + i);
m.array.(start2 + i) <- tmp)
let add_ip m1 m2 =
if m1.columns <> m2.columns || m1.rows <> m2.rows then
raise LayoutMismatch;
for i = 0 to Array.length m1.array - 1 do
m1.array.(i) <- ZZp.add m1.array.(i) m2.array.(i)
done
let add m1 m2 =
if m1.columns <> m2.columns || m1.rows <> m2.rows then
raise LayoutMismatch;
{ m1 with
array = Array.init (m1.columns * m1.rows)
~f:(fun i -> ZZp.add m1.array.(i) m2.array.(i))
}
let rec idot_rec m1 m2 ~i ~pos1 ~pos2 sum =
if i >= m1.columns then sum
else
idot_rec m1 m2 ~i:(i+1) ~pos1:(pos1 + 1) ~pos2:(pos2 + m2.columns)
(ZZp.add sum (ZZp.mult m1.array.(pos1) m2.array.(pos2)))
let idot m1 m2 i j =
idot_rec m1 m2 ~i:0 ~pos1:(m1.columns * i) ~pos2:j ZZp.zero
let mult m1 m2 =
if m1.columns <> m2.rows then
raise LayoutMismatch;
init ~columns:m2.columns ~rows:m1.rows
~f:(fun i j -> idot m1 m2 i j)
let transpose m =
init ~columns:m.rows ~rows:m.columns ~f:(fun i j -> get m j i)
let rowadd m ~src ~dst ~scmult =
for i = 0 to m.columns - 1 do
let newval = ZZp.add (ZZp.mult (get m i src) scmult) (get m i dst) in
set m i dst newval
done
let rowsub m ~src ~dst ~scmult =
if scmult <>: ZZp.one then
for i = 0 to m.columns - 1 do
let sval = get m i src in
if sval <>: ZZp.zero then
let newval = ZZp.sub (get m i dst) (ZZp.mult_fast sval scmult) in
set m i dst newval
done
else
for i = 0 to m.columns - 1 do
let sval = get m i src in
if sval <>: ZZp.zero then
let newval = ZZp.sub (get m i dst) sval in
set m i dst newval
done
let print m =
for j = 0 to m.rows - 1 do
print_string "| ";
for i = 0 to m.columns - 1 do
ZZp.print (get m i j);
print_string " "
done;
print_string " |\n"
done
end
(*********************************************************************************)
(*********************************************************************************)
(*********************************************************************************)
(* Does everything in-place, using the in-place numerix operators *)
module Matrix =
struct
type t = { columns: int;
rows: int;
array: ZZp.zzref array;
}
let columns m = m.columns
let rows m = m.rows
let dims t = (t.columns,t.rows)
let copy m = { m with array = Array.copy m.array; }
let init ~columns ~rows ~f =
{ columns = columns;
rows = rows;
array =
Array.init (columns * rows)
~f:(fun i ->
let (i,j) = i mod columns, i / columns in
ZZp.make_ref (f i j))
}
let make ~columns ~rows x =
init ~columns ~rows ~f:(fun i j -> x)
let lget m i j =
ZZp.look (m.array.(i + j * m.columns))
let rget m i j =
m.array.(i + j * m.columns)
let get m i j = ZZp.copy_out m.array.(i + j * m.columns)
let set m i j v =
ZZp.copy_in m.array.(i + j * m.columns) v
let scmult_row ?(scol=0) m j sc =
let start = j * m.columns in
for i = scol to m.columns - 1 do
let v = m.array.(start + i) in
ZZp.mult_in v (ZZp.look v) sc
done
let swap_rows m j1 j2 =
let start1 = j1 * m.columns
and start2 = j2 * m.columns in
riter 0 m.columns
~f:(fun i ->
let tmp = ZZp.copy_out m.array.(start1 + i) in
ZZp.copy_in m.array.(start1 + i) (ZZp.look m.array.(start2 + i));
ZZp.copy_in m.array.(start2 + i) tmp)
let transpose m =
init ~columns:m.rows ~rows:m.columns ~f:(fun i j -> lget m j i)
let rowsub ?(scol=0) m ~src ~dst ~scmult =
if scmult <>: ZZp.one then
for i = scol to m.columns - 1 do
let sval = rget m i src in
if ZZp.look sval <>: ZZp.zero then
let v = rget m i dst in
ZZp.sub_in v (ZZp.look v) (ZZp.mult_fast (ZZp.look sval) scmult)
done
else
for i = scol to m.columns - 1 do
let sval = rget m i src in
if ZZp.look sval <>: ZZp.zero then
let v = rget m i dst in
ZZp.sub_in v (ZZp.look v) (ZZp.look sval)
done
let print m =
for j = 0 to m.rows - 1 do
print_string "| ";
for i = 0 to m.columns - 1 do
ZZp.print (lget m i j);
print_string " "
done;
print_string " |\n"
done
end
(*********************************************************************************)
(*********************************************************************************)
(*********************************************************************************)
(****** Gauss-Jordan Reduction *****************)
let process_row m j =
try
let v =
let v = Matrix.rget m j j in
if ZZp.look v <>: ZZp.zero then v
else
let jswap =
try
rfind (j + 1) (Matrix.rows m)
~f:(fun jswap -> Matrix.lget m j jswap <>: ZZp.zero)
with Not_found -> raise Exit
in
Matrix.swap_rows m j jswap;
Matrix.rget m j j
in
if ZZp.look v <>: ZZp.one then Matrix.scmult_row m j (ZZp.inv (ZZp.look v));
for j2 = 0 to Matrix.rows m - 1 do
if j2 <> j
then Matrix.rowsub m ~src:j ~dst:j2 ~scmult:(Matrix.get m j j2)
done
with
Exit -> ()
let reduce m =
let (columns,rows) = Matrix.dims m in
if columns < rows then raise (Bug "Matrix is too narrow to reduce");
for j = 0 to Matrix.rows m - 1 do
process_row m j;
done
(****** Gaussian Reduction *****************)
let process_row_forward m j =
try
let v =
let v = Matrix.rget m j j in
if ZZp.look v <>: ZZp.zero then v
else
let jswap =
try
rfind (j + 1) (Matrix.rows m)
~f:(fun jswap -> Matrix.lget m j jswap <>: ZZp.zero)
with Not_found -> raise Exit
in
Matrix.swap_rows m j jswap;
Matrix.rget m j j
in
if ZZp.look v <>: ZZp.one then Matrix.scmult_row ~scol:j m j (ZZp.inv (ZZp.look v));
for j2 = j + 1 to Matrix.rows m - 1 do
Matrix.rowsub ~scol:j m ~src:j ~dst:j2 ~scmult:(Matrix.get m j j2)
done
with
Exit -> ()
let backsubstitute m j =
if Matrix.lget m j j =: ZZp.one
then (
let last = Matrix.rows m - 1 in
for j2 = j - 1 downto 0 do
Matrix.rowsub ~scol:last m ~src:j ~dst:j2 ~scmult:(Matrix.get m j j2);
Matrix.set m j j2 ZZp.zero
done
)
let greduce m =
let (columns,rows) = Matrix.dims m in
if columns < rows then raise (Bug "Matrix is too narrow to reduce");
for j = 0 to Matrix.rows m - 1 do
process_row_forward m j;
done;
for j = Matrix.rows m - 1 downto 1 do
backsubstitute m j;
done
let reduce = greduce