Skip to content

Commit

Permalink
add overload operators to matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamSmego committed Mar 13, 2021
1 parent 897dd55 commit 803859e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
46 changes: 23 additions & 23 deletions main/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,55 @@ float * m_add_helper(float * A, float * B, int elements) {
}

//matrix multiplication
Matrix mMult(Matrix A, Matrix B)
Matrix Matrix::operator*(Matrix B)
{
const int m=A.rows;
const int m=rows;
const int p=B.columns;
const int n=A.columns;
float * values;
if (A.columns==B.rows)
const int n=columns;
float * new_values;
if (columns==B.rows)
{
values=m_mult_helper(A.values, B.values, m, n, p);
new_values=m_mult_helper(values, B.values, m, n, p);
}
return Matrix(m, p, values);
return Matrix(m, p, new_values);
}

//matrix addition function
Matrix mAdd(Matrix A, Matrix B)
Matrix Matrix::operator+(Matrix B)
{
const int m_a=A.rows;
const int m_a=rows;
const int n_b=B.columns;
const int n_a=A.columns;
const int n_a=columns;
const int m_b=B.rows;
float * values;
float * new_values;
if ((m_a==m_b)&&(n_a==n_b))
{
values=m_add_helper(A.values, B.values, m_a*n_a);
new_values=m_add_helper(values, B.values, m_a*n_a);
}
return Matrix(m_a, n_a, values);
return Matrix(m_a, n_a, new_values);
}

//scalar multiplication function
Matrix sMult(Matrix A, float k)
Matrix Matrix :: scale(float k)
{
float * values;
values=s_mult_helper(A.values, k, A.rows*A.columns);
return Matrix(A.rows, A.columns, values);
float * new_values;
new_values=s_mult_helper(values, k, rows*columns);
return Matrix(rows, columns, new_values);
}

//matrix subtraction function
Matrix mSub(Matrix A, Matrix B)
Matrix Matrix::operator-(Matrix B)
{
const int m_a=A.rows;
const int m_a=rows;
const int n_b=B.columns;
const int n_a=A.columns;
const int n_a=columns;
const int m_b=B.rows;
float * values;
float * new_values;
if ((m_a==m_b)&&(n_a==n_b))
{
values=m_sub_helper(A.values, B.values, m_a*n_a);
new_values=m_sub_helper(values, B.values, m_a*n_a);
}
return Matrix(m_a, n_a, values);
return Matrix(m_a, n_a, new_values);
}

void display_matrix(Matrix A)
Expand Down
12 changes: 8 additions & 4 deletions main/matrix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ class Matrix
{
return values[(row-1)*columns+(column-1)];
}
Matrix operator+(Matrix);
Matrix operator-(Matrix);
Matrix scale (float k);
Matrix operator*(Matrix);
};

Matrix mMult(Matrix, Matrix);
Matrix mAdd(Matrix, Matrix);
Matrix sMult(Matrix, float);
Matrix mSub(Matrix, Matrix);
//Matrix mMult(Matrix, Matrix);
//Matrix mAdd(Matrix, Matrix);
//Matrix sMult(Matrix, float);
//Matrix mSub(Matrix, Matrix);
void display_matrix(Matrix);

#endif // __MODING_HH__
8 changes: 7 additions & 1 deletion main/mission_constants.hh
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,34 @@ float A_VALUES [36] = {0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0}; //dynamics matrix
Matrix A=Matrix(6, 6, A_VALUES);

float B_VALUES [12] = {0, 0,
0, 0,
0, 0,
0, 0,
0, 0,
0, 0}; //input matrix
Matrix B=Matrix(6, 2, B_VALUES);

float K_VALUES [12] = {0,0,0,0,0,0,
0,0,0,0,0,0}; //controller gain
Matrix K=Matrix(2, 6, K_VALUES);

float C_VALUES [24] = {0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0}; //sensor matrix
Matrix C=Matrix(4, 6, C_VALUES);

float L_VALUES [24] = {0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}; //kalman gain
Matrix L=Matrix(6, 4, L_VALUES);


#endif // __MISSION_CONSTANTS_HH__

0 comments on commit 803859e

Please sign in to comment.