From a565f0cd0d7e7ea753d2e991205d6861b4b21694 Mon Sep 17 00:00:00 2001 From: LiamSmego Date: Sat, 27 Feb 2021 15:32:26 -0500 Subject: [PATCH 1/3] Create matrix.cpp --- src/matrix.cpp | 148 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/matrix.cpp diff --git a/src/matrix.cpp b/src/matrix.cpp new file mode 100644 index 0000000..d3b0a9e --- /dev/null +++ b/src/matrix.cpp @@ -0,0 +1,148 @@ +#include +using namespace std; + +float ** mMult(float ** A, float ** B) +{ + const int m = sizeof(A) / sizeof(A[0]); + const int n = sizeof(A[0]) / sizeof(A[0][0]); + const int p = sizeof(B[0]) / sizeof(B[0][0]); + if (n==sizeof(B) / sizeof(B[0])) + { + float ** C; + C = new float*[m]; + for (int i = 0; i < m; i++) + { + C[i]=new float[p]; + for (int j = 0; j < p; j++) + { + C[i][j]=0; + for (int k = 0; k < n; k++) + { + C[i][j] += A[i][k] * B[k][j]; + } + } + } + return C; + } + else + { + + } +} + +float ** mScale(float scalar, float ** A) +{ + const int m = sizeof(A) / sizeof(A[0]); + const int n = sizeof(A[0]) / sizeof(A[0][0]); + float ** C; + C = new float*[m]; + for (int i = 0; i < m; i++) + { + C[i]=new float[n]; + for (int j = 0; j < n; j++) + { + C[i][j]=scalar*A[i][j]; + } + } + return C; +} + +float ** mAdd(float ** A, float ** B) +{ + const int m_a = sizeof(A) / sizeof(A[0]); + const int n_a = sizeof(A[0]) / sizeof(A[0][0]); + const int m_b = sizeof(B) / sizeof(B[0]); + const int n_b = sizeof(B[0]) / sizeof(B[0][0]); + if ((n_a==n_b) && (m_a==m_b)) + { + float ** C; + C = new float*[m_a]; + for (int i = 0; i < m_a; i++) + { + C[i]=new float[n_a]; + for (int j = 0; j < n_a; j++) + { + C[i][j]=A[i][j]+B[i][j]; + } + } + return C; + } + else + { + + } +} + +float ** mSubtract(float ** A, float ** B) +{ + const int m_a = sizeof(A) / sizeof(A[0]); + const int n_a = sizeof(A[0]) / sizeof(A[0][0]); + const int m_b = sizeof(B) / sizeof(B[0]); + const int n_b = sizeof(B[0]) / sizeof(B[0][0]); + if ((n_a==n_b) && (m_a==m_b)) + { + float ** C; + C = new float*[m_a]; + for (int i = 0; i < m_a; i++) + { + C[i]=new float[n_a]; + for (int j = 0; j < n_a; j++) + { + C[i][j]=A[i][j]-B[i][j]; + } + } + return C; + } + else + { + + } +} + +float ** create_matrix(float * values, int rows, int columns) +{ + float ** C; + for (int i=0; i Date: Wed, 3 Mar 2021 23:20:54 -0500 Subject: [PATCH 2/3] add abstraction layer to matrix functions from summer --- src/matrix.cpp | 209 +++++++++++++++++++++++-------------------------- 1 file changed, 100 insertions(+), 109 deletions(-) diff --git a/src/matrix.cpp b/src/matrix.cpp index d3b0a9e..e5837e2 100644 --- a/src/matrix.cpp +++ b/src/matrix.cpp @@ -1,125 +1,125 @@ #include using namespace std; -float ** mMult(float ** A, float ** B) +class Matrix { - const int m = sizeof(A) / sizeof(A[0]); - const int n = sizeof(A[0]) / sizeof(A[0][0]); - const int p = sizeof(B[0]) / sizeof(B[0][0]); - if (n==sizeof(B) / sizeof(B[0])) - { - float ** C; - C = new float*[m]; - for (int i = 0; i < m; i++) + public: + int rows; + int columns; + float * values; + + Matrix(int row_number, int column_number, float * mat_values) { - C[i]=new float[p]; - for (int j = 0; j < p; j++) - { - C[i][j]=0; - for (int k = 0; k < n; k++) - { - C[i][j] += A[i][k] * B[k][j]; - } - } + rows=row_number; + columns=column_number; + values=mat_values; } - return C; - } - else - { - } -} - -float ** mScale(float scalar, float ** A) -{ - const int m = sizeof(A) / sizeof(A[0]); - const int n = sizeof(A[0]) / sizeof(A[0][0]); - float ** C; - C = new float*[m]; - for (int i = 0; i < m; i++) - { - C[i]=new float[n]; - for (int j = 0; j < n; j++) + float select(int row, int column) { - C[i][j]=scalar*A[i][j]; + return values[(row-1)*columns+(column-1)]; } - } - return C; +}; + +float * m_sub_helper(float * A, float * B, int elements) { + float * C; + C = new float[elements]; + for (int i = 0; i < elements; i++) { + C[i] = A[i] - B[i]; + } + return C; +} + +float * s_mult_helper(float * A, float rho, int elements) { + float * C; + C = new float[elements]; + for (int i = 0; i < elements; i++) { + C[i] = A[i] * rho; + } + return C; +} + +float * m_mult_helper(float * A, float * B, int m, int n, int p) { + float * C; + C = new float[m*p]; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < p; j++) + { + C[i*p + j] = 0; + for (int k = 0; k < n; k++) + { + C[i*p + j] += A[i*n + k] * B[j + p * k]; + } + } + } + return C; +} + +float * m_add_helper(float * A, float * B, int elements) { + float * C; + C = new float[elements]; + for (int i = 0; i < elements; i++) { + C[i] = A[i] + B[i]; + } + return C; } -float ** mAdd(float ** A, float ** B) +Matrix mMult(Matrix A, Matrix B) { - const int m_a = sizeof(A) / sizeof(A[0]); - const int n_a = sizeof(A[0]) / sizeof(A[0][0]); - const int m_b = sizeof(B) / sizeof(B[0]); - const int n_b = sizeof(B[0]) / sizeof(B[0][0]); - if ((n_a==n_b) && (m_a==m_b)) + const int m=A.rows; + const int p=B.columns; + const int n=A.columns; + float * values; + if (A.columns==B.rows) { - float ** C; - C = new float*[m_a]; - for (int i = 0; i < m_a; i++) - { - C[i]=new float[n_a]; - for (int j = 0; j < n_a; j++) - { - C[i][j]=A[i][j]+B[i][j]; - } - } - return C; - } - else - { - + values=m_mult_helper(A.values, B.values, m, n, p); } + return Matrix(m, p, values); } -float ** mSubtract(float ** A, float ** B) +Matrix mAdd(Matrix A, Matrix B) { - const int m_a = sizeof(A) / sizeof(A[0]); - const int n_a = sizeof(A[0]) / sizeof(A[0][0]); - const int m_b = sizeof(B) / sizeof(B[0]); - const int n_b = sizeof(B[0]) / sizeof(B[0][0]); - if ((n_a==n_b) && (m_a==m_b)) + const int m_a=A.rows; + const int n_b=B.columns; + const int n_a=A.columns; + const int m_b=B.rows; + float * values; + if ((m_a==m_b)&&(n_a==n_b)) { - float ** C; - C = new float*[m_a]; - for (int i = 0; i < m_a; i++) - { - C[i]=new float[n_a]; - for (int j = 0; j < n_a; j++) - { - C[i][j]=A[i][j]-B[i][j]; - } - } - return C; + values=m_add_helper(A.values, B.values, m_a*n_a); } - else - { + return Matrix(m_a, n_a, values); +} - } +Matrix sMult(Matrix A, float k) +{ + float * values; + values=s_mult_helper(A.values, k, A.rows*A.columns); + return Matrix(A.rows, A.columns, values); } -float ** create_matrix(float * values, int rows, int columns) +Matrix mSubtract(Matrix A, Matrix B) { - float ** C; - for (int i=0; i Date: Sat, 6 Mar 2021 19:37:00 -0500 Subject: [PATCH 3/3] add matrix header file --- src/matrix.cpp | 44 ++++++++++---------------------------------- src/matrix.hh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 src/matrix.hh diff --git a/src/matrix.cpp b/src/matrix.cpp index e5837e2..824946e 100644 --- a/src/matrix.cpp +++ b/src/matrix.cpp @@ -1,26 +1,8 @@ #include +#include "matrix.hh" using namespace std; -class Matrix -{ - public: - int rows; - int columns; - float * values; - - Matrix(int row_number, int column_number, float * mat_values) - { - rows=row_number; - columns=column_number; - values=mat_values; - } - - float select(int row, int column) - { - return values[(row-1)*columns+(column-1)]; - } -}; - +//matrix subtraction helper function float * m_sub_helper(float * A, float * B, int elements) { float * C; C = new float[elements]; @@ -30,6 +12,7 @@ float * m_sub_helper(float * A, float * B, int elements) { return C; } +//scalar multiplication helper function float * s_mult_helper(float * A, float rho, int elements) { float * C; C = new float[elements]; @@ -39,6 +22,7 @@ float * s_mult_helper(float * A, float rho, int elements) { return C; } +//matrix multiplication helper function float * m_mult_helper(float * A, float * B, int m, int n, int p) { float * C; C = new float[m*p]; @@ -56,6 +40,7 @@ float * m_mult_helper(float * A, float * B, int m, int n, int p) { return C; } +//matrix addition helper function float * m_add_helper(float * A, float * B, int elements) { float * C; C = new float[elements]; @@ -65,6 +50,7 @@ float * m_add_helper(float * A, float * B, int elements) { return C; } +//matrix multiplication Matrix mMult(Matrix A, Matrix B) { const int m=A.rows; @@ -78,6 +64,7 @@ Matrix mMult(Matrix A, Matrix B) return Matrix(m, p, values); } +//matrix addition function Matrix mAdd(Matrix A, Matrix B) { const int m_a=A.rows; @@ -92,6 +79,7 @@ Matrix mAdd(Matrix A, Matrix B) return Matrix(m_a, n_a, values); } +//scalar multiplication function Matrix sMult(Matrix A, float k) { float * values; @@ -99,7 +87,8 @@ Matrix sMult(Matrix A, float k) return Matrix(A.rows, A.columns, values); } -Matrix mSubtract(Matrix A, Matrix B) +//matrix subtraction function +Matrix mSub(Matrix A, Matrix B) { const int m_a=A.rows; const int n_b=B.columns; @@ -123,17 +112,4 @@ void display_matrix(Matrix A) } cout << "\n"; } -} - -int main() -{ - float d_values [6]={1, 1,1, 1, 1, 1}; - Matrix D=Matrix(2, 3, d_values); - float a_values [6] = {1, 2, 3, 4, 5, 6}; - Matrix A=Matrix(2, 3, a_values); - float b_values [6] = {7, 8, 9, 10, 11, 12}; - Matrix B=Matrix(3, 2, b_values); - Matrix C=mMult(mAdd(A,D), B); - display_matrix(C); - return 0; } \ No newline at end of file diff --git a/src/matrix.hh b/src/matrix.hh new file mode 100644 index 0000000..4b94bae --- /dev/null +++ b/src/matrix.hh @@ -0,0 +1,34 @@ +#ifndef __MATRIX_HH__ +#define __MATRIX_HH__ + +#include +#include + + +class Matrix +{ + public: + int rows; + int columns; + float * values; + + Matrix(int row_number, int column_number, float * mat_values) + { + rows=row_number; + columns=column_number; + values=mat_values; + } + + float select(int row, int column) + { + return values[(row-1)*columns+(column-1)]; + } +}; + +Matrix mMult(Matrix, Matrix); +Matrix mAdd(Matrix, Matrix); +Matrix sMult(Matrix, float); +Matrix mSub(Matrix, Matrix); +void display_matrix(Matrix); + +#endif // __MODING_HH__ \ No newline at end of file