Last active
October 26, 2015 15:51
-
-
Save rleonid/c331caf29e88f78abfa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Lacaml.D | |
let capture_failure_of_list = function | |
| [] -> Mat.empty | |
| (h :: _) as lst -> | |
let m = List.length lst in | |
let n = List.length h in | |
let mat = Mat.create m n in | |
let rec loop i = function | |
| [] -> mat | |
| h :: t -> | |
let s = List.fold_left (fun j e -> mat.{i, j} <- e; j + 1) 1 h in | |
if s <= n then | |
invalid_arg "list not long enough" | |
else | |
loop (i + 1) t | |
in | |
try loop 1 lst | |
with Invalid_argument _ -> | |
failwith "of_list: vectors not of same length" | |
let count_first_of_list = function | |
| [] -> Mat.empty | |
| (h :: t) as lst -> | |
let m = List.length lst in | |
let n = List.length h in | |
let () = List.iter (fun l -> | |
if List.length l <> n then | |
failwith "of_list: vectors not of same length" | |
else | |
()) t | |
in | |
let mat = Mat.create m n in | |
let rec loop i = function | |
| [] -> mat | |
| h :: t -> | |
let () = List.iteri (fun j e -> mat.{i, j + 1} <- e) h in | |
loop (i + 1) t | |
in | |
loop 1 lst | |
let linit n f = | |
let rec loop i acc = | |
if i < 0 then acc | |
else loop (i - 1) (f i :: acc) | |
in | |
loop (n - 1) [] | |
let linit2 n m f = | |
linit n (fun _ -> linit m f) | |
let append_sub n at = | |
List.mapi (fun i lst -> if i = at then n :: lst else lst) | |
let test_sample n m = | |
append_sub (float m) (Random.int n) (linit2 n m float) | |
let time f = | |
let n = Sys.time () in | |
let r = f () in | |
Printf.printf "%f\n" (Sys.time () -. n); | |
r | |
let test n m times f = | |
let data = linit times (fun _ -> test_sample n m) in | |
let conv () = | |
List.iter (fun lst -> | |
try f lst; assert false | |
with Failure _ -> ()) | |
data | |
in | |
time conv | |
let () = | |
let n = int_of_string (Sys.argv.(1)) in | |
let m = int_of_string (Sys.argv.(2)) in | |
let times = 1000 in | |
Random.self_init (); | |
Printf.printf "Testing %d %d %d capture_failure_of_list\n" n m times; | |
test n m times capture_failure_of_list; | |
Printf.printf "Testing %d %d %d count_first_of_list\n" n m times; | |
test n m times count_first_of_list | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment