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