annotate src/fftw-3.3.5/genfft/expr.ml @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 2cd0e3b3e1fd
children
rev   line source
Chris@42 1 (*
Chris@42 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@42 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@42 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@42 5 *
Chris@42 6 * This program is free software; you can redistribute it and/or modify
Chris@42 7 * it under the terms of the GNU General Public License as published by
Chris@42 8 * the Free Software Foundation; either version 2 of the License, or
Chris@42 9 * (at your option) any later version.
Chris@42 10 *
Chris@42 11 * This program is distributed in the hope that it will be useful,
Chris@42 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@42 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@42 14 * GNU General Public License for more details.
Chris@42 15 *
Chris@42 16 * You should have received a copy of the GNU General Public License
Chris@42 17 * along with this program; if not, write to the Free Software
Chris@42 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@42 19 *
Chris@42 20 *)
Chris@42 21
Chris@42 22 (* Here, we define the data type encapsulating a symbolic arithmetic
Chris@42 23 expression, and provide some routines for manipulating it. *)
Chris@42 24
Chris@42 25 (* I will regret this hack : *)
Chris@42 26 (* NEWS: I did *)
Chris@42 27 type transcendent = I | MULTI_A | MULTI_B | CONJ
Chris@42 28
Chris@42 29 type expr =
Chris@42 30 | Num of Number.number
Chris@42 31 | NaN of transcendent
Chris@42 32 | Plus of expr list
Chris@42 33 | Times of expr * expr
Chris@42 34 | CTimes of expr * expr
Chris@42 35 | CTimesJ of expr * expr (* CTimesJ (a, b) = conj(a) * b *)
Chris@42 36 | Uminus of expr
Chris@42 37 | Load of Variable.variable
Chris@42 38 | Store of Variable.variable * expr
Chris@42 39
Chris@42 40 type assignment = Assign of Variable.variable * expr
Chris@42 41
Chris@42 42 (* various hash functions *)
Chris@42 43 let hash_float x =
Chris@42 44 let (mantissa, exponent) = frexp x
Chris@42 45 in truncate (float_of_int(exponent) *. 1234.567 +. mantissa *. 10000.0)
Chris@42 46
Chris@42 47 let sum_list l = List.fold_right (+) l 0
Chris@42 48
Chris@42 49 let transcendent_to_float = function
Chris@42 50 | I -> 2.718281828459045235360287471 (* any transcendent number will do *)
Chris@42 51 | MULTI_A -> 0.6931471805599453094172321214
Chris@42 52 | MULTI_B -> -0.3665129205816643270124391582
Chris@42 53 | CONJ -> 0.6019072301972345747375400015
Chris@42 54
Chris@42 55 let rec hash = function
Chris@42 56 | Num x -> hash_float (Number.to_float x)
Chris@42 57 | NaN x -> hash_float (transcendent_to_float x)
Chris@42 58 | Load v -> 1 + 1237 * Variable.hash v
Chris@42 59 | Store (v, x) -> 2 * Variable.hash v - 2345 * hash x
Chris@42 60 | Plus l -> 5 + 23451 * sum_list (List.map Hashtbl.hash l)
Chris@42 61 | Times (a, b) -> 41 + 31415 * (Hashtbl.hash a + Hashtbl.hash b)
Chris@42 62 | CTimes (a, b) -> 49 + 3245 * (Hashtbl.hash a + Hashtbl.hash b)
Chris@42 63 | CTimesJ (a, b) -> 31 + 3471 * (Hashtbl.hash a + Hashtbl.hash b)
Chris@42 64 | Uminus x -> 42 + 12345 * (hash x)
Chris@42 65
Chris@42 66 (* find all variables *)
Chris@42 67 let rec find_vars x =
Chris@42 68 match x with
Chris@42 69 | Load y -> [y]
Chris@42 70 | Plus l -> List.flatten (List.map find_vars l)
Chris@42 71 | Times (a, b) -> (find_vars a) @ (find_vars b)
Chris@42 72 | CTimes (a, b) -> (find_vars a) @ (find_vars b)
Chris@42 73 | CTimesJ (a, b) -> (find_vars a) @ (find_vars b)
Chris@42 74 | Uminus a -> find_vars a
Chris@42 75 | _ -> []
Chris@42 76
Chris@42 77
Chris@42 78 (* TRUE if expression is a constant *)
Chris@42 79 let is_constant = function
Chris@42 80 | Num _ -> true
Chris@42 81 | NaN _ -> true
Chris@42 82 | Load v -> Variable.is_constant v
Chris@42 83 | _ -> false
Chris@42 84
Chris@42 85 let is_known_constant = function
Chris@42 86 | Num _ -> true
Chris@42 87 | NaN _ -> true
Chris@42 88 | _ -> false
Chris@42 89
Chris@42 90 (* expr to string, used for debugging *)
Chris@42 91 let rec foldr_string_concat l =
Chris@42 92 match l with
Chris@42 93 [] -> ""
Chris@42 94 | [a] -> a
Chris@42 95 | a :: b -> a ^ " " ^ (foldr_string_concat b)
Chris@42 96
Chris@42 97 let string_of_transcendent = function
Chris@42 98 | I -> "I"
Chris@42 99 | MULTI_A -> "MULTI_A"
Chris@42 100 | MULTI_B -> "MULTI_B"
Chris@42 101 | CONJ -> "CONJ"
Chris@42 102
Chris@42 103 let rec to_string = function
Chris@42 104 | Load v -> Variable.unparse v
Chris@42 105 | Num n -> string_of_float (Number.to_float n)
Chris@42 106 | NaN n -> string_of_transcendent n
Chris@42 107 | Plus x -> "(+ " ^ (foldr_string_concat (List.map to_string x)) ^ ")"
Chris@42 108 | Times (a, b) -> "(* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")"
Chris@42 109 | CTimes (a, b) -> "(c* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")"
Chris@42 110 | CTimesJ (a, b) -> "(cj* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")"
Chris@42 111 | Uminus a -> "(- " ^ (to_string a) ^ ")"
Chris@42 112 | Store (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^
Chris@42 113 (to_string a) ^ ")"
Chris@42 114
Chris@42 115 let rec to_string_a d x =
Chris@42 116 if (d = 0) then "..." else match x with
Chris@42 117 | Load v -> Variable.unparse v
Chris@42 118 | Num n -> Number.to_konst n
Chris@42 119 | NaN n -> string_of_transcendent n
Chris@42 120 | Plus x -> "(+ " ^ (foldr_string_concat (List.map (to_string_a (d - 1)) x)) ^ ")"
Chris@42 121 | Times (a, b) -> "(* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")"
Chris@42 122 | CTimes (a, b) -> "(c* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")"
Chris@42 123 | CTimesJ (a, b) -> "(cj* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")"
Chris@42 124 | Uminus a -> "(- " ^ (to_string_a (d-1) a) ^ ")"
Chris@42 125 | Store (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^
Chris@42 126 (to_string_a (d-1) a) ^ ")"
Chris@42 127
Chris@42 128 let to_string = to_string_a 10
Chris@42 129
Chris@42 130 let assignment_to_string = function
Chris@42 131 | Assign (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^ (to_string a) ^ ")"
Chris@42 132
Chris@42 133 let dump print = List.iter (fun x -> print ((assignment_to_string x) ^ "\n"))
Chris@42 134
Chris@42 135 (* find all constants in a given expression *)
Chris@42 136 let rec expr_to_constants = function
Chris@42 137 | Num n -> [n]
Chris@42 138 | Plus a -> List.flatten (List.map expr_to_constants a)
Chris@42 139 | Times (a, b) -> (expr_to_constants a) @ (expr_to_constants b)
Chris@42 140 | CTimes (a, b) -> (expr_to_constants a) @ (expr_to_constants b)
Chris@42 141 | CTimesJ (a, b) -> (expr_to_constants a) @ (expr_to_constants b)
Chris@42 142 | Uminus a -> expr_to_constants a
Chris@42 143 | _ -> []
Chris@42 144
Chris@42 145
Chris@42 146 let add_float_key_value list_so_far k =
Chris@42 147 if List.exists (fun k2 -> Number.equal k k2) list_so_far then
Chris@42 148 list_so_far
Chris@42 149 else
Chris@42 150 k :: list_so_far
Chris@42 151
Chris@42 152 let unique_constants = List.fold_left add_float_key_value []