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