forked from thomaskf/Hetero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjacobi_eigenvalue.cpp
782 lines (585 loc) · 14.6 KB
/
jacobi_eigenvalue.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
# include <stdlib.h>
# include <stdio.h>
# include <math.h>
# include <time.h>
# include <string.h>
# include "jacobi_eigenvalue.h"
/******************************************************************************/
void jacobi_eigenvalue ( int n, double a[], int it_max, double v[],
double d[], int *it_num, int *rot_num )
/******************************************************************************/
/*
Purpose:
JACOBI_EIGENVALUE carries out the Jacobi eigenvalue iteration.
Discussion:
This function computes the eigenvalues and eigenvectors of a
real symmetric matrix, using Rutishauser's modfications of the classical
Jacobi rotation method with threshold pivoting.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
17 September 2013
Author:
C version by John Burkardt
Parameters:
Input, int N, the order of the matrix.
Input, double A[N*N], the matrix, which must be square, real,
and symmetric.
Input, int IT_MAX, the maximum number of iterations.
Output, double V[N*N], the matrix of eigenvectors.
Output, double D[N], the eigenvalues, in descending order.
Output, int *IT_NUM, the total number of iterations.
Output, int *ROT_NUM, the total number of rotations.
*/
{
double *bw;
double c;
double g;
double gapq;
double h;
int i;
int j;
int k;
int l;
int m;
int p;
int q;
double s;
double t;
double tau;
double term;
double termp;
double termq;
double theta;
double thresh;
double w;
double *zw;
r8mat_identity ( n, v );
r8mat_diag_get_vector ( n, a, d );
bw = ( double * ) malloc ( n * sizeof ( double ) );
zw = ( double * ) malloc ( n * sizeof ( double ) );
for ( i = 0; i < n; i++ )
{
bw[i] = d[i];
zw[i] = 0.0;
}
*it_num = 0;
*rot_num = 0;
while ( *it_num < it_max )
{
*it_num = *it_num + 1;
/*
The convergence threshold is based on the size of the elements in
the strict upper triangle of the matrix.
*/
thresh = 0.0;
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < j; i++ )
{
thresh = thresh + a[i+j*n] * a[i+j*n];
}
}
thresh = sqrt ( thresh ) / ( double ) ( 4 * n );
if ( thresh == 0.0 )
{
break;
}
for ( p = 0; p < n; p++ )
{
for ( q = p + 1; q < n; q++ )
{
gapq = 10.0 * fabs ( a[p+q*n] );
termp = gapq + fabs ( d[p] );
termq = gapq + fabs ( d[q] );
/*
Annihilate tiny offdiagonal elements.
*/
if ( 4 < *it_num &&
termp == fabs ( d[p] ) &&
termq == fabs ( d[q] ) )
{
a[p+q*n] = 0.0;
}
/*
Otherwise, apply a rotation.
*/
else if ( thresh <= fabs ( a[p+q*n] ) )
{
h = d[q] - d[p];
term = fabs ( h ) + gapq;
if ( term == fabs ( h ) )
{
t = a[p+q*n] / h;
}
else
{
theta = 0.5 * h / a[p+q*n];
t = 1.0 / ( fabs ( theta ) + sqrt ( 1.0 + theta * theta ) );
if ( theta < 0.0 )
{
t = - t;
}
}
c = 1.0 / sqrt ( 1.0 + t * t );
s = t * c;
tau = s / ( 1.0 + c );
h = t * a[p+q*n];
/*
Accumulate corrections to diagonal elements.
*/
zw[p] = zw[p] - h;
zw[q] = zw[q] + h;
d[p] = d[p] - h;
d[q] = d[q] + h;
a[p+q*n] = 0.0;
/*
Rotate, using information from the upper triangle of A only.
*/
for ( j = 0; j < p; j++ )
{
g = a[j+p*n];
h = a[j+q*n];
a[j+p*n] = g - s * ( h + g * tau );
a[j+q*n] = h + s * ( g - h * tau );
}
for ( j = p + 1; j < q; j++ )
{
g = a[p+j*n];
h = a[j+q*n];
a[p+j*n] = g - s * ( h + g * tau );
a[j+q*n] = h + s * ( g - h * tau );
}
for ( j = q + 1; j < n; j++ )
{
g = a[p+j*n];
h = a[q+j*n];
a[p+j*n] = g - s * ( h + g * tau );
a[q+j*n] = h + s * ( g - h * tau );
}
/*
Accumulate information in the eigenvector matrix.
*/
for ( j = 0; j < n; j++ )
{
g = v[j+p*n];
h = v[j+q*n];
v[j+p*n] = g - s * ( h + g * tau );
v[j+q*n] = h + s * ( g - h * tau );
}
*rot_num = *rot_num + 1;
}
}
}
for ( i = 0; i < n; i++ )
{
bw[i] = bw[i] + zw[i];
d[i] = bw[i];
zw[i] = 0.0;
}
}
/*
Restore upper triangle of input matrix.
*/
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < j; i++ )
{
a[i+j*n] = a[j+i*n];
}
}
/*
Ascending sort the eigenvalues and eigenvectors.
*/
for ( k = 0; k < n - 1; k++ )
{
m = k;
for ( l = k + 1; l < n; l++ )
{
if ( d[l] < d[m] )
{
m = l;
}
}
if ( m != k )
{
t = d[m];
d[m] = d[k];
d[k] = t;
for ( i = 0; i < n; i++ )
{
w = v[i+m*n];
v[i+m*n] = v[i+k*n];
v[i+k*n] = w;
}
}
}
free ( bw );
free ( zw );
return;
}
/******************************************************************************/
void r8mat_diag_get_vector ( int n, double a[], double v[] )
/******************************************************************************/
/*
Purpose:
R8MAT_DIAG_GET_VECTOR gets the value of the diagonal of an R8MAT.
Discussion:
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
in column-major order.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
15 July 2013
Author:
John Burkardt
Parameters:
Input, int N, the number of rows and columns of the matrix.
Input, double A[N*N], the N by N matrix.
Output, double V[N], the diagonal entries
of the matrix.
*/
{
int i;
for ( i = 0; i < n; i++ )
{
v[i] = a[i+i*n];
}
return;
}
/******************************************************************************/
void r8mat_identity ( int n, double a[] )
/******************************************************************************/
/*
Purpose:
R8MAT_IDENTITY sets an R8MAT to the identity matrix.
Discussion:
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
in column-major order.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
06 September 2005
Author:
John Burkardt
Parameters:
Input, int N, the order of A.
Output, double A[N*N], the N by N identity matrix.
*/
{
int i;
int j;
int k;
k = 0;
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < n; i++ )
{
if ( i == j )
{
a[k] = 1.0;
}
else
{
a[k] = 0.0;
}
k = k + 1;
}
}
return;
}
/******************************************************************************/
double r8mat_is_eigen_right ( int n, int k, double a[], double x[],
double lambda[] )
/******************************************************************************/
/*
Purpose:
R8MAT_IS_EIGEN_RIGHT determines the error in a (right) eigensystem.
Discussion:
An R8MAT is a matrix of doubles.
This routine computes the Frobenius norm of
A * X - X * LAMBDA
where
A is an N by N matrix,
X is an N by K matrix (each of K columns is an eigenvector)
LAMBDA is a K by K diagonal matrix of eigenvalues.
This routine assumes that A, X and LAMBDA are all real.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
07 October 2010
Author:
John Burkardt
Parameters:
Input, int N, the order of the matrix.
Input, int K, the number of eigenvectors.
K is usually 1 or N.
Input, double A[N*N], the matrix.
Input, double X[N*K], the K eigenvectors.
Input, double LAMBDA[K], the K eigenvalues.
Output, double R8MAT_IS_EIGEN_RIGHT, the Frobenius norm
of the difference matrix A * X - X * LAMBDA, which would be exactly zero
if X and LAMBDA were exact eigenvectors and eigenvalues of A.
*/
{
double *c;
double error_frobenius;
int i;
int j;
int l;
c = ( double * ) malloc ( n * k * sizeof ( double ) );
for ( j = 0; j < k; j++ )
{
for ( i = 0; i < n; i++ )
{
c[i+j*n] = 0.0;
for ( l = 0; l < n; l++ )
{
c[i+j*n] = c[i+j*n] + a[i+l*n] * x[l+j*n];
}
}
}
for ( j = 0; j < k; j++ )
{
for ( i = 0; i < n; i++ )
{
c[i+j*n] = c[i+j*n] - lambda[j] * x[i+j*n];
}
}
error_frobenius = r8mat_norm_fro ( n, k, c );
free ( c );
return error_frobenius;
}
/******************************************************************************/
double r8mat_norm_fro ( int m, int n, double a[] )
/******************************************************************************/
/*
Purpose:
R8MAT_NORM_FRO returns the Frobenius norm of an R8MAT.
Discussion:
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
in column-major order.
The Frobenius norm is defined as
R8MAT_NORM_FRO = sqrt (
sum ( 1 <= I <= M ) sum ( 1 <= j <= N ) A(I,J)^2 )
The matrix Frobenius norm is not derived from a vector norm, but
is compatible with the vector L2 norm, so that:
r8vec_norm_l2 ( A * x ) <= r8mat_norm_fro ( A ) * r8vec_norm_l2 ( x ).
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
26 July 2008
Author:
John Burkardt
Parameters:
Input, int M, the number of rows in A.
Input, int N, the number of columns in A.
Input, double A[M*N], the matrix whose Frobenius
norm is desired.
Output, double R8MAT_NORM_FRO, the Frobenius norm of A.
*/
{
int i;
int j;
double value;
value = 0.0;
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m; i++ )
{
value = value + pow ( a[i+j*m], 2 );
}
}
value = sqrt ( value );
return value;
}
/******************************************************************************/
void r8mat_print ( int m, int n, double a[], char *title )
/******************************************************************************/
/*
Purpose:
R8MAT_PRINT prints an R8MAT.
Discussion:
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
in column-major order.
Entry A(I,J) is stored as A[I+J*M]
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
28 May 2008
Author:
John Burkardt
Parameters:
Input, int M, the number of rows in A.
Input, int N, the number of columns in A.
Input, double A[M*N], the M by N matrix.
Input, char *TITLE, a title.
*/
{
r8mat_print_some ( m, n, a, 1, 1, m, n, title );
return;
}
/******************************************************************************/
void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi,
int jhi, char *title )
/******************************************************************************/
/*
Purpose:
R8MAT_PRINT_SOME prints some of an R8MAT.
Discussion:
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
in column-major order.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
26 June 2013
Author:
John Burkardt
Parameters:
Input, int M, the number of rows of the matrix.
M must be positive.
Input, int N, the number of columns of the matrix.
N must be positive.
Input, double A[M*N], the matrix.
Input, int ILO, JLO, IHI, JHI, designate the first row and
column, and the last row and column to be printed.
Input, char *TITLE, a title.
*/
{
# define INCX 5
int i;
int i2hi;
int i2lo;
int j;
int j2hi;
int j2lo;
fprintf ( stdout, "\n" );
fprintf ( stdout, "%s\n", title );
if ( m <= 0 || n <= 0 )
{
fprintf ( stdout, "\n" );
fprintf ( stdout, " (None)\n" );
return;
}
/*
Print the columns of the matrix, in strips of 5.
*/
for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
{
j2hi = j2lo + INCX - 1;
if ( n < j2hi )
{
j2hi = n;
}
if ( jhi < j2hi )
{
j2hi = jhi;
}
fprintf ( stdout, "\n" );
/*
For each column J in the current range...
Write the header.
*/
fprintf ( stdout, " Col: ");
for ( j = j2lo; j <= j2hi; j++ )
{
fprintf ( stdout, " %7d ", j - 1 );
}
fprintf ( stdout, "\n" );
fprintf ( stdout, " Row\n" );
fprintf ( stdout, "\n" );
/*
Determine the range of the rows in this strip.
*/
if ( 1 < ilo )
{
i2lo = ilo;
}
else
{
i2lo = 1;
}
if ( m < ihi )
{
i2hi = m;
}
else
{
i2hi = ihi;
}
for ( i = i2lo; i <= i2hi; i++ )
{
/*
Print out (up to) 5 entries in row I, that lie in the current strip.
*/
fprintf ( stdout, "%5d:", i - 1 );
for ( j = j2lo; j <= j2hi; j++ )
{
fprintf ( stdout, " %14f", a[i-1+(j-1)*m] );
}
fprintf ( stdout, "\n" );
}
}
return;
# undef INCX
}
/******************************************************************************/
void r8vec_print ( int n, double a[], char *title )
/******************************************************************************/
/*
Purpose:
R8VEC_PRINT prints an R8VEC.
Discussion:
An R8VEC is a vector of R8's.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
08 April 2009
Author:
John Burkardt
Parameters:
Input, int N, the number of components of the vector.
Input, double A[N], the vector to be printed.
Input, char *TITLE, a title.
*/
{
int i;
fprintf ( stdout, "\n" );
fprintf ( stdout, "%s\n", title );
fprintf ( stdout, "\n" );
for ( i = 0; i < n; i++ )
{
fprintf ( stdout, " %8d: %14f\n", i, a[i] );
}
return;
}
/******************************************************************************/
void timestamp ( void )
/******************************************************************************/
/*
Purpose:
TIMESTAMP prints the current YMDHMS date as a time stamp.
Example:
31 May 2001 09:45:54 AM
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
24 September 2003
Author:
John Burkardt
Parameters:
None
*/
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm *tm;
size_t len;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
fprintf ( stdout, "%s\n", time_buffer );
return;
# undef TIME_SIZE
}