annotate fft/fftw/fftw-3.3.4/genfft/oracle.ml @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 (*
Chris@19 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@19 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 5 *
Chris@19 6 * This program is free software; you can redistribute it and/or modify
Chris@19 7 * it under the terms of the GNU General Public License as published by
Chris@19 8 * the Free Software Foundation; either version 2 of the License, or
Chris@19 9 * (at your option) any later version.
Chris@19 10 *
Chris@19 11 * This program is distributed in the hope that it will be useful,
Chris@19 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 14 * GNU General Public License for more details.
Chris@19 15 *
Chris@19 16 * You should have received a copy of the GNU General Public License
Chris@19 17 * along with this program; if not, write to the Free Software
Chris@19 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 19 *
Chris@19 20 *)
Chris@19 21
Chris@19 22 (*
Chris@19 23 * the oracle decrees whether the sign of an expression should
Chris@19 24 * be changed.
Chris@19 25 *
Chris@19 26 * Say the expression (A - B) appears somewhere. Elsewhere in the
Chris@19 27 * expression dag the expression (B - A) may appear.
Chris@19 28 * The oracle determines which of the two forms is canonical.
Chris@19 29 *
Chris@19 30 * Algorithm: evaluate the expression at a random input, and
Chris@19 31 * keep the expression with the positive sign.
Chris@19 32 *)
Chris@19 33
Chris@19 34 let make_memoizer hash equal =
Chris@19 35 let table = ref Assoctable.empty
Chris@19 36 in
Chris@19 37 (fun f k ->
Chris@19 38 match Assoctable.lookup hash equal k !table with
Chris@19 39 Some value -> value
Chris@19 40 | None ->
Chris@19 41 let value = f k in
Chris@19 42 begin
Chris@19 43 table := Assoctable.insert hash k value !table;
Chris@19 44 value
Chris@19 45 end)
Chris@19 46
Chris@19 47 let almost_equal x y =
Chris@19 48 let epsilon = 1.0E-8 in
Chris@19 49 (abs_float (x -. y) < epsilon) ||
Chris@19 50 (abs_float (x -. y) < epsilon *. (abs_float x +. abs_float y))
Chris@19 51
Chris@19 52 let absid = make_memoizer
Chris@19 53 (fun x -> Expr.hash_float (abs_float x))
Chris@19 54 (fun a b -> almost_equal a b || almost_equal (-. a) b)
Chris@19 55 (fun x -> x)
Chris@19 56
Chris@19 57 let make_random_oracle () = make_memoizer
Chris@19 58 Variable.hash
Chris@19 59 Variable.same
Chris@19 60 (fun _ -> (float (Random.bits())) /. 1073741824.0)
Chris@19 61
Chris@19 62 let the_random_oracle = make_random_oracle ()
Chris@19 63
Chris@19 64 let sum_list l = List.fold_right (+.) l 0.0
Chris@19 65
Chris@19 66 let eval_aux random_oracle =
Chris@19 67 let memoizing = make_memoizer Expr.hash (==) in
Chris@19 68 let rec eval x =
Chris@19 69 memoizing
Chris@19 70 (function
Chris@19 71 | Expr.Num x -> Number.to_float x
Chris@19 72 | Expr.NaN x -> Expr.transcendent_to_float x
Chris@19 73 | Expr.Load v -> random_oracle v
Chris@19 74 | Expr.Store (v, x) -> eval x
Chris@19 75 | Expr.Plus l -> sum_list (List.map eval l)
Chris@19 76 | Expr.Times (a, b) -> (eval a) *. (eval b)
Chris@19 77 | Expr.CTimes (a, b) ->
Chris@19 78 1.098612288668109691395245236 +.
Chris@19 79 1.609437912434100374600759333 *. (eval a) *. (eval b)
Chris@19 80 | Expr.CTimesJ (a, b) ->
Chris@19 81 0.9102392266268373936142401657 +.
Chris@19 82 0.6213349345596118107071993881 *. (eval a) *. (eval b)
Chris@19 83 | Expr.Uminus x -> -. (eval x))
Chris@19 84 x
Chris@19 85 in eval
Chris@19 86
Chris@19 87 let eval = eval_aux the_random_oracle
Chris@19 88
Chris@19 89 let should_flip_sign node =
Chris@19 90 let v = eval node in
Chris@19 91 let v' = absid v in
Chris@19 92 not (almost_equal v v')
Chris@19 93
Chris@19 94 (*
Chris@19 95 * determine with high probability if two expressions are equal.
Chris@19 96 *
Chris@19 97 * The test is randomized: if the two expressions have the
Chris@19 98 * same value for NTESTS random inputs, then they are proclaimed
Chris@19 99 * equal. (Note that two distinct linear functions L1(x0, x1, ..., xn)
Chris@19 100 * and L2(x0, x1, ..., xn) have the same value with probability
Chris@19 101 * 0 for random x's, and thus this test is way more paranoid than
Chris@19 102 * necessary.)
Chris@19 103 *)
Chris@19 104 let likely_equal a b =
Chris@19 105 let tolerance = 1.0e-8
Chris@19 106 and ntests = 20
Chris@19 107 in
Chris@19 108 let rec loop n =
Chris@19 109 if n = 0 then
Chris@19 110 true
Chris@19 111 else
Chris@19 112 let r = make_random_oracle () in
Chris@19 113 let va = eval_aux r a
Chris@19 114 and vb = eval_aux r b
Chris@19 115 in
Chris@19 116 if (abs_float (va -. vb)) >
Chris@19 117 tolerance *. (abs_float va +. abs_float vb +. 0.0001)
Chris@19 118 then
Chris@19 119 false
Chris@19 120 else
Chris@19 121 loop (n - 1)
Chris@19 122 in
Chris@19 123 match (a, b) with
Chris@19 124
Chris@19 125 (*
Chris@19 126 * Because of the way eval is constructed, we have
Chris@19 127 * eval (Store (v, x)) == eval x
Chris@19 128 * However, we never consider the two expressions equal
Chris@19 129 *)
Chris@19 130 | (Expr.Store _, _) -> false
Chris@19 131 | (_, Expr.Store _) -> false
Chris@19 132
Chris@19 133 (*
Chris@19 134 * Expressions of the form ``Uminus (Store _)''
Chris@19 135 * are artifacts of algsimp
Chris@19 136 *)
Chris@19 137 | ((Expr.Uminus (Expr.Store _)), _) -> false
Chris@19 138 | (_, Expr.Uminus (Expr.Store _)) -> false
Chris@19 139
Chris@19 140 | _ -> loop ntests
Chris@19 141
Chris@19 142 let hash x =
Chris@19 143 let f = eval x in
Chris@19 144 truncate (f *. 65536.0)