-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.cpp
More file actions
1524 lines (1348 loc) · 46 KB
/
lib.cpp
File metadata and controls
1524 lines (1348 loc) · 46 KB
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
/*
** The library program module
** lib.cpp
**
TID time_step(int num)
** calculates start and stop time and returns the difference.
** Input data:
** int number = 1 for start - zero return values
** = 2 for stop - return time from last start
** TID run - returns the time difference.
void **matrix(int row, int col, int num_bytes)
** reserves dynamic memory for a two-dimensional matrix
** using the C++ command new . No initialization of the elements.
** Input data:
** int row - number of rows
** int col - number of columns
** int num_bytes- number of bytes for each
** element
** Returns a void **pointer to the reserved memory location.
void free_matrix(void **matr)
** releases the memory reserved by the function matrix()
** for the two-dimensional matrix[][]
** Input data:
** void **matr - pointer to the matrix
void rk4(double *y, double *dydx, int n, double x, double h, double *yout,
void (*derivs)(double, double *, double *))
** takes a set of variables y[1:n] for the function y(x) together with the
** reserves dynamic memory for a two-dimensional matrix
** using the C++ command new . No initialization of the elements.
** Input data:
** int row - number of rows
** int col - number of columns
�* int num_bytes- number of bytes for each
** element
** Returns a void **pointer to the reserved memory location.
void free_matrix(void **matr)
** releases the memory reserved by the function matrix() for t
** derivatives dydx[1:n] and uses the fourth-order Runge-Kutta method to
** advance the solution over an interval h and return incremented variables
** as yout[1:n], which not need to be a disstinct arra from y[1:n]. The
** users supply the routine derivs(x,y,dydx), which returns the derivatives
** dydx at x.
void ludcmp(double **a, int n, int *indx, double *d)
** takes as input a two-dimensional matrix a[][] of dimension n and
** replaces it by the LU decomposition of a rowwise permutation of
** itself. The results is stored in a[][] in the form given by
** eq. (2.3.14) in "Numerical Recipe", sect. 2.3, page 45. The vector
** indx[] records the row permutation effected by the partial pivoting;
** d is output as +1 or -1 depending on whether the number of row
** interchanges was even or odd, respectively. This routine is used in
** combination with the function lubksb() to solve linear equations or
** invert a matrix. The function is slightly modified from the version
** in in Numerical recipe and uses memory allocation functions in the
** present module.
void lubksb(double **a, int n, int *indx, double *b)
** solves the set of linear equations A X = B of dimension n.
** a[][] is input, not as the matrix A[][] but rather as
** its LU decomposition, determined by the function ludcmp(),
** indx[] is input as the permutation vector returned by
** ludcmp(). b[] is input as the right-hand side vector B,
** The solution X is returned in B. The input data a[][],
** n and indx[] are not modified. This routine take into
** account the possibility that b[] will begin with many
** zero elements, so it is efficient for use in matrix
** inversion.
** The function is slightly modified from the version in
** in Numerical recipe.
void tqli(double d[], double e[], int n, double **z)
** determine eigenvalues and eigenvectors of a real symmetric
** tri-diagonal matrix, or a real, symmetric matrix previously
** reduced by function tred2[] to tri-diagonal form. On input,
** d[] contains the diagonal element and e[] the sub-diagonal
** of the tri-diagonal matrix. On output d[] contains the
** eigenvalues and e[] is destroyed. If eigenvectors are
** desired z[][] on input contains the identity matrix. If
** eigenvectors of a matrix reduced by tred2() are required,
** then z[][] on input is the matrix output from tred2().
** On output, the k'th column returns the normalized eigenvector
** corresponding to d[k].
** The function is modified from the version in Numerical recipe.
void tred2(double **a, int n, double d[], double e[])
** perform a Housholder reduction of a real symmetric matrix
** a[][]. On output a[][] is replaced by the orthogonal matrix
** effecting the transformation. d[] returns the diagonal elements
** of the tri-diagonal matrix, and e[] the off-diagonal elements,
** with e[0] = 0.
** The function is modified from the version in Numerical recipe.
double pythag(double a, double b)
** The function is modified from the version in Numerical recipe.
void gauleg(double x1, double x2, double x[], double w[], int n)
** takes the lower and upper limits of integration x1, x2, calculates
** and return the abcissas in x[0,...,n - 1] and the weights in w[0,...,n - 1]
** of length n of the Gauss--Legendre n--point quadrature formulae.
void jacobi(double** a, double* d, double** v, int n, int& nrot)
** Computes the eigenvalues and eigenvectors of the square symmetric matrix
** a by use of the Jacobi method.
** It puts the eigenvalues in d and eigenvectors in v.
** n is an integer denoting the size of a
**nrot keeps track of the number of rotations
** The function is as in the Numerical recipe
void jacobi_rot(double** a, double s, double tau, int i, int j, int k, int l)
** A helping function for jacobi making the actual rotations
** a is the matrix to be rotated, s is sine of the rotation
** angle, tau is s/(1 + c) where c is cosine of the angle.
** The integers i-l denotes the matrix element to be
** rotated.
double rectangle_rule(double a, double b, int n, double (*func)(double))
** integration by rectangle rule, in a and b and number of points n and name
** of function
double trapezoidal_rule(double a, double b, int n, double (*func)(double))
** integration by trapezoidal rule, in a and b and number of points n and name
** of function
void spline(double x[], double y[], int n, double yp1, double yp2, double y2[])
** takes as input x[0,..,n - 1] and y[0,..,n - 1] containing a tabulation
** y_i = f(x_i) with x_0 < x_1 < .. < x_(n - 1) together with yp_1 and yp2
** for first derivatives f(x) at x_0 and x_(n-1), respectively. Then the
** function returns y2[0,..,n-1] which contanin the second derivatives of
** f(x_i)at each point x_i. If yp1 and/or yp2 is larger than the constant
** INFINITY the function will put corresponding second derivatives to zero.
void splint(double xa[], double ya[], double y2a[], int n, double x, double *y)
** takes xa[0,..,n - 1] and y[0,..,n - 1] which tabulates a function
** (with the xa[i]'s in order) and given ya[0,..,n - 1], which is the
** output from function spline() and with given value of x returns a
** cubic--spline interpolation value y.
void polint(double xa[], double ya[], int n, double x, double *y, double *dy)
** takes as input xa[0,..,n-1] and ya[0,..,n-1] together with a given value
** of x and returns a value y and an error estimate dy. If P(x) is a polynomial
** of degree N - 1 such that P(xa_i) = ya_i, i = 0,..,n-1, then the returned
** value is y = P(x).
double rtbis(double (*func)(double), double x1, double x2, double xacc)
** calculates a root between x1 and x2 of a function
** pointed to by (*func) using the method of bisection
** The root is returned with an accuracy of +- xacc.
double rtsec(double (*func)(double), double x1, double x2, double xacc)
** calculates a root between x1 and x2 of a function
** pointed to by (*func) using the secant method.
** The root is returned with an accuracy of +- xacc.
double rtnewt(void (*funcd)(double, double *, double *), double x1, double x2, double xacc)
** calculates a root between x1 and x2 of a function pointed to
** by (*funcd) using the Newton-Raphson method. The user-defined
** function funcd() returns both the function value and its first
** derivative at the point x,
** The root is returned with an accuracy of +- xacc.
double zbrent(double (*func)(double), double x1, double x2, double xacc)
** calculates a root between x1 and x2 of a function
** pointed to by (*funcd) using the Brent's method.
** The root is returned with an accuracy of +- xacc.
double ran0(long *idum)
** is an "Minimal" random number generator of Park and Miller
** (see Numerical recipe page 279). Set or reset the input value
** idum to any integer value (except the unlikely value MASK)
** to initialize the sequence; idum must not be altered between
** calls for sucessive deviates in a sequence.
** The function returns a uniform deviate between 0.0 and 1.0.
double ran1(long *idum)
** is an "Minimal" random number generator of Park and Miller
** (see Numerical recipe page 280) with Bays-Durham shuffle and
** added safeguards. Call with idum a negative integer to initialize;
** thereafter, do not alter idum between sucessive deviates in a
** sequence. RNMX should approximate the largest floating point value
** that is less than 1.
** The function returns a uniform deviate between 0.0 and 1.0
** (exclusive of end-point values).
double ran2(long *idum)
** is a long periode (> 2 x 10^18) random number generator of
** L'Ecuyer and Bays-Durham shuffle and added safeguards.
** Call with idum a negative integer to initialize; thereafter,
** do not alter idum between sucessive deviates in a
** sequence. RNMX should approximate the largest floating point value
** that is less than 1.
** The function returns a uniform deviate between 0.0 and 1.0
** (exclusive of end-point values).
double ran3(long *idum)
** returns a uniform random number deviate between 0.0 and 1.0. Set
** the idum to any negative value to initialize or reinitialize the
** sequence. Any large MBIG, and any small (but still large) MSEED
** can be substituted for the present values.
*/
#include "lib.h"
/*
** The function
** TID time_step(..)
** calculates start and stop time and returns the difference.
** Input data:
** int number = 1 for start - zero return values
** = 2 for stop - return time from last start
** TID run - returns the time difference.
*/
TID time_step(int num)
{
unsigned long long int
num_sec;
static long
zsec = 0, zusec = 0;
double
retval;
TID
ex_time;
struct timeval
tp;
if(num == 1) { // initialization of time
zsec = tp.tv_sec;
zusec = tp.tv_usec;
ex_time.sec = 0;
ex_time.min = 0;
ex_time.hour = 0;
}
else if(num == 2) {
retval = (double)(tp.tv_sec - zsec) + (tp.tv_usec - zusec) * 0.000001;
num_sec = (unsigned long long int)retval;
ex_time.sec = num_sec % 60;
ex_time.min = num_sec / 60;
ex_time.hour = ex_time.min/ 60;
ex_time.min = ex_time.min % 60;
}
else {
printf("\n\nError in function time_step(): ");
printf("\nInput data num = %d is wrong !!\n\n", num);
exit(1);
}
return ex_time;
} // End: function time_step()
/*
* The function
* void **matrix()
* reserves dynamic memory for a two-dimensional matrix
* using the C++ command new . No initialization of the elements.
* Input data:
* int row - number of rows
* int col - number of columns
* int num_bytes- number of bytes for each
* element
* Returns a void **pointer to the reserved memory location.
*/
void **matrix(int row, int col, int num_bytes)
{
int i, num;
char **pointer, *ptr;
pointer = new(nothrow) char* [row];
if(!pointer) {
cout << "Exception handling: Memory allocation failed";
cout << " for "<< row << "row addresses !" << endl;
return NULL;
}
i = (row * col * num_bytes)/sizeof(char);
pointer[0] = new(nothrow) char [i];
if(!pointer[0]) {
cout << "Exception handling: Memory allocation failed";
cout << " for address to " << i << " characters !" << endl;
return NULL;
}
ptr = pointer[0];
num = col * num_bytes;
for(i = 0; i < row; i++, ptr += num ) {
pointer[i] = ptr;
}
return (void **)pointer;
} // end: function void **matrix()
/*
* The function
* void free_matrix()
* releases the memory reserved by the function matrix()
*for the two-dimensional matrix[][]
* Input data:
* void far **matr - pointer to the matrix
*/
void free_matrix(void **matr)
{
delete [] (char *) matr[0];
delete [] matr;
} // End: function free_matrix()
/*
** The function
** rk4()
** takes a set of variables y[1:n] for the function y(x) together with the
** derivatives dydx[1:n] and uses the fourth-order Runge-Kutta method to
** advance the solution over an interval h and return incremented variables
** as yout[1:n], which not need to be a disstinct arra from y[1:n]. The
** users supply the routine derivs(x,y,dydx), which returns the derivatives
** dydx at x.
*/
void rk4(double *y, double *dydx, int n, double x, double h, double *yout,
void (*derivs)(double, double *, double *))
{
int i;
double xh,hh,h6,*dym,*dyt,*yt;
// local memory allocation
dym = new(nothrow) double [n];
if(!dym) {
printf("\n\nError in function rk4():");
printf("\nNot enough memory for dym[%d]\n",n);
exit(1);
}
dyt = new(nothrow) double [n];
if(!dyt) {
printf("\n\nError in function rk4():");
printf("\nNot enough memory for dyt[%d]\n",n);
exit(1);
}
yt = new(nothrow) double [n];
if(!yt) {
printf("\n\nError in function rk4():");
printf("\nNot enough memory for yt[%d]\n",n);
exit(1);
}
hh = h * 0.5;
h6 = h/6.0;
xh = x+hh;
for(i = 0; i < n; i++) { // first step
yt[i] = y[i] + hh * dydx[i];
}
(*derivs)(xh, yt, dyt); // second step
for(i = 1; i < n; i++) {
yt[i] = y[i] + hh * dyt[i];
}
(*derivs)(xh, yt, dym); // third step
for(i = 1; i < n; i++) {
yt[i] = y[i] + h * dym[i];
dym[i] += dyt[i];
}
(*derivs)(x+h,yt,dyt); // fourth step
// acummulate increments with proper weights
for(i = 0; i< n; i++) {
yout[i] = y[i] + h6 *(dydx[i] + dyt[i] + 2.0 * dym[i]);
}
delete [] yt; // release local memory
delete [] dyt;
delete [] dym;
} // End: function rk4()
/*
** The function
** ludcmp()
** takes as input a two-dimensional matrix a[][] of dimension n and
** replaces it by the LU decomposition of a rowwise permutation of
** itself. The results is stored in a[][] in the form given by
** eq. (2.3.14) in "Numerical Recipe", sect. 2.3, page 45. The vector
** indx[] records the row permutation effected by the partial pivoting;
** d is output as +1 or -1 depending on whether the number of row
** interchanges was even or odd, respectively. This routine is used in
** combination with the function lubksb() to solve linear equations or
** invert a matrix. The function is slightly modified from the version
** in in Numerical recipe and uses memory allocation functions in the
** present module.
*/
void ludcmp(double **a, int n, int *indx, double *d)
{
int i, imax, j, k;
double big, dum, sum, temp, *vv;
vv = new(nothrow) double [n];
if(!vv) {
printf("\n\nError in function ludcm():");
printf("\nNot enough memory for vv[%d]\n",n);
exit(1);
}
*d = 1.0; // no row interchange yet
for(i = 0; i < n; i++) { // loop over rows to get scaling information
big = ZERO;
for(j = 0; j < n; j++) {
if((temp = fabs(a[i][j])) > big) big = temp;
}
if(big == ZERO) {
printf("\n\nSingular matrix in routine ludcmp()\n");
//exit(1);
}
vv[i] = 1.0/big; // save scaling */
} // end i-loop */
for(j = 0; j < n; j++) { // loop over columns of Crout's method
for(i = 0; i< j; i++) { // not i = j
sum = a[i][j];
for(k = 0; k < i; k++) sum -= a[i][k]*a[k][j];
a[i][j] = sum;
}
big = ZERO; // initialization for search for largest pivot element
for(i = j; i< n; i++) {
sum = a[i][j];
for(k = 0; k < j; k++) sum -= a[i][k]*a[k][j];
a[i][j] = sum;
if((dum = vv[i]*fabs(sum)) >= big) {
big = dum;
imax = i;
}
} // end i-loop
if(j != imax) { // do we need to interchange rows ?
for(k = 0;k< n; k++) { // yes
dum = a[imax][k];
a[imax][k] = a[j][k];
a[j][k] = dum;
}
(*d) *= -1; // and change the parit of d
vv[imax] = vv[j]; // also interchange scaling factor
}
indx[j] = imax;
if(fabs(a[j][j]) < ZERO) a[j][j] = ZERO;
/*
** if the pivot element is zero the matrix is singular
** (at least to the precision of the algorithm). For
** some application of singular matrices, it is desirable
** to substitute ZERO for zero,
*/
if(j < (n - 1)) { // divide by pivot element
dum = 1.0/a[j][j];
for(i=j+1;i < n; i++) a[i][j] *= dum;
}
} // end j-loop over columns
delete [] vv; // release local memory
} // End: function ludcmp()
/*
** The function
** lubksb()
** solves the set of linear equations A X = B of dimension n.
** a[][] is input, not as the matrix A[][] but rather as
** its LU decomposition, determined by the function ludcmp(),
** indx[] is input as the permutation vector returned by
** ludcmp(). b[] is input as the right-hand side vector B,
** The solution X is returned in B. The input data a[][],
** n and indx[] are not modified. This routine take into
** account the possibility that b[] will begin with many
** zero elements, so it is efficient for use in matrix
** inversion.
** The function is slightly modified from the version in
** in Numerical recipe.
*/
void lubksb(double **a, int n, int *indx, double *b)
{
int i, ii = -1, ip, j;
double sum;
for(i = 0; i< n; i++) {
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if(ii > -1) for(j = ii; j < i; j++) sum -= a[i][j] * b[j];
else if(sum) ii = i;
b[i] = sum;
}
for(i = n - 1; i >= 0; i--) {
sum = b[i];
for(j = i+1; j < n; j++) sum -= a[i][j] * b[j];
b[i] = sum/a[i][i];
}
} // End: function lubksb()
/*
** The function
** tqli()
** determine eigenvalues and eigenvectors of a real symmetric
** tri-diagonal matrix, or a real, symmetric matrix previously
** reduced by function tred2[] to tri-diagonal form. On input,
** d[] contains the diagonal element and e[] the sub-diagonal
** of the tri-diagonal matrix. On output d[] contains the
** eigenvalues and e[] is destroyed. If eigenvectors are
** desired z[][] on input contains the identity matrix. If
** eigenvectors of a matrix reduced by tred2() are required,
** then z[][] on input is the matrix output from tred2().
** On output, the k'th column returns the normalized eigenvector
** corresponding to d[k].
** The function is modified from the version in Numerical recipe.
*/
void tqli(double *d, double *e, int n, double **z)
{
register int m,l,iter,i,k;
double s,r,p,g,f,dd,c,b;
for(i = 1; i < n; i++) e[i-1] = e[i];
e[n] = 0.0;
for(l = 0; l < n; l++) {
iter = 0;
do {
for(m = l; m < n-1; m++) {
dd = fabs(d[m]) + fabs(d[m+1]);
if((double)(fabs(e[m])+dd) == dd) break;
}
if(m != l) {
if(iter++ == 30) {
printf("\n\nToo many iterations in tqli.\n");
exit(1);
}
g = (d[l+1] - d[l])/(2.0 * e[l]);
r = pythag(g,1.0);
g = d[m]-d[l]+e[l]/(g+SIGN(r,g));
s = c = 1.0;
p = 0.0;
for(i = m-1; i >= l; i--) {
f = s * e[i];
b = c*e[i];
e[i+1] = (r=pythag(f,g));
if(r == 0.0) {
d[i+1] -= p;
e[m] = 0.0;
break;
}
s = f/r;
c = g/r;
g = d[i+1] - p;
r = (d[i] - g) * s + 2.0 * c * b;
d[i+1] = g + (p = s * r);
g = c * r - b;
for(k = 0; k < n; k++) {
f = z[k][i+1];
z[k][i+1] = s * z[k][i] + c * f;
z[k][i] = c * z[k][i] - s * f;
} /* end k-loop */
} /* end i-loop */
if(r == 0.0 && i >= l) continue;
d[l] -= p;
e[l] = g;
e[m] = 0.0;
} /* end if-loop for m != 1 */
} while(m != l);
} /* end l-loop */
} /* End: function tqli(), (C) Copr. 1986-92 Numerical Recipes Software )%. */
/*
** The function
** tred2()
** perform a Housholder reduction of a real symmetric matrix
** a[][]. On output a[][] is replaced by the orthogonal matrix
** effecting the transformation. d[] returns the diagonal elements
** of the tri-diagonal matrix, and e[] the off-diagonal elements,
** with e[0] = 0.
** The function is modified from the version in Numerical recipe.
*/
void tred2(double **a, int n, double *d, double *e)
{
register int l,k,j,i;
double scale,hh,h,g,f;
for(i = n - 1; i > 0; i--) {
l = i-1;
h = scale= 0.0;
if(l > 0) {
for(k = 0; k <= l; k++)
scale += fabs(a[i][k]);
if(scale == 0.0) // skip transformation
e[i] = a[i][l];
else {
for(k = 0; k <= l; k++) {
a[i][k] /= scale; // used scaled a's for transformation
h += a[i][k]*a[i][k];
}
f = a[i][l];
g = (f >= 0.0 ? -sqrt(h) : sqrt(h));
e[i] = scale*g;
h -= f * g;
a[i][l] = f - g;
f = 0.0;
for(j = 0;j <= l;j++) {
a[j][i] = a[i][j]/h; // can be omitted if eigenvector not wanted
g = 0.0;
for(k = 0; k <= j; k++) {
g += a[j][k]*a[i][k];
}
for(k = j+1; k <= l; k++)
g += a[k][j]*a[i][k];
e[j]=g/h;
f += e[j]*a[i][j];
}
hh=f/(h+h);
for(j = 0; j <= l;j++) {
f = a[i][j];
e[j]=g=e[j]-hh*f;
for(k = 0; k <= j; k++)
a[j][k] -= (f*e[k]+g*a[i][k]);
}
}
} // end if-loop for l > 1
else {
e[i]=a[i][l];
}
d[i]=h;
} // end i-loop
d[0] = 0.0;
e[0] = 0.0;
/* Contents of this loop can be omitted if eigenvectors not
** wanted except for statement d[i]=a[i][i];
*/
for(i = 0; i < n; i++) {
l = i-1;
if(d[i]) {
for(j = 0; j <= l; j++) {
g= 0.0;
for(k = 0; k <= l; k++) {
g += a[i][k] * a[k][j];
}
for (k = 0; k <= l; k++) {
a[k][j] -= g * a[k][i];
}
}
}
d[i] = a[i][i];
a[i][i] = 1.0;
for(j = 0; j <= l; j++) {
a[j][i]=a[i][j] = 0.0;
}
}
} // End: function tred2(), (C) Copr. 1986-92 Numerical Recipes Software )
double pythag(double a, double b)
{
double absa,absb;
absa=fabs(a);
absb=fabs(b);
if (absa > absb) return absa*sqrt(1.0+SQR(absb/absa));
else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+SQR(absa/absb)));
}
// End: function pythag(), (C) Copr. 1986-92 Numerical Recipes Software )%.
/*
** The function
** gauleg()
** takes the lower and upper limits of integration x1, x2, calculates
** and return the abcissas in x[0,...,n - 1] and the weights in w[0,...,n - 1]
** of length n of the Gauss--Legendre n--point quadrature formulae.
*/
void gauleg(double x1, double x2, double x[], double w[], int n)
{
int m,j,i;
double z1,z,xm,xl,pp,p3,p2,p1;
double const pi = 3.14159265359;
double *x_low, *x_high, *w_low, *w_high;
m = (n + 1)/2; // roots are symmetric in the interval
xm = 0.5 * (x2 + x1);
xl = 0.5 * (x2 - x1);
x_low = x; // pointer initialization
x_high = x + n - 1;
w_low = w;
w_high = w + n - 1;
for(i = 1; i <= m; i++) { // loops over desired roots
z = cos(pi * (i - 0.25)/(n + 0.5));
/*
** Starting with the above approximation to the ith root
** we enter the mani loop of refinement bt Newtons method.
*/
do {
p1 =1.0;
p2 =0.0;
/*
** loop up recurrence relation to get the
** Legendre polynomial evaluated at x
*/
for(j = 1; j <= n; j++) {
p3 = p2;
p2 = p1;
p1 = ((2.0 * j - 1.0) * z * p2 - (j - 1.0) * p3)/j;
}
/*
** p1 is now the desired Legrendre polynomial. Next compute
** ppp its derivative by standard relation involving also p2,
** polynomial of one lower order.
*/
pp = n * (z * p1 - p2)/(z * z - 1.0);
z1 = z;
z = z1 - p1/pp; // Newton's method
} while(fabs(z - z1) > ZERO);
/*
** Scale the root to the desired interval and put in its symmetric
** counterpart. Compute the weight and its symmetric counterpart
*/
*(x_low++) = xm - xl * z;
*(x_high--) = xm + xl * z;
*w_low = 2.0 * xl/((1.0 - z * z) * pp * pp);
*(w_high--) = *(w_low++);
}
} // End_ function gauleg()
/*
** The function
** jacobi_rot()
** A helping function for jacobi making the actual rotations
** a is the matrix to be rotated, s is sine of the rotation
** angle, tau is s/(1 + c) where c is cosine of the angle.
** The integers i-l denotes the matrix element to be
** rotated.
**
*/
inline void jacobi_rot(double** a, double s, double tau, int i, int j, int k, int l){
double g,h;
g = a[i][j];
h = a[k][l];
a[i][j] = g - s * (h + g*tau);
a[k][l] = h + s * (g - h*tau);
}//End function jacobi_rot
/*
** The function
** jacobi()
** Computes the eigenvalues and eigenvectors of the square symmetric matrix
** A by use of the Jacobi method.
** It puts the eigenvalues in d and eigenvectors in v.
** n is an integer denoting the size of A
** The function is as in the Numerical recipe
*/
void jacobi(double** a, double* d, double** v, int n, int &nrot){
int i,j, ip, iq;
double tresh, theta, tau, t, sm, s, h, g, c;
double* b = new double[n];
double* z = new double[n];
for(ip = 0; ip < n; ip++){
for(iq = 0; iq < n; iq++){
v[ip][iq] = 0.0; //Initializing v to the identity matrix
v[ip][ip] = 1.0;
}
}
for(ip = 0; ip <n; ip++){
b[ip] = d[ip] = a[ip][ip]; //Initializing d and b to the diagonal of a
z[ip] = 0.0; //z will accumulate terms of the form
//t*a[ip][iq]
}
nrot = 0;
for(i = 1; i <= 50; i++){
sm = 0.0;
for(ip = 0; ip < n - 1; ip++){
for(iq = ip + 1; iq < n; iq++){
sm += fabs(a[ip][iq]); //Sum magnitude of off-diagonal elements
}
}
if(sm == 0.0){
return; //The normal return at convergence
}
if(i < 4){
tresh = 0.2 * sm/(n*n); //On the first four sweeps
}else{
tresh = 0.0; //... thereafter
}
for(ip = 0; ip < n-1; ip++){
for(iq = ip + 1; iq < n; iq++){
g = 100.0*fabs(a[ip][iq]);
//After four sweeps we skip the rotation if the off-diagonal element is small
if(i >4 && (fabs(d[ip]) + g) == fabs(d[ip])
&& (fabs(d[iq]) + g) == fabs(d[iq])){
a[ip][iq] = 0.0;
}else if(fabs(a[ip][iq]) > tresh){
h = d[iq] - d[ip];
if((fabs(h) + g) == fabs(h)){
t = (a[ip][iq])/h;
}else{
theta = 0.5*h/(a[ip][iq]);
t = 1.0/(fabs(theta) + sqrt(1.0 + theta*theta));
if(theta < 0.0){
t = -t;
}
}
c = 1.0/sqrt(1 + t*t);
s = t*c;
tau = s/(1.0 + c);
h = t*a[ip][iq];
z[ip] -= h;
z[iq] += h;
d[ip] -= h;
d[iq] += h;
a[ip][iq] = 0.0;
for(j = 0; j < ip; j++){
jacobi_rot(a, s, tau, j, ip, j, iq); // Rotations for 0 <= j < ip
}
for(j = ip + 1; j < iq; j++){
jacobi_rot(a, s, tau, ip, j, j, iq); // Rotations for ip < j < iq
}
for(j = iq + 1; j < n; j++){
jacobi_rot(a, s, tau, ip, j, iq, j); // Rotations for q < j < n
}
for(j = 0; j < n; j++){
jacobi_rot(v, s, tau, j, ip, j, iq); //Updating v
}
nrot++;
}
}
}
for(ip = 0; ip < n; ip ++){
b[ip] += z[ip];
d[ip] = b[ip];
z[ip] = 0.0;
}
}
printf("\n\nToo many iterations in routine jacobi.\n");
exit(1);
}//End function jacobi()
/*
** The function
** spline()
** takes as input x[0,..,n - 1] and y[0,..,n - 1] containing a tabulation
** y_i = f(x_i) with x_0 < x_1 < .. < x_(n - 1) together with yp_1 and yp2
** for first derivatives f(x) at x_0 and x_(n-1), respectively. Then the
** function returns y2[0,..,n-1] which contanin the second derivatives of
** f(x_i)at each point x_i. If yp1 and/or yp2 is larger than the constant
** INFINITY the function will put corresponding second derivatives to zero.
*/
void spline(double x[], double y[], int n, double yp1, double yp2, double y2[])
{
int i,k;
double p,qn,sig,un,*u;
u = new(nothrow) double [n];
if(!u) {
printf("\n\nError in function spline():");
printf("\nNot enough memory for u[%d]\n",n);
exit(1);
}
if(yp1 > INFINITY) y2[0] = u[0] = 0.0;
else {
y2[0] = -0.5;
u[0] = (3.0/(x[1] - x[0])) * ((y[1] - y[0])/(x[1] - x[0]) - yp1);
}
for(i = 1; i < (n - 1); i++) {
sig = (x[i] - x[i - 1])/(x[i + 1] - x[i - 1]);
p = sig * y2[i - 1] + 2.0;
y2[i] = (sig - 1.0)/p;
u[i] = (y[i + 1] - y[i])/(x[i + 1] - x[i]) - (y[i] - y[i - 1])/(x[i] - x[i - 1]);
u[i] = (6.0 * u[i]/(x[i + 1] - x[i - 1]) - sig*u[i - 1])/p;
}
if(yp2 > INFINITY) qn = un = ZERO;
else {
qn = 0.5;
un = (3.0/(x[n - 1] - x[n - 2])) * (yp2 - (y[n - 1] - y[n - 2])/(x[n - 1] - x[n - 2]));
}
y2[n - 1] = (un - qn * u[n - 2])/(qn * y2[n - 2] + 1.0);
for(k = n - 2; k >= 0; k--) {
y2[k] = y2[k]*y2[k+1]+u[k];
}
delete [] u; ;
} // End: function spline()
/*
** The function
** splint()
** takes xa[0,..,n - 1] and y[0,..,n - 1] which tabulates a function
** (with the xa[i]'s in order) and given ya[0,..,n - 1], which is the
** output from function spline() and with given value of x returns a
** cubic--spline interpolation value y.
*/
void splint(double xa[], double ya[], double y2a[], int n, double x, double *y)
{
int klo,khi,k;
double h,b,a;
klo = 0;
khi = n - 1;
while((khi - klo) > 1) { // binary search
k = (khi + klo) >> 1;
if(xa[k] > x) khi = k;
else klo = k;
}
h = xa[khi] - xa[klo];
if(fabs(h) < ZERO) {
printf("\n\n Error in function splint(): ");
printf("\n The difference h = %4.1E -- too small\n",h);
exit(1);
}
a = (xa[khi] - x)/h;
b = (x - xa[klo])/h;
*y = a * ya[klo] + b * ya[khi] + ((a * a * a - a) * y2a[klo]
+ (b * b * b - b) * y2a[khi]) * (h * h)/6.0;
} // End: function splint()
/*
** The function
** polint()
** takes as input xa[0,..,n-1] and ya[0,..,n-1] together with a given value
** of x and returns a value y and an error estimate dy. If P(x) is a polynomial
** of degree N - 1 such that P(xa_i) = ya_i, i = 0,..,n-1, then the returned
** value is y = P(x).
*/
void polint(double xa[], double ya[], int n, double x, double *y, double *dy)
{
int i, m, ns = 1;
double den,dif,dift,ho,hp,w;
double *c,*d;
dif = fabs(x - xa[0]);
c = new(nothrow) double [n];
if(!c) {
printf("\n\nError in function polint():");
printf("\nNot enough memory for c[%d]\n",n);
exit(1);
}
d = new(nothrow) double [n];
if(!d) {
printf("\n\nError in function polint():");
printf("\nNot enough memory for d[%d]\n",n);
exit(1);
}
for(i = 0; i < n; i++) {
if((dift = fabs(x - xa[i])) < dif) {
ns = i;
dif = dift;
}
c[i] = ya[i];
d[i] = ya[i];