| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-options "-D__STDCPP_WANT_MATH_SPEC_FUNCS__" } |
| 3 | // |
| 4 | // Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | // |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // legendre |
| 22 | // Compare against values generated by the GNU Scientific Library. |
| 23 | // The GSL can be found on the web: http://www.gnu.org/software/gsl/ |
| 24 | #include <limits> |
| 25 | #include <cmath> |
| 26 | #if defined(__TEST_DEBUG) |
| 27 | # include <iostream> |
| 28 | # define VERIFY(A) \ |
| 29 | if (!(A)) \ |
| 30 | { \ |
| 31 | std::cout << "line " << __LINE__ \ |
| 32 | << " max_abs_frac = " << max_abs_frac \ |
| 33 | << std::endl; \ |
| 34 | } |
| 35 | #else |
| 36 | # include <testsuite_hooks.h> |
| 37 | #endif |
| 38 | #include <specfun_testcase.h> |
| 39 | |
| 40 | // Test data for l=0. |
| 41 | // max(|f - f_GSL|): 0.0000000000000000 at index 0 |
| 42 | // max(|f - f_GSL| / |f_GSL|): 0.0000000000000000 |
| 43 | // mean(f - f_GSL): 0.0000000000000000 |
| 44 | // variance(f - f_GSL): 0.0000000000000000 |
| 45 | // stddev(f - f_GSL): 0.0000000000000000 |
| 46 | const testcase_legendre<double> |
| 47 | data001[21] = |
| 48 | { |
| 49 | { 1.0000000000000000, 0, -1.0000000000000000, 0.0 }, |
| 50 | { 1.0000000000000000, 0, -0.90000000000000002, 0.0 }, |
| 51 | { 1.0000000000000000, 0, -0.80000000000000004, 0.0 }, |
| 52 | { 1.0000000000000000, 0, -0.69999999999999996, 0.0 }, |
| 53 | { 1.0000000000000000, 0, -0.59999999999999998, 0.0 }, |
| 54 | { 1.0000000000000000, 0, -0.50000000000000000, 0.0 }, |
| 55 | { 1.0000000000000000, 0, -0.39999999999999991, 0.0 }, |
| 56 | { 1.0000000000000000, 0, -0.29999999999999993, 0.0 }, |
| 57 | { 1.0000000000000000, 0, -0.19999999999999996, 0.0 }, |
| 58 | { 1.0000000000000000, 0, -0.099999999999999978, 0.0 }, |
| 59 | { 1.0000000000000000, 0, 0.0000000000000000, 0.0 }, |
| 60 | { 1.0000000000000000, 0, 0.10000000000000009, 0.0 }, |
| 61 | { 1.0000000000000000, 0, 0.20000000000000018, 0.0 }, |
| 62 | { 1.0000000000000000, 0, 0.30000000000000004, 0.0 }, |
| 63 | { 1.0000000000000000, 0, 0.40000000000000013, 0.0 }, |
| 64 | { 1.0000000000000000, 0, 0.50000000000000000, 0.0 }, |
| 65 | { 1.0000000000000000, 0, 0.60000000000000009, 0.0 }, |
| 66 | { 1.0000000000000000, 0, 0.70000000000000018, 0.0 }, |
| 67 | { 1.0000000000000000, 0, 0.80000000000000004, 0.0 }, |
| 68 | { 1.0000000000000000, 0, 0.90000000000000013, 0.0 }, |
| 69 | { 1.0000000000000000, 0, 1.0000000000000000, 0.0 }, |
| 70 | }; |
| 71 | const double toler001 = 2.5000000000000020e-13; |
| 72 | |
| 73 | // Test data for l=1. |
| 74 | // max(|f - f_GSL|): 0.0000000000000000 at index 0 |
| 75 | // max(|f - f_GSL| / |f_GSL|): 0.0000000000000000 |
| 76 | // mean(f - f_GSL): 0.0000000000000000 |
| 77 | // variance(f - f_GSL): 0.0000000000000000 |
| 78 | // stddev(f - f_GSL): 0.0000000000000000 |
| 79 | const testcase_legendre<double> |
| 80 | data002[21] = |
| 81 | { |
| 82 | { -1.0000000000000000, 1, -1.0000000000000000, 0.0 }, |
| 83 | { -0.90000000000000002, 1, -0.90000000000000002, 0.0 }, |
| 84 | { -0.80000000000000004, 1, -0.80000000000000004, 0.0 }, |
| 85 | { -0.69999999999999996, 1, -0.69999999999999996, 0.0 }, |
| 86 | { -0.59999999999999998, 1, -0.59999999999999998, 0.0 }, |
| 87 | { -0.50000000000000000, 1, -0.50000000000000000, 0.0 }, |
| 88 | { -0.39999999999999991, 1, -0.39999999999999991, 0.0 }, |
| 89 | { -0.29999999999999993, 1, -0.29999999999999993, 0.0 }, |
| 90 | { -0.19999999999999996, 1, -0.19999999999999996, 0.0 }, |
| 91 | { -0.099999999999999978, 1, -0.099999999999999978, 0.0 }, |
| 92 | { 0.0000000000000000, 1, 0.0000000000000000, 0.0 }, |
| 93 | { 0.10000000000000009, 1, 0.10000000000000009, 0.0 }, |
| 94 | { 0.20000000000000018, 1, 0.20000000000000018, 0.0 }, |
| 95 | { 0.30000000000000004, 1, 0.30000000000000004, 0.0 }, |
| 96 | { 0.40000000000000013, 1, 0.40000000000000013, 0.0 }, |
| 97 | { 0.50000000000000000, 1, 0.50000000000000000, 0.0 }, |
| 98 | { 0.60000000000000009, 1, 0.60000000000000009, 0.0 }, |
| 99 | { 0.70000000000000018, 1, 0.70000000000000018, 0.0 }, |
| 100 | { 0.80000000000000004, 1, 0.80000000000000004, 0.0 }, |
| 101 | { 0.90000000000000013, 1, 0.90000000000000013, 0.0 }, |
| 102 | { 1.0000000000000000, 1, 1.0000000000000000, 0.0 }, |
| 103 | }; |
| 104 | const double toler002 = 2.5000000000000020e-13; |
| 105 | |
| 106 | // Test data for l=2. |
| 107 | // max(|f - f_GSL|): 1.1102230246251565e-16 at index 17 |
| 108 | // max(|f - f_GSL| / |f_GSL|): 1.3877787807814482e-15 |
| 109 | // mean(f - f_GSL): 1.8503717077085941e-17 |
| 110 | // variance(f - f_GSL): 1.7975346147614202e-35 |
| 111 | // stddev(f - f_GSL): 4.2397342071896678e-18 |
| 112 | const testcase_legendre<double> |
| 113 | data003[21] = |
| 114 | { |
| 115 | { 1.0000000000000000, 2, -1.0000000000000000, 0.0 }, |
| 116 | { 0.71500000000000008, 2, -0.90000000000000002, 0.0 }, |
| 117 | { 0.46000000000000019, 2, -0.80000000000000004, 0.0 }, |
| 118 | { 0.23499999999999988, 2, -0.69999999999999996, 0.0 }, |
| 119 | { 0.039999999999999925, 2, -0.59999999999999998, 0.0 }, |
| 120 | { -0.12500000000000000, 2, -0.50000000000000000, 0.0 }, |
| 121 | { -0.26000000000000012, 2, -0.39999999999999991, 0.0 }, |
| 122 | { -0.36500000000000005, 2, -0.29999999999999993, 0.0 }, |
| 123 | { -0.44000000000000006, 2, -0.19999999999999996, 0.0 }, |
| 124 | { -0.48499999999999999, 2, -0.099999999999999978, 0.0 }, |
| 125 | { -0.50000000000000000, 2, 0.0000000000000000, 0.0 }, |
| 126 | { -0.48499999999999999, 2, 0.10000000000000009, 0.0 }, |
| 127 | { -0.43999999999999989, 2, 0.20000000000000018, 0.0 }, |
| 128 | { -0.36499999999999999, 2, 0.30000000000000004, 0.0 }, |
| 129 | { -0.25999999999999984, 2, 0.40000000000000013, 0.0 }, |
| 130 | { -0.12500000000000000, 2, 0.50000000000000000, 0.0 }, |
| 131 | { 0.040000000000000147, 2, 0.60000000000000009, 0.0 }, |
| 132 | { 0.23500000000000032, 2, 0.70000000000000018, 0.0 }, |
| 133 | { 0.46000000000000019, 2, 0.80000000000000004, 0.0 }, |
| 134 | { 0.71500000000000030, 2, 0.90000000000000013, 0.0 }, |
| 135 | { 1.0000000000000000, 2, 1.0000000000000000, 0.0 }, |
| 136 | }; |
| 137 | const double toler003 = 2.5000000000000020e-13; |
| 138 | |
| 139 | // Test data for l=5. |
| 140 | // max(|f - f_GSL|): 2.0122792321330962e-16 at index 19 |
| 141 | // max(|f - f_GSL| / |f_GSL|): 4.8911475274405560e-15 |
| 142 | // mean(f - f_GSL): -2.3129646346357427e-18 |
| 143 | // variance(f - f_GSL): 2.8086478355647191e-37 |
| 144 | // stddev(f - f_GSL): 5.2996677589870847e-19 |
| 145 | const testcase_legendre<double> |
| 146 | data004[21] = |
| 147 | { |
| 148 | { -1.0000000000000000, 5, -1.0000000000000000, 0.0 }, |
| 149 | { 0.041141250000000087, 5, -0.90000000000000002, 0.0 }, |
| 150 | { 0.39951999999999993, 5, -0.80000000000000004, 0.0 }, |
| 151 | { 0.36519874999999991, 5, -0.69999999999999996, 0.0 }, |
| 152 | { 0.15263999999999994, 5, -0.59999999999999998, 0.0 }, |
| 153 | { -0.089843750000000000, 5, -0.50000000000000000, 0.0 }, |
| 154 | { -0.27064000000000010, 5, -0.39999999999999991, 0.0 }, |
| 155 | { -0.34538624999999995, 5, -0.29999999999999993, 0.0 }, |
| 156 | { -0.30751999999999996, 5, -0.19999999999999996, 0.0 }, |
| 157 | { -0.17882874999999995, 5, -0.099999999999999978, 0.0 }, |
| 158 | { 0.0000000000000000, 5, 0.0000000000000000, 0.0 }, |
| 159 | { 0.17882875000000015, 5, 0.10000000000000009, 0.0 }, |
| 160 | { 0.30752000000000013, 5, 0.20000000000000018, 0.0 }, |
| 161 | { 0.34538625000000001, 5, 0.30000000000000004, 0.0 }, |
| 162 | { 0.27063999999999988, 5, 0.40000000000000013, 0.0 }, |
| 163 | { 0.089843750000000000, 5, 0.50000000000000000, 0.0 }, |
| 164 | { -0.15264000000000016, 5, 0.60000000000000009, 0.0 }, |
| 165 | { -0.36519875000000024, 5, 0.70000000000000018, 0.0 }, |
| 166 | { -0.39951999999999993, 5, 0.80000000000000004, 0.0 }, |
| 167 | { -0.041141249999999151, 5, 0.90000000000000013, 0.0 }, |
| 168 | { 1.0000000000000000, 5, 1.0000000000000000, 0.0 }, |
| 169 | }; |
| 170 | const double toler004 = 2.5000000000000020e-13; |
| 171 | |
| 172 | // Test data for l=10. |
| 173 | // max(|f - f_GSL|): 3.8857805861880479e-16 at index 19 |
| 174 | // max(|f - f_GSL| / |f_GSL|): 1.4766655123690915e-15 |
| 175 | // mean(f - f_GSL): -2.5112187461759493e-17 |
| 176 | // variance(f - f_GSL): 3.3107652853513909e-35 |
| 177 | // stddev(f - f_GSL): 5.7539249954716919e-18 |
| 178 | const testcase_legendre<double> |
| 179 | data005[21] = |
| 180 | { |
| 181 | { 1.0000000000000000, 10, -1.0000000000000000, 0.0 }, |
| 182 | { -0.26314561785585960, 10, -0.90000000000000002, 0.0 }, |
| 183 | { 0.30052979560000004, 10, -0.80000000000000004, 0.0 }, |
| 184 | { 0.085805795531640333, 10, -0.69999999999999996, 0.0 }, |
| 185 | { -0.24366274560000001, 10, -0.59999999999999998, 0.0 }, |
| 186 | { -0.18822860717773438, 10, -0.50000000000000000, 0.0 }, |
| 187 | { 0.096839064400000258, 10, -0.39999999999999991, 0.0 }, |
| 188 | { 0.25147634951601561, 10, -0.29999999999999993, 0.0 }, |
| 189 | { 0.12907202559999983, 10, -0.19999999999999996, 0.0 }, |
| 190 | { -0.12212499738710943, 10, -0.099999999999999978, 0.0 }, |
| 191 | { -0.24609375000000000, 10, 0.0000000000000000, 0.0 }, |
| 192 | { -0.12212499738710922, 10, 0.10000000000000009, 0.0 }, |
| 193 | { 0.12907202560000042, 10, 0.20000000000000018, 0.0 }, |
| 194 | { 0.25147634951601561, 10, 0.30000000000000004, 0.0 }, |
| 195 | { 0.096839064399999633, 10, 0.40000000000000013, 0.0 }, |
| 196 | { -0.18822860717773438, 10, 0.50000000000000000, 0.0 }, |
| 197 | { -0.24366274559999984, 10, 0.60000000000000009, 0.0 }, |
| 198 | { 0.085805795531641277, 10, 0.70000000000000018, 0.0 }, |
| 199 | { 0.30052979560000004, 10, 0.80000000000000004, 0.0 }, |
| 200 | { -0.26314561785586010, 10, 0.90000000000000013, 0.0 }, |
| 201 | { 1.0000000000000000, 10, 1.0000000000000000, 0.0 }, |
| 202 | }; |
| 203 | const double toler005 = 2.5000000000000020e-13; |
| 204 | |
| 205 | // Test data for l=20. |
| 206 | // max(|f - f_GSL|): 3.6082248300317588e-16 at index 19 |
| 207 | // max(|f - f_GSL| / |f_GSL|): 2.4166281401316513e-15 |
| 208 | // mean(f - f_GSL): 0.0000000000000000 |
| 209 | // variance(f - f_GSL): 4.9424644697959907e-65 |
| 210 | // stddev(f - f_GSL): 7.0302663319365015e-33 |
| 211 | const testcase_legendre<double> |
| 212 | data006[21] = |
| 213 | { |
| 214 | { 1.0000000000000000, 20, -1.0000000000000000, 0.0 }, |
| 215 | { -0.14930823530984835, 20, -0.90000000000000002, 0.0 }, |
| 216 | { 0.22420460541741347, 20, -0.80000000000000004, 0.0 }, |
| 217 | { -0.20457394463834172, 20, -0.69999999999999996, 0.0 }, |
| 218 | { 0.15916752910098109, 20, -0.59999999999999998, 0.0 }, |
| 219 | { -0.048358381067373557, 20, -0.50000000000000000, 0.0 }, |
| 220 | { -0.10159261558628112, 20, -0.39999999999999991, 0.0 }, |
| 221 | { 0.18028715947998047, 20, -0.29999999999999993, 0.0 }, |
| 222 | { -0.098042194344594796, 20, -0.19999999999999996, 0.0 }, |
| 223 | { -0.082077130944527663, 20, -0.099999999999999978, 0.0 }, |
| 224 | { 0.17619705200195312, 20, 0.0000000000000000, 0.0 }, |
| 225 | { -0.082077130944528023, 20, 0.10000000000000009, 0.0 }, |
| 226 | { -0.098042194344594089, 20, 0.20000000000000018, 0.0 }, |
| 227 | { 0.18028715947998042, 20, 0.30000000000000004, 0.0 }, |
| 228 | { -0.10159261558628192, 20, 0.40000000000000013, 0.0 }, |
| 229 | { -0.048358381067373557, 20, 0.50000000000000000, 0.0 }, |
| 230 | { 0.15916752910098075, 20, 0.60000000000000009, 0.0 }, |
| 231 | { -0.20457394463834136, 20, 0.70000000000000018, 0.0 }, |
| 232 | { 0.22420460541741347, 20, 0.80000000000000004, 0.0 }, |
| 233 | { -0.14930823530984758, 20, 0.90000000000000013, 0.0 }, |
| 234 | { 1.0000000000000000, 20, 1.0000000000000000, 0.0 }, |
| 235 | }; |
| 236 | const double toler006 = 2.5000000000000020e-13; |
| 237 | |
| 238 | // Test data for l=50. |
| 239 | // max(|f - f_GSL|): 1.6653345369377348e-16 at index 2 |
| 240 | // max(|f - f_GSL| / |f_GSL|): 1.6665460706897444e-15 |
| 241 | // mean(f - f_GSL): -8.0953762212251003e-18 |
| 242 | // variance(f - f_GSL): 3.4405935985667807e-36 |
| 243 | // stddev(f - f_GSL): 1.8548837156454796e-18 |
| 244 | const testcase_legendre<double> |
| 245 | data007[21] = |
| 246 | { |
| 247 | { 1.0000000000000000, 50, -1.0000000000000000, 0.0 }, |
| 248 | { -0.17003765994383671, 50, -0.90000000000000002, 0.0 }, |
| 249 | { 0.13879737345093113, 50, -0.80000000000000004, 0.0 }, |
| 250 | { -0.014572731645892852, 50, -0.69999999999999996, 0.0 }, |
| 251 | { -0.058860798844002096, 50, -0.59999999999999998, 0.0 }, |
| 252 | { -0.031059099239609811, 50, -0.50000000000000000, 0.0 }, |
| 253 | { 0.041569033381824674, 50, -0.39999999999999991, 0.0 }, |
| 254 | { 0.10911051574714790, 50, -0.29999999999999993, 0.0 }, |
| 255 | { 0.083432272204197494, 50, -0.19999999999999996, 0.0 }, |
| 256 | { -0.038205812661313600, 50, -0.099999999999999978, 0.0 }, |
| 257 | { -0.11227517265921705, 50, 0.0000000000000000, 0.0 }, |
| 258 | { -0.038205812661314155, 50, 0.10000000000000009, 0.0 }, |
| 259 | { 0.083432272204196564, 50, 0.20000000000000018, 0.0 }, |
| 260 | { 0.10911051574714797, 50, 0.30000000000000004, 0.0 }, |
| 261 | { 0.041569033381826007, 50, 0.40000000000000013, 0.0 }, |
| 262 | { -0.031059099239609811, 50, 0.50000000000000000, 0.0 }, |
| 263 | { -0.058860798844001430, 50, 0.60000000000000009, 0.0 }, |
| 264 | { -0.014572731645890737, 50, 0.70000000000000018, 0.0 }, |
| 265 | { 0.13879737345093113, 50, 0.80000000000000004, 0.0 }, |
| 266 | { -0.17003765994383679, 50, 0.90000000000000013, 0.0 }, |
| 267 | { 1.0000000000000000, 50, 1.0000000000000000, 0.0 }, |
| 268 | }; |
| 269 | const double toler007 = 2.5000000000000020e-13; |
| 270 | |
| 271 | // Test data for l=100. |
| 272 | // max(|f - f_GSL|): 3.4694469519536142e-16 at index 2 |
| 273 | // max(|f - f_GSL| / |f_GSL|): 6.8214063779431592e-15 |
| 274 | // mean(f - f_GSL): -4.1385545784018113e-17 |
| 275 | // variance(f - f_GSL): 8.9920078491655612e-35 |
| 276 | // stddev(f - f_GSL): 9.4826198116161765e-18 |
| 277 | const testcase_legendre<double> |
| 278 | data008[21] = |
| 279 | { |
| 280 | { 1.0000000000000000, 100, -1.0000000000000000, 0.0 }, |
| 281 | { 0.10226582055871893, 100, -0.90000000000000002, 0.0 }, |
| 282 | { 0.050861167913584228, 100, -0.80000000000000004, 0.0 }, |
| 283 | { -0.077132507199778641, 100, -0.69999999999999996, 0.0 }, |
| 284 | { -0.023747023905133141, 100, -0.59999999999999998, 0.0 }, |
| 285 | { -0.060518025961861198, 100, -0.50000000000000000, 0.0 }, |
| 286 | { -0.072258202125685025, 100, -0.39999999999999991, 0.0 }, |
| 287 | { 0.057127392202801046, 100, -0.29999999999999993, 0.0 }, |
| 288 | { 0.014681835355659706, 100, -0.19999999999999996, 0.0 }, |
| 289 | { -0.063895098434750205, 100, -0.099999999999999978, 0.0 }, |
| 290 | { 0.079589237387178727, 100, 0.0000000000000000, 0.0 }, |
| 291 | { -0.063895098434749761, 100, 0.10000000000000009, 0.0 }, |
| 292 | { 0.014681835355657875, 100, 0.20000000000000018, 0.0 }, |
| 293 | { 0.057127392202801566, 100, 0.30000000000000004, 0.0 }, |
| 294 | { -0.072258202125684082, 100, 0.40000000000000013, 0.0 }, |
| 295 | { -0.060518025961861198, 100, 0.50000000000000000, 0.0 }, |
| 296 | { -0.023747023905134217, 100, 0.60000000000000009, 0.0 }, |
| 297 | { -0.077132507199780501, 100, 0.70000000000000018, 0.0 }, |
| 298 | { 0.050861167913584228, 100, 0.80000000000000004, 0.0 }, |
| 299 | { 0.10226582055872063, 100, 0.90000000000000013, 0.0 }, |
| 300 | { 1.0000000000000000, 100, 1.0000000000000000, 0.0 }, |
| 301 | }; |
| 302 | const double toler008 = 5.0000000000000039e-13; |
| 303 | |
| 304 | template<typename Ret, unsigned int Num> |
| 305 | void |
| 306 | test(const testcase_legendre<Ret> (&data)[Num], Ret toler) |
| 307 | { |
| 308 | bool test __attribute__((unused)) = true; |
| 309 | const Ret eps = std::numeric_limits<Ret>::epsilon(); |
| 310 | Ret max_abs_diff = -Ret(1); |
| 311 | Ret max_abs_frac = -Ret(1); |
| 312 | unsigned int num_datum = Num; |
| 313 | for (unsigned int i = 0; i < num_datum; ++i) |
| 314 | { |
| 315 | const Ret f = std::legendre(data[i].l, data[i].x); |
| 316 | const Ret f0 = data[i].f0; |
| 317 | const Ret diff = f - f0; |
| 318 | if (std::abs(diff) > max_abs_diff) |
| 319 | max_abs_diff = std::abs(diff); |
| 320 | if (std::abs(f0) > Ret(10) * eps |
| 321 | && std::abs(f) > Ret(10) * eps) |
| 322 | { |
| 323 | const Ret frac = diff / f0; |
| 324 | if (std::abs(frac) > max_abs_frac) |
| 325 | max_abs_frac = std::abs(frac); |
| 326 | } |
| 327 | } |
| 328 | VERIFY(max_abs_frac < toler); |
| 329 | } |
| 330 | |
| 331 | int |
| 332 | main() |
| 333 | { |
| 334 | test(data001, toler001); |
| 335 | test(data002, toler002); |
| 336 | test(data003, toler003); |
| 337 | test(data004, toler004); |
| 338 | test(data005, toler005); |
| 339 | test(data006, toler006); |
| 340 | test(data007, toler007); |
| 341 | test(data008, toler008); |
| 342 | return 0; |
| 343 | } |