annotate src/fftw-3.3.5/genfft/number.ml @ 58:eab3b14ddc95

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