From d2410f9c28a8d04075b9a29ef6b31702bfe321c4 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 13:53:55 -0400 Subject: [PATCH 1/7] update qr_eigen valueswith selfcontained tests Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> --- numerical_methods/qr_eigen_values.c | 309 ++++++++++++++++++++++------ 1 file changed, 244 insertions(+), 65 deletions(-) diff --git a/numerical_methods/qr_eigen_values.c b/numerical_methods/qr_eigen_values.c index 5028b848f..504cab0e3 100644 --- a/numerical_methods/qr_eigen_values.c +++ b/numerical_methods/qr_eigen_values.c @@ -6,12 +6,17 @@ * \author [Krishna Vedala](https://github.com/kvedala) */ #include "qr_decompose.h" +#include #include #include #include #include +#ifdef _OPENMP +#include +#endif -#define LIMS 9 /**< limit of range of matrix values */ +#define LIMS 9 /**< limit of range of matrix values */ +#define EPSILON 1e-10 /**< accuracy tolerance limit */ /** * create a square matrix of given size with random elements @@ -21,8 +26,10 @@ void create_matrix(double **A, int N) { int i, j, tmp, lim2 = LIMS >> 1; - srand(time(NULL)); +#ifdef _OPENMP +#pragma omp for +#endif for (i = 0; i < N; i++) { A[i][i] = (rand() % LIMS) - lim2; @@ -56,7 +63,12 @@ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2, perror("Matrix dimensions mismatch!"); return OUT; } - for (int i = 0; i < R1; i++) + + int i; +#ifdef _OPENMP +#pragma omp for +#endif + for (i = 0; i < R1; i++) for (int j = 0; j < C2; j++) { OUT[i][j] = 0.f; @@ -66,14 +78,240 @@ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2, return OUT; } +/** Compute eigen values using iterative shifted QR decomposition algorithm as + * follows: + * 1. Use last diagonal element of A as eigen value approximation \f$c\f$ + * 2. Shift diagonals of matrix \f$A' = A - cI\f$ + * 3. Decompose matrix \f$A'=QR\f$ + * 4. Compute next approximation \f$A'_1 = RQ \f$ + * 5. Shift diagonals back \f$A_1 = A'_1 + cI\f$ + * 6. Termination condition check: last element below diagonal is almost 0 + * 1. If not 0, go back to step 1 with the new approximation \f$A_1\f$ + * 2. If 0, continue to step 7 + * 7. Save last known \f$c\f$ as the eigen value. + * 8. Are all eigen values found? + * 1. If not, remove last row and column of \f$A_1\f$ and go back to step 1. + * 2. If yes, stop. + * + * \note The matrix \f$A\f$ gets modified + * + * \param[in,out] A matrix to compute eigen values for + * \param[out] eig_vals resultant vector containing computed eigen values + * \param[in] mat_size matrix size + * \param[in] debug_print 1 to print intermediate Q & R matrices, 0 for not to + * \returns time for computation in seconds + */ +double eigen_values(double **A, double *eigen_vals, int mat_size, + char debug_print) +{ + double **R = (double **)malloc(sizeof(double *) * mat_size); + double **Q = (double **)malloc(sizeof(double *) * mat_size); + + if (!eigen_vals) + { + perror("Output eigen value vector cannot be NULL!"); + return -1; + } + else if (!Q || !R) + { + perror("Unable to allocate memory for Q & R!"); + return -1; + } + + /* allocate dynamic memory for matrices */ + for (int i = 0; i < mat_size; i++) + { + R[i] = (double *)malloc(sizeof(double) * mat_size); + Q[i] = (double *)malloc(sizeof(double) * mat_size); + if (!Q[i] || !R[i]) + { + perror("Unable to allocate memory for Q & R."); + return -1; + } + } + + if (debug_print) + print_matrix(A, mat_size, mat_size); + + int rows = mat_size, columns = mat_size; + int counter = 0, num_eigs = rows - 1; + double last_eig = 0; + + clock_t t1 = clock(); + while (num_eigs > 0) /* continue till all eigen values are found */ + { + /* iterate with QR decomposition */ + while (fabs(A[num_eigs][num_eigs - 1]) > EPSILON) + { + last_eig = A[num_eigs][num_eigs]; + for (int i = 0; i < rows; i++) + A[i][i] -= last_eig; /* A - cI */ + qr_decompose(A, Q, R, rows, columns); + + if (debug_print) + { + print_matrix(A, rows, columns); + print_matrix(Q, rows, columns); + print_matrix(R, columns, columns); + printf("-------------------- %d ---------------------\n", + ++counter); + } + + mat_mul(R, Q, A, columns, columns, rows, columns); + for (int i = 0; i < rows; i++) + A[i][i] += last_eig; /* A + cI */ + } + + /* store the converged eigen value */ + eigen_vals[num_eigs] = last_eig; + + if (debug_print) + { + printf("========================\n"); + printf("Eigen value: % g,\n", last_eig); + printf("========================\n"); + } + + num_eigs--; + rows--; + columns--; + } + eigen_vals[0] = A[0][0]; + double dtime = (double)(clock() - t1) / CLOCKS_PER_SEC; + + if (debug_print) + { + print_matrix(R, mat_size, mat_size); + print_matrix(Q, mat_size, mat_size); + } + + /* cleanup dynamic memory */ + for (int i = 0; i < mat_size; i++) + { + free(R[i]); + free(Q[i]); + } + free(R); + free(Q); + + return dtime; +} + +/** + * test function to compute eigen values of a 2x2 matrix + * \f[\begin{bmatrix} + * 5 & 7\\ + * 7 & 11 + * \end{bmatrix}\f] + * which are approximately, {15.56158, 0.384227} + */ +void test1() +{ + int mat_size = 2; + double X[][2] = {{5, 7}, {7, 11}}; + double y[] = {15.56158, 0.384227}; // corresponding y-values + double eig_vals[2]; + + // The following steps are to convert a "double[][]" to "double **" + double **A = (double **)malloc(mat_size * sizeof(double *)); + for (int i = 0; i < mat_size; i++) + A[i] = X[i]; + + printf("------- Test 1 -------\n"); + + double dtime = eigen_values(A, eig_vals, mat_size, 0); + + for (int i = 0; i < mat_size; i++) + { + printf("%d/5 Checking for %.3g --> ", i + 1, y[i]); + char result = 0; + for (int j = 0; j < mat_size && !result; j++) + { + if (fabs(y[i] - eig_vals[j]) < 0.1) + { + result = 1; + printf("(%.3g) ", eig_vals[j]); + } + } + + // ensure that i^th expected eigen value was computed + assert(result != 0); + printf("found\n"); + } + printf("Test 1 Passed in %.3g sec\n\n", dtime); + free(A); +} + +/** + * test function to compute eigen values of a 2x2 matrix + * \f[\begin{bmatrix} + * -4& 4& 2& 0& -3\\ + * 4& -4& 4& -3& -1\\ + * 2& 4& 4& 3& -3\\ + * 0& -3& 3& -1&-1\\ + * -3& -1& -3& -3& 0 + * \end{bmatrix}\f] + * which are approximately, {9.27648, -9.26948, 2.0181, -1.03516, -5.98994} + */ +void test2() +{ + int mat_size = 5; + double X[][5] = {{-4, 4, 2, 0, -3}, + {4, -4, 4, -3, -1}, + {2, 4, 4, 3, -3}, + {0, -3, 3, -1, -3}, + {-3, -1, -3, -3, 0}}; + double y[] = {9.27648, -9.26948, 2.0181, -1.03516, + -5.98994}; // corresponding y-values + double eig_vals[5]; + + // The following steps are to convert a "double[][]" to "double **" + double **A = (double **)malloc(mat_size * sizeof(double *)); + for (int i = 0; i < mat_size; i++) + A[i] = X[i]; + + printf("------- Test 2 -------\n"); + + double dtime = eigen_values(A, eig_vals, mat_size, 0); + + for (int i = 0; i < mat_size; i++) + { + printf("%d/5 Checking for %.3g --> ", i + 1, y[i]); + char result = 0; + for (int j = 0; j < mat_size && !result; j++) + { + if (fabs(y[i] - eig_vals[j]) < 0.1) + { + result = 1; + printf("(%.3g) ", eig_vals[j]); + } + } + + // ensure that i^th expected eigen value was computed + assert(result != 0); + printf("found\n"); + } + printf("Test 2 Passed in %.3g sec\n\n", dtime); + free(A); +} + /** * main function */ int main(int argc, char **argv) { + srand(time(NULL)); + int mat_size = 5; if (argc == 2) mat_size = atoi(argv[1]); + else + { // if invalid input argument is given run tests + test1(); + test2(); + printf("Usage: ./qr_eigen_values [mat_size]\n"); + return 0; + } if (mat_size < 2) { @@ -84,26 +322,16 @@ int main(int argc, char **argv) int i, rows = mat_size, columns = mat_size; double **A = (double **)malloc(sizeof(double *) * mat_size); - double **R = (double **)malloc(sizeof(double *) * mat_size); - double **Q = (double **)malloc(sizeof(double *) * mat_size); - /* number of eigen values = matrix size */ double *eigen_vals = (double *)malloc(sizeof(double) * mat_size); - if (!Q || !R || !eigen_vals) + if (!eigen_vals) { - perror("Unable to allocate memory for Q & R!"); + perror("Unable to allocate memory for eigen values!"); return -1; } for (i = 0; i < mat_size; i++) { A[i] = (double *)malloc(sizeof(double) * mat_size); - R[i] = (double *)malloc(sizeof(double) * mat_size); - Q[i] = (double *)malloc(sizeof(double) * mat_size); - if (!Q[i] || !R[i]) - { - perror("Unable to allocate memory for Q & R."); - return -1; - } } /* create a random matrix */ @@ -111,64 +339,15 @@ int main(int argc, char **argv) print_matrix(A, mat_size, mat_size); - int counter = 0, num_eigs = rows - 1; - double last_eig = 0; - - clock_t t1 = clock(); - while (num_eigs > 0) /* continue till all eigen values are found */ - { - /* iterate with QR decomposition */ - while (fabs(A[num_eigs][num_eigs - 1]) > 1e-10) - { - last_eig = A[num_eigs][num_eigs]; - for (int i = 0; i < rows; i++) - A[i][i] -= last_eig; /* A - cI */ - qr_decompose(A, Q, R, rows, columns); - -#if defined(DEBUG) || !defined(NDEBUG) - print_matrix(A, rows, columns); - print_matrix(Q, rows, columns); - print_matrix(R, columns, columns); - printf("-------------------- %d ---------------------\n", - ++counter); -#endif - mat_mul(R, Q, A, columns, columns, rows, columns); - for (int i = 0; i < rows; i++) - A[i][i] += last_eig; /* A + cI */ - } - - /* store the converged eigen value */ - eigen_vals[num_eigs] = A[num_eigs][num_eigs]; -#if defined(DEBUG) || !defined(NDEBUG) - printf("========================\n"); - printf("Eigen value: % g,\n", last_eig); - printf("========================\n"); -#endif - num_eigs--; - rows--; - columns--; - } - eigen_vals[0] = A[0][0]; - double dtime = (double)(clock() - t1) / CLOCKS_PER_SEC; - -#if defined(DEBUG) || !defined(NDEBUG) - print_matrix(R, mat_size, mat_size); - print_matrix(Q, mat_size, mat_size); -#endif + double dtime = eigen_values(A, eigen_vals, mat_size, 0); printf("Eigen vals: "); for (i = 0; i < mat_size; i++) printf("% 9.4g\t", eigen_vals[i]); printf("\nTime taken to compute: % .4g sec\n", dtime); for (int i = 0; i < mat_size; i++) - { free(A[i]); - free(R[i]); - free(Q[i]); - } free(A); - free(R); - free(Q); free(eigen_vals); return 0; } From b05987eb80631d470402a19aa3cec79f668e92e7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:06:02 -0400 Subject: [PATCH 2/7] initial gitpod --- .gitpod.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 000000000..58433c3c0 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,5 @@ +tasks: +- init: > + cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug && + cmake --build build -t all + \ No newline at end of file From 97f44874c47be84f43ad987a25de9b84b2a650ff Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:07:55 -0400 Subject: [PATCH 3/7] added gitpod and gittr to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index edd87dcf5..21c6c81f2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # The Algorithms - C # {#mainpage} +[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/C-Plus-Plus) +[![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms) [![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/kvedala/C-Plus-Plus/blob/master/CONTRIBUTION.md)  ![GitHub repo size](https://img.shields.io/github/repo-size/kvedala/C-Plus-Plus?color=red&style=flat-square) ![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/kvedala/C?color=green&style=flat-square) From 414d7c153ad0e3af401f43a6e322a573d2b503a9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:09:53 -0400 Subject: [PATCH 4/7] rename CONTRIBUTIONS.md to CONTRIBUTING.md --- .github/pull_request_template.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index efaaf3765..ef1f9ad03 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -3,17 +3,17 @@ Thank you for your Pull Request. Please provide a description above and review the requirements below. -Contributors guide: https://github.com/TheAlgorithms/C-Plus-Plus/CONTRIBUTION.md +Contributors guide: https://github.com/TheAlgorithms/C-Plus-Plus/CONTRIBUTING.md --> #### Checklist - [ ] Added description of change -- [ ] Added file name matches [File name guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTION.md#New-File-Name-guidelines) +- [ ] Added file name matches [File name guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTING.md#New-File-Name-guidelines) - [ ] Added tests and example, test must pass - [ ] Relevant documentation/comments is changed or added -- [ ] PR title follows semantic [commit guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTION.md#Commit-Guidelines) +- [ ] PR title follows semantic [commit guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTING.md#Commit-Guidelines) - [ ] Search previous suggestions before making a new one, as yours may be a duplicate. - [ ] Sort by alphabetical order - [ ] I acknowledge that all my contributions will be made under the project's license. From f579acae6472b38d4d84f09f29d5d00fec29f924 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:10:25 -0400 Subject: [PATCH 5/7] rename CONTRIBUTIONS.md to CONTRIBUTING.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 21c6c81f2..5d145cee3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # The Algorithms - C # {#mainpage} [![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/C-Plus-Plus) [![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms) -[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/kvedala/C-Plus-Plus/blob/master/CONTRIBUTION.md)  +[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/kvedala/C-Plus-Plus/blob/master/CONTRIBUTING.md)  ![GitHub repo size](https://img.shields.io/github/repo-size/kvedala/C-Plus-Plus?color=red&style=flat-square) ![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/kvedala/C?color=green&style=flat-square) ![Doxygen CI](https://github.com/kvedala/C/workflows/Doxygen%20CI/badge.svg) @@ -17,4 +17,4 @@ All the code can be executed and tested online: [![using Google Colab Notebook]( The implementations are for learning purpose. They may be less efficient than the implementation in the standard library. ### Contribute Guidelines -Read our [Contribution Guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTION.md) before you contribute. +Read our [Contribution Guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTING.md) before you contribute. From f25c306054597ffcef92789089f52c975cee935d Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:14:39 -0400 Subject: [PATCH 6/7] install cpplint in gitpod --- .gitpod.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitpod.yml b/.gitpod.yml index 58433c3c0..14f45d6a2 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -1,5 +1,6 @@ tasks: - init: > + pip install cpplint cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug && cmake --build build -t all \ No newline at end of file From fef89214105256d969dc26b4b4770514720a4660 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:19:19 -0400 Subject: [PATCH 7/7] gitpod: cmake export commands --- .gitpod.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitpod.yml b/.gitpod.yml index 14f45d6a2..c0720dcb5 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -1,6 +1,6 @@ tasks: - init: > pip install cpplint - cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug && - cmake --build build -t all + cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && + cmake --build build -t all \ No newline at end of file