annotate src/fftw-3.3.3/genfft/number.ml @ 140:59a8758c56b1

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