-
Notifications
You must be signed in to change notification settings - Fork 23
/
eigen.c
196 lines (161 loc) · 4.88 KB
/
eigen.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "matrix.h"
#include "matrixadv.h"
#include "qr.h"
#include "qsort.h"
/*===========================================================================
* francisQRstep
* This algorithm performs the Francis QR Step to find the eigen values
* of a square matrix.
*
* I don't know what this algorithm should produce and I just read this
* technique on wikipedia. Currently I have this method to test the
* approach.
*=========================================================================*/
matrix* francisQRstep(matrix* a) {
int i, j, k = 0;
int upper_triangle;
const double EPSILON = 0.001;
matrix* a_k; // Matrix A_k
matrix* a_kp1; // Matrix A_(k+1)
matrix* q = NULL;
matrix* r = NULL;
matrix* out;
double* ptr;
double* outPtr;
assert(a->width == a->height, "Matrix must be square.");
out = makeMatrix(1, a->height);
a_k = copyMatrix(a);
while (1) {
// Perform the QR decomposition of a_k
printf("Loop.\n");
gram_schmidt(a_k, &q, &r);
// a_kp1 is the product of r * q
a_kp1 = multiplyMatrix(r, q);
// And the cleanup
freeMatrix(a_k);
freeMatrix(q);
freeMatrix(r);
a_k = copyMatrix(a_kp1);
freeMatrix(a_kp1);
q = NULL;
r = NULL;
printMatrix(a_k);
// Test for completion.
// We need to make sure this is an upper triangular matrix
upper_triangle = 1; // TRUE
for (i = 1; i < a_k->height; i++) {
for (j = 0; j < i; j++) {
if (fabs(a_k->data[i * a_k->width + j]) >= EPSILON) {
upper_triangle = 0; // FALSE
break;
}
}
if (upper_triangle == 0) {
break;
}
}
if (upper_triangle == 1) {
break;
}
k++;
}
// Gather up all of the elements along the diagonal.
ptr = a_k->data;
outPtr = out->data;
for (i = 0; i < a_k->width; i++) {
*outPtr = *ptr;
outPtr++;
ptr += a_k->width + 1;
}
// Sort the eigen values
quicksort(out->data, 0, out->width * out->height - 1);
freeMatrix(a_k);
return out;
}
/*===========================================================================
* powerMethod
* This algorithm determines the largest eigenvalue of a matrix using the
* power method.
*
* This was described to me in a Randomized Algoirthms course.
*=========================================================================*/
double powerMethod(matrix* a) {
matrix* x;
matrix* xp1; // x plus 1
const double EPSILON = 0.001;
double sum;
int i;
int k = 0;
int converge;
assert(a->width == a->height, "Matrix must be square.");
srand(time(0)); // Initalize our RNG
// Let's initalize x to a random vector
x = makeMatrix(1, a->height);
for (i = 0; i < a->height; i++) {
x->data[i] = (double) rand() / RAND_MAX;
}
// Iterate until the x vector converges.
while (1) {
k++;
// Multiply A * x
xp1 = multiplyMatrix(a, x);
// Add up all of the values in xp1
sum = 0;
for (i = 0; i < a->height; i++) {
sum += xp1->data[i];
}
// Divide each value in xp1 by sum. (Normalize)
for (i = 0; i < a->height; i++) {
xp1->data[i] /= sum;
}
// Test to see if we need to quit.
converge = 1; // Converged
for (i = 0; i < a->height; i++) {
if (fabs(x->data[i] - xp1->data[i]) >= EPSILON) {
converge = 0; // Not converged.
break;
}
}
// Set up for the next loop.
freeMatrix(x);
x = copyMatrix(xp1);
freeMatrix(xp1);
// Really test for quit.
if (converge == 1) {
break;
}
}
freeMatrix(x);
return sum;
}
/*===========================================================================
* eigenvector
* This algorithm determines the eigenvector of a matrix given an eigenvalue.
*=========================================================================*/
matrix* eigenvector(matrix* a, double eigenvalue) {
matrix* b; // This matrix will store A-eI
matrix* zero; // This matrix will store a column vector of zeros
matrix* out;
double* ptr;
int i;
assert(a->width == a->height, "Matrix must be square.");
// Create our column vector of zeros
zero = makeMatrix(1, a->height);
// Copy A
b = copyMatrix(a);
// Subtract eigenvalue from the diagonal elements
ptr = b->data;
for (i = 0; i < b->height; i++) {
*ptr -= eigenvalue;
ptr += b->width + 1;
}
// Find the eigenvector
out = solver(b, zero);
freeMatrix(b);
freeMatrix(zero);
return out;
}