-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_grunts.cpp
1310 lines (1171 loc) · 43.2 KB
/
math_grunts.cpp
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <fstream>
#include <string>
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;
#include "math_parameters.h"
#include "io_grunts.h"
#include "io_diag.h"
#include "math_grunts.h"
// Sets all elements of an array equal to a constant ----------------------------------
void const_array(double *x, int N, double konst)
{ for (int i=0; i<N; ++i) *(x+i) = konst; return; }
void const_array(double *x, int N)
{
const_array(x, N, 0);
}
void const_array(int *x, int N, int konst)
{ for (int i=0; i<N; ++i) *(x+i) = konst; return; }
// Sets a column (or row) of an array equal to a constant -----------------------------
void set_array_col(double *x, int Nr, int Nc, int j_col, double konst)
{ for (int i=0; i<Nr; ++i) *(x + i*Nc + j_col) = konst; return; }
void set_array_col(int *x, int Nr, int Nc, int j_col, int konst)
{ for (int i=0; i<Nr; ++i) *(x + i*Nc + j_col) = konst; return; }
void set_array_row(double *x, int Nr, int Nc, int j_row, double konst)
{ for (int i=0; i<Nc; ++i) *(x + j_row*Nc + i) = konst; return; }
void set_array_row(int *x, int Nr, int Nc, int j_row, int konst)
{ for (int i=0; i<Nc; ++i) *(x + j_row*Nc + i) = konst; return; }
// Copies an array --------------------------------------------------------------------
void copy_array(double *to, double *from, int N)
{ for (int i=0; i<N; ++i) *(to+i) = *(from+i); return; }
void copy_array(int *to, int *from, int N)
{ for (int i=0; i<N; ++i) *(to+i) = *(from+i); return; }
void copy_array(double *to, const double *from, int N)
{ for (int i=0; i<N; ++i) *(to+i) = *(from+i); return; }
// Copies an array column (or row) -----------------------------------------------------
void copy_array_col(double *to, double *from, int Nr_from, int Nc_from, int j_col_from)
{ for (int i=0; i<Nr_from; ++i) *(to+i) = *(from+i*Nc_from+j_col_from); return; }
void copy_array_col(int *to, int *from, int Nr_from, int Nc_from, int j_col_from)
{ for (int i=0; i<Nr_from; ++i) *(to+i) = *(from+i*Nc_from+j_col_from); return; }
void copy_array_row(double *to, double *from, int Nr_from, int Nc_from, int j_row_from)
{ for (int i=0; i<Nc_from; ++i) *(to+i) = *(from+j_row_from*Nc_from+i); return; }
void copy_array_row(int *to, int *from, int Nr_from, int Nc_from, int j_row_from)
{ for (int i=0; i<Nc_from; ++i) *(to+i) = *(from+j_row_from*Nc_from+i); return; }
// copies a submatrix of size (endrow-startrow+1) by (endcol-startcol+1)
void copy_submatrix(double *to, double *from, int Nr_from, int Nc_from, int startrow,
int startcol, int Nr_sub, int Nc_sub)
{
// diagnostics
if ((Nr_sub < 1) || (Nc_sub < 1)) {
log_fout << "Error: copy_submatrix. Nonpositive submat dimensions. Exiting...";
return;
}
if (((startrow+Nr_sub)>Nr_from) || ((startcol+Nc_sub)>Nc_from)) {
log_fout << "Error: copy_submatrix. Submat dimensions bigger than original.";
return;
}
// copy submatrix row by row
for (int i=startrow; i<startrow+Nr_sub; ++i)
copy_array(to+i*Nc_sub, from+i*Nc_from+startcol, Nc_sub);
return;
}
// Puts a column (or row) into an array -----------------------------------------------
void put_array_col(double *to, double *from, int Nr_to, int Nc_to, int j_col_to)
{ for (int i=0; i<Nr_to; ++i) *(to+i*Nc_to+j_col_to) = *(from+i); return; }
void put_array_col(int *to, int *from, int Nr_to, int Nc_to, int j_col_to)
{ for (int i=0; i<Nr_to; ++i) *(to+i*Nc_to+j_col_to) = *(from+i); return; }
void put_array_row(double *to, double *from, int Nr_to, int Nc_to, int j_row_to)
{ for (int i=0; i<Nc_to; ++i) *(to+j_row_to*Nc_to+i) = *(from+i); return; }
void put_array_row(int *to, int *from, int Nr_to, int Nc_to, int j_row_to)
{ for (int i=0; i<Nc_to; ++i) *(to+j_row_to*Nc_to+i) = *(from+i); return; }
// scalar multiplication of a matrix --------------------------------------------------
void mat_rescale(double *m_new, double *m_old, double scalar, int N)
{
double *m_tmp; m_tmp = new double [N];
copy_array(m_tmp,m_old,N);
for (int i=0; i<N; ++i)
*(m_new+i) = *(m_tmp+i)*scalar;
delete[] m_tmp;
return;
}
void mat_rescale(double *m, double scalar, int N)
{
mat_rescale(m, m, scalar, N);
return;
}
// Computes the norm of an array or distance between 2 arrays in R(N) ------------------
double norm_L2(double *m1, double *m2, int N)
{
double norm = 0;
for (int i=0; i<N; i++) {
double current = *(m1+i) - (*(m2+i));
current = current*current;
norm += current;
} // end for(i)
norm = pow(norm, 0.5) / N;
return norm;
} // end norm_L2()
double norm_L2(double *x, int N)
{
double *zeroN; zeroN = new double [N];
const_array(zeroN, N, 0);
double norm = norm_L2(x, zeroN, N);
delete[] zeroN;
return norm;
}
double norm_sup(double *m1, double *m2, int N)
{
double current, norm = 0;
for (int i=0; i<N; ++i) {
current = *(m1+i) - (*(m2+i));
current = fabs(current);
if (current>norm) norm = current;
} // end for(i)
return norm;
} // end norm_sup()
double norm_sup(int *m1, int *m2, int N)
{
double norm = 0; double temp;
for (int i=0; i<N; ++i) {
temp = *(m1+i) - (*(m2+i));
temp = fabs(temp);
if (temp>norm) norm = temp;
}
return norm;
}
double norm_sup(double *m, int N)
{
// create zero vector
double *zero_tmp; zero_tmp = new double [N];
const_array(zero_tmp, N, 0);
double norm = norm_sup(m, zero_tmp, N);
delete[] zero_tmp;
return norm;
}
// Computes the sup norm of the percentage difference between m1 and m2 using their average as the denominator
double norm_sup_percent(double *m1, double *m2, int N)
{
double *m_aver; m_aver = new double [N];
double *dm_percent; dm_percent = new double [N];
for (int i=0; i<N; ++i) {
// compute average
*(m_aver+i) = 0.5*(*(m1+i) + *(m2+i));
// compute change as percentage of average
if (*(m_aver+i)!=0)
*(dm_percent+i) = (*(m1+i) - *(m2+i))/ *(m_aver+i);
else *(dm_percent+i) = 0;
}
double norm = norm_sup(dm_percent, N);
delete[] m_aver; delete[] dm_percent;
return norm;
}
// ------------------- MATRIX ADDITION --------------------------------------------------------
// MS = ML + scalar*MR
void mat_add(double *msum, double *mleft, double *mright, double scalar_mright, int N)
{
double *mleft_tmp, *mright_tmp;
mleft_tmp = new double [N]; mright_tmp = new double [N];
copy_array(mleft_tmp, mleft, N);
copy_array(mright_tmp, mright, N);
for (int i=0; i<N; ++i)
*(msum+i) = *(mleft_tmp+i) + scalar_mright* *(mright_tmp+i);
delete[] mleft_tmp; delete[] mright_tmp;
return;
}
// MS = scalarL*ML + scalarR*MR
void mat_add(double *sum, double *mleft, double *mright,
double scalar_left, double scalar_right, int N)
{
double *mleft_tmp; mleft_tmp = new double [N];
mat_rescale(mleft_tmp,mleft,scalar_left,N);
mat_add(sum,mleft_tmp,mright,scalar_right,N);
delete[] mleft_tmp;
return;
}
// adds a constant (scalar) to each element
void mat_add(double *sum, double *m, double scalar, int N)
{
double *m_temp; m_temp = new double [N];
const_array(m_temp, N, scalar);
mat_add(sum, m, m_temp, 1.0, N);
delete[] m_temp;
return;
}
// sums the elements of an array
double mat_sum(double *m, int N)
{ double sum = 0; for (int i=0; i<N; ++i) { sum += *(m+i); } return sum; }
int mat_sum(int *m, int N)
{ int sum = 0; for (int i=0; i<N; ++i) sum += m[i]; return sum; }
// returns the number of "non-positive" elements of a matrix
int mat_positivity(double *m, int N, double eps_tmp)
{
int mflag = 0;
for (int i=0; i<N; ++i) {
if (m[i] < eps_tmp) ++mflag;
}
return mflag;
}
// matrix multiplication ----------------------------------------------------------------
void mat_mult(double *left_mat, double *right_mat, double *prod_mat,
int Nrow, int Ncommon, int Ncol)
{
// make copy of original matrices
double *mleft_tmp; mleft_tmp = new double [Nrow*Ncommon];
double *mright_tmp; mright_tmp = new double [Ncommon*Ncol];
copy_array(mleft_tmp, left_mat, Nrow*Ncommon);
copy_array(mright_tmp, right_mat, Ncommon*Ncol);
// compute product mat
double *col_tmp; col_tmp = new double [Ncommon];
for (int i_row=0; i_row<Nrow; ++i_row) {
for (int i_col=0; i_col<Ncol; ++i_col) {
copy_array_col(col_tmp, right_mat, Ncommon, Ncol, i_col);
*(prod_mat+i_row*Ncol+i_col) = mat_mult(left_mat+i_row*Ncommon,
col_tmp, Ncommon);
} // end for(i_col)
} // end for(i_row)
delete[] mleft_tmp; delete[] mright_tmp;
return;
} // end mat_mult()
double mat_mult(double *left_mat, double *right_mat, int N)
{
double dot_prod = 0;
for (int i=0; i<N; ++i)
dot_prod += *(left_mat+i) * *(right_mat+i);
return dot_prod;
}
void mat_mult(double *left, double *right, double *elementwiseprod, int N)
{ for (int i=0; i<N; ++i) { *(elementwiseprod+i) = *(left+i) * *(right+i); } return; }
void mat_mult(double *left, double *right, double *outerprod, int Nleft, int Nright)
{
for (int i=0; i<Nleft; ++i) {
for (int j=0; j<Nright; ++j) {
*(outerprod+i*Nright+j) = *(left+i) * *(right+j);
}
}
return;
}
void mat_div(double *div_elementwise, double *num, double *den, int N)
{
for (int i=0; i<N; ++i) {
if (*(den+i) != 0) *(div_elementwise+i) = *(num+i) / *(den+i);
else *(div_elementwise+i) = 0;
}
return;
}
// Computes mean, deviations from mean and variance of an array ------------------------
double get_mean(double *x, int N)
{ double mean=0; for (int i=0; i<N; ++i) mean += *(x+i); mean /= N; return mean; }
double get_mean(double *x, int N, double MISSING_FLAG)
{
double mean = 0; int Nmean = 0;
for (int i=0; i<N; ++i)
if (*(x+i)!=MISSING_FLAG) { mean += *(x+i); ++Nmean; }
mean /= Nmean;
return mean;
}
double get_mean(double *x, int Nx, double MISSING_FLAG, int& Nmissing)
{
double mean = 0; int Nmean = 0;
for (int i=0; i<Nx; ++i)
if (*(x+i)!=MISSING_FLAG) { mean += *(x+i); ++Nmean; }
if (Nmean > 0) mean /= Nmean;
Nmissing = Nx - Nmean;
return mean;
}
void get_deviation_from_mean(double *xdev, double *x, int N)
{ double mean = get_mean(x,N); for (int i=0; i<N; ++i) { *(xdev+i) = *(x+i)-mean; } return; }
double get_variance(double *x, int N)
{
double variance;
double *x_dev; x_dev = new double [N];
double x_mean = get_mean(x,N);
mat_add(x_dev, x, x_mean, N);
variance = mat_mult(x_dev,x_dev,N)/(N-1.0);
delete[] x_dev;
return variance;
}
double get_standev(double *x, int N)
{
double std = get_variance(x, N);
std = pow(std, 0.5);
return std;
}
double get_correlation(double *m1, double *m2, int N)
{
double correl = vector_correlation(m1, m2, N);
return correl;
}
// computes variance and stdev of a series with (assumed) mean zero
double get_log_variance(double *x, int N)
{
double var_x = mat_mult(x, x, N);
var_x /= N-1.0;
return var_x;
}
double get_log_standev(double *x, int N)
{
double std_x = get_log_variance(x, N);
std_x = pow(std_x, 0.5);
return std_x;
}
void get_percentage_change(double *dx, double *x_bench, double *x_current, int N)
{
double *dx_raw; dx_raw = new double [N];
int Nprec = 6;
// compute the absolute change from benchmark values
mat_add(dx_raw, x_current, x_bench, -1, N);
// get change as fraction of benchmark values
mat_div(dx, dx_raw, x_bench, N);
// rescale change into percentage
mat_rescale(dx, dx, 100, N);
// free mem
delete[] dx_raw;
return;
}
double get_percentage_change(double xBM, double xXP)
{
double dx = 0;
if (xBM != 0)
dx = (xXP - xBM) / xBM * 100;
return dx;
}
/* matrix interpolation (minterp = theta*m1 + (1-theta)*m2) --------------------------- */
void mat_interpolation(double *minterp, double *m1, double *m2, double theta_m1, int N)
{
double *m1_tmp; m1_tmp = new double [N];
double *m2_tmp; m2_tmp = new double [N];
if ((theta_m1>1)||(theta_m1<0))
fout_mat_interpolation_error( log_fout, m1, m2, theta_m1, N);
mat_rescale(m1_tmp,m1,theta_m1,N);
mat_rescale(m2_tmp,m2,1-theta_m1,N);
mat_add(minterp,m1_tmp,m2_tmp,1,N);
delete[] m1_tmp; delete[] m2_tmp;
return;
}
// Transpose matrice --------------------------------------------------------------------
void transpose_mat(double *mtrans, double *m, int Nr, int Nc) // TESTED
{ // Nrows and Ncols = rows and columns of original matrix m
double *m_tmp; m_tmp = new double [Nr*Nc];
copy_array(m_tmp,m,Nr*Nc);
for (int i=0; i<Nr; ++i)
for (int j=0; j<Nc; ++j)
*(mtrans+j*Nr+i) = *(m_tmp+i*Nc+j);
delete[] m_tmp;
return;
} // end transpose_mat()
void mat_transpose(double *mtrans, double *m, int Nr, int Nc)
{ transpose_mat(mtrans, m, Nr, Nc); return; }
// creates a matrix whose rows are basis for R(N) (creates an identity matrix) ------------
void make_basis_vectors(double *u, int N)
{ const_array(u,N*N,0); for (int i=0; i<N; ++i) *(u+i*N+i) = 1; return; }
// creates an identity matrix of dim Nx ---------------------------------------------------
void make_identity_matrix(double *mat, int Nx)
{ const_array(mat,Nx*Nx,0); for (int i=0; i<Nx; ++i) *(mat+i*Nx+i) = 1; return; }
/* returns the max (min) of elements of an array *x of length Nx */
double get_max(double *x, int Nx)
{
double xcurrent;
double xmax = *(x+0);
for (int i=0; i<Nx; ++i) {
xcurrent = *(x+i);
if (xcurrent>xmax) xmax = xcurrent;
}
return xmax;
}
double get_min(double *x, int Nx)
{
double xcurrent;
double xmin = *(x+0);
for (int i=0; i<Nx; ++i) {
xcurrent = *(x+i);
if (xcurrent<xmin) xmin = xcurrent;
}
return xmin;
}
// given an array *mat, finds the max (non-neg) element and reports its value, max_mat, and its index, jmax_mat.
void get_max(double *mat, int Nx, double& max_mat, int& jmax_mat)
{
double maxmat = 0;
int jmaxmat = -1;
for (int i=0; i<Nx; ++i) {
if (mat[i] > maxmat) {
maxmat = mat[i];
jmaxmat = i;
}
}
max_mat = maxmat;
jmax_mat = jmaxmat;
return;
}
// given an array *mat, finds the largest absolute value and reports its absolute value and its index.
void get_max_fabs(double *mat, int Nx, double& max_mat, int& jmax_mat)
{
double maxmat = 0;
int jmaxmat = -1;
for (int i=0; i<Nx; ++i) {
if (fabs(mat[i]) > maxmat) {
maxmat = fabs(mat[i]);
jmaxmat = i;
}
}
max_mat = maxmat;
jmax_mat = jmaxmat;
return;
}
int get_max(int *x, int Nx)
{
int xcurrent;
int xmax = *(x+0);
for (int i=0; i<Nx; ++i) {
xcurrent = *(x+i);
if (xcurrent>xmax) xmax = xcurrent;
}
return xmax;
}
int get_min(int *x, int Nx)
{
int xcurrent;
int xmin = *(x+0);
for (int i=0; i<Nx; ++i) {
xcurrent = *(x+i);
if (xcurrent<xmin) xmin = xcurrent;
}
return xmin;
}
// numerical computation of 1st and 2nd derivative in R(1)
double get_dfdx(double& fx, double x, double (*fct)(double x), double step_df)
{
double dfdx;
double fx_above, fx_below, x_above, x_below;
// set x(+) = x+h and x(-) = x-h where h = step_df
x_above = x + step_df;
x_below = x - step_df;
// evaluate f(+) = f(x+h) and f(-) = f(x-h)
fx_above = fct(x_above);
fx_below = fct(x_below);
// evaluate f = 0.5(f(+)+f(-)) and dfdx = (f(+)-f(-))/2h
fx = 0.5*(fx_above+fx_below);
dfdx = (fx_above-fx_below)/(2*step_df);
return dfdx;
}
double get_d2fx2(double& fx, double x, double (*fct)(double x), double step_df)
{
double fx_above, fx_below, x_above, x_below;
// set x(+) and x(-)
x_above = x+step_df;
x_below = x-step_df;
// compute derivative (from above) and (from below) at x
double dfdx_above = get_dfdx(fx_above, x_above, fct, step_df);
double dfdx_below = get_dfdx(fx_below, x_below, fct, step_df);
// compute 2nd derivative
double d2f = (dfdx_above - dfdx_below)/(2*step_df);
fx = 0.5*(fx_above+fx_below);
return d2f;
}
double get_d2f(double& fx, double &df, double x, double (*fct)(double x), double h)
{
double fabove, ffabove, ffbelow, fbelow, xxabove, xabove, xbelow, xxbelow;
// set x+h, x+2h, x-h and x-2h
xabove = x+h;
xxabove = x+2.0*h;
xbelow = x-h;
xxbelow = x-2.0*h;
// evaluate function at 5 points
ffabove = fct(xxabove);
fabove = fct(xabove);
ffbelow = fct(xxbelow);
fbelow = fct(xbelow);
fx = fct(x);
// compute derivatives
df = -ffabove + 8*fabove - 8*fabove + ffbelow;
df /= 12*h;
double d2f = -ffabove + 16*fabove - 30*fx + 16*fbelow - ffbelow;
d2f /= 12*pow(h, 2.0);
return d2f;
}
/* <get_jacob> computes jacobian of a function <*fct> from R(N) to R(N) such that J(x) = (f(x+h) - f(x-h))/2h and f(x) = 0.5(f(x+h)+f(x-h)). The step size h can be specified as an absolute or as a percentage of x. <get_gradient> computes the gradient of a function from R(N) to R. <get_dfdx_along_s> and <get_d2fdx2_along_s> computes the 1st and 2nd derivatives of a function along the direction s. */
void get_jacob_old(double *x, double *jacob, double *fx, int N,
void (*fct)(double *fx, double *x), double step_jacob)
{
double *fx_above; fx_above = new double [N];
double *fx_below; fx_below = new double [N];
double *x_above; x_above = new double [N];
double *x_below; x_below = new double [N];
// init x(+) = x_above and x(-) = x_below
copy_array(x_above, x, N);
copy_array(x_below, x, N);
// turn off updating of initial guess for calibration
int update_x0_flag_tmp = update_x0_FLAG;
update_x0_FLAG = OFF;
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
// get f(x+h)
*(x_above+j) += step_jacob;
fct(fx_above, x_above);
// get f(x-h)
*(x_below+j) -= step_jacob;
fct(fx_below, x_below);
// get J(x)
*(jacob+i*N+j)=(*(fx_above+i)-*(fx_below+i))/(2*step_jacob);
// reset x(+) and x(-)
*(x_above+j) -= step_jacob;
*(x_below+j) += step_jacob;
} // end for(j)
// get f(x)
*(fx+i) = 0.5* (*(fx_above+i) + *(fx_below+i));
} // end for(i)
// restore old update_x0_flag
update_x0_FLAG = update_x0_flag_tmp;
delete[] fx_above; delete[] fx_below; delete[] x_above; delete[] x_below;
return;
}
void get_jacob(double *x, double *jacob, double *fx, int N,
void (*fct)(double *fx, double *x), double step_jacob)
{
double *fabove; fabove = new double [N];
double *fbelow; fbelow = new double [N];
double *df; df = new double [N];
double *xabove; xabove = new double [N];
double *xbelow; xbelow = new double [N];
// init x(+) = x_above and x(-) = x_below
copy_array(xabove, x, N);
copy_array(xbelow, x, N);
// turn off updating of initial guess for calibration
int update_x0_flag_tmp = update_x0_FLAG;
update_x0_FLAG = OFF;
// compute f(x)
fct(fx, x);
// compute jacobian
for (int i=0; i<N; ++i) {
// get f(x+h)
*(xabove+i) += step_jacob;
fct(fabove, xabove);
// get f(x-h)
*(xbelow+i) -= step_jacob;
fct(fbelow, xbelow);
// get J(x)
mat_add(df, fabove, fbelow, -1.0, N);
mat_rescale(df, df, 1/(2*step_jacob), N);
put_array_col(jacob, df, N, N, i);
// reset x(+) and x(-)
*(xabove+i) -= step_jacob;
*(xbelow+i) += step_jacob;
}
// restore old updating-x0 flag
update_x0_FLAG = update_x0_flag_tmp;
// free mem
delete[] fabove; delete[] fbelow; delete[] df; delete[] xabove; delete[] xbelow;
return;
}
void get_jacob(double *x, double *jacob, double *fx,
void (*fct)(double *fx, double *x),
int N, double percent_step_jacob)
{
double step_jacob;
double *fx_above; fx_above = new double [N];
double *fx_below; fx_below = new double [N];
double *x_above; x_above = new double [N];
double *x_below; x_below = new double [N];
copy_array(x_above, x, N);
copy_array(x_below, x, N);
// turn off updating of initial guess for calibration
int update_x0_flag_tmp = update_x0_FLAG;
update_x0_FLAG = OFF;
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
step_jacob = *(x+j)*percent_step_jacob;
*(x_above+j) += step_jacob;
*(x_below+j) -= step_jacob;
fct(fx_above, x_above);
fct(fx_below, x_below);
*(jacob+i*N+j)=(*(fx_above+i)-*(fx_below+i))/(2*step_jacob);
*(x_above+j) -= step_jacob;
*(x_below+j) += step_jacob;
} // end for(j)
*(fx+i) = 0.5*(*(fx_above+i) + *(fx_below+i));
} // end for(i)
// restore old update_x0_flag
update_x0_FLAG = update_x0_flag_tmp;
// free mem
delete[] x_above; delete[] x_below; delete[] fx_above; delete[] fx_below;
return;
}
void get_gradient(double *Jx, double *x, double& fx, int Nx, double (*fct)(double *x),
double step_jacob)
{
// turn off updating of initial guess for calibration
int update_x0_flag_tmp = update_x0_FLAG;
update_x0_FLAG = OFF;
//double fx_above, fx_below;
double *fx_above; fx_above = new double [Nx];
double *fx_below; fx_below = new double [Nx];
double *x_above; x_above = new double [Nx];
double *x_below; x_below = new double [Nx];
// init x+h and x-h
copy_array(x_above, x, Nx);
copy_array(x_below, x, Nx);
// get gradient J(x)
for (int i=0; i<Nx; ++i) {
*(x_above+i) += step_jacob;
*(x_below+i) -= step_jacob;
*(fx_above+i) = fct(x_above);
*(fx_below+i) = fct(x_below);
*(Jx+i) = (fx_above[i] - fx_below[i])/(2*step_jacob);
*(x_above+i) -= step_jacob;
*(x_below+i) += step_jacob;
}
// get f(x)
fx = fct(x);
// write out fct call summmary
if (fout_get_gradient_FLAG == ON) {
fout_get_gradient_diag(log_fout, x, Jx, Nx, fx, fx_above, fx_below, step_jacob);
}
// restore old update_x0_flag
update_x0_FLAG = update_x0_flag_tmp;
// free mem
delete[] x_above; delete[] x_below;
delete[] fx_above; delete[] fx_below;
return;
}
void get_gradient(double *Jx, double *x, double& fx, double (*fct)(double *x),
int Nx, double percent_change)
{
// turn off updating of initial guess for calibration
int update_x0_flag_tmp = update_x0_FLAG;
update_x0_FLAG = OFF;
double fx_above, fx_below;
double *x_above; x_above = new double [Nx];
double *x_below; x_below= new double [Nx];
double step_jacob;
// init copies of x+h and x-h
copy_array(x_above, x, Nx);
copy_array(x_below, x, Nx);
// check for bounds error in percent change parameter
if (percent_change>1) {
log_fout << endl << "get_gradient: ERROR!!! percent_change > 100% !!!!"
<< " percent change=" << percent_change;
}
// get gradient J(x)
for (int i=0; i<Nx; ++i) {
step_jacob = *(x+i)*percent_change;
*(x_above+i) += step_jacob;
*(x_below+i) -= step_jacob;
fx_above = fct(x_above);
fx_below = fct(x_below);
*(Jx+i) = (fx_above - fx_below)/(2*step_jacob);
*(x_above+i) -= step_jacob;
*(x_below+i) += step_jacob;
}
// get f(x)
fx = fct(x);
// write out fct call summary
if (fout_get_gradient_FLAG == ON) {
log_fout << endl << "Percent_step_jacob=" << percent_change;
fout_get_gradient_diag(log_fout, x, Jx, Nx, fx, step_jacob);
}
// restore old update_x0_flag
update_x0_FLAG = update_x0_flag_tmp;
delete[] x_above; delete[] x_below;
return;
}
double get_dfdx_along_s(double& fx, double *x, double *s_dir, int Nx,
double (*fct)(double *x), double step_jacob)
{
// compute f(x+h) where h = x + s_dir*step_Jacob
double *xbelow; xbelow = new double [Nx];
double *xabove; xabove = new double [Nx];
double *dir_tmp; dir_tmp = new double [Nx];
// make copy of search direction
copy_array(dir_tmp, s_dir, Nx);
// rescale dir into unit vector
double rescale_dir = norm_L2(dir_tmp, Nx);
mat_rescale(dir_tmp, dir_tmp, 1/rescale_dir, Nx);
// compute x+h and f(x+h)
mat_add(xabove, x, dir_tmp, step_jacob, Nx);
double fxabove = fct(xabove);
// compute x-h and f(x-h)
mat_add(xbelow, x, dir_tmp, -step_jacob, Nx);
double fxbelow = fct(xbelow);
// compute dfdx(x) = [f(x+h)-f(x-h)]/2h and f(x) = 0.5*(f(x+h)+f(x-h))
double dfdx = (fxabove-fxbelow)/(2*step_jacob);
fx = 0.5*(fxabove+fxbelow);
// write out function call diagnostics
if ( fout_get_dfdx_along_s_FLAG == ON) {
fout_get_dfdx_diag( log_fout, dir_tmp, fx, fxabove, fxbelow, x, xabove, xbelow,
dfdx, Nx, step_jacob);
}
delete[] xabove; delete[] xbelow; delete[] dir_tmp;
return dfdx;
}
// 3 and 5 point approximation without calling get_dfdx_along_s (use flag to set switch, default is 3 point approx)
double get_d2fdx2_along_s(double &fx, double *x, double *sdir, int Nx,
double (*fct)(double *x), double h, double& dfdx, int flag)
{
// vars for 3pt approx
double *xabove; xabove = new double [Nx];
double *xbelow; xbelow = new double [Nx];
// add vars for 5pt approx
double *xxabove; xxabove = new double [Nx];
double *xxbelow; xxbelow = new double [Nx];
const_array(xxabove, Nx, 0);
const_array(xxbelow, Nx, 0);
// make copy of search direction
double *sdir_tmp; sdir_tmp = new double [Nx];
copy_array(sdir_tmp, sdir, Nx);
// rescale search direction into unit vector
double rescale_sdir = norm_L2(sdir_tmp, Nx);
mat_rescale(sdir_tmp, sdir_tmp, 1/rescale_sdir, Nx);
// compute f(x+h)
mat_add(xabove, x, sdir_tmp, h, Nx);
double fabove = fct(xabove);
// compute f(x-h)
mat_add(xbelow, x, sdir_tmp, -h, Nx);
double fbelow = fct(xbelow);
// compute f(x)
fx = fct(x);
double d2fdx2, ffabove = 0, ffbelow = 0;
// compute 2nd derivative along sdir (default case = 3 pt approx of order O(h2) )
if (flag == DIFF_5PT_FLAG) {
// compute f(x+2h)
mat_add(xxabove, x, sdir_tmp, 2.0*h, Nx);
ffabove = fct(xxabove);
// compute f(x-2h)
mat_add(xxbelow, x, sdir_tmp, -2.0*h, Nx);
ffbelow = fct(xxbelow);
// 5 pt approx for 2nd derivative
d2fdx2 = -ffabove + 16*fabove - 30*fx + 16*fbelow -ffbelow;
d2fdx2 /= 12*pow(h, 2.0);
// 5 pt approx for 1st derivative
dfdx = -ffabove + 8*fabove - 8*fbelow + ffbelow;
dfdx /= 12*h;
// spit out 4th derivative for diag info for computing optimal h
double d4f = (ffabove - 4*fabove + 6*fx - 4*fbelow + ffbelow);
d4f /= pow(h, 4.0);
log_fout << endl << "d4f=" << d4f << endl;
}
else {
d2fdx2 = (fabove - 2*fx + fbelow)/pow(h, 2.0);
dfdx = (fabove - fbelow)/(2*h);
}
// write out function call diagnostics
if (fout_get_d2fdx2_along_s_FLAG == ON) {
fout_get_d2fdx2_diag(log_fout, sdir_tmp, ffabove, fabove, fx, fbelow, ffbelow, Nx,
h, xxabove, xabove, x, xbelow, xxbelow, d2fdx2, dfdx);
}
// free up memory
delete[] sdir_tmp; delete[] xabove; delete[] xbelow;
return d2fdx2;
}
double get_d2fdx2_along_s(double& fx, double *x, double *s_dir, int Nx,
double (*fct)(double *x), double step_jacob)
{
double *xabove; xabove = new double [Nx];
double *xbelow; xbelow = new double [Nx];
double fx_above, fx_below, dfdx;
// make copy of search direction
double *dir_tmp; dir_tmp = new double [Nx];
copy_array(dir_tmp, s_dir, Nx);
// rescale search direction into unit vector
double rescale_dir = norm_L2(dir_tmp, Nx);
mat_rescale(dir_tmp, dir_tmp, 1/rescale_dir, Nx);
// compute derivative (from above) at x
mat_add(xabove, x, dir_tmp, step_jacob, Nx);
double dfdx_above = get_dfdx_along_s(fx_above, xabove, dir_tmp, Nx,fct, step_jacob);
// compute derivative (from below) at x
mat_add(xbelow, x, dir_tmp, -step_jacob, Nx);
double dfdx_below = get_dfdx_along_s(fx_below, xbelow, dir_tmp, Nx, fct, step_jacob);
dfdx = 0.5*(dfdx_above+dfdx_below);
// compute 2nd derivative and function at x
double d2fdx2 = (dfdx_above - dfdx_below)/(2*step_jacob);
fx = 0.5*(fx_above+fx_below);
// write out function call diagnostics
if (fout_get_d2fdx2_along_s_FLAG == ON) {
fout_get_d2fdx2_diag( log_fout, dir_tmp, dfdx_above, dfdx_below, xabove, xbelow,
Nx, d2fdx2, step_jacob, dfdx);
}
// free up memory
delete[] dir_tmp; delete[] xabove; delete[] xbelow;
return d2fdx2;
}
// matrix inverter - Given matrix and current row (pivot), finds the next candidate pivot row. */
int get_pivot(double *mat /* ptr to matrix */, int current_row, int N_rows)
{
int pivot_row = NO_PIVOT_FLAG;
for (int i=current_row; i<N_rows; ++i) {
if (fabs(*(mat+i*N_rows+current_row)) > eps_MatInv)
{ pivot_row=i; break; }
} // end for(i)
return pivot_row;
}
/* Swaps rows pivot row and cur_row in square matrices m and minv */
void swap_row(double *m, double *minv, int pivot_row, int cur_row, int N_rows)
{
double *row_temp; row_temp = new double [N_rows];
if ( fout_swap_row_FLAG == ON) {
log_fout << endl << "swap row: row1=" << pivot_row << ", row2=" << cur_row;
}
// copy m old row to temp
copy_array(row_temp,m+cur_row*N_rows,N_rows);
// copy m pivot row to old row
copy_array(m+cur_row*N_rows,m+pivot_row*N_rows,N_rows);
// copy temp to m old row
copy_array(m+pivot_row*N_rows,row_temp,N_rows);
// copy m_inv_ptr old row to temp
copy_array(row_temp,minv+cur_row*N_rows,N_rows);
// copy m_inv pivot row to old row
copy_array(minv+cur_row*N_rows,minv+pivot_row*N_rows,N_rows);
// copy temp to minv old row
copy_array(minv+pivot_row*N_rows,row_temp,N_rows);
delete[] row_temp;
return;
}
/* Gauss-Jordan mat inv - sets all entries below pivot to zero */
void zero_below(double *m, double *minv, int pivot, int N_rows)
{
// make current row have leading one
double m_pivot_entry = *(m+pivot*N_rows+pivot);
for (int j=0; j<N_rows; ++j) {
*(m+pivot*N_rows+j) = *(m+pivot*N_rows+j)/m_pivot_entry;
*(minv+pivot*N_rows+j) = *(minv+pivot*N_rows+j)/m_pivot_entry;
} // end for(j)
// over loop over rows
for (int i=pivot+1; i<N_rows; ++i) {
double m_factor = *(m+i*N_rows+pivot);
// inner loop over columns
for (int j=0; j<N_rows; ++j) {
*(m+i*N_rows+j) = *(m+i*N_rows+j) - m_factor* *(m+pivot*N_rows+j);
*(minv+i*N_rows+j) = *(minv+i*N_rows+j) - m_factor* *(minv+pivot*N_rows+j);
} // end for(j)
} // end for(i)
return;
}
/* Guass-Jordan mat inv - sets all entries above pivot to zero */
void zero_above(double *m, double *minv, int pivot, int N_rows)
{
// loop over rows
for (int i=pivot-1; i>=0; --i) {
double m_factor = *(m+i*N_rows+pivot);
// loop over columns
for (int j=0; j<N_rows; ++j) {
*(m+i*N_rows+j) = *(m+i*N_rows+j) - *(m+pivot*N_rows+j)*m_factor;
*(minv+i*N_rows+j) = *(minv+i*N_rows+j) - *(minv+pivot*N_rows+j)*m_factor;
}// end for(j)
} // end for(i)
return;
}
/* Guass-Jordan matrix inversion of square matrix m_ptr with #rows=N_rows. */
int inv_mat(double *m_inv_ptr, double *m_ptr, int N_rows)
{
double *m_original; m_original = new double [N_rows*N_rows];
copy_array(m_original, m_ptr, N_rows*N_rows);
int process_flag = 0;
// init m_inv as iden matrice
const_array(m_inv_ptr, N_rows*N_rows, 0);
for (int i=0; i<N_rows; ++i) *(m_inv_ptr+i*N_rows+i) = 1;
// make m_ptr upper triangular - loop over rows
for (int cur_row=0; cur_row<N_rows; ++cur_row) {
int pivot_row = get_pivot(m_ptr, cur_row, N_rows);
if (pivot_row != NO_PIVOT_FLAG) {
swap_row(m_ptr, m_inv_ptr, pivot_row, cur_row, N_rows);
zero_below(m_ptr, m_inv_ptr, cur_row, N_rows);
} // end if()
if (pivot_row == NO_PIVOT_FLAG) {
fout_inv_mat_error( log_fout, cur_row, N_rows, m_ptr, m_original);
process_flag = SINGULAR_MATRIX_FLAG;
break;
} // end if()
} // end for(cur_row)
// make m_ptr an iden matrix - loop over rows
for (int j=N_rows-1; j>=0; --j)
zero_above(m_ptr, m_inv_ptr, j, N_rows);
copy_array(m_ptr, m_original, N_rows*N_rows);
delete[] m_original;
return process_flag;
}
int inv_mat(double *m, int Nrows)
{
double *minv; minv = new double [Nrows*Nrows];
int inv_mat_flag = inv_mat(minv, m, Nrows);
copy_array(m, minv, Nrows*Nrows);
delete[] minv;
return inv_mat_flag;
}
// computes a smooth percentage change of variables over Nt periods given base period values and end period values where
// xend[i] = pow(dx[i], Nt) * xbase[i] forall i. Note Nt=total # periods including base and end.
void get_growth_rate(double *dx, double *xbase, double *xend, int Nx, int Nt)
{
double *xchange; xchange = new double [Nx];
for (int i=0; i<Nx; ++i) {
xchange[i] = xend[i] / xbase[i];
dx[i] = pow(xchange[i], 1.0/(Nt-1));
}
return;
}
// Cholesky decomp of a positive definite matrix A=a(i,j) into a lower triangular matrix M=m(i,j) NEED TO BE WRITTEN ********************
int mat_decomp(double *a, double *m, int N)
{
// local vars
int flag = 0;
double row_sum_m_i;
// init M to be zero matrix (bc upper triangular part is zero), and we write over the rest
const_array(m, N*N, 0);
// loop over rows of A
for (int i=0; i<N; ++i) {