From 985cb95f31e28f25c09f46595060e8698d2012b3 Mon Sep 17 00:00:00 2001 From: LiamSmego Date: Wed, 3 Mar 2021 23:20:54 -0500 Subject: [PATCH] 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