-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivest.cc
419 lines (379 loc) · 69 KB
/
derivest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "derivest.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <vector>
double* get_fdarule(int derivative_order, int method_order, DerivestStyle style, int *n) {
if (style == DerivestStyle_Central && method_order == 2 && derivative_order == 1) { static double data[] = { 1.00000000000000000000, }; *n = 1; return data; }
if (style == DerivestStyle_Central && method_order == 2 && derivative_order == 2) { static double data[] = { 2.00000000000000000000, }; *n = 1; return data; }
if (style == DerivestStyle_Central && method_order == 2 && derivative_order == 3) { static double data[] = { 7.99999973333336370000, -16.00000026666669900000, }; *n = 2; return data; }
if (style == DerivestStyle_Central && method_order == 2 && derivative_order == 4) { static double data[] = { 31.99999893333345500000, -128.00000853333367000000, }; *n = 2; return data; }
if (style == DerivestStyle_Central && method_order == 4 && derivative_order == 1) { static double data[] = { -0.33333328888889396000, 2.66666671111111640000, }; *n = 2; return data; }
if (style == DerivestStyle_Central && method_order == 4 && derivative_order == 2) { static double data[] = { -0.66666657777778793000, 10.66666737777780600000, }; *n = 2; return data; }
if (style == DerivestStyle_Central && method_order == 4 && derivative_order == 3) { static double data[] = { -2.66666622222227550000, 90.66667315555601200000, -170.66668942222367000000, }; *n = 3; return data; }
if (style == DerivestStyle_Central && method_order == 4 && derivative_order == 4) { static double data[] = { -10.66666488888913000000, 725.33342151111754000000, -2730.66730382228930000000, }; *n = 3; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && derivative_order == 1) { static double data[] = { 1.00000000000000000000, }; *n = 1; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && derivative_order == 2) { static double data[] = { 3.99999980000002120000, -8.00000000000002130000, }; *n = 2; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && derivative_order == 3) { static double data[] = { 15.99999866666682300000, -96.00000000000036900000, 128.00000853333381000000, }; *n = 3; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && derivative_order == 4) { static double data[] = { 73.14284948027338400000, -1024.00001706667810000000, 4096.00047786673080000000, -4681.14377108039120000000, }; *n = 4; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && derivative_order == 1) { static double data[] = { -0.99999990000001060000, 4.00000000000001070000, }; *n = 2; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && derivative_order == 2) { static double data[] = { -3.99999940000007470000, 39.99999920000021100000, -64.00000320000023600000, }; *n = 3; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && derivative_order == 3) { static double data[] = { -15.99999706666723800000, 351.99999466667157000000, -1664.00014933335680000000, 2048.00034133337610000000, }; *n = 4; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && derivative_order == 4) { static double data[] = { -73.14284216598025500000, 3364.57144912082190000000, -36864.00552959775100000000, 135753.17825549439000000000, -149796.62314405275000000000, }; *n = 5; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && derivative_order == 1) { static double data[] = { 0.33333325555556748000, -3.99999960000004600000, 10.66666684444448300000, }; *n = 3; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && derivative_order == 2) { static double data[] = { 1.33333295555564010000, -34.66666284444525600000, 234.66667484444733000000, -341.33337315556037000000, }; *n = 4; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && derivative_order == 3) { static double data[] = { 5.33333164444366050000, -287.99997119996817000000, 4309.33369031081380000000, -18432.00368639926800000000, 21845.33952284409700000000, }; *n = 5; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && derivative_order == 4) { static double data[] = { 24.38094413806237200000, -2681.90455978352110000000, 84065.53641580433800000000, -831683.30242319033000000000, 2946000.48652337310000000000, -3195661.82635462100000000000, }; *n = 6; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && derivative_order == 1) { static double data[] = { -0.04761902834467632300, 1.33333302222228410000, -10.66666577777794500000, 24.38095348390041300000, }; *n = 4; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && derivative_order == 2) { static double data[] = { -0.19047610385484859000, 11.04761640453383700000, -191.99999039998590000000, 1121.52392852603750000000, -1560.38125702673600000000, }; *n = 5; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && derivative_order == 3) { static double data[] = { -0.76190439003349786000, 89.90474156592972600000, -3248.76192546417310000000, 42032.77030951646200000000, -171641.96048256109000000000, 199728.84417419793000000000, }; *n = 6; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && derivative_order == 4) { static double data[] = { -3.48299142217729240000, 828.95221520044026000000, -61049.90956226736300000000, 1656010.53460411840000000000, -15628783.09987751400000000000, 54326255.84047879300000000000, -58434969.54424954200000000000, }; *n = 7; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && derivative_order == 1) { static double data[] = { -1.00000000000000000000, }; *n = 1; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && derivative_order == 2) { static double data[] = { -3.99999980000002120000, 8.00000000000002130000, }; *n = 2; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && derivative_order == 3) { static double data[] = { -15.99999866666682300000, 96.00000000000036900000, -128.00000853333381000000, }; *n = 3; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && derivative_order == 4) { static double data[] = { -73.14284948027338400000, 1024.00001706667810000000, -4096.00047786673080000000, 4681.14377108039120000000, }; *n = 4; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && derivative_order == 1) { static double data[] = { 0.99999990000001060000, -4.00000000000001070000, }; *n = 2; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && derivative_order == 2) { static double data[] = { 3.99999940000007470000, -39.99999920000021100000, 64.00000320000023600000, }; *n = 3; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && derivative_order == 3) { static double data[] = { 15.99999706666723800000, -351.99999466667157000000, 1664.00014933335680000000, -2048.00034133337610000000, }; *n = 4; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && derivative_order == 4) { static double data[] = { 73.14284216598025500000, -3364.57144912082190000000, 36864.00552959775100000000, -135753.17825549439000000000, 149796.62314405275000000000, }; *n = 5; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && derivative_order == 1) { static double data[] = { -0.33333325555556748000, 3.99999960000004600000, -10.66666684444448300000, }; *n = 3; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && derivative_order == 2) { static double data[] = { -1.33333295555564010000, 34.66666284444525600000, -234.66667484444733000000, 341.33337315556037000000, }; *n = 4; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && derivative_order == 3) { static double data[] = { -5.33333164444366050000, 287.99997119996817000000, -4309.33369031081380000000, 18432.00368639926800000000, -21845.33952284409700000000, }; *n = 5; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && derivative_order == 4) { static double data[] = { -24.38094413806237200000, 2681.90455978352110000000, -84065.53641580433800000000, 831683.30242319033000000000, -2946000.48652337310000000000, 3195661.82635462100000000000, }; *n = 6; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && derivative_order == 1) { static double data[] = { 0.04761902834467632300, -1.33333302222228410000, 10.66666577777794500000, -24.38095348390041300000, }; *n = 4; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && derivative_order == 2) { static double data[] = { 0.19047610385484859000, -11.04761640453383700000, 191.99999039998590000000, -1121.52392852603750000000, 1560.38125702673600000000, }; *n = 5; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && derivative_order == 3) { static double data[] = { 0.76190439003349786000, -89.90474156592972600000, 3248.76192546417310000000, -42032.77030951646200000000, 171641.96048256109000000000, -199728.84417419793000000000, }; *n = 6; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && derivative_order == 4) { static double data[] = { 3.48299142217729240000, -828.95221520044026000000, 61049.90956226736300000000, -1656010.53460411840000000000, 15628783.09987751400000000000, -54326255.84047879300000000000, 58434969.54424954200000000000, }; *n = 7; return data; }
return NULL;
}
double* get_qromb(DerivestStyle style, int method_order, int romberg_terms, double* err, int *rows, int *cols) {
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.80178372828261801000, -0.57735026918962573000, -0.26726125463914940000, -0.57735026918962573000, -0.53452247364346872000, }; *rows = 3; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.84446761661462100000, -0.19180248441488726000, -0.50000000000000000000, -0.10370656604344643000, 0.83738165766726269000, -0.50000000000000000000, -0.34075008800361062000, -0.15905576008736078000, -0.50000000000000000000, -0.40001096256756408000, -0.48652341316501468000, }; *rows = 4; *cols = 3; *err = 11.28910830518118900000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.86951154379237661000, -0.20930892312396848000, 0.01180801671322090900, -0.44721359549995787000, -0.01944596409764019100, 0.86116343827768216000, -0.24056218197401694000, -0.44721359549995787000, -0.24168531884620845000, -0.00116156800321774550, 0.83634320470883416000, -0.44721359549995787000, -0.29724515197736723000, -0.28736422575787757000, -0.13359136044978273000, -0.44721359549995787000, -0.31113510887116114000, -0.36332872139261840000, -0.47399767899825535000, }; *rows = 5; *cols = 4; *err = 11.41461762122740900000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.81537424876164311000, -0.57735026918962573000, -0.37062466571589092000, -0.57735026918962573000, -0.44474958304575207000, }; *rows = 3; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.86455518812718046000, -0.05043695986251890700, -0.50000000000000000000, -0.24069390070918101000, 0.83016935908162626000, -0.50000000000000000000, -0.30977195494584181000, -0.34358639231827554000, -0.50000000000000000000, -0.31408933247215770000, -0.43614600690083188000, }; *rows = 4; *cols = 3; *err = 9.37821802880461600000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.89280678475220199000, -0.05380917758419464400, 0.00078581261477213239, -0.44721359549995787000, -0.17118408690398346000, 0.87561269307297884000, -0.06322706096056875100, -0.44721359549995787000, -0.23768350308261099000, -0.21465762277166284000, 0.83321148366170139000, -0.44721359549995787000, -0.24183971576253271000, -0.30074261054975115000, -0.33682089764660450000, -0.44721359549995787000, -0.24209947900307516000, -0.30640328216737012000, -0.43394933766930033000, }; *rows = 5; *cols = 4; *err = 9.39819391877455070000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.77151675311695977000, -0.57735026918962573000, -0.15430335988159269000, -0.57735026918962573000, -0.61721339323536706000, }; *rows = 3; *cols = 2; *err = 15.56185830738861700000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.79262909300473194000, -0.34425762916576425000, -0.50000000000000000000, 0.04662522760021102500, 0.76795936550777366000, -0.50000000000000000000, -0.32637668645195395000, 0.10592540296990959000, -0.50000000000000000000, -0.51287763415298904000, -0.52962713931191907000, }; *rows = 4; *cols = 3; *err = 20.53186907921052700000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.80338665686788613000, 0.38502567510124175000, -0.07926396207040849200, -0.44721359549995787000, 0.14756079913492426000, -0.72384831642015013000, 0.49471649359093328000, -0.44721359549995787000, -0.18035211333591117000, -0.29261949870265103000, -0.68877660993146006000, -0.44721359549995787000, -0.34430856137350641000, 0.16941132117282084000, -0.20772623801787565000, -0.44721359549995787000, -0.42628678129339326000, 0.46203081884873842000, 0.48105031642881102000, }; *rows = 5; *cols = 4; *err = 23.61661951033535400000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.80178372828261801000, -0.57735026918962573000, -0.26726125463914940000, -0.57735026918962573000, -0.53452247364346872000, }; *rows = 3; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.84446761661462100000, -0.19119350358751439000, -0.50000000000000000000, -0.10370656604344643000, 0.82779909927952600000, -0.50000000000000000000, -0.34075008800361062000, -0.12393156388281552000, -0.50000000000000000000, -0.40001096256756408000, -0.51267403180919602000, }; *rows = 4; *cols = 3; *err = 11.89518727161034800000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.86951154379237661000, -0.20835623979994633000, 0.02315600834054250300, -0.44721359549995787000, -0.01944596409764019100, 0.84685767235810328000, -0.28545621607824045000, -0.44721359549995787000, -0.24168531884620845000, 0.04342242610766726400, 0.81642948218692357000, -0.44721359549995787000, -0.29724515197736723000, -0.29084118259704000000, -0.05582740912409084300, -0.44721359549995787000, -0.31113510887116114000, -0.39108267606878400000, -0.49830186532513476000, }; *rows = 5; *cols = 4; *err = 12.50181794614211100000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.81229142154292655000, -0.57735026918962573000, -0.33447294840088976000, -0.57735026918962573000, -0.47781847314203701000, }; *rows = 3; *cols = 2; *err = 9.69036717274277140000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.86029358477996298000, -0.09934796953392802100, -0.50000000000000000000, -0.19346316425742197000, 0.83486011620936373000, -0.50000000000000000000, -0.32518273812915799000, -0.26133824504090541000, -0.50000000000000000000, -0.34164768239338328000, -0.47417390163453021000, }; *rows = 4; *cols = 3; *err = 10.07561302210157300000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.88801989605826837000, -0.10669148801357399000, 0.00612920220951396980, -0.44721359549995787000, -0.11921580097564324000, 0.87369714409517996000, -0.14954811225808304000, -0.44721359549995787000, -0.24512024421921488000, -0.11719111543669131000, 0.83958977249581290000, -0.44721359549995787000, -0.26085829726395332000, -0.31064191664126439000, -0.22487854411807515000, -0.44721359549995787000, -0.26282555359945708000, -0.33917262400365028000, -0.47129231832916879000, }; *rows = 5; *cols = 4; *err = 10.27725707034641200000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.81537424876164311000, -0.57735026918962573000, -0.37062466571589092000, -0.57735026918962573000, -0.44474958304575207000, }; *rows = 3; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.86455518812718046000, -0.05042417095990074600, -0.50000000000000000000, -0.24069390070918101000, 0.82940552064749506000, -0.50000000000000000000, -0.30977195494584181000, -0.33409250825901193000, -0.50000000000000000000, -0.31408933247215770000, -0.44488884142858237000, }; *rows = 4; *cols = 3; *err = 9.46226527575433710000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.89280678475220199000, -0.05379210347288370500, 0.00156661714150256720, -0.44721359549995787000, -0.17118408690398346000, 0.87460307861702569000, -0.07589573421572049000, -0.44721359549995787000, -0.23768350308261099000, -0.20256093577085887000, 0.83474832807189647000, -0.44721359549995787000, -0.24183971576253271000, -0.30535831126387952000, -0.31573509971323455000, -0.44721359549995787000, -0.24209947900307516000, -0.31289172810940358000, -0.44468411128444402000, }; *rows = 5; *cols = 4; *err = 9.54585552301028170000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.77151675311695977000, -0.57735026918962573000, -0.15430335988159269000, -0.57735026918962573000, -0.61721339323536706000, }; *rows = 3; *cols = 2; *err = 15.56185830738861700000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.79262909300473194000, -0.34425762916576425000, -0.50000000000000000000, 0.04662522760021102500, 0.76795936550777366000, -0.50000000000000000000, -0.32637668645195395000, 0.10592540296990959000, -0.50000000000000000000, -0.51287763415298904000, -0.52962713931191907000, }; *rows = 4; *cols = 3; *err = 20.53186907921052700000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.80338665686788613000, 0.38502567510124175000, -0.07926396207040849200, -0.44721359549995787000, 0.14756079913492426000, -0.72384831642015013000, 0.49471649359093328000, -0.44721359549995787000, -0.18035211333591117000, -0.29261949870265103000, -0.68877660993146006000, -0.44721359549995787000, -0.34430856137350641000, 0.16941132117282084000, -0.20772623801787565000, -0.44721359549995787000, -0.42628678129339326000, 0.46203081884873842000, 0.48105031642881102000, }; *rows = 5; *cols = 4; *err = 23.61661951033535400000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.80178372828261801000, -0.57735026918962573000, -0.26726125463914940000, -0.57735026918962573000, -0.53452247364346872000, }; *rows = 3; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.84446761661462100000, -0.19119350358751439000, -0.50000000000000000000, -0.10370656604344643000, 0.82779909927952600000, -0.50000000000000000000, -0.34075008800361062000, -0.12393156388281552000, -0.50000000000000000000, -0.40001096256756408000, -0.51267403180919602000, }; *rows = 4; *cols = 3; *err = 11.89518727161034800000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.86951154379237661000, -0.20835623979994633000, 0.02315600834054250300, -0.44721359549995787000, -0.01944596409764019100, 0.84685767235810328000, -0.28545621607824045000, -0.44721359549995787000, -0.24168531884620845000, 0.04342242610766726400, 0.81642948218692357000, -0.44721359549995787000, -0.29724515197736723000, -0.29084118259704000000, -0.05582740912409084300, -0.44721359549995787000, -0.31113510887116114000, -0.39108267606878400000, -0.49830186532513476000, }; *rows = 5; *cols = 4; *err = 12.50181794614211100000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.81229142154292655000, -0.57735026918962573000, -0.33447294840088976000, -0.57735026918962573000, -0.47781847314203701000, }; *rows = 3; *cols = 2; *err = 9.69036717274277140000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.86029358477996298000, -0.09934796953392802100, -0.50000000000000000000, -0.19346316425742197000, 0.83486011620936373000, -0.50000000000000000000, -0.32518273812915799000, -0.26133824504090541000, -0.50000000000000000000, -0.34164768239338328000, -0.47417390163453021000, }; *rows = 4; *cols = 3; *err = 10.07561302210157300000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.88801989605826837000, -0.10669148801357399000, 0.00612920220951396980, -0.44721359549995787000, -0.11921580097564324000, 0.87369714409517996000, -0.14954811225808304000, -0.44721359549995787000, -0.24512024421921488000, -0.11719111543669131000, 0.83958977249581290000, -0.44721359549995787000, -0.26085829726395332000, -0.31064191664126439000, -0.22487854411807515000, -0.44721359549995787000, -0.26282555359945708000, -0.33917262400365028000, -0.47129231832916879000, }; *rows = 5; *cols = 4; *err = 10.27725707034641200000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 0) { static double data[] = { -0.70710678118654724000, -0.70710678118654746000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 1) { static double data[] = { -0.57735026918962573000, 0.81537424876164311000, -0.57735026918962573000, -0.37062466571589092000, -0.57735026918962573000, -0.44474958304575207000, }; *rows = 3; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, 0.86455518812718046000, -0.05042417095990074600, -0.50000000000000000000, -0.24069390070918101000, 0.82940552064749506000, -0.50000000000000000000, -0.30977195494584181000, -0.33409250825901193000, -0.50000000000000000000, -0.31408933247215770000, -0.44488884142858237000, }; *rows = 4; *cols = 3; *err = 9.46226527575433710000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 3) { static double data[] = { -0.44721359549995787000, 0.89280678475220199000, -0.05379210347288370500, 0.00156661714150256720, -0.44721359549995787000, -0.17118408690398346000, 0.87460307861702569000, -0.07589573421572049000, -0.44721359549995787000, -0.23768350308261099000, -0.20256093577085887000, 0.83474832807189647000, -0.44721359549995787000, -0.24183971576253271000, -0.30535831126387952000, -0.31573509971323455000, -0.44721359549995787000, -0.24209947900307516000, -0.31289172810940358000, -0.44468411128444402000, }; *rows = 5; *cols = 4; *err = 9.54585552301028170000; return data; }
return NULL;
}
double* get_rmat(DerivestStyle style, int method_order, int romberg_terms, double* err, int *rows, int *cols) {
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 1.00000000000000000000, 0.06249998750000159600, }; *rows = 3; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 0.06249998750000159600, 1.00000000000000000000, 0.06249998750000159600, 0.00390624843750035600, 1.00000000000000000000, 0.01562499531250083300, 0.00024414047851567300, }; *rows = 4; *cols = 3; *err = 11.28910830518118900000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 0.06249998750000159600, 0.01562499531250083300, 1.00000000000000000000, 0.06249998750000159600, 0.00390624843750035600, 0.00024414047851567300, 1.00000000000000000000, 0.01562499531250083300, 0.00024414047851567300, 0.00000381469383239910, 1.00000000000000000000, 0.00390624843750035600, 0.00001525877685547397, 0.00000005960457324986, }; *rows = 5; *cols = 4; *err = 11.41461762122740900000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 1.00000000000000000000, 0.00390624843750035600, }; *rows = 3; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 0.01562499531250083300, 1.00000000000000000000, 0.00390624843750035600, 0.00024414047851567300, 1.00000000000000000000, 0.00024414047851567300, 0.00000381469383239910, }; *rows = 4; *cols = 3; *err = 9.37821802880461600000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 0.01562499531250083300, 0.00390624843750035600, 1.00000000000000000000, 0.00390624843750035600, 0.00024414047851567300, 0.00001525877685547397, 1.00000000000000000000, 0.00024414047851567300, 0.00000381469383239910, 0.00000005960457324986, 1.00000000000000000000, 0.00001525877685547397, 0.00000005960457324986, 0.00000000023283027113, }; *rows = 5; *cols = 4; *err = 9.39819391877455070000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.49999997500000132000, 1.00000000000000000000, 0.24999997500000196000, }; *rows = 3; *cols = 2; *err = 15.56185830738861700000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.49999997500000132000, 0.24999997500000196000, 1.00000000000000000000, 0.24999997500000196000, 0.06249998750000159600, 1.00000000000000000000, 0.12499998125000193000, 0.01562499531250083300, }; *rows = 4; *cols = 3; *err = 20.53186907921052700000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.49999997500000132000, 0.24999997500000196000, 0.12499998125000193000, 1.00000000000000000000, 0.24999997500000196000, 0.06249998750000159600, 0.01562499531250083300, 1.00000000000000000000, 0.12499998125000193000, 0.01562499531250083300, 0.00195312412109397220, 1.00000000000000000000, 0.06249998750000159600, 0.00390624843750035600, 0.00024414047851567300, }; *rows = 5; *cols = 4; *err = 23.61661951033535400000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 1.00000000000000000000, 0.06249998750000159600, }; *rows = 3; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 0.12499998125000193000, 1.00000000000000000000, 0.06249998750000159600, 0.01562499531250083300, 1.00000000000000000000, 0.01562499531250083300, 0.00195312412109397220, }; *rows = 4; *cols = 3; *err = 11.89518727161034800000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 0.12499998125000193000, 0.06249998750000159600, 1.00000000000000000000, 0.06249998750000159600, 0.01562499531250083300, 0.00390624843750035600, 1.00000000000000000000, 0.01562499531250083300, 0.00195312412109397220, 0.00024414047851567300, 1.00000000000000000000, 0.00390624843750035600, 0.00024414047851567300, 0.00001525877685547397, }; *rows = 5; *cols = 4; *err = 12.50181794614211100000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.12499998125000193000, 1.00000000000000000000, 0.01562499531250083300, }; *rows = 3; *cols = 2; *err = 9.69036717274277140000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.12499998125000193000, 0.06249998750000159600, 1.00000000000000000000, 0.01562499531250083300, 0.00390624843750035600, 1.00000000000000000000, 0.00195312412109397220, 0.00024414047851567300, }; *rows = 4; *cols = 3; *err = 10.07561302210157300000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.12499998125000193000, 0.06249998750000159600, 0.03124999218750119200, 1.00000000000000000000, 0.01562499531250083300, 0.00390624843750035600, 0.00097656201171888557, 1.00000000000000000000, 0.00195312412109397220, 0.00024414047851567300, 0.00003051755523682562, 1.00000000000000000000, 0.00024414047851567300, 0.00001525877685547397, 0.00000095367336273244, }; *rows = 5; *cols = 4; *err = 10.27725707034641200000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 1.00000000000000000000, 0.00390624843750035600, }; *rows = 3; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 0.03124999218750119200, 1.00000000000000000000, 0.00390624843750035600, 0.00097656201171888557, 1.00000000000000000000, 0.00024414047851567300, 0.00003051755523682562, }; *rows = 4; *cols = 3; *err = 9.46226527575433710000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 0.03124999218750119200, 0.01562499531250083300, 1.00000000000000000000, 0.00390624843750035600, 0.00097656201171888557, 0.00024414047851567300, 1.00000000000000000000, 0.00024414047851567300, 0.00003051755523682562, 0.00000381469383239910, 1.00000000000000000000, 0.00001525877685547397, 0.00000095367336273244, 0.00000005960457324986, }; *rows = 5; *cols = 4; *err = 9.54585552301028170000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.49999997500000132000, 1.00000000000000000000, 0.24999997500000196000, }; *rows = 3; *cols = 2; *err = 15.56185830738861700000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.49999997500000132000, 0.24999997500000196000, 1.00000000000000000000, 0.24999997500000196000, 0.06249998750000159600, 1.00000000000000000000, 0.12499998125000193000, 0.01562499531250083300, }; *rows = 4; *cols = 3; *err = 20.53186907921052700000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.49999997500000132000, 0.24999997500000196000, 0.12499998125000193000, 1.00000000000000000000, 0.24999997500000196000, 0.06249998750000159600, 0.01562499531250083300, 1.00000000000000000000, 0.12499998125000193000, 0.01562499531250083300, 0.00195312412109397220, 1.00000000000000000000, 0.06249998750000159600, 0.00390624843750035600, 0.00024414047851567300, }; *rows = 5; *cols = 4; *err = 23.61661951033535400000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 1.00000000000000000000, 0.06249998750000159600, }; *rows = 3; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 0.12499998125000193000, 1.00000000000000000000, 0.06249998750000159600, 0.01562499531250083300, 1.00000000000000000000, 0.01562499531250083300, 0.00195312412109397220, }; *rows = 4; *cols = 3; *err = 11.89518727161034800000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.24999997500000196000, 0.12499998125000193000, 0.06249998750000159600, 1.00000000000000000000, 0.06249998750000159600, 0.01562499531250083300, 0.00390624843750035600, 1.00000000000000000000, 0.01562499531250083300, 0.00195312412109397220, 0.00024414047851567300, 1.00000000000000000000, 0.00390624843750035600, 0.00024414047851567300, 0.00001525877685547397, }; *rows = 5; *cols = 4; *err = 12.50181794614211100000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.12499998125000193000, 1.00000000000000000000, 0.01562499531250083300, }; *rows = 3; *cols = 2; *err = 9.69036717274277140000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.12499998125000193000, 0.06249998750000159600, 1.00000000000000000000, 0.01562499531250083300, 0.00390624843750035600, 1.00000000000000000000, 0.00195312412109397220, 0.00024414047851567300, }; *rows = 4; *cols = 3; *err = 10.07561302210157300000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.12499998125000193000, 0.06249998750000159600, 0.03124999218750119200, 1.00000000000000000000, 0.01562499531250083300, 0.00390624843750035600, 0.00097656201171888557, 1.00000000000000000000, 0.00195312412109397220, 0.00024414047851567300, 0.00003051755523682562, 1.00000000000000000000, 0.00024414047851567300, 0.00001525877685547397, 0.00000095367336273244, }; *rows = 5; *cols = 4; *err = 10.27725707034641200000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 0) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, }; *rows = 2; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 1) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 1.00000000000000000000, 0.00390624843750035600, }; *rows = 3; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 2) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 0.03124999218750119200, 1.00000000000000000000, 0.00390624843750035600, 0.00097656201171888557, 1.00000000000000000000, 0.00024414047851567300, 0.00003051755523682562, }; *rows = 4; *cols = 3; *err = 9.46226527575433710000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 3) { static double data[] = { 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 1.00000000000000000000, 0.06249998750000159600, 0.03124999218750119200, 0.01562499531250083300, 1.00000000000000000000, 0.00390624843750035600, 0.00097656201171888557, 0.00024414047851567300, 1.00000000000000000000, 0.00024414047851567300, 0.00003051755523682562, 0.00000381469383239910, 1.00000000000000000000, 0.00001525877685547397, 0.00000095367336273244, 0.00000005960457324986, }; *rows = 5; *cols = 4; *err = 9.54585552301028170000; return data; }
return NULL;
}
double* get_rinv(DerivestStyle style, int method_order, int romberg_terms, double* err, int *rows, int *cols) {
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.62360953476663328000, 0.00000000000000000000, 1.42539326304925140000, }; *rows = 2; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.41976458478839890000, -0.60264446556758777000, 0.00000000000000000000, 1.26423220140301980000, 7.54319252713725330000, -0.00000000000000000000, -0.00000000000000000000, -7.13235054598455510000, }; *rows = 3; *cols = 3; *err = 11.28910830518118900000; return data; }
if (style == DerivestStyle_Central && method_order == 2 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.31576509388509433000, -0.38904255765132340000, -0.59663290041541939000, 0.00000000000000000000, 1.18527663767747150000, 6.60785455261811630000, 31.98867870607875800000, -0.00000000000000000000, -0.00000000000000000000, -6.42812091809076060000, -152.55004334508700000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 121.16980555613689000000, }; *rows = 4; *cols = 4; *err = 11.41461762122740900000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.44969124314685543000, 0.00000000000000000000, 1.26506549190849830000, }; *rows = 2; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.31437715757917595000, -0.44264172403697011000, 0.00000000000000000000, 1.17893234570635650000, 27.02257272812083100000, -0.00000000000000000000, -0.00000000000000000000, -26.63036796394638700000, }; *rows = 3; *cols = 3; *err = 9.37821802880461600000; return data; }
if (style == DerivestStyle_Central && method_order == 4 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.24211679654875012000, -0.30678540704538571000, -0.44085152410011957000, 0.00000000000000000000, 1.13492358130095240000, 25.14017160803042500000, 461.19683332646736000000, -0.00000000000000000000, -0.00000000000000000000, -24.88719537856923500000, -2273.90760075605660000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 1813.15240476630420000000, }; *rows = 4; *cols = 4; *err = 9.39819391877455070000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -1.08012338029814340000, 0.00000000000000000000, 1.85164013341510310000, }; *rows = 2; *cols = 2; *err = 15.56185830738861700000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.69937856320393132000, -1.36820329919331840000, 0.00000000000000000000, 1.49200765620866350000, 7.52070509117139530000, -0.00000000000000000000, -0.00000000000000000000, -6.49675942114383980000, }; *rows = 3; *cols = 3; *err = 20.53186907921052700000; return data; }
if (style == DerivestStyle_Forward && method_order == 1 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.50826499301545858000, 0.79571967925928744000, 1.53725229153425900000, 0.00000000000000000000, 1.31165164988334500000, -5.66757801962910970000, -20.11300452875310900000, 0.00000000000000000000, 0.00000000000000000000, 5.25688401547106210000, 53.61524488895194900000, -0.00000000000000000000, -0.00000000000000000000, -0.00000000000000000000, -35.11875661380352700000, }; *rows = 4; *cols = 4; *err = 23.61661951033535400000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.62360953476663328000, 0.00000000000000000000, 1.42539326304925140000, }; *rows = 2; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.41976458478839890000, -0.67098051242130341000, 0.00000000000000000000, 1.26423220140301980000, 11.51045053226340500000, -0.00000000000000000000, -0.00000000000000000000, -11.03066352342961700000, }; *rows = 3; *cols = 3; *err = 11.89518727161034800000; return data; }
if (style == DerivestStyle_Forward && method_order == 2 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.31576509388509433000, -0.42767280681611786000, -0.69676103760086427000, 0.00000000000000000000, 1.18527663767747150000, 9.97692779812147630000, 61.51233692826684100000, -0.00000000000000000000, -0.00000000000000000000, -9.75761123110530800000, -178.67605610753458000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 117.88363622520909000000, }; *rows = 4; *cols = 4; *err = 12.50181794614211100000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.49829640173741302000, 0.00000000000000000000, 1.31058782328033940000, }; *rows = 2; *cols = 2; *err = 9.69036717274277140000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.34399981688504949000, -0.51035505606463816000, 0.00000000000000000000, 1.20429340166501240000, 21.11243786329836400000, -0.00000000000000000000, -0.00000000000000000000, -20.70143077676765500000, }; *rows = 3; *cols = 3; *err = 10.07561302210157300000; return data; }
if (style == DerivestStyle_Forward && method_order == 3 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.26310659017063703000, -0.34357981843807500000, -0.51666837690336553000, 0.00000000000000000000, 1.15112648622890590000, 19.23954504143976900000, 225.76844923359738000000, -0.00000000000000000000, -0.00000000000000000000, -19.00265671101527000000, -666.08031757363960000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 440.83466591915499000000, }; *rows = 4; *cols = 4; *err = 10.27725707034641200000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.44969124314685543000, 0.00000000000000000000, 1.26506549190849830000, }; *rows = 2; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.31437715757917595000, -0.45358546285800316000, 0.00000000000000000000, 1.17893234570635650000, 40.65255637895116800000, -0.00000000000000000000, -0.00000000000000000000, -40.24939508705306700000, }; *rows = 3; *cols = 3; *err = 9.46226527575433710000; return data; }
if (style == DerivestStyle_Forward && method_order == 4 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.24211679654875012000, -0.31343210054417769000, -0.45558104137318373000, 0.00000000000000000000, 1.13492358130095240000, 37.75749146462025200000, 868.86758056256576000000, -0.00000000000000000000, -0.00000000000000000000, -37.49785146754895500000, -2582.76011166920990000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 1714.34967876515930000000, }; *rows = 4; *cols = 4; *err = 9.54585552301028170000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -1.08012338029814340000, 0.00000000000000000000, 1.85164013341510310000, }; *rows = 2; *cols = 2; *err = 15.56185830738861700000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.69937856320393132000, -1.36820329919331840000, 0.00000000000000000000, 1.49200765620866350000, 7.52070509117139530000, -0.00000000000000000000, -0.00000000000000000000, -6.49675942114383980000, }; *rows = 3; *cols = 3; *err = 20.53186907921052700000; return data; }
if (style == DerivestStyle_Backward && method_order == 1 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.50826499301545858000, 0.79571967925928744000, 1.53725229153425900000, 0.00000000000000000000, 1.31165164988334500000, -5.66757801962910970000, -20.11300452875310900000, 0.00000000000000000000, 0.00000000000000000000, 5.25688401547106210000, 53.61524488895194900000, -0.00000000000000000000, -0.00000000000000000000, -0.00000000000000000000, -35.11875661380352700000, }; *rows = 4; *cols = 4; *err = 23.61661951033535400000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.62360953476663328000, 0.00000000000000000000, 1.42539326304925140000, }; *rows = 2; *cols = 2; *err = 10.79819737203314700000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.41976458478839890000, -0.67098051242130341000, 0.00000000000000000000, 1.26423220140301980000, 11.51045053226340500000, -0.00000000000000000000, -0.00000000000000000000, -11.03066352342961700000, }; *rows = 3; *cols = 3; *err = 11.89518727161034800000; return data; }
if (style == DerivestStyle_Backward && method_order == 2 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.31576509388509433000, -0.42767280681611786000, -0.69676103760086427000, 0.00000000000000000000, 1.18527663767747150000, 9.97692779812147630000, 61.51233692826684100000, -0.00000000000000000000, -0.00000000000000000000, -9.75761123110530800000, -178.67605610753458000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 117.88363622520909000000, }; *rows = 4; *cols = 4; *err = 12.50181794614211100000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.49829640173741302000, 0.00000000000000000000, 1.31058782328033940000, }; *rows = 2; *cols = 2; *err = 9.69036717274277140000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.34399981688504949000, -0.51035505606463816000, 0.00000000000000000000, 1.20429340166501240000, 21.11243786329836400000, -0.00000000000000000000, -0.00000000000000000000, -20.70143077676765500000, }; *rows = 3; *cols = 3; *err = 10.07561302210157300000; return data; }
if (style == DerivestStyle_Backward && method_order == 3 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.26310659017063703000, -0.34357981843807500000, -0.51666837690336553000, 0.00000000000000000000, 1.15112648622890590000, 19.23954504143976900000, 225.76844923359738000000, -0.00000000000000000000, -0.00000000000000000000, -19.00265671101527000000, -666.08031757363960000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 440.83466591915499000000, }; *rows = 4; *cols = 4; *err = 10.27725707034641200000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 0) { static double data[] = { -0.70710678118654746000, }; *rows = 1; *cols = 1; *err = 8.98464353209375590000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 1) { static double data[] = { -0.57735026918962584000, -0.44969124314685543000, 0.00000000000000000000, 1.26506549190849830000, }; *rows = 2; *cols = 2; *err = 9.29861164842509960000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 2) { static double data[] = { -0.50000000000000000000, -0.31437715757917595000, -0.45358546285800316000, 0.00000000000000000000, 1.17893234570635650000, 40.65255637895116800000, -0.00000000000000000000, -0.00000000000000000000, -40.24939508705306700000, }; *rows = 3; *cols = 3; *err = 9.46226527575433710000; return data; }
if (style == DerivestStyle_Backward && method_order == 4 && romberg_terms == 3) { static double data[] = { -0.44721359549995793000, -0.24211679654875012000, -0.31343210054417769000, -0.45558104137318373000, 0.00000000000000000000, 1.13492358130095240000, 37.75749146462025200000, 868.86758056256576000000, -0.00000000000000000000, -0.00000000000000000000, -37.49785146754895500000, -2582.76011166920990000000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000, 1714.34967876515930000000, }; *rows = 4; *cols = 4; *err = 9.54585552301028170000; return data; }
return NULL;
}
// The following code is auto-generated with make_c_code() from derivutils.m
int derivest_sort_compare(const void * a, const void * b)
{
const double A = *(double*)a;
const double B = *(double*)b;
if (A < B) return -1;
if (A > B) return 1;
return 0;
}
bool derivest(std::function<double(double)> fun, double x0, int derivative_order, int method_order, DerivestStyle style, int romberg_terms, double *der, double* err, double* finaldelta) {
const double step_ratio = 2.0000001; // DERIVESTcpp is hardcoded to this specific step_ratio in several places.
const double h = fmax(x0, 0.02); // same as nominal_step
const double max_step = 100;
// validate parameters
if ((derivative_order < 1) || (derivative_order > 4)) return false;
if (style != DerivestStyle_Central && style != DerivestStyle_Forward && style != DerivestStyle_Backward) return false;
if (style == DerivestStyle_Central && (method_order != 2 && method_order != 4)) return false;
if ((style == DerivestStyle_Central || style == DerivestStyle_Backward) && (method_order < 1 && method_order > 4)) return false;
if ((romberg_terms < 0) || (romberg_terms > 3)) return false;
if (style == DerivestStyle_Central && method_order == 4 && derivative_order == 4 && romberg_terms == 3) return false;
if (der == NULL && err == NULL && finaldelta == NULL) return false;
static const int ndel = 26;
double delta[ndel];
for (int i = 0; i < ndel; i++) delta[i] = max_step*pow(step_ratio, -i);
// generate finite differencing rule in advance.
// The rule is for a nominal unit step size, and will
// be scaled later to reflect the local step size.
int nfda;
double* fdarule = get_fdarule(derivative_order, method_order, style, &nfda);
if (fdarule == NULL) return false;
// will we need fun(x0)?
const double f_x0 = (derivative_order % 2 == 0 || style != DerivestStyle_Central) ? fun(x0) : 0.0;
double f_del[ndel];
if (style == DerivestStyle_Central) {
// A central rule, so we will need to evaluate symmetrically around x0i.
double f_plusdel[ndel], f_minusdel[ndel];
for (int i = 0; i < ndel; i++) f_plusdel[i] = fun(x0 + h*delta[i]);
for (int i = 0; i < ndel; i++) f_minusdel[i] = fun(x0 - h*delta[i]);
if (derivative_order == 1 || derivative_order == 3) { for (int i = 0; i < ndel; i++) f_del[i] = (f_plusdel[i] - f_minusdel[i]) / 2.0; } // odd transformation
else { for (int i = 0; i < ndel; i++) f_del[i] = (f_plusdel[i] + f_minusdel[i]) / 2.0 - f_x0; }
} else if (style == DerivestStyle_Forward) {
for (int i = 0; i < ndel; i++) f_del[i] = fun(x0 + h*delta[i]) - f_x0; // forward rule; drop off the constant only
} else if (style == DerivestStyle_Backward) {
for (int i = 0; i < ndel; i++) f_del[i] = fun(x0 - h*delta[i]) - f_x0; // backward rule; drop off the constant only
}
// Apply the finite difference rule at each delta, scalingas appropriate for delta and the requested DerivativeOrder.
// First, decide how many of these estimates we will end up with.
const int ne = ndel + 1 - nfda - romberg_terms;
// Form the initial derivative estimates from the chosen finite difference method.
// der_init = vec2mat(f_del, ne, nfda)*fdarule.'
double der_init[ndel];
for (int i = 0; i < ne; i++) {
der_init[i] = 0;
for (int j = 0; j < nfda; j++) der_init[i] += f_del[i + j] * fdarule[j];
}
// scale to reflect the local delta (" der_init = der_init(:). / (h*delta(1:ne)).^DerivativeOrder " )
for (int i = 0; i < ne; i++) der_init[i] = der_init[i] / pow(h*delta[i], derivative_order);
// flip sign if style == backward and derivative of even order
if (style == DerivestStyle_Backward && derivative_order % 2 == 0) for (int i = 0; i < ne; i++) der_init[i] = -der_init[i];
// Each approximation that results is an approximation of order DerivativeOrder to the desired derivative.
// Additional (higher order, even or odd) terms in the Taylor series also remain. Use a generalized (multi-term)
// Romberg extrapolation to improve these estimates.
// get matrices qromb, rmat and rinv (represented as row-major order, e.i. as common in C++)
double romb_err;
int qromb_rows, qromb_cols, rmat_rows, rmat_cols, rinv_rows, rinv_cols;
double* qromb = get_qromb(style, method_order, romberg_terms, &romb_err, &qromb_rows, &qromb_cols);
double* rmat = get_rmat(style, method_order, romberg_terms, &romb_err, &rmat_rows, &rmat_cols);
double* rinv = get_rinv(style, method_order, romberg_terms, &romb_err, &rinv_rows, &rinv_cols);
if (qromb == NULL || rmat == NULL || rinv == NULL) return false;
// do romberg extrapolation for each estimate (extrapolation to a zero step size)
const int nexpon = rmat_rows - 2;
const int nrombcoefs_tmp = (ne - (nexpon + 2));
const int nrombcoefs = (nrombcoefs_tmp > 1) ? nrombcoefs_tmp : 1;
// rhs = vec2mat(der_init, nexpon + 2, max(1, ne - (nexpon + 2)));
// rombcoefs = rinv * (qromb.'*rhs);
assert((rinv_cols <= 4) && (nrombcoefs < ndel)); // make sure we allocate enough memory for rombcoefs
double rombcoefs[ndel * 4] = { 0.0 }; // matrix of size (nrows=rinv_cols, ncols=nrombcoefs)
for (int i = 0; i < rinv_rows; i++)
for (int j = 0; j < nrombcoefs; j++)
for (int k = 0; k < rinv_cols; k++)
for (int q = 0; q < qromb_rows; q++)
rombcoefs[i*nrombcoefs + j] += rinv[i*rinv_cols + k] * qromb[q*qromb_cols + k] * der_init[q + j];
// der_romb = rombcoefs(1,:).';
double der_romb[ndel] = { 0.0 }; // only first "max(1,ne - (nexpon+2))" elements will be used
for (int i = 0; i < nrombcoefs; i++)
der_romb[i] = rombcoefs[0 * nrombcoefs + i];
// uncertainty estimate of derivative prediction
// s = sqrt(sum((rhs - rmat*rombcoefs).^2,1));
// errest = s.'*err;
double errest[ndel] = { 0.0 };
for (int j = 0; j < nrombcoefs; j++) {
for (int i = 0; i < rmat_rows; i++) {
double rmat_rombcoefs = 0.0;
for (int k = 0; k < rmat_cols; k++)
rmat_rombcoefs += rmat[i*rmat_cols + k] * rombcoefs[k*nrombcoefs + j];
errest[j] += pow(der_init[i + j] - rmat_rombcoefs, 2);
}
errest[j] = sqrt(errest[j]) * romb_err;
}
// trim off the estimates at each end of the scale
// i.e., ignore ntrim lowest & highest estimates; out of those that remain select the one with the smallest error
static const int ntrimvec[] = { 2, 2, 4, 6, };
const int ntrim = ntrimvec[derivative_order - 1];
double der_romb_sort[ndel] = { 0.0 }; // only first "max(1,ne - (nexpon+2))" elements will be used
for (int i = 0; i < nrombcoefs; i++) der_romb_sort[i] = der_romb[i];
qsort(der_romb_sort, nrombcoefs, sizeof(double), derivest_sort_compare);
const double min_der_romb = der_romb_sort[ntrim];
const double max_der_romb = der_romb_sort[nrombcoefs - ntrim - 1];
int index = -1;
for (int i = 0; i < nrombcoefs; i++) {
if (der_romb[i] < min_der_romb || der_romb[i] > max_der_romb) continue;
if (index == -1 || errest[i] < errest[index]) index = i;
}
if (index == -1) return false;
if (der != NULL) *der = der_romb[index];
if (err != NULL) *err = errest[index];
if (finaldelta != NULL) *finaldelta = h * delta[index];
return true;
}
bool hessian(std::function<double(double*)> fun, double* x0, int n, double* hess, double* err) {
const std::vector<double> x0vec(x0, x0+n);
// Let "x1" be a vector equal to vector "x0", but with "index" argument replaced by a new value "x". E.i.,
// x1(index, x) := [x0[0], ..., x0[index-1), x, x0[index+1], x0[n-1]]
// Then fun1(index) generatest a new function of x, which evaluates to fun(x1), where x1 depends on x as defined above.
// fun1(index)(x) = fun(x1(index, x))
auto fun1 = [&fun, x0vec](int index) {
return [index, &fun, x0vec](double x) {
std::vector<double> x1vec(x0vec);
x1vec[index] = x;
return fun(&x1vec[0]);
};
};
// Similar to fun1, but replace two arguments
auto fun2 = [&fun, x0vec](int index1, int index2) {
return [index1, index2, &fun, x0vec](double x1, double x2) {
std::vector<double> x1vec(x0vec);
x1vec[index1] = x1;
x1vec[index2] = x2;
return fun(&x1vec[0]);
};
};
const int romberg_terms = 2; // I think in the original DERIVESTsuite matlab code there was a typo: it says RombergTerms=3, but in fact it corresponds to RombergTerms=2 (at least when compared to derivest.m)
const DerivestStyle style = DerivestStyle_Central;
const int method_order = 2;
// get the diagonal elements of the hessian (2nd partial derivatives wrt each variable.)
bool ok = true;
for (int index = 0; index < n; index++)
ok &= derivest(fun1(index), x0[index], 2, 4, style, romberg_terms, (hess == NULL) ? NULL : &hess[index*(n + 1)], (err == NULL) ? NULL : &err[index*(n + 1)], NULL);
if (n < 2) return ok; // the hessian matrix is 1x1. all done
// get the gradient vector. This is done only to decide on intelligent step sizes for the mixed partials
std::vector<double> stepsize(n, 0.0);
for (int index = 0; index < n; index++)
ok &= derivest(fun1(index), x0[index], 1, method_order, style, romberg_terms, NULL, NULL, &stepsize[index]);
const double step_ratio = 2.0000001; // DERIVESTcpp is hardcoded to this specific step_ratio in several places.
const double dfrac[romberg_terms + 2] = { 1, pow(step_ratio, -1), pow(step_ratio, -2), pow(step_ratio, -3) };
double romb_err;
int qromb_rows, qromb_cols, rmat_rows, rmat_cols, rinv_rows, rinv_cols;
double* qromb = get_qromb(style, method_order, romberg_terms, &romb_err, &qromb_rows, &qromb_cols);
double* rmat = get_rmat(style, method_order, romberg_terms, &romb_err, &rmat_rows, &rmat_cols);
double* rinv = get_rinv(style, method_order, romberg_terms, &romb_err, &rinv_rows, &rinv_cols);
if (qromb == NULL || rmat == NULL || rinv == NULL) return false;
// Get params.RombergTerms + 2 estimates of the upper triangle of the hessian matrix
for (int index1 = 1; index1 < n; index1++) {
for (int index2 = 0; index2 < index1; index2++) {
double dij[romberg_terms + 2] = { 0.0 };
auto fun2ij = fun2(index1, index2);
for (int k = 0; k < romberg_terms + 2; k++) {
dij[k] = fun2ij(x0[index1] + dfrac[k] * stepsize[index1], x0[index2] + dfrac[k] * stepsize[index2]) +
fun2ij(x0[index1] - dfrac[k] * stepsize[index1], x0[index2] - dfrac[k] * stepsize[index2]) -
fun2ij(x0[index1] + dfrac[k] * stepsize[index1], x0[index2] - dfrac[k] * stepsize[index2]) -
fun2ij(x0[index1] - dfrac[k] * stepsize[index1], x0[index2] + dfrac[k] * stepsize[index2]);
dij[k] /= (4.0 * stepsize[index1] * stepsize[index2] * pow(dfrac[k], 2));
}
// Romberg extrapolation step
// note: the following code somewhat duplicates 'rombextrap' piece of derivest()
// the difference is that below we don't have 'j' index --- i.e. it works as if nrombcoefs==1.
// rombcoefs = rinv * (qromb.'*der_init);
assert(rinv_cols <= 4); // make sure we allocate enough memory for rombcoefs
double rombcoefs[4] = { 0.0 }; // matrix of size (nrows=rinv_cols, ncols=nrombcoefs)
for (int i = 0; i < rinv_rows; i++)
for (int k = 0; k < rinv_cols; k++)
for (int q = 0; q < qromb_rows; q++)
rombcoefs[i] += rinv[i*rinv_cols + k] * qromb[q*qromb_cols + k] * dij[q];
// der_romb = rombcoefs(1,:)';
double der_romb = rombcoefs[0];
// uncertainty estimate of derivative prediction
// s = sqrt(sum((rhs - rmat*rombcoefs).^2,1));
// errest = s.'*err;
double errest = 0.0;
for (int i = 0; i < rmat_rows; i++) {
double rmat_rombcoefs = 0.0;
for (int k = 0; k < rmat_cols; k++)
rmat_rombcoefs += rmat[i*rmat_cols + k] * rombcoefs[k];
errest += pow(dij[i] - rmat_rombcoefs, 2);
}
errest = sqrt(errest) * romb_err;
if (hess != NULL) hess[index1 + n*index2] = hess[index2 + n*index1] = der_romb;
if (err != NULL) err[index1 + n*index2] = err[index2 + n*index1] = errest;
}
}
return ok;
}