| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file test_lg.cpp |
| 5 | /// Test log gamma function. |
| 6 | |
| 7 | #define WANT_STREAM |
| 8 | #define WANT_MATH |
| 9 | |
| 10 | #include "include.h" |
| 11 | #include "format.h" |
| 12 | |
| 13 | #include "newran.h" |
| 14 | |
| 15 | #ifdef use_namespace |
| 16 | using namespace RBD_LIBRARIES; |
| 17 | #endif |
| 18 | |
| 19 | // compare values calculated by ln_gamma_lanczos and ln_gamma near 0 and 1 |
| 20 | // ln_gamma_lanczos doesn't use special methods so this provides a cross-check |
| 21 | // of the special methods |
| 22 | Real spot_check(Real z) |
| 23 | { |
| 24 | Real z0 = floor(z + 0.5); |
| 25 | Real v1 = ln_gamma(z); |
| 26 | Real v2 = ln_gamma_lanczos(z) - ln_gamma_lanczos(z0); |
| 27 | return fabs((v1 - v2) / v2); |
| 28 | } |
| 29 | |
| 30 | // need this for CC |
| 31 | inline Real fabsx(long_Real x) { return fabs((Real)x); } |
| 32 | |
| 33 | int my_main() |
| 34 | { |
| 35 | Tracer et("my_main"); |
| 36 | int i; Real diff; |
| 37 | |
| 38 | Format SCI2; |
| 39 | SCI2.FormatType(Format::SCIENTIFIC); |
| 40 | SCI2.Precision(2); |
| 41 | SCI2.Width(10); |
| 42 | |
| 43 | |
| 44 | cout << "Test of production code for gamma function" << endl << endl; |
| 45 | Real max_error_pos = 0.0; |
| 46 | Real max_error_neg = 0.0; |
| 47 | |
| 48 | const long_Real gam_1_3 = 2.6789385347077476336L; |
| 49 | const long_Real gam_1_2 = 1.7724538509055160272L; |
| 50 | const long_Real gam_2_3 = 1.3541179394264004169L; |
| 51 | |
| 52 | long_Real Xp = 1; |
| 53 | long_Real X1_3p = gam_1_3; |
| 54 | long_Real X1_3n = gam_1_3; |
| 55 | long_Real X1_2p = gam_1_2; |
| 56 | long_Real X1_2n = gam_1_2; |
| 57 | long_Real X2_3p = gam_2_3; |
| 58 | long_Real X2_3n = gam_2_3; |
| 59 | |
| 60 | |
| 61 | cout << "Test values from -75 to 75" << endl; |
| 62 | for (i = 1; i <= 75; ++i) |
| 63 | { |
| 64 | int i1 = i - 1; |
| 65 | X1_3p *= (long_Real)i1 + 1.0L/3.0L; |
| 66 | X1_2p *= (long_Real)i1 + 1.0L/2.0L; |
| 67 | X2_3p *= (long_Real)i1 + 2.0L/3.0L; |
| 68 | Real diffp = fabsx( (Xp - rbd_gamma((Real)i)) / Xp ); |
| 69 | Xp *= i; |
| 70 | Real diff1_3p = fabsx( (X1_3p - rbd_gamma((Real)i + 1.0/3.0)) / X1_3p); |
| 71 | Real diff1_2p = fabsx( (X1_2p - rbd_gamma((Real)i + 1.0/2.0)) / X1_2p); |
| 72 | Real diff2_3p = fabsx( (X2_3p - rbd_gamma((Real)i + 2.0/3.0)) / X2_3p); |
| 73 | Real diff1_3n = fabsx( (X1_3n - rbd_gamma((Real)(-i1) + 1.0/3.0)) / X1_3n); |
| 74 | Real diff1_2n = fabsx( (X1_2n - rbd_gamma((Real)(-i1) + 1.0/2.0)) / X1_2n); |
| 75 | Real diff2_3n = fabsx( (X2_3n - rbd_gamma((Real)(-i1) + 2.0/3.0)) / X2_3n); |
| 76 | |
| 77 | X1_3n /= (long_Real)(-i) + 1.0L/3.0L; |
| 78 | X1_2n /= (long_Real)(-i) + 1.0L/2.0L; |
| 79 | X2_3n /= (long_Real)(-i) + 2.0L/3.0L; |
| 80 | |
| 81 | if (max_error_pos < diffp) max_error_pos = diffp; |
| 82 | if (max_error_pos < diff1_3p) max_error_pos = diff1_3p; |
| 83 | if (max_error_pos < diff1_2p) max_error_pos = diff1_2p; |
| 84 | if (max_error_pos < diff2_3p) max_error_pos = diff2_3p; |
| 85 | if (max_error_neg < diff1_3n) max_error_neg = diff1_3n; |
| 86 | if (max_error_neg < diff1_2n) max_error_neg = diff1_2n; |
| 87 | if (max_error_neg < diff2_3n) max_error_neg = diff2_3n; |
| 88 | |
| 89 | |
| 90 | } |
| 91 | cout << endl; |
| 92 | cout << "Maximum relative error" << endl; |
| 93 | cout << " arguments >= 1: " << SCI2 << max_error_pos << endl; |
| 94 | cout << " arguments < 1: " << SCI2 << max_error_neg << endl; |
| 95 | cout << endl; |
| 96 | |
| 97 | cout << "Now do logs" << endl; |
| 98 | Xp = 1; |
| 99 | X1_3p = gam_1_3; |
| 100 | X1_3n = gam_1_3; |
| 101 | X1_2p = gam_1_2; |
| 102 | X1_2n = gam_1_2; |
| 103 | X2_3p = gam_2_3; |
| 104 | X2_3n = gam_2_3; |
| 105 | |
| 106 | max_error_pos = max_error_neg = 0.0; int sign_error = 0; |
| 107 | for (i = 1; i <= 75; ++i) |
| 108 | { |
| 109 | int i1 = i - 1; |
| 110 | X1_3p *= (long_Real)i1 + 1.0L/3.0L; |
| 111 | X1_2p *= (long_Real)i1 + 1.0L/2.0L; |
| 112 | X2_3p *= (long_Real)i1 + 2.0L/3.0L; |
| 113 | Real LXp = log((Real)Xp); |
| 114 | Real LX1_3p = log((Real)X1_3p); |
| 115 | Real LX1_2p = log((Real)X1_2p); |
| 116 | Real LX2_3p = log((Real)X2_3p); |
| 117 | Real LX1_3n = log(fabsx(X1_3n)); |
| 118 | Real LX1_2n = log(fabsx(X1_2n)); |
| 119 | Real LX2_3n = log(fabsx(X2_3n)); |
| 120 | Real diffp = 0; |
| 121 | if (i > 2) diffp = fabsx( (LXp - ln_gamma((Real)i)) / LXp); |
| 122 | Xp *= i; |
| 123 | Real diff1_3p = fabsx( (LX1_3p - ln_gamma((Real)i + 1.0/3.0)) / LX1_3p); |
| 124 | Real diff1_2p = fabsx( (LX1_2p - ln_gamma((Real)i + 1.0/2.0)) / LX1_2p); |
| 125 | Real diff2_3p = fabsx( (LX2_3p - ln_gamma((Real)i + 2.0/3.0)) / LX2_3p); |
| 126 | int s1_3, s1_2, s2_3; |
| 127 | Real diff1_3n |
| 128 | = fabs( (LX1_3n - ln_gamma((Real)(-i1) + 1.0/3.0, s1_3)) / LX1_3n); |
| 129 | Real diff1_2n |
| 130 | = fabs( (LX1_2n - ln_gamma((Real)(-i1) + 1.0/2.0, s1_2)) / LX1_2n); |
| 131 | Real diff2_3n |
| 132 | = fabs( (LX2_3n - ln_gamma((Real)(-i1) + 2.0/3.0, s2_3)) / LX2_3n); |
| 133 | if (s1_3 * X1_3n < 0 || s1_2 * X1_2n < 0 || s2_3 * X2_3n < 0) |
| 134 | ++sign_error; |
| 135 | |
| 136 | X1_3n /= (long_Real)(-i) + 1.0L/3.0L; |
| 137 | X1_2n /= (long_Real)(-i) + 1.0L/2.0L; |
| 138 | X2_3n /= (long_Real)(-i) + 2.0L/3.0L; |
| 139 | |
| 140 | if (max_error_pos < diffp) max_error_pos = diffp; |
| 141 | if (max_error_pos < diff1_3p) max_error_pos = diff1_3p; |
| 142 | if (max_error_pos < diff1_2p) max_error_pos = diff1_2p; |
| 143 | if (max_error_pos < diff2_3p) max_error_pos = diff2_3p; |
| 144 | if (max_error_neg < diff1_3n) max_error_neg = diff1_3n; |
| 145 | if (max_error_neg < diff1_2n) max_error_neg = diff1_2n; |
| 146 | if (max_error_neg < diff2_3n) max_error_neg = diff2_3n; |
| 147 | |
| 148 | |
| 149 | } |
| 150 | cout << endl; |
| 151 | cout << "Maximum relative error" << endl; |
| 152 | cout << " arguments >= 1: " << SCI2 << max_error_pos << endl; |
| 153 | cout << " arguments < 1: " << SCI2 << max_error_neg << endl; |
| 154 | cout << "error at 1 (should be 0):" << SCI2 << ln_gamma(1.0) << endl; |
| 155 | cout << "error at 2 (should be 0):" << SCI2 << ln_gamma(2.0) << endl; |
| 156 | cout << endl; |
| 157 | cout << "Number of sign errors (should be 0) = " << sign_error << endl; |
| 158 | cout << endl; |
| 159 | |
| 160 | cout << "Test with integer z in range 100 to 10000" << endl << endl; |
| 161 | // compare with Lanczos method |
| 162 | max_error_pos = 0.0; |
| 163 | for (i = 100; i <= 10000; i += 100) |
| 164 | { |
| 165 | Real z = i; |
| 166 | Real lgl = ln_gamma_lanczos(z); |
| 167 | Real diff = fabs(ln_gamma(z) - lgl) / lgl; |
| 168 | if (max_error_pos < diff) max_error_pos = diff; |
| 169 | } |
| 170 | cout << "Maximum relative error = " << SCI2 << max_error_pos << endl; |
| 171 | cout << endl; |
| 172 | |
| 173 | cout << "Test for z near 1 or 2" << endl; |
| 174 | max_error_pos = 0.0; |
| 175 | diff = spot_check(0.901); if (max_error_pos < diff) max_error_pos = diff; |
| 176 | diff = spot_check(0.95); if (max_error_pos < diff) max_error_pos = diff; |
| 177 | diff = spot_check(0.99); if (max_error_pos < diff) max_error_pos = diff; |
| 178 | diff = spot_check(1.01); if (max_error_pos < diff) max_error_pos = diff; |
| 179 | diff = spot_check(1.05); if (max_error_pos < diff) max_error_pos = diff; |
| 180 | diff = spot_check(1.099); if (max_error_pos < diff) max_error_pos = diff; |
| 181 | diff = spot_check(1.901); if (max_error_pos < diff) max_error_pos = diff; |
| 182 | diff = spot_check(1.95); if (max_error_pos < diff) max_error_pos = diff; |
| 183 | diff = spot_check(1.99); if (max_error_pos < diff) max_error_pos = diff; |
| 184 | diff = spot_check(2.01); if (max_error_pos < diff) max_error_pos = diff; |
| 185 | diff = spot_check(2.05); if (max_error_pos < diff) max_error_pos = diff; |
| 186 | diff = spot_check(2.099); if (max_error_pos < diff) max_error_pos = diff; |
| 187 | |
| 188 | cout << "Maximum relative error = " << SCI2 << max_error_pos << endl; |
| 189 | cout << endl; |
| 190 | |
| 191 | |
| 192 | return 0; |
| 193 | |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | int main() |
| 203 | { |
| 204 | |
| 205 | Try |
| 206 | { |
| 207 | return my_main(); |
| 208 | } |
| 209 | Catch(Exception) |
| 210 | { |
| 211 | cout << "\nProgram fails - exception generated\n\n"; |
| 212 | cout << Exception::what() << "\n"; |
| 213 | } |
| 214 | CatchAll |
| 215 | { |
| 216 | cout << "\nProgram fails - exception generated\n\n"; |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | ///@} |
| 223 | |
| 224 | |
| 225 | |
| 226 |