forked from zholzer/Eigenvalue-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helperFunctions.c
327 lines (274 loc) · 8.76 KB
/
helperFunctions.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
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include "functions.h"
void conjugateTranspose(int n, int m, double complex A[n][m], double complex AH[m][n]){
int i,j;
for (i=0; i<n; i++){
for (j=0; j<m; j++){
AH[j][i] = conj(A[i][j]); // get conjugate of values and then allocate as the transpose to AH
}
}
}
// function for the determinant of a 2x2 matrix
// inputs: n size matrix, A[n][n] matrix
double complex det2by2(int n, double complex A[n][n])
{
// if statement if the matrix is not a 2x2
if (n != 2)
{
printf("Please input a 2x2 matrix.\n");
return 1;
}
double complex a, b, c, d, det;
a = A[0][0]; b = A[0][1];
c = A[1][0]; d = A[1][1];
det = a*d - b*c;
return det;
}
void displayMatrix(int n, int m, double complex A[n][m]){
int i, j;
for (i=0; i<n; i++){
for (j=0; j<m; j++){
printf("%.2lf %+.2lfi ", creal(A[i][j]), cimag(A[i][j]));
}
printf("\n");
}
}
void fillIdentityN(int n, double complex id[n][n]){
int i,j;
for (i=0; i<n; i++){
for (j=0; j<n; j++){
if (i == j){
id[i][j] = 1.0; // sets diagonal as 1
}
else{
id[i][j] = 0.0; // sets non-diagonal as 0
}
}
}
}
// cofactor function (only works for higher than 2x2 matrices, cofactor for 2x2 is written directly in main )
// GetCofactor is a function to compute the cofactor of a matrix A[p][q]
//
// inputs: int size of matrix N, given matrix A[N][N], empty matrix cof[N][N] to hold output,
// ints m/n to specify the element we're at in A
void GetCofactor(int N, double complex A[N][N], double complex cof[N-1][N-1], int m, int n)
{
int i, j, r, c;
// initialize the index
i = 0; j = 0;
// for loop thorugh each element
for (r = 0; r < N; r++)
{
for (c = 0; c < N; c++)
{
// identify the rows/cols that we are NOT at
if (r != m && c != n)
{
cof[i][j] = A[r][c];
j++;
// once the row is filled, we reset for next row
if(j == N-1)
{
j = 0;
i++;
}
}
}
}
}
// determinant function (for any NxN matrix)
// inputs: size N, matrix
// output: determinant
double complex GetDeterminant(int N, double complex matrix[N][N])
{
int sign, c;
double complex determinant, intmatrix[N-1][N-1];
// initialize
determinant = 0 + 0*I;
// if the matrix is 2x2
if (N == 2)
{
determinant = det2by2(N, matrix);
}
// else, break down the matrix
else{
// for each column in the first row:
for (c = 0; c < N; c++){
// call the cofactor of the row/col we are at
GetCofactor(N, matrix, intmatrix, 0, c);
// method is too intensive with larger matrices
// define sign, save the element we are at, call determinant function again
sign = pow(-1, c);
determinant = determinant + (sign * matrix[0][c]*GetDeterminant( N-1, intmatrix ));
}
}
return determinant;
}
int GetInverse(int N, double complex A[N][N], double complex inverse[N][N])
{
int i, j, sign;
// initialize for cofactor function
double complex determinant, det, cofM[N][N]; // determinant, and cofactor matrix (det is the determinant in the cofactor loop)
// get the determinant of the matrix
determinant = GetDeterminant(N, A);
//printf("Determinant= %.2f %+.2fi\n", creal(determinant),cimag(determinant));
// if the determinant = 0, there is no inverse!!
if (determinant == 0) {
printf("Determinant = 0. There is no inverse. Please input another matrix.\n");
return 1;
}
// find the Adjoint- first find the cofactor matrix
// find the cofactor for each element, find the determinant of that matrix, determinant = that element
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
// if statement if the matrix is a 2x2
if (N == 2) {
double complex cof; // empty matrix for cofactor
int row,col;
for (row = 0; row < N; row++){
for (col = 0; col < N; col++){
if (row != i && col != j)
{
cof = A[row][col];
det = cof;
}
}
}
}
// for all other N sized matrices:
else {
double complex cof[N-1][N-1]; // empty matrix for cofactor
// call cofactor function
GetCofactor(N, A, cof, i, j);
// call the determinant function
det = GetDeterminant(N-1, cof);
}
// save the determinant in the corresponding element
// this is the cofactor matrix cofM
sign = pow(-1, i+j);
cofM[i][j] = det*sign;
}
}
// display the cofactor matrix
//printf("The cofactor matrix:\n");
//displayMatrix(N,cofM);
// now that we have the cofactor matrix, the transpose = Adjugate
// adj will hold the adjugate matrix
double complex adj[N][N];
GetTranspose(N, cofM, adj);
//printf("The adjugate:\n");
//displayMatrix(N,adj);
// inverse = Adjugate/determinant
// divide the determinant from each element
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
inverse[i][j] = adj[i][j] / determinant;
}
}
// print
//printf("The Inverse: \n");
//displayMatrix(N,N,inverse);
return 0;
}
void GetTranspose(int N, double complex matrix[N][N], double complex matrixT[N][N])
{
int row, col;
for (row = 0; row < N; row++)
{
for (col = 0; col < N; col++)
{
matrixT[col][row] = matrix[row][col]; //gets the transpose
}
}
}
void matrix_addition(int n, int m, double complex A[n][m], double complex B[n][m], double complex APlusB[n][m]){
int i; int j;
for(i=0; i < n; ++i){
for(j=0; j < m; ++j){
APlusB[i][j] = A[i][j] + B[i][j]; // adds matrix elements
}
}
}
// multiplies two matrices of any compatible size
void matrix_multiplication(int a, int b, double complex matrix1[a][b], int m, int n, double complex matrix2[m][n], double complex matAXmatB[a][n]){
int i; int j; int k;
// error messages used to find source of segfault ****
if(b != m){ //number of columns of matrix1 is not the same as number of rows of matrix2
printf("Cannot do the multiplication. \n");
}
else{
for(i = 0; i < a; i++){
for(j = 0; j < n; j++){
matAXmatB[i][j] = 0.0 + 0.0*I;
}
}
for(i = 0; i < a; i++){
for(j = 0; j < n; j++){
for(k = 0; k < m; k++){
matAXmatB[i][j] += matrix1[i][k]*matrix2[k][j];
}
}
}
}
}
double my_cabs(double complex x){
return sqrt(creal(x)*creal(x) + cimag(x)*cimag(x)); // complex absolute value
}
// takes the complex norm of a vector
double norm (int n, double complex vec[n][1]){
double term, sum, sol;
int i;
// error code for an empty vector
if (n == 0){
printf("Error: Empty Vector. Retry.\n");
return 1;
}
sum = 0; // initialize the sum
for (i = 0; i < n; ++i)
{
term = pow(creal(vec[i][0]),2) + pow(cimag(vec[i][0]),2);
sum = sum + term;
}
// after the loop, square root the sum
sol = sqrt(sum);
return sol;
}
void scalarByMatrixMultiplication(double complex scalar, int n, int m, double complex A[n][m], double complex scalarXA[n][m]){
int i; int j;
for(i=0; i < n; ++i){
for(j=0; j < m; ++j){
scalarXA[i][j] = scalar*A[i][j]; // multiplies matrix entries by a scalar
}
}
}
// sets a vector with normalized values (1/sqrt(dim))
void setNormalVec(int n, double complex v[n][1]){
int i;
for (i=0; i<n; i++){
v[i][0] = 1/sqrt(n);
}
}
// sets the T matix for Lanczos, puts alphas on the diagonal and betas one above and below the diagonal
void setT(int n, double complex alpha[1][n], double complex beta[1][n], double complex T[n][n]){
int i, j;
for (i=0; i<n; i++){
for (j=0; j<n; j++){
if (i == j){
T[i][j] = alpha[0][j];
}
else if ((i-1) == j){
T[i][j] = beta[0][j];
}
else if ((i+1) == j){
T[i][j] = beta[0][j-1];
}
else{T[i][j] = 0;}
}
}
}