annotate src/fftw-3.3.8/genfft/util.ml @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents d0c2a83c1364
children
rev   line source
Chris@82 1 (*
Chris@82 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@82 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@82 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@82 5 *
Chris@82 6 * This program is free software; you can redistribute it and/or modify
Chris@82 7 * it under the terms of the GNU General Public License as published by
Chris@82 8 * the Free Software Foundation; either version 2 of the License, or
Chris@82 9 * (at your option) any later version.
Chris@82 10 *
Chris@82 11 * This program is distributed in the hope that it will be useful,
Chris@82 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@82 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@82 14 * GNU General Public License for more details.
Chris@82 15 *
Chris@82 16 * You should have received a copy of the GNU General Public License
Chris@82 17 * along with this program; if not, write to the Free Software
Chris@82 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@82 19 *
Chris@82 20 *)
Chris@82 21
Chris@82 22 (* various utility functions *)
Chris@82 23 open List
Chris@82 24 open Unix
Chris@82 25
Chris@82 26 (*****************************************
Chris@82 27 * Integer operations
Chris@82 28 *****************************************)
Chris@82 29 (* fint the inverse of n modulo m *)
Chris@82 30 let invmod n m =
Chris@82 31 let rec loop i =
Chris@82 32 if ((i * n) mod m == 1) then i
Chris@82 33 else loop (i + 1)
Chris@82 34 in
Chris@82 35 loop 1
Chris@82 36
Chris@82 37 (* Yooklid's algorithm *)
Chris@82 38 let rec gcd n m =
Chris@82 39 if (n > m)
Chris@82 40 then gcd m n
Chris@82 41 else
Chris@82 42 let r = m mod n
Chris@82 43 in
Chris@82 44 if (r == 0) then n
Chris@82 45 else gcd r n
Chris@82 46
Chris@82 47 (* reduce the fraction m/n to lowest terms, modulo factors of n/n *)
Chris@82 48 let lowest_terms n m =
Chris@82 49 if (m mod n == 0) then
Chris@82 50 (1,0)
Chris@82 51 else
Chris@82 52 let nn = (abs n) in let mm = m * (n / nn)
Chris@82 53 in let mpos =
Chris@82 54 if (mm > 0) then (mm mod nn)
Chris@82 55 else (mm + (1 + (abs mm) / nn) * nn) mod nn
Chris@82 56 and d = gcd nn (abs mm)
Chris@82 57 in (nn / d, mpos / d)
Chris@82 58
Chris@82 59 (* find a generator for the multiplicative group mod p
Chris@82 60 (where p must be prime for a generator to exist!!) *)
Chris@82 61
Chris@82 62 exception No_Generator
Chris@82 63
Chris@82 64 let find_generator p =
Chris@82 65 let rec period x prod =
Chris@82 66 if (prod == 1) then 1
Chris@82 67 else 1 + (period x (prod * x mod p))
Chris@82 68 in let rec findgen x =
Chris@82 69 if (x == 0) then raise No_Generator
Chris@82 70 else if ((period x x) == (p - 1)) then x
Chris@82 71 else findgen ((x + 1) mod p)
Chris@82 72 in findgen 1
Chris@82 73
Chris@82 74 (* raise x to a power n modulo p (requires n > 0) (in principle,
Chris@82 75 negative powers would be fine, provided that x and p are relatively
Chris@82 76 prime...we don't need this functionality, though) *)
Chris@82 77
Chris@82 78 exception Negative_Power
Chris@82 79
Chris@82 80 let rec pow_mod x n p =
Chris@82 81 if (n == 0) then 1
Chris@82 82 else if (n < 0) then raise Negative_Power
Chris@82 83 else if (n mod 2 == 0) then pow_mod (x * x mod p) (n / 2) p
Chris@82 84 else x * (pow_mod x (n - 1) p) mod p
Chris@82 85
Chris@82 86 (******************************************
Chris@82 87 * auxiliary functions
Chris@82 88 ******************************************)
Chris@82 89 let rec forall id combiner a b f =
Chris@82 90 if (a >= b) then id
Chris@82 91 else combiner (f a) (forall id combiner (a + 1) b f)
Chris@82 92
Chris@82 93 let sum_list l = fold_right (+) l 0
Chris@82 94 let max_list l = fold_right (max) l (-999999)
Chris@82 95 let min_list l = fold_right (min) l 999999
Chris@82 96 let count pred = fold_left
Chris@82 97 (fun a elem -> if (pred elem) then 1 + a else a) 0
Chris@82 98 let remove elem = List.filter (fun e -> (e != elem))
Chris@82 99 let cons a b = a :: b
Chris@82 100 let null = function
Chris@82 101 [] -> true
Chris@82 102 | _ -> false
Chris@82 103 let for_list l f = List.iter f l
Chris@82 104 let rmap l f = List.map f l
Chris@82 105
Chris@82 106 (* functional composition *)
Chris@82 107 let (@@) f g x = f (g x)
Chris@82 108
Chris@82 109 let forall_flat a b = forall [] (@) a b
Chris@82 110
Chris@82 111 let identity x = x
Chris@82 112
Chris@82 113 let rec minimize f = function
Chris@82 114 [] -> None
Chris@82 115 | elem :: rest ->
Chris@82 116 match minimize f rest with
Chris@82 117 None -> Some elem
Chris@82 118 | Some x -> if (f x) >= (f elem) then Some elem else Some x
Chris@82 119
Chris@82 120
Chris@82 121 let rec find_elem condition = function
Chris@82 122 [] -> None
Chris@82 123 | elem :: rest ->
Chris@82 124 if condition elem then
Chris@82 125 Some elem
Chris@82 126 else
Chris@82 127 find_elem condition rest
Chris@82 128
Chris@82 129
Chris@82 130 (* find x, x >= a, such that (p x) is true *)
Chris@82 131 let rec suchthat a pred =
Chris@82 132 if (pred a) then a else suchthat (a + 1) pred
Chris@82 133
Chris@82 134 (* print an information message *)
Chris@82 135 let info string =
Chris@82 136 if !Magic.verbose then begin
Chris@82 137 let now = Unix.times ()
Chris@82 138 and pid = Unix.getpid () in
Chris@82 139 prerr_string ((string_of_int pid) ^ ": " ^
Chris@82 140 "at t = " ^ (string_of_float now.tms_utime) ^ " : ");
Chris@82 141 prerr_string (string ^ "\n");
Chris@82 142 flush Pervasives.stderr;
Chris@82 143 end
Chris@82 144
Chris@82 145 (* iota n produces the list [0; 1; ...; n - 1] *)
Chris@82 146 let iota n = forall [] cons 0 n identity
Chris@82 147
Chris@82 148 (* interval a b produces the list [a; 1; ...; b - 1] *)
Chris@82 149 let interval a b = List.map ((+) a) (iota (b - a))
Chris@82 150
Chris@82 151 (*
Chris@82 152 * freeze a function, i.e., compute it only once on demand, and
Chris@82 153 * cache it into an array.
Chris@82 154 *)
Chris@82 155 let array n f =
Chris@82 156 let a = Array.init n (fun i -> lazy (f i))
Chris@82 157 in fun i -> Lazy.force a.(i)
Chris@82 158
Chris@82 159
Chris@82 160 let rec take n l =
Chris@82 161 match (n, l) with
Chris@82 162 (0, _) -> []
Chris@82 163 | (n, (a :: b)) -> a :: (take (n - 1) b)
Chris@82 164 | _ -> failwith "take"
Chris@82 165
Chris@82 166 let rec drop n l =
Chris@82 167 match (n, l) with
Chris@82 168 (0, _) -> l
Chris@82 169 | (n, (_ :: b)) -> drop (n - 1) b
Chris@82 170 | _ -> failwith "drop"
Chris@82 171
Chris@82 172
Chris@82 173 let either a b =
Chris@82 174 match a with
Chris@82 175 Some x -> x
Chris@82 176 | _ -> b