annotate src/fftw-3.3.8/genfft/number.ml @ 168:ceec0dd9ec9c

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 07 Feb 2020 11:51:13 +0000
parents bd3cc4d1df30
children
rev   line source
cannam@167 1 (*
cannam@167 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
cannam@167 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
cannam@167 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
cannam@167 5 *
cannam@167 6 * This program is free software; you can redistribute it and/or modify
cannam@167 7 * it under the terms of the GNU General Public License as published by
cannam@167 8 * the Free Software Foundation; either version 2 of the License, or
cannam@167 9 * (at your option) any later version.
cannam@167 10 *
cannam@167 11 * This program is distributed in the hope that it will be useful,
cannam@167 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@167 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@167 14 * GNU General Public License for more details.
cannam@167 15 *
cannam@167 16 * You should have received a copy of the GNU General Public License
cannam@167 17 * along with this program; if not, write to the Free Software
cannam@167 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@167 19 *
cannam@167 20 *)
cannam@167 21
cannam@167 22 (* The generator keeps track of numeric constants in symbolic
cannam@167 23 expressions using the abstract number type, defined in this file.
cannam@167 24
cannam@167 25 Our implementation of the number type uses arbitrary-precision
cannam@167 26 arithmetic from the built-in Num package in order to maintain an
cannam@167 27 accurate representation of constants. This allows us to output
cannam@167 28 constants with many decimal places in the generated C code,
cannam@167 29 ensuring that we will take advantage of the full precision
cannam@167 30 available on current and future machines.
cannam@167 31
cannam@167 32 Note that we have to write our own routine to compute roots of
cannam@167 33 unity, since the Num package only supplies simple arithmetic. The
cannam@167 34 arbitrary-precision operations in Num look like the normal
cannam@167 35 operations except that they have an appended slash (e.g. +/ -/ */
cannam@167 36 // etcetera). *)
cannam@167 37
cannam@167 38 open Num
cannam@167 39
cannam@167 40 type number = N of num
cannam@167 41
cannam@167 42 let makeNum n = N n
cannam@167 43
cannam@167 44 (* decimal digits of precision to maintain internally, and to print out: *)
cannam@167 45 let precision = 50
cannam@167 46 let print_precision = 45
cannam@167 47
cannam@167 48 let inveps = (Int 10) **/ (Int precision)
cannam@167 49 let epsilon = (Int 1) // inveps
cannam@167 50
cannam@167 51 let pinveps = (Int 10) **/ (Int print_precision)
cannam@167 52 let pepsilon = (Int 1) // pinveps
cannam@167 53
cannam@167 54 let round x = epsilon */ (round_num (x */ inveps))
cannam@167 55
cannam@167 56 let of_int n = N (Int n)
cannam@167 57 let zero = of_int 0
cannam@167 58 let one = of_int 1
cannam@167 59 let two = of_int 2
cannam@167 60 let mone = of_int (-1)
cannam@167 61
cannam@167 62 (* comparison predicate for real numbers *)
cannam@167 63 let equal (N x) (N y) = (* use both relative and absolute error *)
cannam@167 64 let absdiff = abs_num (x -/ y) in
cannam@167 65 absdiff <=/ pepsilon ||
cannam@167 66 absdiff <=/ pepsilon */ (abs_num x +/ abs_num y)
cannam@167 67
cannam@167 68 let is_zero = equal zero
cannam@167 69 let is_one = equal one
cannam@167 70 let is_mone = equal mone
cannam@167 71 let is_two = equal two
cannam@167 72
cannam@167 73
cannam@167 74 (* Note that, in the following computations, it is important to round
cannam@167 75 to precision epsilon after each operation. Otherwise, since the
cannam@167 76 Num package uses exact rational arithmetic, the number of digits
cannam@167 77 quickly blows up. *)
cannam@167 78 let mul (N a) (N b) = makeNum (round (a */ b))
cannam@167 79 let div (N a) (N b) = makeNum (round (a // b))
cannam@167 80 let add (N a) (N b) = makeNum (round (a +/ b))
cannam@167 81 let sub (N a) (N b) = makeNum (round (a -/ b))
cannam@167 82
cannam@167 83 let negative (N a) = (a </ (Int 0))
cannam@167 84 let negate (N a) = makeNum (minus_num a)
cannam@167 85
cannam@167 86 let greater a b = negative (sub b a)
cannam@167 87
cannam@167 88 let epsilonsq = epsilon */ epsilon
cannam@167 89 let epsilonsq2 = (Int 100) */ epsilonsq
cannam@167 90
cannam@167 91 let sqr a = a */ a
cannam@167 92 let almost_equal (N a) (N b) = (sqr (a -/ b)) <=/ epsilonsq2
cannam@167 93
cannam@167 94 (* find square root by Newton's method *)
cannam@167 95 let sqrt a =
cannam@167 96 let rec sqrt_iter guess =
cannam@167 97 let newguess = div (add guess (div a guess)) two in
cannam@167 98 if (almost_equal newguess guess) then newguess
cannam@167 99 else sqrt_iter newguess
cannam@167 100 in sqrt_iter (div a two)
cannam@167 101
cannam@167 102 let csub (xr, xi) (yr, yi) = (round (xr -/ yr), round (xi -/ yi))
cannam@167 103 let cdiv (xr, xi) r = (round (xr // r), round (xi // r))
cannam@167 104 let cmul (xr, xi) (yr, yi) = (round (xr */ yr -/ xi */ yi),
cannam@167 105 round (xr */ yi +/ xi */ yr))
cannam@167 106 let csqr (xr, xi) = (round (xr */ xr -/ xi */ xi), round ((Int 2) */ xr */ xi))
cannam@167 107 let cabssq (xr, xi) = xr */ xr +/ xi */ xi
cannam@167 108 let cconj (xr, xi) = (xr, minus_num xi)
cannam@167 109 let cinv x = cdiv (cconj x) (cabssq x)
cannam@167 110
cannam@167 111 let almost_equal_cnum (xr, xi) (yr, yi) =
cannam@167 112 (cabssq (xr -/ yr,xi -/ yi)) <=/ epsilonsq2
cannam@167 113
cannam@167 114 (* Put a complex number to an integer power by repeated squaring: *)
cannam@167 115 let rec ipow_cnum x n =
cannam@167 116 if (n == 0) then
cannam@167 117 (Int 1, Int 0)
cannam@167 118 else if (n < 0) then
cannam@167 119 cinv (ipow_cnum x (- n))
cannam@167 120 else if (n mod 2 == 0) then
cannam@167 121 ipow_cnum (csqr x) (n / 2)
cannam@167 122 else
cannam@167 123 cmul x (ipow_cnum x (n - 1))
cannam@167 124
cannam@167 125 let twopi = 6.28318530717958647692528676655900576839433879875021164194989
cannam@167 126
cannam@167 127 (* Find the nth (complex) primitive root of unity by Newton's method: *)
cannam@167 128 let primitive_root_of_unity n =
cannam@167 129 let rec root_iter guess =
cannam@167 130 let newguess = csub guess (cdiv (csub guess
cannam@167 131 (ipow_cnum guess (1 - n)))
cannam@167 132 (Int n)) in
cannam@167 133 if (almost_equal_cnum guess newguess) then newguess
cannam@167 134 else root_iter newguess
cannam@167 135 in let float_to_num f = (Int (truncate (f *. 1.0e9))) // (Int 1000000000)
cannam@167 136 in root_iter (float_to_num (cos (twopi /. (float n))),
cannam@167 137 float_to_num (sin (twopi /. (float n))))
cannam@167 138
cannam@167 139 let cexp n i =
cannam@167 140 if ((i mod n) == 0) then
cannam@167 141 (one,zero)
cannam@167 142 else
cannam@167 143 let (n2,i2) = Util.lowest_terms n i
cannam@167 144 in let (c,s) = ipow_cnum (primitive_root_of_unity n2) i2
cannam@167 145 in (makeNum c, makeNum s)
cannam@167 146
cannam@167 147 let to_konst (N n) =
cannam@167 148 let f = float_of_num n in
cannam@167 149 let f' = if f < 0.0 then f *. (-1.0) else f in
cannam@167 150 let f2 = if (f' >= 1.0) then (f' -. (float (truncate f'))) else f'
cannam@167 151 in let q = string_of_int (truncate(f2 *. 1.0E9))
cannam@167 152 in let r = "0000000000" ^ q
cannam@167 153 in let l = String.length r
cannam@167 154 in let prefix = if (f < 0.0) then "KN" else "KP" in
cannam@167 155 if (f' >= 1.0) then
cannam@167 156 (prefix ^ (string_of_int (truncate f')) ^ "_" ^
cannam@167 157 (String.sub r (l - 9) 9))
cannam@167 158 else
cannam@167 159 (prefix ^ (String.sub r (l - 9) 9))
cannam@167 160
cannam@167 161 let to_string (N n) = approx_num_fix print_precision n
cannam@167 162
cannam@167 163 let to_float (N n) = float_of_num n
cannam@167 164