Chris@10: (* Chris@10: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * This program is free software; you can redistribute it and/or modify Chris@10: * it under the terms of the GNU General Public License as published by Chris@10: * the Free Software Foundation; either version 2 of the License, or Chris@10: * (at your option) any later version. Chris@10: * Chris@10: * This program is distributed in the hope that it will be useful, Chris@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: * GNU General Public License for more details. Chris@10: * Chris@10: * You should have received a copy of the GNU General Public License Chris@10: * along with this program; if not, write to the Free Software Chris@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@10: * Chris@10: *) Chris@10: Chris@10: (* various utility functions *) Chris@10: open List Chris@10: open Unix Chris@10: Chris@10: (***************************************** Chris@10: * Integer operations Chris@10: *****************************************) Chris@10: (* fint the inverse of n modulo m *) Chris@10: let invmod n m = Chris@10: let rec loop i = Chris@10: if ((i * n) mod m == 1) then i Chris@10: else loop (i + 1) Chris@10: in Chris@10: loop 1 Chris@10: Chris@10: (* Yooklid's algorithm *) Chris@10: let rec gcd n m = Chris@10: if (n > m) Chris@10: then gcd m n Chris@10: else Chris@10: let r = m mod n Chris@10: in Chris@10: if (r == 0) then n Chris@10: else gcd r n Chris@10: Chris@10: (* reduce the fraction m/n to lowest terms, modulo factors of n/n *) Chris@10: let lowest_terms n m = Chris@10: if (m mod n == 0) then Chris@10: (1,0) Chris@10: else Chris@10: let nn = (abs n) in let mm = m * (n / nn) Chris@10: in let mpos = Chris@10: if (mm > 0) then (mm mod nn) Chris@10: else (mm + (1 + (abs mm) / nn) * nn) mod nn Chris@10: and d = gcd nn (abs mm) Chris@10: in (nn / d, mpos / d) Chris@10: Chris@10: (* find a generator for the multiplicative group mod p Chris@10: (where p must be prime for a generator to exist!!) *) Chris@10: Chris@10: exception No_Generator Chris@10: Chris@10: let find_generator p = Chris@10: let rec period x prod = Chris@10: if (prod == 1) then 1 Chris@10: else 1 + (period x (prod * x mod p)) Chris@10: in let rec findgen x = Chris@10: if (x == 0) then raise No_Generator Chris@10: else if ((period x x) == (p - 1)) then x Chris@10: else findgen ((x + 1) mod p) Chris@10: in findgen 1 Chris@10: Chris@10: (* raise x to a power n modulo p (requires n > 0) (in principle, Chris@10: negative powers would be fine, provided that x and p are relatively Chris@10: prime...we don't need this functionality, though) *) Chris@10: Chris@10: exception Negative_Power Chris@10: Chris@10: let rec pow_mod x n p = Chris@10: if (n == 0) then 1 Chris@10: else if (n < 0) then raise Negative_Power Chris@10: else if (n mod 2 == 0) then pow_mod (x * x mod p) (n / 2) p Chris@10: else x * (pow_mod x (n - 1) p) mod p Chris@10: Chris@10: (****************************************** Chris@10: * auxiliary functions Chris@10: ******************************************) Chris@10: let rec forall id combiner a b f = Chris@10: if (a >= b) then id Chris@10: else combiner (f a) (forall id combiner (a + 1) b f) Chris@10: Chris@10: let sum_list l = fold_right (+) l 0 Chris@10: let max_list l = fold_right (max) l (-999999) Chris@10: let min_list l = fold_right (min) l 999999 Chris@10: let count pred = fold_left Chris@10: (fun a elem -> if (pred elem) then 1 + a else a) 0 Chris@10: let remove elem = List.filter (fun e -> (e != elem)) Chris@10: let cons a b = a :: b Chris@10: let null = function Chris@10: [] -> true Chris@10: | _ -> false Chris@10: let for_list l f = List.iter f l Chris@10: let rmap l f = List.map f l Chris@10: Chris@10: (* functional composition *) Chris@10: let (@@) f g x = f (g x) Chris@10: Chris@10: let forall_flat a b = forall [] (@) a b Chris@10: Chris@10: let identity x = x Chris@10: Chris@10: let rec minimize f = function Chris@10: [] -> None Chris@10: | elem :: rest -> Chris@10: match minimize f rest with Chris@10: None -> Some elem Chris@10: | Some x -> if (f x) >= (f elem) then Some elem else Some x Chris@10: Chris@10: Chris@10: let rec find_elem condition = function Chris@10: [] -> None Chris@10: | elem :: rest -> Chris@10: if condition elem then Chris@10: Some elem Chris@10: else Chris@10: find_elem condition rest Chris@10: Chris@10: Chris@10: (* find x, x >= a, such that (p x) is true *) Chris@10: let rec suchthat a pred = Chris@10: if (pred a) then a else suchthat (a + 1) pred Chris@10: Chris@10: (* print an information message *) Chris@10: let info string = Chris@10: if !Magic.verbose then begin Chris@10: let now = Unix.times () Chris@10: and pid = Unix.getpid () in Chris@10: prerr_string ((string_of_int pid) ^ ": " ^ Chris@10: "at t = " ^ (string_of_float now.tms_utime) ^ " : "); Chris@10: prerr_string (string ^ "\n"); Chris@10: flush Pervasives.stderr; Chris@10: end Chris@10: Chris@10: (* iota n produces the list [0; 1; ...; n - 1] *) Chris@10: let iota n = forall [] cons 0 n identity Chris@10: Chris@10: (* interval a b produces the list [a; 1; ...; b - 1] *) Chris@10: let interval a b = List.map ((+) a) (iota (b - a)) Chris@10: Chris@10: (* Chris@10: * freeze a function, i.e., compute it only once on demand, and Chris@10: * cache it into an array. Chris@10: *) Chris@10: let array n f = Chris@10: let a = Array.init n (fun i -> lazy (f i)) Chris@10: in fun i -> Lazy.force a.(i) Chris@10: Chris@10: Chris@10: let rec take n l = Chris@10: match (n, l) with Chris@10: (0, _) -> [] Chris@10: | (n, (a :: b)) -> a :: (take (n - 1) b) Chris@10: | _ -> failwith "take" Chris@10: Chris@10: let rec drop n l = Chris@10: match (n, l) with Chris@10: (0, _) -> l Chris@10: | (n, (_ :: b)) -> drop (n - 1) b Chris@10: | _ -> failwith "drop" Chris@10: Chris@10: Chris@10: let either a b = Chris@10: match a with Chris@10: Some x -> x Chris@10: | _ -> b