-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprgrad_reg.c
4378 lines (3623 loc) · 132 KB
/
prgrad_reg.c
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
/*
This code contains functions which solve Fredholm's equations of the 1st
kind and Abel integral equatiob by the method of projection of conjugate
gradients and uses the Tikhonov's regularization method.
*/
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <float.h> // This is where DBL_MAX constant is kept.
#define NTEST 1001 // Max size for various test arrays which I do not allocate dynamically. IMPORTANT!!!
// This number MUST be equal or larger than NMAX, MMAX constants in the calling program
// wr_prgrad or abel_prgrad!!!
// Equations kernels are declared here.
extern void kernel1();
extern void kernel2();
extern void kernel_abel();
extern void kernel_test();
// Spline approximation
extern void spline_approx();
/*
Functions which allow me to allocate/free one- and two-dimensional arrays
dynamically. The problem here is that I have to pass 2D arrays as function
arguments and within such a function I want to use convenient notation
like a[i][j] and not some form of (*a)[i*ncols+j]. Yet I do not know the
number of columns in advance so I cannot declare the formal argument "a" as
e.g. double a[][NCOLS]. The functions below allow me to get around this problem.
*/
double **matrix(int nrows, int ncols);
void free_matrix(double **m, int nrows);
double *vector( int n );
void free_vector( double *v );
int *int_vector( int n );
void free_int_vector( int *v );
/*
Standard functions common for all methods of solving the Fredholm's eq. from the book of
Goncharsky, Cherepashxhuk, Yagola. See comments in the code of the functions.
*/
void pticr0( int kernel_type, double **a, double *x, // The function is changed compared to the original.
double *y, double r, double ax, int n, int m );
void pticr1( double *b, double *c, double p, double *a, int n );
void pticr2( double *a, double r, int n );
void ptici2( int *a, int r, int n );
void pticr3( double **a, double *z, double *u, int n, int m );
void pticr4( double **a, double *u, double *u0, double *v, double *hy, double sumv, double *g, int n, int m );
void pticr5( double *a, double *b, double *v, double sumv, double *hy, int m, double *s );
void pticr6( double *a, double *b, int n, double *s );
void pticr7( double *a, double *b, int *v, int n, double *s );
void pticr8( double *z, double alpha, double hs, double *gr, int n, char *metric );
void pticr8_new( double *z, double alpha, double hs, double *gr, int n, char *metric, int *iend );
void pticr9( double an2, double *z, int n, double alpha, double hs, double *as2, char *metric );
void deriv4( double *x, int n, double h, double *deriv );
/*
Functions ptizr* are to solve the Fredholm's equation of the 1st kind by
the regularization method of Tikhonov. The choice of the regularization
parameter alpha is done by the principle of generalized discrepancy. The
functions are DIFFERENT from those presented in the original books, even
if they have identical names. In my case, I have some additional a-priory
constraints on the unknown functions:
1.They are moonotonically non-increasing;
2.They are convexo-concaved.
3.In the primary minimum of the light curve (WR in front), the value Ia(0) is fixed.
All these constraints do not allow me to use the original form of the
functions as those are using the only a-priori constraint that the
unknown fuction is non-negative.
So instead of using the original function ptizr1 I use ptilr1. ptilr1 and
the functions it calls are also modified to make use of the regularization
method (see the code and the comments below).
*/
void ptizr_proj( int kernel_type, int switch_contype, int n_con, double **con, double *b, double rstar,
double *u0, double *v, double sumv, double *x, double *y, double ymin, double ymax,
int n, int m, double *z, double c2, double dl2, double eps, double h, int adjust_alpha,
double *alpha, char *metric, int l, int icore, double ax, double *eta,
int imax_reg, int *iter_reg, int imax_minim, int *iter_minim, int verbose, int *ierr );
void ptizra( double an2, double z[], int n, double dl, double dh, double hs, double *an4 );
/*
The functions ptilr* are to minimize a quadratic(!) functional by the
method of projection of conjugate gradients.
The functions ptilr, ptilr0, ptilr1, ptilr6, ptilrb, pticr0 are modified
compared to their original versions to adapt them to my particular problem
and to make use of the regularization method.
Note that when using regularization, I do not use ptilr. use ptizr
instead. ptilr is used when solving the problem on the compact class of
functions without regularization.
*/
int ptilr0( double **a, int n );
/*
void ptilr(int kernel_type, double rstar, double u0[], double v[], double
x[], double y[], int n, int m, double z[], double c2, double dl,
int imax, int l, double *eta, int *iter, int *ierr );
*/
void ptilr1( int kernel_type, double **a, double *u0, double *v, double sumv, double *hy,
int mn, double **con, double *b, int icore, int l, double hs, double del2,
double dgr, int icm, double **c, double *d, double *z, int n, int n_con,
double alpha, char *metric, double *eta, int *ici, int *ierr );
void ptilr3( double **aj, double **con, int n, int n_con, int *mask );
int ptilr4( double **aj, double **p, double **pi, int m, int n, int n_con );
void ptilr5( double **con, double *b, int *mask, int n, int *m,
int n_con, double **pi, double **c, double *d,
double *z, double alpha, double hs, int *k, int *iend, double dgr,
double **a, double *u0, double *v, double sumv, double *hy, int mn, char *metric );
void ptilr6( int kernel_type, int icore, double **con, double *b, double *z, double **p, int *mask,
int n, int *m, int n_con, double **c, double *d, int *iend, double alpha, double hs, char *metric, int l );
//void ptilr6( int kernel_type, int icore, double **con, double *z, double **p, int *mask,
// int n, int *m, int n_con, double **c, double *d, int *iend, double alpha, double hs, char *metric );
void ptilr7( double *z, double **c, double *d, double *gr, int n );
//void ptilra( int kernel_type, double **a, double *u, double *v, double sumv, double *hy, double **c, double *d, int n, int m );
void ptilra( double **a, double *u, double *v, double sumv, double *hy, double **c, double *d, int n, int m );
void ptilrb( int kernel_type, int switch_contype, int *n_con, double ***con,
double **b, int n, double c2, int icore, int l, int *ierr );
// Function declarations begin here.
double **matrix(int nrows, int ncols) {
// Allocate memory for a matrix of doubles.
int i, j;
double **m;
if( !(m = (double **)malloc((unsigned)nrows * sizeof(double *))) )
return NULL;
for( i=0; i<nrows; i++ ) {
if( !(m[i] = (double *)malloc((unsigned)ncols * sizeof(double))) ) {
for( j=i-1; j>=0; j-- )
free( (double *)m[j] );
return NULL;
}
}
return m;
}
void free_matrix(double **m, int nrows) {
int i;
for( i=nrows-1; i>=0; i-- )
free( (double *)m[i] );
free( (double *)m );
return;
}
double *vector( int n ) {
// Allocate memory for a vector of doubles.
double *v;
if( !( v = (double *)malloc( (unsigned)n*sizeof(double) ) ) )
return NULL;
return v;
}
int *int_vector( int n ) {
// Allocate memory for a vector of integers.
int *v;
if( !(v = (int *)malloc( (unsigned)n*sizeof(int) ) ) )
return NULL;
return v;
}
void free_vector( double *v ) {
free( (char *)v );
return;
}
void free_int_vector( int *v ) {
free( (char *)v );
return;
}
void pticr0( int kernel_type, double **a, double *x, double *y, double r, double ax, int n, int m ) {
/*
Compute the kernel of the Fredholm or Abel equation.
This function is modified compared to the original one. The original one
assumes that grids on x,y are even and the matrix "a" is calculated using
a function which give the value of the kernel at the point (y,x). In my
case, the grid on y is uneven. Also, I have the kernel functions kernel1 and
kernel 2 which compute the whole matrix "a" at once. They need x and y as
arrays. Also, the kernels for the primary and secondary minima are
different, that's why the kernel_type parameter.
Important: I include step of the grid on x (hx) in the matrix "a" so in
functions using a to compute e.g. discrepancy functional etc. I do not
have to multiply the sums by hx.
Conversion of the intergal eq.
x2
integral[ ak(y,x)*z(x)*dx ] = u0(y), y=y[0],...y[m-1]
x1
to a linear system. Rectangle formula for the above integral is used.
Note that my grid on x is such that its knots atre located in the middle
of the grid cells. That is, if the size of the grid cell is hx, my knots
are 0.5hx, 1.5hx, .... For this reason in all formulae approximating
various integrals I just multiply the integrated function by hx. I do not
have to multiply f(a) and f(b) by hx/2 as was done in the original version
of the program.
kernel_type - kernel type number. 1,2 for Fredholm eq, 3 for Abel, 4 for test model.
a[m,n] - the matrix of the operator "a"
x - array containing the grid (even) on x (unknown function)
y - array containing the grid (uneven, phases of the light curve) on y (right-hand side of eq)
r - radius of the normal (O) star
ax - coefficient of the linear darkening for the O star disk
n - dimension of the grid on x
m - dimension of the grid on y
kernel1,kernel2 - functions which compute ak above
Integration occurs on the second variable ak(y,x)
*/
int i, j;
double hx;
if( kernel_type == 1 )
kernel1( a, x, y, r, ax, n, m );
else if( kernel_type == 2 )
kernel2( a, x, y, r, n, m );
else if( kernel_type == 3 )
kernel_abel( a, x, y, n, m );
else if( kernel_type == 4 )
kernel_test(a, x, y, n, m);
else
kernel_test(a, x, y, n, m);
if( kernel_type != 3 ) { // In case 3 (Abel eq) I do not need hx in the kernel a.
hx = x[1]-x[0]; // Grid step on x
for( i=0; i<n; i++ ) {
for( j=0; j<m; j++ ) {
a[j][i] *= hx;
// If the grid knots are located in the middle of the grid intervals, comment out the following two lines.
if( i == 0 || i == n-1 )
a[j][i] /= 2.0;
}
}
}
return;
}
void pticr1( double *b, double *c, double p, double *a, int n) {
// Compute a[i] = b[i]+p*c[i], i = 0,n-1.
int i;
for( i=0; i<n; i++ ) {
a[i] = b[i]+p*c[i];
}
return;
}
void pticr2( double *a, double r, int n ) {
// Fill array with a number.
int i;
for( i=0; i<n; i++ ) {
a[i] = r;
}
return;
}
void ptici2( int *a, int r, int n ) {
// Fill array with an integer.
int i;
for( i=0; i<n; i++ ) {
a[i] = r;
}
return;
}
void pticr3( double **a, double *z, double *u, int n, int m ) {
/*
Multiplication of a matrix a[m,n] by the vector z[n]
u[m] - resulting vector.
*/
int i, j;
for( i=0; i<m; i++ ) {
u[i] = 0.0;
for( j=0; j<n; j++ ) {
(u[i]) += a[i][j]*z[j];
}
}
return;
}
void pticr4( double **a, double *u, double *u0, double *v, double *hy, double sumv, double *g, int n, int m ) {
/*
Compute the gradient of the discrepancy norm (norm of az-u0).
This function is different from the original one as my grid on "u0" is
uneven, and also the data points have varying weights. For mathematical
details, see my notes on the algorithm in the "latex" directory.
a[m][n] - operator's matrix
u[m] - model light curve u=(a,z)
u0[m] - right-hand side of the Fredholm's equation
v[m] - weights of the "u0" points
y[m] - the grid on u0
sumv - normalized integral of data point weights
g[n] - resulting gradient
n - size of grid on z
m - size of grid on u0
*/
int i, j;
for( j=0; j<n; j++ ) {
g[j] = 0.0;
for( i=0; i<m; i++ ) {
(g[j]) += a[i][j]*(u[i]-u0[i])*v[i]*hy[i];
}
(g[j]) *= 2.0/sumv;
}
return;
}
void pticr5( double *az, double *u, double *v, double sumv, double *hy, int m, double *s ) {
/*
Compute the discrepancy functional. This is a modified version of the
original function, taking weights (v) into account. Another difference: in
the original version, the grid in the right-hand side of the equation is
even. Thus, in this function, only the sum of (az-u)^2 was computed. Then,
in the calling routine, it is multiplied by the step size. In my case, the
grid is uneven so I have to directly compute the norm using the rectangle
approximation of the integral
int = sum (az_i-u_i)^2*v_i*hy_i/sumv
hy_i is defined as the interval from (y_i+y_(i-1))/2 to (y_i+y_(i+1))/2;
for y_0 the first point is ymin, for y_(n-1) the last point is ymax. In the
calling routine, ymin is set to cos(inclination), ymax is set by ThetaCrit.
Input parameters:
az,u[m] - input vectors (a*z and u0 in the calling routine)
v - weights of u
sumv - integral of weights divided by integration interval
hy - array of steps in y
m - dimension of a,b,v,x
s - result
*/
int i;
*s = 0.0;
for( i=0; i<m; i++ ) {
(*s) += (az[i]-u[i])*(az[i]-u[i])*v[i]*hy[i];
}
*s = *s/sumv;
return;
}
void pticr6( double *a, double *b, int n, double *s ) {
/*
Compute scalar product of vectors a and b of length n.
s - result.
*/
int i;
*s = 0.0;
for( i=0; i<n; i++ ) {
(*s) += a[i]*b[i];
}
return;
}
void pticr7( double *a, double *b, int *v, int n, double *s ) {
/*
s is the scalar product of the vectors a and b of length n with weights ( or steps, depends on context) v.
s - result.
*/
int i;
*s = 0.0;
for( i=0; i<n; i++ ) {
(*s) += a[i]*b[i]*v[i];
}
return;
}
void pticr8_simpson( double *z, double alpha, double hs, double *gr, int n, char *metric ) {
/*
Compute the gradient of the stabilizing term and add it to gr[n]
grad(||z||^2) = 2z
grad(||z'||^2 = -2z"
grad(||z"||^2 = 2z^(4)
The function is significantly reworked compared to the original version.
This is an improved version in that the gradients are computed from the
discrete approximations of the above norms, and the approximations are
using trapezidal form, not rectangular, for gradients of ||z||^2 and ||z'||^2 and
simpson's rule for grad||z"||^2 in the W22 metric.
z[n] - the point
alpha - regularization parameter.
hs - step of the grid on "z"
gr - result
n - dimension of z
metric - string denoting the metric used - "L2", "W21", "W22".
*/
int i;
double deriv4;
// double deriv2;
if( alpha <= 0.0 ) {
return;
}
if( strcmp( metric, "L2" ) == 0 ) {
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs;
}
} else if( strcmp( metric, "W21" ) == 0 ) {
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs;
if( i>0 && i<n-1 ) {
(gr[i]) += -2.0*alpha*(z[i-1]-2*z[i]+z[i+1])/hs; // hs because I have /hs^2*hs
}
if( i == 0 ) {
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
}
if( i == n-1 ) {
(gr[i]) += -2.0*alpha*(z[n-2]-z[n-1])/hs;
}
}
/*
// A bit more accurate approximation, identical to that in the W22 case below.
for( i=0; i<n; i++ ) {
if( i>=1 && i<=n-2 ) { // 2z
(gr[i]) += 2.0*alpha*z[i]*hs;
}
if( i==0 || i==n-1) {
(gr[i]) += alpha*z[i]*hs;
}
if( i>0 && i<=n-3 ) { // -2z"
(gr[i]) += -2.0*alpha*(z[i+1]-2*z[i]+z[i-1])/hs; // hs because I have /hs^2*hs
deriv2 = (z[i+1]-2*z[i]+z[i-1])/hs;
}
if( i==0 ) {
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
}
if( i==n-2 ) {
(gr[i]) += -alpha*(z[n-1]-3*z[n-2]+2*z[n-3])/hs;
}
if( i==n-1 ) {
(gr[i]) += alpha*(z[n-1]-z[n-2])/hs;
}
}
*/
} else if( strcmp( metric, "W22" ) == 0 ) {
for( i=0; i<n; i++ ) {
if( i>=1 && i<=n-2 ) { // 2z
(gr[i]) += 2.0*alpha*z[i]*hs;
}
if( i==0 || i ==n-1) {
(gr[i]) += alpha*z[i]*hs;
}
/*
if( i>0 && i<=n-3 ) { // -2z"
(gr[i]) += -2.0*alpha*(z[i+1]-2*z[i]+z[i-1])/hs; // hs because I have /hs^2*hs
deriv2 = (z[i+1]-2*z[i]+z[i-1])/hs;
}
*/
if( i==0 ) {
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
}
if( i==n-2 ) {
(gr[i]) += -alpha*(z[n-1]-3*z[n-2]+2*z[n-3])/hs;
}
if( i==n-1 ) {
(gr[i]) += alpha*(z[n-1]-z[n-2])/hs;
}
/*
// Compute the 4th derivation of z. Approximation by 5 points, 4th degree polinomial.
deriv4 = 0.0;
if( i>1 && i<n-2 ) {
deriv4 = ( z[i+2]-4*z[i+1]+6*z[i]-4*z[i-1]+z[i-2] );
}
// This approximation is from the condition that z is constant beyond the limits of the interval on s.
if( i == 0 ) {
// deriv4 = ( z[2]-4*z[1]+3*z[0] ); // from main formula
deriv4 = ( 2*z[2]-5*z[1]+3*z[0] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( z[2]-2*z[1]+z[0] ); // from derivative of discrete approx, left-interval approx.
}
if( i == 1 ) {
// deriv4 = ( z[3]-4*z[2]+6*z[1]-3*z[0] ); // from main formula
deriv4 = ( 2*z[3]-8*z[2]+11*z[1]-5*z[0] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( z[3]-4*z[2]+5*z[1]-2*z[0] ); // from derivative of discrete approx, left-interval approx.
}
if( i == n-2 ) {
// deriv4 = ( -3*z[n-1]+6*z[n-2]-4*z[n-3]+z[n-4] ); // from main formula
deriv4 = ( -5*z[n-1]+11*z[n-2]-8*z[n-3]+2*z[n-4] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( -3*z[n-1]+6*z[n-2]-4*z[n-3]+z[n-4] ); // from derivative of discrete approx, left-interval approx.
}
if( i == n-1 ) {
// deriv4 = ( 3*z[n-1]-4*z[n-2]+z[n-3] ); // from main formula
deriv4 = ( 3*z[n-1]-5*z[n-2]+2*z[n-3] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( 2*z[n-1]-3*z[n-2]+z[n-3] ); // from derivative of discrete approx, left-interval approx.
}
*/
// Compute the derivation of \int(z")^2ds. Approximation of the integral by simpson's rule.
deriv4 = 0.0;
if( i>=2 && i<=n-3 && (i%2)==0 ) { // Even indexes.
// printf( "i even = %3d\n", i );
deriv4 = 8.0/3.0*( z[i+2]-3*z[i+1]+4*z[i]-3*z[i-1]+z[i-2] );
}
if( i>=3 && i<=n-4 && (i%2) ) { // Odd indexes.
// printf( "i odd = %3d\n", i );
deriv4 = 4.0/3.0*( z[i+2]-6*z[i+1]+10*z[i]-6*z[i-1]+z[i-2] );
}
// This approximation is from the condition that z is constant beyond the limits of the interval on s.
if( i == 0 ) {
deriv4 = 2.0/3.0*( 4*z[2]-9*z[1]+5*z[0] );
}
if( i == 1 ) {
deriv4 = 2.0/3.0*( 2*z[3]-12*z[2]+19*z[1]-9*z[0] );
}
if( i == n-2 ) {
deriv4 = 2.0/3.0*( -9*z[n-1]+19*z[n-2]-12*z[n-3]+2*z[n-4] );
}
if( i == n-1 ) {
deriv4 = 2.0/3.0*( 5*z[n-1]-9*z[n-2]+4*z[n-3] );
}
deriv4 = deriv4/hs/hs/hs; // h^-3 because I have /hs^4*hs
// printf( "i= %3d d4= %17.10e d2= %17.10e\n", i, deriv4, deriv2 );
// if( deriv4 > 4000.0 ) deriv4 = 4000.0;
(gr[i]) += alpha*deriv4; // 2z^(4)
}
}
return;
}
void pticr8_old( double *z, double alpha, double hs, double *gr, int n, char *metric ) {
/*
Compute the gradient of the stabilizing term and add it to gr[n]
grad(||z||^2) = 2z
grad(||z'||^2 = -2z"
grad(||z"||^2 = 2z^(4)
The function is significantly reworked compared to the original version.
z[n] - the point
alpha - regularization parameter.
hs - step of the grid on "z"
gr - result
n - dimension of z
metric - string denoting the metric used - "L2", "W21", "W22".
*/
int i;
double deriv4;
// double deriv2;
// double xs[NTEST], zz[NTEST], z_sp[NTEST], weight[NTEST];
// int j, mbin, nsh;
if( alpha <= 0.0 ) {
return;
}
if( strcmp( metric, "L2" ) == 0 ) {
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs;
}
} else if( strcmp( metric, "W21" ) == 0 ) {
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs;
if( i>0 && i<n-1 ) {
(gr[i]) += -2.0*alpha*(z[i-1]-2*z[i]+z[i+1])/hs; // hs because I have /hs^2*hs
}
if( i == 0 ) {
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
}
if( i == n-1 ) {
(gr[i]) += -2.0*alpha*(z[n-2]-z[n-1])/hs;
}
}
} else if( strcmp( metric, "W22" ) == 0 ) {
/*
// Compute spline-approximation of z
for( i=1; i<n+1; i++ ) {
xs[i] = i*hs;
weight[i] = 1.0;
zz[i] = z[i-1];
}
spline_approx( xs, zz, weight, n+1, z_sp );
// printf( "!!!!!!!!!!\n" );
for(i=0; i<n; i++ ) {
z_sp[i] = z_sp[i+1];
}
*/
/*
// Compute moving average of z for smoothing it.
mbin = 3;
nsh = mbin/2;
for( i=0; i<n; i++ ) {
z_sp[i] = 0.0;
if( i < nsh ) {
for( j=1;j<=nsh-i; j++ ) {
z_sp[i] += z[0];
}
for( j=0; j<=i+nsh; j++ ) {
z_sp[i] += z[j];
}
} else if( i>=nsh && i<=n-1-nsh ) {
for( j=i-nsh; j<=i+nsh; j++ ) {
z_sp[i] += z[j];
}
} else {
for( j=1; j<=i+nsh-(n-1); j++ ) {
z_sp[i] +=z[n-1];
}
for( j=i-nsh; j<n; j++ ) {
z_sp[i] +=z[j];
}
}
z_sp[i] /= mbin;
}
*/
/*
// Compute moving average of z for smoothing it.
mbin = 3;
nsh = mbin/2;
for( i=0; i<n; i++ ) {
if( i < nsh ) {
z_sp[i] += z[i];
} else if( i>=nsh && i<=n-1-nsh ) {
z_sp[i] = 0.0;
for( j=i-nsh; j<=i+nsh; j++ ) {
z_sp[i] += z[j];
}
z_sp[i] /= mbin;
} else {
z_sp[i] +=z[i];
}
}
*/
/*
printf( "mbin=%d nsh=%d\n", mbin, nsh );
for( i=0; i<n; i++ ) {
printf( "%3d %17.10e %17.10e %17.10e\n", i, z[i], z_sp[i], z[i]-z_sp[i] );
}
*/
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs; // 2z
/*
if( i>0 && i<n-1 ) { // -2z"
(gr[i]) += -2.0*alpha*(z[i-1]-2*z[i]+z[i+1])/hs; // hs because I have /hs^2*hs
deriv2 = (z[i-1]-2*z[i]+z[i+1])/hs;
}
if( i == 0 ) {
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
deriv2 = (z[1]-z[0])/hs;
}
if( i == n-1 ) {
(gr[i]) += -2.0*alpha*(z[n-2]-z[n-1])/hs;
deriv2 = (z[n-2]-z[n-1])/hs;
}
*/
/*
// Compute the 4th derivation of z. Approximation by 5 points, 4th degree polinomial.
deriv4 = 0.0;
if( i>1 && i<n-2 ) {
deriv4 = ( z[i+2]-4*z[i+1]+6*z[i]-4*z[i-1]+z[i-2] );
}
// This approximation is from the condition that z is constant beyond the limits of the interval on s.
if( i == 0 ) {
// deriv4 = ( z[2]-4*z[1]+3*z[0] ); // from main formula
deriv4 = ( 2*z[2]-5*z[1]+3*z[0] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( z[2]-2*z[1]+z[0] ); // from derivative of discrete approx, left-interval approx.
}
if( i == 1 ) {
// deriv4 = ( z[3]-4*z[2]+6*z[1]-3*z[0] ); // from main formula
deriv4 = ( 2*z[3]-8*z[2]+11*z[1]-5*z[0] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( z[3]-4*z[2]+5*z[1]-2*z[0] ); // from derivative of discrete approx, left-interval approx.
}
if( i == n-2 ) {
// deriv4 = ( -3*z[n-1]+6*z[n-2]-4*z[n-3]+z[n-4] ); // from main formula
deriv4 = ( -5*z[n-1]+11*z[n-2]-8*z[n-3]+2*z[n-4] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( -3*z[n-1]+6*z[n-2]-4*z[n-3]+z[n-4] ); // from derivative of discrete approx, left-interval approx.
}
if( i == n-1 ) {
// deriv4 = ( 3*z[n-1]-4*z[n-2]+z[n-3] ); // from main formula
deriv4 = ( 3*z[n-1]-5*z[n-2]+2*z[n-3] )/2.0; // from derivative of discrete approx, symmetric approx.
// deriv4 = ( 2*z[n-1]-3*z[n-2]+z[n-3] ); // from derivative of discrete approx, left-interval approx.
}
*/
// Compute the derivation of \int(z")^2ds. Approximation of the integral by simpson's rule.
deriv4 = 0.0;
if( i>=2 && i<=n-3 && (i%2)==0 ) { // Even indexes.
deriv4 = 8.0/3.0*( z[i+2]-3*z[i+1]+4*z[i]-3*z[i-1]+z[i-2] );
}
if( i>=3 && i<=n-4 && (i%2) ) { // Odd indexes.
deriv4 = 4.0/3.0*( z[i+2]-6*z[i+1]+10*z[i]-6*z[i-1]+z[i-2] );
}
// This approximation is from the condition that z is constant beyond the limits of the interval on s.
if( i == 0 ) {
deriv4 = 2.0/3.0*( 4*z[2]-9*z[1]+5*z[0] );
}
if( i == 1 ) {
deriv4 = 2.0/3.0*( 2*z[3]-12*z[2]+19*z[1]-9*z[0] );
}
if( i == n-2 ) {
deriv4 = 2.0/3.0*( -9*z[n-1]+19*z[n-2]-12*z[n-3]+2*z[n-4] );
}
if( i == n-1 ) {
deriv4 = 2.0/3.0*( 5*z[n-1]-9*z[n-2]+4*z[n-3] );
}
deriv4 = deriv4/hs/hs/hs; // h^-3 because I have /hs^4*hs
// Compute the 4th derivation of z. Approximation by 7 points, 6th degree polinomial.
/*
if( i>2 && i<n-3 ) {
deriv4 = ( -z[i+3]+12*z[i+2]-39*z[i+1]+56*z[i]-39*z[i-1]+12*z[i-2]-z[i-3] );
}
if( i == 2 ) {
deriv4 = ( -z[5]+12*z[4]-39*z[3]+56*z[2]-39*z[1]+11*z[0] );
}
if( i == 1 ) {
deriv4 = ( -z[4]+12*z[3]-39*z[2]+56*z[1]-28*z[0] );
}
if( i == 0 ) {
deriv4 = ( -z[3]+12*z[2]-39*z[1]+28*z[0] );
}
if( i == n-3 ) {
deriv4 = ( 11*z[n-1]-39*z[n-2]+56*z[n-3]-39*z[n-4]+12*z[n-5]-z[n-6] );
}
if( i == n-2 ) {
deriv4 = ( -28*z[n-1]+56*z[n-2]-39*z[n-3]+12*z[n-4]-z[n-5] );
}
if( i == n-1 ) {
deriv4 = ( 28*z[n-1]-39*z[n-2]+12*z[n-3]-z[n-4] );
}
deriv4 = deriv4/6.0/hs/hs/hs; // h^-3 because I have /hs^4*hs
// deriv4 = (deriv41+deriv42)/2.0;
*/
// printf( "i= %3d d4= %17.10e d2= %17.10e\n", i, deriv4, deriv2 );
// if( deriv4 > 4000.0 ) deriv4 = 4000.0;
(gr[i]) += alpha*deriv4; // 2z^(4)
}
}
return;
}
void pticr8( double *z, double alpha, double hs, double *gr, int n, char *metric ) {
/*
Compute the gradient of the stabilizing term and add it to gr[n]
grad(||z||^2) = 2z*hs
grad(||z'||^2 = -2z"*hs
grad(||z"||^2 = 2z^(4)*hs
The function is significantly reworked compared to the original version.
In this version I compute the 4th derivative of z using int doubles.
floats are 4 bytes, doubles are 8, int doubles are 16 bytes.
z[n] - the point
alpha - regularization parameter.
hs - step of the grid on "z"
gr - result
n - dimension of z
metric - string denoting the metric used - "L2", "W21", "W22".
*/
int i;
double deriv4; //, deriv2;
if( alpha <= 0.0 ) {
return;
}
if( strcmp( metric, "L2" ) == 0 ) {
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs;
}
} else if( strcmp( metric, "W21" ) == 0 ) {
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs;
if( i>0 && i<n-1 ) {
(gr[i]) += -2.0*alpha*(z[i-1]-2*z[i]+z[i+1])/hs; // hs because I have /hs^2*hs
}
if( i == 0 ) {
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
}
if( i == n-1 ) {
(gr[i]) += -2.0*alpha*(z[n-2]-z[n-1])/hs;
}
}
} else if( strcmp( metric, "W22" ) == 0 ) {
// printf( " i z deriv2 deriv4\n" );
for( i=0; i<n; i++ ) {
(gr[i]) += 2.0*alpha*z[i]*hs; // 2z
if( i>0 && i<n-1 ) { // -2z"
// deriv2 = -2.0*alpha*(z[i-1]-2*z[i]+z[i+1])/hs;
(gr[i]) += -2.0*alpha*(z[i-1]-2*z[i]+z[i+1])/hs; // hs because I have /hs^2*hs
}
if( i == 0 ) {
// deriv2 = -2.0*alpha*(z[1]-z[0])/hs;
(gr[i]) += -2.0*alpha*(z[1]-z[0])/hs;
}
if( i == n-1 ) { // Comment out if discrete approx. = rectangular left.
// deriv2 = -2.0*alpha*(z[n-2]-z[n-1])/hs;
(gr[i]) += -2.0*alpha*(z[n-2]-z[n-1])/hs;
}
// Compute the 4th derivation of z. Approximation by 5 points, 4th degree
// polynomial. Actually, the formulae below are direct derivatives of the
// formula approximating ||z"||^2 by the rectangular formula.
// n assumption is used that beyound the limits of integration, z is constant.
if( i>1 && i<n-2 ) {
deriv4 = 2*( z[i+2]-4*z[i+1]+6*z[i]-4*z[i-1]+z[i-2] );
}
if( i == 0 ) {
deriv4 = 2*( z[2]-3*z[1]+2*z[0] ); // From direct differentiation of the discrete approximation of \int z"ds, constant beyond the edges.
// deriv4 = 2*( z[2]-2*z[1]+z[0] ); // From direct differentiation of the discrete approximation of \int z"ds, free edges.
// deriv4 = 2*( z[2]-4*z[1]+3*z[0] ); // From main formula, z constant beyond the edges.
// deriv4 = 2*( z[2]-2*z[1]+z[0] ); // From main formula, free ends in z.
// deriv4 = 0.0;
}
if( i == 1 ) {
deriv4 = 2*( z[3]-4*z[2]+6*z[1]-3*z[0] ); // From direct differentiation of the discrete approximation of \int z"ds, constant beyond the edges.
// deriv4 = 2*( z[3]-4*z[2]+5*z[1]-2*z[0] ); // From direct differentiation of the discrete approximation of \int z"ds, free ends.
// deriv4 = 2*( z[3]-4*z[2]+6*z[1]-3*z[0] ); // From main formula, z constant beyond the edges.
// deriv4 = 2*( z[3]-4*z[2]+5*z[1]-2*z[0] ); // From main formula, free ends in z.
// deriv4 = 0.0;
}
if( i == n-2 ) {
deriv4 = 2*( -2*z[n-1]+5*z[n-2]-4*z[n-3]+z[n-4] ); // From direct differentiation of the discrete approximation of \int z"ds, constant beyond the edges.
// deriv4 = 2*( -2*z[n-1]+5*z[n-2]-4*z[n-3]+z[n-4] ); // From direct differentiation of the discrete approximation of \int z"ds, free ends.
// deriv4 = 2*( -3*z[n-1]+6*z[n-2]-4*z[n-3]+z[n-4] ); // From main formula, z constant beyond the edges.
// deriv4 = 2*( -2*z[n-1]+5*z[n-2]-4*z[n-3]+z[n-4] ); // From main formula, free ends in z.
// deriv4 = 0.0;
}
if( i == n-1 ) {
deriv4 = 2*( z[n-1]-2*z[n-2]+z[n-3] ); // From direct differentiation of the discrete approximation of \int z"ds, constant beyond the edges.
// deriv4 = 2*( z[n-1]-2*z[n-2]+z[n-3] ); // From direct differentiation of the discrete approximation of \int z"ds, free ends.
// deriv4 = 2*( 3*z[n-1]-4*z[n-2]+z[n-3] ); // From main formula, z constant beyond the edges.
// deriv4 = 2*( z[n-1]-2*z[n-2]+z[n-3] ); // From main formula, free ends in z.
// deriv4 = 0.0;
}
(gr[i]) += alpha*deriv4/hs/hs/hs; // 2z^(4)*hs
// printf( "%3d %22.15e %22.15e %22.15e\n", i, z[i], deriv2, alpha*deriv4/hs/hs/hs );
}
}
return;
}
void pticr9( double an2, double *z, int n, double alpha,
double hs, double *as2, char *metric ) {
/*
Compute stabilizing term and add it to an2.
an2 - discrepancy ( ||az-u0||^2 )
z[n] - the point
alpha - regularization parameter
hs - step of the grid on z
as2 - result.
*/
int i;
double s;
// printf( "alpha=%e an2=%e\n", alpha, an2 );
if( alpha <= 0.0 ) {
*as2 = an2;
return;
}
s = 0.0;
if( strcmp( metric, "L2" ) == 0 ) {
for( i=0; i<n; i++ ) {
s += z[i]*z[i];
}
}
if( strcmp( metric, "W21" ) == 0 ) {
for( i=1; i<n; i++ ) {
s += z[i]*z[i]+(z[i]-z[i-1])*(z[i]-z[i-1])/hs/hs;
}
s += z[0]*z[0];
}
if( strcmp( metric, "W22" ) == 0 ) {
for( i=1; i<n-1; i++ ) {
s += z[i]*z[i] + // \int z
(z[i]-z[i-1])*(z[i]-z[i-1])/hs/hs + // \int z'
(z[i+1]-2*z[i]+z[i-1])*(z[i+1]-2*z[i]+z[i-1])/hs/hs/hs/hs; // \int z"
}
// Add terms at i=0,1,n-2,n-1. Must be consistent with approximations of
// gradients in pticr8, optimal step in ptilr5!!!
s += z[0]*z[0] + z[n-1]*z[n-1] + // \int z
(z[n-1]-z[n-2])*(z[n-1]-z[n-2])/hs/hs + // \int z', at z'(0)=0 so no term at zero index.
(z[1]-z[0])*(z[1]-z[0])/hs/hs/hs/hs ; // \int z" // If z is constant beyond the edges. For free ends, comment this line.
}
*as2 = an2+alpha*s*hs;
return;
}
void deriv4( double *y, int n, double h, double *deriv ) {
/*
Compute the 4th derivative of a tabulated function.
y - input array of dimension n containing the function,