-
Notifications
You must be signed in to change notification settings - Fork 0
/
eigen.cpp
84 lines (72 loc) · 1.88 KB
/
eigen.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
/**
* @file eigen.cpp
* @author Melih Altun @2015
**/
#include "eigen.h"
//calculates eigenvalues for an input matrix. eigenvectors are also computed if the matrix is symmetric
//parameters: (outputs) eigenvector matrix as column vectors, eigenvalues, (inputs) input matrix - must be square, size of matrix, tolerance for precision.
void eig_symetric(float V[], float D[], float in[], int size, float tolr) {
int i, j, k;
float norm1, residue;
float *diagVec;
diagVec = new float[size];
float *M;
M = new float[size*size];
float *Q;
Q = new float[size*size];
float *R;
R = new float[size*size];
float *Vnew;
Vnew = new float[size*size];
copy_matrix(M, in,size,size);
if (tolr == 0)
tolr = DEFAULT_TOLR;
memset(V, 0, size*size*sizeof(float));
#if defined (CHECK_SYMMETRY)
bool symmetric = true;
bool init = true;
for (i = 0; i < size; i++) {
for (j = i+1; j < size; j++){
if (M[lin_index(i, j, size)] != M[lin_index(j, i, size)]) {
symmetric = false;
break;
}
}
if (!symmetric)
break;
}
#endif
for (k = 0; k < MAX_ITER; k++) {
diagonal_to_vector(diagVec, M, size);
norm1 = vector_norm(diagVec, size);
QR(Q, R, M, size, size);
#if defined(CHECK_SYMMETRY)
if (symmetric) {
if (init) {
copy_matrix(V, Q, size, size);
init = false;
} else {
multiply_square_matrices(Vnew, V, Q, size);
copy_matrix(V, Vnew, size, size);
}
}
#endif
multiply_square_matrices(M, R, Q, size);
diagonal_to_vector(diagVec, M, size);
residue = fabs(norm1 - vector_norm(diagVec, size));
if (residue < tolr)
break;
}
diagonal_to_vector(D, M, size);
//TODO: Add Hessenberg reduction for non-symmetric matrices for later use.
delete[] diagVec;
diagVec = NULL;
delete[] M;
M = NULL;
delete[] Q;
Q = NULL;
delete[] R;
R = NULL;
delete[] Vnew;
Vnew = NULL;
}