Skip to content

Commit

Permalink
add matrix header file
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamSmego committed Mar 7, 2021
1 parent 985cb95 commit b27bfcd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
44 changes: 10 additions & 34 deletions src/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
#include <iostream>
#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];
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -92,14 +79,16 @@ Matrix mAdd(Matrix A, Matrix B)
return Matrix(m_a, n_a, values);
}

//scalar multiplication function
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);
}

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;
Expand All @@ -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;
}
34 changes: 34 additions & 0 deletions src/matrix.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __MATRIX_HH__
#define __MATRIX_HH__

#include <cstdbool>
#include <cstdint>


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__

0 comments on commit b27bfcd

Please sign in to comment.