Mercurial > hg > sv-dependency-builds
comparison src/fftw-3.3.5/genfft/expr.ml @ 42:2cd0e3b3e1fd
Current fftw source
author | Chris Cannam |
---|---|
date | Tue, 18 Oct 2016 13:40:26 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
41:481f5f8c5634 | 42:2cd0e3b3e1fd |
---|---|
1 (* | |
2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology | |
3 * Copyright (c) 2003, 2007-14 Matteo Frigo | |
4 * Copyright (c) 2003, 2007-14 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 (* Here, we define the data type encapsulating a symbolic arithmetic | |
23 expression, and provide some routines for manipulating it. *) | |
24 | |
25 (* I will regret this hack : *) | |
26 (* NEWS: I did *) | |
27 type transcendent = I | MULTI_A | MULTI_B | CONJ | |
28 | |
29 type expr = | |
30 | Num of Number.number | |
31 | NaN of transcendent | |
32 | Plus of expr list | |
33 | Times of expr * expr | |
34 | CTimes of expr * expr | |
35 | CTimesJ of expr * expr (* CTimesJ (a, b) = conj(a) * b *) | |
36 | Uminus of expr | |
37 | Load of Variable.variable | |
38 | Store of Variable.variable * expr | |
39 | |
40 type assignment = Assign of Variable.variable * expr | |
41 | |
42 (* various hash functions *) | |
43 let hash_float x = | |
44 let (mantissa, exponent) = frexp x | |
45 in truncate (float_of_int(exponent) *. 1234.567 +. mantissa *. 10000.0) | |
46 | |
47 let sum_list l = List.fold_right (+) l 0 | |
48 | |
49 let transcendent_to_float = function | |
50 | I -> 2.718281828459045235360287471 (* any transcendent number will do *) | |
51 | MULTI_A -> 0.6931471805599453094172321214 | |
52 | MULTI_B -> -0.3665129205816643270124391582 | |
53 | CONJ -> 0.6019072301972345747375400015 | |
54 | |
55 let rec hash = function | |
56 | Num x -> hash_float (Number.to_float x) | |
57 | NaN x -> hash_float (transcendent_to_float x) | |
58 | Load v -> 1 + 1237 * Variable.hash v | |
59 | Store (v, x) -> 2 * Variable.hash v - 2345 * hash x | |
60 | Plus l -> 5 + 23451 * sum_list (List.map Hashtbl.hash l) | |
61 | Times (a, b) -> 41 + 31415 * (Hashtbl.hash a + Hashtbl.hash b) | |
62 | CTimes (a, b) -> 49 + 3245 * (Hashtbl.hash a + Hashtbl.hash b) | |
63 | CTimesJ (a, b) -> 31 + 3471 * (Hashtbl.hash a + Hashtbl.hash b) | |
64 | Uminus x -> 42 + 12345 * (hash x) | |
65 | |
66 (* find all variables *) | |
67 let rec find_vars x = | |
68 match x with | |
69 | Load y -> [y] | |
70 | Plus l -> List.flatten (List.map find_vars l) | |
71 | Times (a, b) -> (find_vars a) @ (find_vars b) | |
72 | CTimes (a, b) -> (find_vars a) @ (find_vars b) | |
73 | CTimesJ (a, b) -> (find_vars a) @ (find_vars b) | |
74 | Uminus a -> find_vars a | |
75 | _ -> [] | |
76 | |
77 | |
78 (* TRUE if expression is a constant *) | |
79 let is_constant = function | |
80 | Num _ -> true | |
81 | NaN _ -> true | |
82 | Load v -> Variable.is_constant v | |
83 | _ -> false | |
84 | |
85 let is_known_constant = function | |
86 | Num _ -> true | |
87 | NaN _ -> true | |
88 | _ -> false | |
89 | |
90 (* expr to string, used for debugging *) | |
91 let rec foldr_string_concat l = | |
92 match l with | |
93 [] -> "" | |
94 | [a] -> a | |
95 | a :: b -> a ^ " " ^ (foldr_string_concat b) | |
96 | |
97 let string_of_transcendent = function | |
98 | I -> "I" | |
99 | MULTI_A -> "MULTI_A" | |
100 | MULTI_B -> "MULTI_B" | |
101 | CONJ -> "CONJ" | |
102 | |
103 let rec to_string = function | |
104 | Load v -> Variable.unparse v | |
105 | Num n -> string_of_float (Number.to_float n) | |
106 | NaN n -> string_of_transcendent n | |
107 | Plus x -> "(+ " ^ (foldr_string_concat (List.map to_string x)) ^ ")" | |
108 | Times (a, b) -> "(* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")" | |
109 | CTimes (a, b) -> "(c* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")" | |
110 | CTimesJ (a, b) -> "(cj* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")" | |
111 | Uminus a -> "(- " ^ (to_string a) ^ ")" | |
112 | Store (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^ | |
113 (to_string a) ^ ")" | |
114 | |
115 let rec to_string_a d x = | |
116 if (d = 0) then "..." else match x with | |
117 | Load v -> Variable.unparse v | |
118 | Num n -> Number.to_konst n | |
119 | NaN n -> string_of_transcendent n | |
120 | Plus x -> "(+ " ^ (foldr_string_concat (List.map (to_string_a (d - 1)) x)) ^ ")" | |
121 | Times (a, b) -> "(* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")" | |
122 | CTimes (a, b) -> "(c* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")" | |
123 | CTimesJ (a, b) -> "(cj* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")" | |
124 | Uminus a -> "(- " ^ (to_string_a (d-1) a) ^ ")" | |
125 | Store (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^ | |
126 (to_string_a (d-1) a) ^ ")" | |
127 | |
128 let to_string = to_string_a 10 | |
129 | |
130 let assignment_to_string = function | |
131 | Assign (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^ (to_string a) ^ ")" | |
132 | |
133 let dump print = List.iter (fun x -> print ((assignment_to_string x) ^ "\n")) | |
134 | |
135 (* find all constants in a given expression *) | |
136 let rec expr_to_constants = function | |
137 | Num n -> [n] | |
138 | Plus a -> List.flatten (List.map expr_to_constants a) | |
139 | Times (a, b) -> (expr_to_constants a) @ (expr_to_constants b) | |
140 | CTimes (a, b) -> (expr_to_constants a) @ (expr_to_constants b) | |
141 | CTimesJ (a, b) -> (expr_to_constants a) @ (expr_to_constants b) | |
142 | Uminus a -> expr_to_constants a | |
143 | _ -> [] | |
144 | |
145 | |
146 let add_float_key_value list_so_far k = | |
147 if List.exists (fun k2 -> Number.equal k k2) list_so_far then | |
148 list_so_far | |
149 else | |
150 k :: list_so_far | |
151 | |
152 let unique_constants = List.fold_left add_float_key_value [] |