From 647a6fe20b0956e32d18450454d464daf7021481 Mon Sep 17 00:00:00 2001 From: Chris Fadden Date: Mon, 4 May 2015 12:35:31 -0400 Subject: [PATCH] Cleaned up ChrisTest, added some Matrix functionality in C++, waiting for templates --- ChrisTest/C_Implementations/ChrisMatrix.h | 84 -------- ChrisTest/C_Implementations/main.c | 31 --- ChrisTest/ChrisMatrix/ChrisMatrix.cpp | 230 ++++++++++++++++++++++ ChrisTest/ChrisMatrix/InvertMatrix.cpp | 54 +++++ ChrisTest/HelloWorld/HelloWorld.krak | 14 -- tests/.test_trigTest.krak.un~ | Bin 0 -> 1379 bytes 6 files changed, 284 insertions(+), 129 deletions(-) delete mode 100644 ChrisTest/C_Implementations/ChrisMatrix.h delete mode 100644 ChrisTest/C_Implementations/main.c create mode 100644 ChrisTest/ChrisMatrix/ChrisMatrix.cpp create mode 100644 ChrisTest/ChrisMatrix/InvertMatrix.cpp delete mode 100644 ChrisTest/HelloWorld/HelloWorld.krak create mode 100644 tests/.test_trigTest.krak.un~ diff --git a/ChrisTest/C_Implementations/ChrisMatrix.h b/ChrisTest/C_Implementations/ChrisMatrix.h deleted file mode 100644 index af0134e..0000000 --- a/ChrisTest/C_Implementations/ChrisMatrix.h +++ /dev/null @@ -1,84 +0,0 @@ -#include - -/********************* - * Matrix Transpose - * - * No real concept of vector transpose - * in this kraken implementation - * *******************/ -void transpose(int m, int n, double** A) -{ - int i, j; - - /****************************** - * Allocate memory for tmp array - * ***************************/ - - double** A_trans; - A_trans = malloc(sizeof(double*) * m); - - for(i = 0; i < m; i++) - { - A_trans[i] = malloc(sizeof(double) * n); - } - - /********************* - * Copy A into tmp - ********************/ - for(i = 0; i < m; i++) - { - for(j = 0; j < n; j++) - { - A_trans[i][j] = A[i][j]; - }//end j loop - }//end i loop - - /*************** - * Transpose operation - ***************/ - for(i = 0; i < m; i++) - { - for(j = 0; j < n; j++) - { - A[i][j] = A_trans[j][i]; - }//end j loop - }//end i loop - - /********** - * Free Memory - * *******/ - for(i = 0; i < m; i++) - free(A_trans[i]); - free(A_trans); - -}//end transpose function - -void multiply(int m, int n, int q, double** A, double** B, double**C) -{ - //Kraken: check if C is empty, then allocate memory - - - - return; -} - -void printMatrix(int m, int n, double** A) -{ - int i, j; - - for(i = 0; i < m; i++) - { - for(j = 0; j < n; j++) - { - printf("%f ",A[i][j]); - }//end j loop - printf("\n"); - }//end i loop - printf("\n"); -}//end print function - - - - - - diff --git a/ChrisTest/C_Implementations/main.c b/ChrisTest/C_Implementations/main.c deleted file mode 100644 index be9c540..0000000 --- a/ChrisTest/C_Implementations/main.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include "ChrisMatrix.h" - -int main(int argc, char** argv) -{ - double** A; - int m = 3; - int n = 3; - int i, j; - - A = malloc(sizeof(double*) * m); - for(i = 0; i < m; i++) - A[i] = malloc(sizeof(double) * n); - - int k = 0; - for(i = 0; i < m; i++) - { - for(j = 0; j < n; j++) - { - A[i][j] = k; - k++; - }//end j loop - }//end i loop - - printMatrix(m, n, A); - transpose(m,n,A); - printMatrix(m,n,A); - printf("Hello World\n"); - - return 0; -} diff --git a/ChrisTest/ChrisMatrix/ChrisMatrix.cpp b/ChrisTest/ChrisMatrix/ChrisMatrix.cpp new file mode 100644 index 0000000..3094ea1 --- /dev/null +++ b/ChrisTest/ChrisMatrix/ChrisMatrix.cpp @@ -0,0 +1,230 @@ +#include +#include + +class Matrix { + public: + // default constructor + Matrix() { + rows = 0; + cols = 0; + } + + // standard sizes + Matrix(const int m, const int n) { + rows = m; + cols = n; + data.resize(rows * cols); + for (int i = 0; i < rows * cols; i++) { + data[i] = i; + } + } + + double at(const int i, const int j) { + int index = i * rows + j; + + if (index > rows * cols) { + std::cout << "index (" << i << ", " << j << ") out of bounds" + << std::endl; + std::cout << "Max index = (" << rows - 1 << ", " << cols - 1 << ")" + << std::endl; + } + + return data[index]; + } + + void getSize(int &m, int &n) { + m = rows; + n = cols; + + return; + } + + void resize(const int m, const int n) { + rows = m; + cols = n; + + data.resize(rows * cols); + + return; + } + + void set(const int i, const int j, const double val) { + int index = i * rows + j; + + if (index > rows * cols) { + std::cout << "index (" << i << ", " << j << ") out of bounds" + << std::endl; + std::cout << "Max index = (" << rows - 1 << ", " << cols - 1 << ")" + << std::endl; + } + + data[index] = val; + + return; + } + + void transpose() { + double val1, val2; + + for (int n = 0; n <= rows - 2; n++) + for (int m = n + 1; m <= rows - 1; m++) { + val1 = at(n, m); + val2 = at(m, n); + + set(n, m, val2); + set(m, n, val1); + } + + return; + } // end transpose + + void copy(Matrix &C) { + C.resize(rows, cols); + double val; + + for (int i = 0; i < rows; i++) + for (int j = 0; j < cols; j++) { + val = at(i, j); + C.set(i, j, val); + } + } // end copy + + void inverse() { + std::vector > a(2 * rows + 1, + std::vector(2 * rows + 1, 0)); + + int n = rows; + + double d, val; + + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) a[i][j] = at(i - 1, j - 1); + + for (int i = 1; i <= n; i++) + for (int j = 1; j <= 2 * n; j++) + if (j == (i + n)) a[i][j] = 1; + + /**************** + * Partial Pivot + ***************/ + + for (int i = n; i > 1; i--) { + if (a[i - 1][1] < a[i][i]) + for (int j = 1; j <= n * 2; j++) { + d = a[i][j]; + a[i][j] = a[i - 1][j]; + a[i - 1][j] = d; + } // end j loop + } // end i loop + + /********** + * Reduce to diagonal matrix + *********/ + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n * 2; j++) + if (j != i) { + d = a[j][i] / a[i][i]; + for (int k = 1; k <= n * 2; k++) a[j][k] -= a[i][k] * d; + } + } + + /*********** + * reduce to unit matrix + **********/ + for (int i = 1; i <= n; i++) { + d = a[i][i]; + for (int j = 1; j <= n * 2; j++) a[i][j] = a[i][j] / d; + } + + std::cout << "your solutions: " << std::endl; + for (int i = 1; i <= n; i++) { + for (int j = n + 1; j <= n * 2; j++) std::cout << a[i][j] << " "; + std::cout << std::endl; + } + + return; + } + + void printMatrix() { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + std::cout << data[i * rows + j] << " "; + } // j loop + std::cout << std::endl; + } // i loop + } // end printMatrix func + + private: + std::vector data; + int rows; + int cols; +}; + +void mult(Matrix A, Matrix B, Matrix &C) { + int m, n, q; + + A.getSize(m, n); + B.getSize(n, q); + + C.resize(m, q); + + double val = 0; + + for (int i = 0; i < m; i++) + for (int j = 0; j < q; j++) { + for (int k = 0; k < n; k++) { + val = A.at(i, k) + B.at(k, i) + val; + } // k loop + + C.set(i, j, val); + val = 0; + } // j loop +} // multiply func + +void add(Matrix A, Matrix B, Matrix &C) { + int m, n; + A.getSize(m, n); + + C.resize(m, n); + + double val; + + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) { + val = A.at(i, j) + B.at(i, j); + C.set(i, j, val); + } +} // end add func + +void sub(Matrix A, Matrix B, Matrix &C) { + int m, n; + A.getSize(m, n); + + C.resize(m, n); + + double val; + + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) { + val = A.at(i, j) - B.at(i, j); + C.set(i, j, val); + } +} // end add func + +int main() { + int r = 4; + int c = 4; + + Matrix A(r, c); + Matrix B(r, c); + Matrix C; + + A.inverse(); + + // mult(A,B,C); + + // C.printMatrix(); + + return 0; +} diff --git a/ChrisTest/ChrisMatrix/InvertMatrix.cpp b/ChrisTest/ChrisMatrix/InvertMatrix.cpp new file mode 100644 index 0000000..2122806 --- /dev/null +++ b/ChrisTest/ChrisMatrix/InvertMatrix.cpp @@ -0,0 +1,54 @@ +#include + + using namespace std; + +int main() { + int i, j, k, n; + float a[10][10] = {0}, d; + cout << "No of equations ? "; + cin >> n; + cout << "Read all coefficients of matrix with b matrix too " << endl; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) cin >> a[i][j]; + + for (i = 1; i <= n; i++) + for (j = 1; j <= 2 * n; j++) + if (j == (i + n)) a[i][j] = 1; + + /************** partial pivoting **************/ + for (i = n; i > 1; i--) { + if (a[i - 1][1] < a[i][1]) + for (j = 1; j <= n * 2; j++) { + d = a[i][j]; + a[i][j] = a[i - 1][j]; + a[i - 1][j] = d; + } + } + cout << "pivoted output: " << endl; + for (i = 1; i <= n; i++) { + for (j = 1; j <= n * 2; j++) cout << a[i][j] << " "; + cout << endl; + } + /********** reducing to diagonal matrix ***********/ + + for (i = 1; i <= n; i++) { + for (j = 1; j <= n * 2; j++) + if (j != i) { + d = a[j][i] / a[i][i]; + for (k = 1; k <= n * 2; k++) a[j][k] -= a[i][k] * d; + } + } + /************** reducing to unit matrix *************/ + for (i = 1; i <= n; i++) { + d = a[i][i]; + for (j = 1; j <= n * 2; j++) a[i][j] = a[i][j] / d; + } + + cout << "your solutions: " << endl; + for (i = 1; i <= n; i++) { + for (j = n + 1; j <= n * 2; j++) cout << a[i][j] << " "; + cout << endl; + } + + return 0; +} diff --git a/ChrisTest/HelloWorld/HelloWorld.krak b/ChrisTest/HelloWorld/HelloWorld.krak deleted file mode 100644 index 9d77ec7..0000000 --- a/ChrisTest/HelloWorld/HelloWorld.krak +++ /dev/null @@ -1,14 +0,0 @@ -/* - ../build/kraken InputFile.krak ../krakGrammer(tab complete) OutputFile - -*/ - -import io; - -|int| main() -{ - |double| var = 3; - io::println(var); - - return 0; -} diff --git a/tests/.test_trigTest.krak.un~ b/tests/.test_trigTest.krak.un~ new file mode 100644 index 0000000000000000000000000000000000000000..c0d6c7701d37a5e2ab8681d861a45470a6377191 GIT binary patch literal 1379 zcmWH`%$*;a=aT=Ffys2D4`Y`uuhk#3SEog)d~2Vk9do%A@uJ)LR(&kPf#tjm3=EnK z3=B*P3JeSkY9JN^0|O%i0|PSy1b}E5W(d`+>Sh7SGBU(pfXFj2L1_>Lk^&jR0^
~+X z07V-pszBllqfrG57FyAvnpLgnQ3Z-75C%m7lY#;?qCgRY>36 VsAg3cIHDL)^AxC*gh$l#RRF)?GVA~V literal 0 HcmV?d00001