Skip to content

Commit

Permalink
Merge pull request #21 from Propulsive-Landing/matrix-support
Browse files Browse the repository at this point in the history
Matrix support
  • Loading branch information
lps17005 authored Mar 12, 2021
2 parents af9ac8d + b27bfcd commit d132f86
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
115 changes: 115 additions & 0 deletions src/matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <iostream>
#include "matrix.hh"
using namespace std;

//matrix subtraction helper function
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;
}

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

//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];
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;
}

//matrix addition helper function
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;
}

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

//matrix addition function
Matrix mAdd(Matrix A, Matrix 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))
{
values=m_add_helper(A.values, B.values, m_a*n_a);
}
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 subtraction function
Matrix mSub(Matrix A, Matrix 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))
{
values=m_sub_helper(A.values, B.values, m_a*n_a);
}
return Matrix(m_a, n_a, values);
}

void display_matrix(Matrix A)
{
for (int i=0; i<A.rows; i++)
{
for (int j = 0; j < A.columns; j++)
{
cout << A.select(i+1,j+1) << " ";
}
cout << "\n";
}
}
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 d132f86

Please sign in to comment.