Skip to content

Commit

Permalink
Documented Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
C7C8 committed Dec 27, 2015
1 parent 97b39ac commit 1e4bc4d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
74 changes: 74 additions & 0 deletions lib/hydra/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,66 @@

namespace Hydra
{
/**
* @brief Constructor; sets dimensions of matrix.
* @param x X size of matrix.
* @param y Y size of matrix.
*/
Matrix::Matrix(unsigned short int x, unsigned short int y)
{
setSize(x, y);
}

/**
* @return X size of the matrix
*/
unsigned short int Matrix::getXSize() const
{
return xSize;
}

/**
* @return Y size of the matrix.
* @note Avoid EMP usage when using this class.
*/
unsigned short int Matrix::getYSize() const
{
return ySize;
}

/**
* @brief Gets the value at a point in the matrix.
* @param x X position of value.
* @param y Y position of value.
* @return Value at point.
* @note The Y axis is inverted and both X and Y are zero ordered.
*/
double Matrix::getValue(unsigned short int x, unsigned short int y) const
{
if (x >= xSize || y >= ySize)
return 0;
return (mat[x])[y]; //Does this work?
}

/**
* @brief Sets the value at a point in the matrix.
* @param value The new value.
* @param x X position of value.
* @param y Y position of value.
* @note The Y axis is inverted and both X and Y are zero-ordered.
*/
void Matrix::setValue(double value, unsigned short int x, unsigned short int y)
{
if (x >= xSize || y >= ySize)
return;
(mat[x])[y] = value;
}

/**
* @brief Changes the size of this matrix, setting all elements to zero in the process.
* @param newXSize The new X size of the matrix.
* @param newYSize The new Y size of the matrix.
*/
void Matrix::setSize(unsigned short int newXSize, unsigned short int newYSize)
{
if (newXSize < 1 || newYSize < 1)
Expand All @@ -45,6 +81,12 @@ namespace Hydra
mat.push_back(vec);
}
}

/**
* @brief Standard matrix addition.
* @param matr Second matrix of same dimensions as this matrix.
* @return Resultant matrix.
*/
Matrix Matrix::operator+(const Matrix &matr)
{
Matrix result;
Expand All @@ -62,6 +104,12 @@ namespace Hydra
}
return result;
}

/**
* @brief Standard matrix subtraction.
* @param matr Second matrix of same dimensions as this matrix.
* @return Resultant matrix.
*/
Matrix Matrix::operator-(const Matrix &matr)
{
Matrix result;
Expand All @@ -79,6 +127,12 @@ namespace Hydra
}
return result;
}

/**
* @brief Standard matrix multiplication.
* @param matr Second matrix with same Y size as this matrix's X size.
* @return Resultant matrix with X of the second matrix and Y of the first matrix.
*/
Matrix Matrix::operator*(const Matrix& matr)
{
//Note: this algorithm assumes that this matrix is the matrix on the LEFT.
Expand All @@ -103,6 +157,12 @@ namespace Hydra

return result;
}

/**
* @brief Standard scalar multiplication.
* @param num Scalar.
* @return Resultant matrix with same dimensions as this matrix.
*/
Matrix Matrix::operator*(const double num)
{
Matrix result;
Expand All @@ -118,4 +178,18 @@ namespace Hydra
}
return result;
}

/**
* @brief Debugging overload for output to console.
*/
ostream& operator<<(ostream& output, Matrix& matr)
{
for (int iY = 0; iY < matr.getYSize(); iY++)
{
for (int iX = 0; iX < matr.getXSize(); iX++)
output << matr.getValue(iX, iY) << ", ";
output << endl;
}
return output;
}
}
22 changes: 13 additions & 9 deletions lib/hydra/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@

#include <math.h>
#include <vector>
using std::vector;
#include <iostream>
#include "mathdefs.h"
using namespace std; //Ugh

namespace Hydra
{
/** \brief A generic class for matrix math. All matrix slots are zero-ordered. Note that the y axis is inverted.*/
/** @brief A class for matrix math.*/
class Matrix
{
public:
Matrix(unsigned short int x = 2, unsigned short int y = 2);

Matrix operator+(const Matrix &matr); //!< Standard addition.
Matrix operator-(const Matrix &matr); //!< Standard subtraction.
Matrix operator*(const Matrix &matr); //!< Standard multiplication.
Matrix operator*(const double num); //!< Scalar multiplication.
Matrix operator+(const Matrix &matr);
Matrix operator-(const Matrix &matr);
Matrix operator*(const Matrix &matr);
Matrix operator*(const double num);

void setValue(double value, unsigned short int x = 0, unsigned short int y = 0);
void setSize(unsigned short int newXSize = 2, unsigned short int newYSize = 2); //!< Sets the matrix to the designated size, completely wiping it in the process.
void setValue(double value, unsigned short int x, unsigned short int y);
void setSize(unsigned short int newXSize, unsigned short int newYSize);

double getValue(unsigned short int x = 0, unsigned short int y = 0) const;
double getValue(unsigned short int x, unsigned short int y) const;
unsigned short int getXSize() const;
unsigned short int getYSize() const;
protected:
unsigned short int xSize;
unsigned short int ySize;
vector<vector<double>> mat;
};

ostream& operator<<(ostream& output, Matrix& matr);
}

0 comments on commit 1e4bc4d

Please sign in to comment.