annotate src/fftw-3.3.5/genfft/number.ml @ 158:fa7c54aeb697

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