annotate fft/fftw/fftw-3.3.4/genfft/number.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 (* The generator keeps track of numeric constants in symbolic
Chris@19 23 expressions using the abstract number type, defined in this file.
Chris@19 24
Chris@19 25 Our implementation of the number type uses arbitrary-precision
Chris@19 26 arithmetic from the built-in Num package in order to maintain an
Chris@19 27 accurate representation of constants. This allows us to output
Chris@19 28 constants with many decimal places in the generated C code,
Chris@19 29 ensuring that we will take advantage of the full precision
Chris@19 30 available on current and future machines.
Chris@19 31
Chris@19 32 Note that we have to write our own routine to compute roots of
Chris@19 33 unity, since the Num package only supplies simple arithmetic. The
Chris@19 34 arbitrary-precision operations in Num look like the normal
Chris@19 35 operations except that they have an appended slash (e.g. +/ -/ */
Chris@19 36 // etcetera). *)
Chris@19 37
Chris@19 38 open Num
Chris@19 39
Chris@19 40 type number = N of num
Chris@19 41
Chris@19 42 let makeNum n = N n
Chris@19 43
Chris@19 44 (* decimal digits of precision to maintain internally, and to print out: *)
Chris@19 45 let precision = 50
Chris@19 46 let print_precision = 45
Chris@19 47
Chris@19 48 let inveps = (Int 10) **/ (Int precision)
Chris@19 49 let epsilon = (Int 1) // inveps
Chris@19 50
Chris@19 51 let pinveps = (Int 10) **/ (Int print_precision)
Chris@19 52 let pepsilon = (Int 1) // pinveps
Chris@19 53
Chris@19 54 let round x = epsilon */ (round_num (x */ inveps))
Chris@19 55
Chris@19 56 let of_int n = N (Int n)
Chris@19 57 let zero = of_int 0
Chris@19 58 let one = of_int 1
Chris@19 59 let two = of_int 2
Chris@19 60 let mone = of_int (-1)
Chris@19 61
Chris@19 62 (* comparison predicate for real numbers *)
Chris@19 63 let equal (N x) (N y) = (* use both relative and absolute error *)
Chris@19 64 let absdiff = abs_num (x -/ y) in
Chris@19 65 absdiff <=/ pepsilon or
Chris@19 66 absdiff <=/ pepsilon */ (abs_num x +/ abs_num y)
Chris@19 67
Chris@19 68 let is_zero = equal zero
Chris@19 69 let is_one = equal one
Chris@19 70 let is_mone = equal mone
Chris@19 71 let is_two = equal two
Chris@19 72
Chris@19 73
Chris@19 74 (* Note that, in the following computations, it is important to round
Chris@19 75 to precision epsilon after each operation. Otherwise, since the
Chris@19 76 Num package uses exact rational arithmetic, the number of digits
Chris@19 77 quickly blows up. *)
Chris@19 78 let mul (N a) (N b) = makeNum (round (a */ b))
Chris@19 79 let div (N a) (N b) = makeNum (round (a // b))
Chris@19 80 let add (N a) (N b) = makeNum (round (a +/ b))
Chris@19 81 let sub (N a) (N b) = makeNum (round (a -/ b))
Chris@19 82
Chris@19 83 let negative (N a) = (a </ (Int 0))
Chris@19 84 let negate (N a) = makeNum (minus_num a)
Chris@19 85
Chris@19 86 let greater a b = negative (sub b a)
Chris@19 87
Chris@19 88 let epsilonsq = epsilon */ epsilon
Chris@19 89 let epsilonsq2 = (Int 100) */ epsilonsq
Chris@19 90
Chris@19 91 let sqr a = a */ a
Chris@19 92 let almost_equal (N a) (N b) = (sqr (a -/ b)) <=/ epsilonsq2
Chris@19 93
Chris@19 94 (* find square root by Newton's method *)
Chris@19 95 let sqrt a =
Chris@19 96 let rec sqrt_iter guess =
Chris@19 97 let newguess = div (add guess (div a guess)) two in
Chris@19 98 if (almost_equal newguess guess) then newguess
Chris@19 99 else sqrt_iter newguess
Chris@19 100 in sqrt_iter (div a two)
Chris@19 101
Chris@19 102 let csub (xr, xi) (yr, yi) = (round (xr -/ yr), round (xi -/ yi))
Chris@19 103 let cdiv (xr, xi) r = (round (xr // r), round (xi // r))
Chris@19 104 let cmul (xr, xi) (yr, yi) = (round (xr */ yr -/ xi */ yi),
Chris@19 105 round (xr */ yi +/ xi */ yr))
Chris@19 106 let csqr (xr, xi) = (round (xr */ xr -/ xi */ xi), round ((Int 2) */ xr */ xi))
Chris@19 107 let cabssq (xr, xi) = xr */ xr +/ xi */ xi
Chris@19 108 let cconj (xr, xi) = (xr, minus_num xi)
Chris@19 109 let cinv x = cdiv (cconj x) (cabssq x)
Chris@19 110
Chris@19 111 let almost_equal_cnum (xr, xi) (yr, yi) =
Chris@19 112 (cabssq (xr -/ yr,xi -/ yi)) <=/ epsilonsq2
Chris@19 113
Chris@19 114 (* Put a complex number to an integer power by repeated squaring: *)
Chris@19 115 let rec ipow_cnum x n =
Chris@19 116 if (n == 0) then
Chris@19 117 (Int 1, Int 0)
Chris@19 118 else if (n < 0) then
Chris@19 119 cinv (ipow_cnum x (- n))
Chris@19 120 else if (n mod 2 == 0) then
Chris@19 121 ipow_cnum (csqr x) (n / 2)
Chris@19 122 else
Chris@19 123 cmul x (ipow_cnum x (n - 1))
Chris@19 124
Chris@19 125 let twopi = 6.28318530717958647692528676655900576839433879875021164194989
Chris@19 126
Chris@19 127 (* Find the nth (complex) primitive root of unity by Newton's method: *)
Chris@19 128 let primitive_root_of_unity n =
Chris@19 129 let rec root_iter guess =
Chris@19 130 let newguess = csub guess (cdiv (csub guess
Chris@19 131 (ipow_cnum guess (1 - n)))
Chris@19 132 (Int n)) in
Chris@19 133 if (almost_equal_cnum guess newguess) then newguess
Chris@19 134 else root_iter newguess
Chris@19 135 in let float_to_num f = (Int (truncate (f *. 1.0e9))) // (Int 1000000000)
Chris@19 136 in root_iter (float_to_num (cos (twopi /. (float n))),
Chris@19 137 float_to_num (sin (twopi /. (float n))))
Chris@19 138
Chris@19 139 let cexp n i =
Chris@19 140 if ((i mod n) == 0) then
Chris@19 141 (one,zero)
Chris@19 142 else
Chris@19 143 let (n2,i2) = Util.lowest_terms n i
Chris@19 144 in let (c,s) = ipow_cnum (primitive_root_of_unity n2) i2
Chris@19 145 in (makeNum c, makeNum s)
Chris@19 146
Chris@19 147 let to_konst (N n) =
Chris@19 148 let f = float_of_num n in
Chris@19 149 let f' = if f < 0.0 then f *. (-1.0) else f in
Chris@19 150 let f2 = if (f' >= 1.0) then (f' -. (float (truncate f'))) else f'
Chris@19 151 in let q = string_of_int (truncate(f2 *. 1.0E9))
Chris@19 152 in let r = "0000000000" ^ q
Chris@19 153 in let l = String.length r
Chris@19 154 in let prefix = if (f < 0.0) then "KN" else "KP" in
Chris@19 155 if (f' >= 1.0) then
Chris@19 156 (prefix ^ (string_of_int (truncate f')) ^ "_" ^
Chris@19 157 (String.sub r (l - 9) 9))
Chris@19 158 else
Chris@19 159 (prefix ^ (String.sub r (l - 9) 9))
Chris@19 160
Chris@19 161 let to_string (N n) = approx_num_fix print_precision n
Chris@19 162
Chris@19 163 let to_float (N n) = float_of_num n
Chris@19 164