Add constructors and operator== to Matrix<>

This commit is contained in:
Eryn Wells 2014-08-08 20:44:31 -07:00
parent a9c8d84ac2
commit c5b9d73a96
2 changed files with 97 additions and 10 deletions

View file

@ -9,8 +9,8 @@
#ifndef __BASICS_BASICS_HH__ #ifndef __BASICS_BASICS_HH__
#define __BASICS_BASICS_HH__ #define __BASICS_BASICS_HH__
#include "matrix.hh" #include "basics/matrix.hh"
#include "types.hh" #include "basics/types.hh"
namespace charles { namespace charles {

View file

@ -18,27 +18,42 @@ namespace basics {
/** /**
* A generic, templated Matrix class taking two template parameters. `N` is the * A generic, templated Matrix class taking two template parameters. `N` is the
* number of rows. `M` is the number of columns. * number of rows. `M` is the number of columns. If `M` is not specified, the
* matrix will be square.
*/ */
template<uint N, uint M> template<uint N, uint M = N>
struct Matrix struct Matrix
{ {
/** Construct an N x M matrix of zeros. */ /** Construct an N x M matrix of zeros. */
static Matrix<N,M> Zero(); static Matrix<N,M> Zero();
/** Construct an N x M identity matrix. */ /**
* Construct an N x M identity matrix. Identity matrices are always square.
* It is a (compile time) error to call Identity on a Matrix class where
* N != M.
*/
static Matrix<N,M> Identity(); static Matrix<N,M> Identity();
Matrix();
Matrix(const Double data[N*M]);
Matrix(const Matrix<N,M>& rhs);
Matrix<N,M>& operator=(const Matrix<N,M>& rhs);
bool operator==(const Matrix<N,M>& rhs);
bool operator!=(const Matrix<N,M>& rhs);
/** Value accessor. Get the ij'th item. */ /** Value accessor. Get the ij'th item. */
Double& operator(uint i, uint j); Double& operator(uint i, uint j);
/** Scalar multiplication */ /** Scalar multiplication */
Matrix<N,M> operator*(const Double& lhs) const; Matrix<N,M> operator*(const Double& rhs) const;
/** Matrix multiplication */ /** Matrix multiplication */
template<uint P> template<uint P>
Matrix<N,P> operator*(Matrix<M,P> lhs) const; Matrix<N,P> operator*(Matrix<M,P> rhs) const;
/** Get the underlying C array */
const Double* CArray() const; const Double* CArray() const;
private: private:
@ -47,11 +62,83 @@ private:
}; };
typedef Matrix<4> Matrix4;
/** Scalar multiplication, scalar factor on the left. */ /** Scalar multiplication, scalar factor on the left. */
template<uint N, uint M> template<uint N, uint M>
Matrix<N,M> operator*(const Double& lhs, const Matrix<N,M>& rhs); Matrix<N,M> operator*(const Double& lhs, const Matrix<N,M>& rhs);
/*
* charles::basics::Matrix<>::Matrix --
*/
template<uint N, uint M>
Matrix<N,M>::Matrix()
: mData()
{ }
/*
* charles::basics::Matrix<>::Matrix --
*/
template<uint N, uint M>
Matrix<N,M>::Matrix(const Double data[N*M])
{
memcpy(mData, data, sizeof(Double) * N * M);
}
/*
* charles::basics::Matrix<>::Matrix --
*/
template<uint N, uint M>
Matrix<N,M>::Matrix(const Matrix<N,M>& rhs)
: Matrix(rhs.mData)
{ }
/*
* charles::basics::Matrix<>::operator= --
*/
template<uint N, uint M>
Matrix<N,M>&
Matrix<N,M>::operator=(const Matrix<N,M>& rhs)
{
memcpy(mData, rhs.mData, sizeof(Double) * N * M);
return *this;
}
/*
* charles::basics::Matrix<>::operator== --
*/
template<uint N, uint M>
bool
Matrix<N,M>::operator==(const Matrix<N,M>& rhs)
const
{
for (int i = 0; i < N*M; i++) {
if (mData[i] != rhs.mData[i]) {
return false;
}
}
return true;
}
/*
* charles::basics::Matrix<>::operator!= --
*/
template<uint N, uint M>
bool
Matrix<N,M>::operator!=(const Matrix<N,M>& rhs)
const
{
return !(*this == rhs);
}
/* /*
* charles::basics::Matrix<>::Zero -- * charles::basics::Matrix<>::Zero --
*/ */
@ -103,12 +190,12 @@ Matrix<N,M>::operator()(uint i, uint j)
*/ */
template<uint N, uint M> template<uint N, uint M>
Matrix<N,M> Matrix<N,M>
Matrix<N,M>::operator*(const Double& lhs) Matrix<N,M>::operator*(const Double& rhs)
const const
{ {
Matrix<N,M> result; Matrix<N,M> result;
for (int i = 0; i < N*M; i++) { for (int i = 0; i < N*M; i++) {
result.mData = mData[i] * lhs; result.mData = mData[i] * rhs;
} }
return result; return result;
} }
@ -120,7 +207,7 @@ Matrix<N,M>::operator*(const Double& lhs)
template<uint N, uint M> template<uint N, uint M>
template<uint P> template<uint P>
Matrix<N,P> Matrix<N,P>
Matrix<N,M>::operator*(Matrix<M,P> lhs) Matrix<N,M>::operator*(Matrix<M,P> rhs)
const const
{ {
Matrix<N,P> result; Matrix<N,P> result;