annotate src/fftw-3.3.3/genfft/oracle.ml @ 46:efe5b9f38b13

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