create a random symmetric matrix with real eigen values

This commit is contained in:
Krishna Vedala 2020-04-20 13:25:06 -04:00
parent 64fd13388e
commit 68d5d6a131
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,15 +1,27 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <function_timer.h> #include <function_timer.h>
#define ROWS 3 #define LIMS 9
#define COLUMNS ROWS /* Ensure square matrix */
double _A[][COLUMNS] = { void create_matrix(double **A, int N)
{-3.44827586, -1.62068966, -3.03448276}, {
{-1.03448276, -0.5862069, -1.31034483}, int i, j, tmp, lim2 = LIMS >> 1;
{-1.55172414, -0.37931034, 0.03448276}}; srand(time(NULL));
for (i = 0; i < N; i++)
{
A[i][i] = (rand() % LIMS) - lim2;
for (j = i + 1; j < N; j++)
{
tmp = (rand() % LIMS) - lim2;
A[i][j] = tmp;
A[j][i] = tmp;
}
}
}
void print_matrix(double **A, int M, int N) void print_matrix(double **A, int M, int N)
{ {
@ -115,37 +127,41 @@ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2, i
return OUT; return OUT;
} }
int main(void) int main(int argc, char **argv)
{ {
int i, rows = ROWS, columns = COLUMNS; int mat_size = 5;
if (argc == 2)
mat_size = atoi(argv[1]);
double **A = (double **)malloc(sizeof(double) * ROWS); int i, rows = mat_size, columns = mat_size;
double **R = (double **)malloc(sizeof(double) * COLUMNS);
double **Q = (double **)malloc(sizeof(double) * ROWS); double **A = (double **)malloc(sizeof(double) * mat_size);
double *eigen_vals = (double *)malloc(sizeof(double) * COLUMNS); double **R = (double **)malloc(sizeof(double) * mat_size);
double **Q = (double **)malloc(sizeof(double) * mat_size);
double *eigen_vals = (double *)malloc(sizeof(double) * mat_size);
if (!Q || !R || !eigen_vals) if (!Q || !R || !eigen_vals)
{ {
perror("Unable to allocate memory for Q & R!"); perror("Unable to allocate memory for Q & R!");
return -1; return -1;
} }
for (i = 0; i < ROWS; i++) for (i = 0; i < mat_size; i++)
{ {
A[i] = (double *)malloc(sizeof(double) * COLUMNS); A[i] = (double *)malloc(sizeof(double) * mat_size);
R[i] = (double *)malloc(sizeof(double) * COLUMNS); R[i] = (double *)malloc(sizeof(double) * mat_size);
Q[i] = (double *)malloc(sizeof(double) * COLUMNS); Q[i] = (double *)malloc(sizeof(double) * mat_size);
if (!Q[i] || !R[i]) if (!Q[i] || !R[i])
{ {
perror("Unable to allocate memory for Q & R."); perror("Unable to allocate memory for Q & R.");
return -1; return -1;
} }
for (columns = 0; columns < COLUMNS; columns++)
A[i][columns] = _A[i][columns];
} }
print_matrix(A, ROWS, COLUMNS); create_matrix(A, mat_size);
print_matrix(A, mat_size, mat_size);
int counter = 0, num_eigs = rows - 1; int counter = 0, num_eigs = rows - 1;
double last_eig = 0, eig_val = 0.; double last_eig = 0;
function_timer *t1 = new_timer(); function_timer *t1 = new_timer();
start_timer(t1); start_timer(t1);
@ -162,7 +178,7 @@ int main(void)
print_matrix(A, rows, columns); print_matrix(A, rows, columns);
print_matrix(Q, rows, columns); print_matrix(Q, rows, columns);
print_matrix(R, columns, columns); print_matrix(R, columns, columns);
printf("-------------------- %d ---------------------\n", counter++); printf("-------------------- %d ---------------------\n", ++counter);
#endif #endif
mat_mul(R, Q, A, columns, columns, rows, columns); mat_mul(R, Q, A, columns, columns, rows, columns);
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
@ -180,21 +196,18 @@ int main(void)
columns--; columns--;
} }
eigen_vals[0] = A[0][0]; eigen_vals[0] = A[0][0];
#if defined(DEBUG) || !defined(NDEBUG)
printf("========================\n");
printf("Eigen value: % g,\n", last_eig);
printf("========================\n");
#endif
double dtime = end_timer(t1); double dtime = end_timer(t1);
print_matrix(R, ROWS, COLUMNS); #if defined(DEBUG) || !defined(NDEBUG)
print_matrix(Q, ROWS, COLUMNS); print_matrix(R, mat_size, mat_size);
print_matrix(Q, mat_size, mat_size);
#endif
printf("Eigen vals: "); printf("Eigen vals: ");
for (i = 0; i < ROWS; i++) for (i = 0; i < mat_size; i++)
printf("% 9.3g\t", eigen_vals[i]); printf("% 9.4g\t", eigen_vals[i]);
printf("\nTime taken to compute: %.3g sec\n", dtime); printf("\nTime taken to compute: % .4g sec\n", dtime);
for (int i = 0; i < ROWS; i++) for (int i = 0; i < mat_size; i++)
{ {
free(A[i]); free(A[i]);
free(R[i]); free(R[i]);
@ -203,5 +216,6 @@ int main(void)
free(A); free(A);
free(R); free(R);
free(Q); free(Q);
free(eigen_vals);
return 0; return 0;
} }