annotate src/fftw-3.3.5/genfft/expr.ml @ 169:223a55898ab9 tip default

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