annotate src/fftw-3.3.8/genfft/expr.ml @ 82:d0c2a83c1364

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