Here we go, I quite a bit of stuff and I still need to work on determinants and more other stuff (making it possible to add vectors of numbers, adding scalar * matrix, adding matrix transpose...). I'm not completely satisfied with the design yet but it's not that ugly either.
Here it is:
Code:
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include <iostream>
#include <vector>
#include <memory>
template < class T >
class Matrix
{
public:
typedef std::auto_ptr< Matrix< T > > MATRIX_PTR;
Matrix(unsigned int n_cols = 0, unsigned int n_rows = 0);
Matrix(const Matrix& m);
void Output( )
{
for(int j = 0; j < Rows; j++)
{
for(int i = 0; i < Cols; i++)
{
std::cout << Data[j][i];
}
std::cout << std::endl;
}
}
void SetElement(unsigned int col, unsigned int row, T val);
void InterchangeRows(unsigned int row_1, unsigned int row_2);
void InterchangeCols(unsigned int col_1, unsigned int col_2);
bool LowerTriangle( ) const;
bool UpperTriangle( ) const;
bool SquareMatrix( ) const;
T Determinant( );
static MATRIX_PTR IdentityMatrix(unsigned int size);
static MATRIX_PTR NullMatrix(unsigned int size);
MATRIX_PTR Minor(unsigned int col, unsigned int row);
Matrix< T >& operator = (const Matrix< T >&);
Matrix< T >& operator += (const Matrix< T >&);
Matrix< T >& operator -= (const Matrix< T >&);
Matrix< T > operator * (const Matrix< T >&) const;
Matrix< T > operator - (const Matrix< T >&) const;
Matrix< T > operator + (const Matrix< T >&) const;
private:
unsigned int Rows, Cols;
std::vector< std::vector< T > > Data;
};
template < class T >
Matrix< T >::Matrix< T >(unsigned int n_cols, unsigned int n_rows)
: Cols(n_cols), Rows(n_rows)
{
if(Cols > 0 && Rows > 0)
{
Data.reserve(Rows);
for(int i = 0; i < Rows; i++)
{
Data.push_back(std::vector< T >( ));
Data[i].reserve(Cols);
Data[i].insert(Data[i].begin(), Cols, 0);
}
}
else if(Cols < 0 && Rows < 0)
{
Cols = Rows = 0;
}
}
template < class T >
Matrix< T >::Matrix< T >(const Matrix& m)
{
*this = m;
}
template < class T >
void Matrix< T >::SetElement(unsigned int col, unsigned int row, T val)
{
if(col >= 0 && row >= 0 && col < Cols && row < Rows)
{
Data[row][col] = val;
}
}
template < class T >
void Matrix< T >::InterchangeRows(unsigned int row_1, unsigned int row_2)
{
if(row_1 != row_2 && (row_1 >= 0 && row_2 >= 0))
{
Data[row_1].swap(Data[row_2]);
}
}
template < class T>
void Matrix< T >::InterchangeCols(unsigned int col_1, unsigned int col_2)
{
// Parentheses for easier reading only
// Make sure both columns are different and range in [0, Cols[
if((col_1 != col_2) && (col_1 >= 0) && (col_2 >= 0) && (col_1 < Cols) && (col_2 < Cols))
{
for(int j = 0; j < Rows; j++)
{
T tmp = Data[j][col_1];
Data[j][col_1] = Data[j][col_2];
Data[j][col_2] = tmp;
}
}
}
template < class T >
bool Matrix< T >::LowerTriangle( ) const
{
// Only square matrixes bigger than 0,0
if(SquareMatrix() && (Cols != 0 && Rows != 0))
{
for(int j = 0; j < Rows; j++)
{
for(int i = 0; i < Cols; i++)
{
if(i > j && Data[j][i] != 0)
{
return false;
}
}
}
}
else
{
return false;
}
return true;
}
template < class T >
bool Matrix< T >::UpperTriangle( ) const
{
// Only square matrixes bigger than 0,0
if(SquareMatrix() && (Cols != 0 && Rows != 0))
{
for(int j = 0; j < Rows; j++)
{
for(int i = 0; i < Cols; i++)
{
if(i < j && Data[j][i] != 0)
{
return false;
}
}
}
}
else
{
return false;
}
return true;
}
template < class T >
bool Matrix< T >::SquareMatrix( ) const
{
return (Rows == Cols);
}
template < class T >
T Matrix< T >::Determinant( )
{
if(SquareMatrix( ))
{
if(Cols == 0 && Rows == 0)
{
return static_cast< T >(0);
}
if(Cols == 1 && Rows == 1)
{
return Data[0][0];
}
if(UpperTriangle( ) || LowerTriangle( ))
{
int total = 1;
for(int i = 0; i < Cols; i++)
{
total *= Data[i][i];
}
return total;
}
if(Cols == 2 && Rows == 2)
{
return (Data[0][0] * Data[1][1] - Data[0][1] * Data[1][0]);
}
if(Cols == 3 && Rows == 3)
{
return (Data[0][0] * Data[1][1] * Data[2][2] +
Data[0][2] * Data[1][0] * Data[2][1] +
Data[0][1] * Data[1][2] * Data[2][0] -
Data[0][2] * Data[1][1] * Data[2][0] -
Data[0][0] * Data[1][2] * Data[2][1] -
Data[0][1] * Data[1][0] * Data[2][2]);
}
// Let's use the M = LU decomposition method for faster processing
else
{
}
}
}
template < class T >
typename Matrix< T >::MATRIX_PTR Matrix< T >::IdentityMatrix(unsigned int size)
{
MATRIX_PTR tmp(new Matrix< T >(size, size));
for(int j = 0; j < tmp->Rows; j++)
{
for(int i = 0; i < tmp->Cols; i++)
{
tmp->Data[j][i] = i == j ? 1 : 0;
}
}
return tmp;
}
template < class T >
typename Matrix< T >::MATRIX_PTR Matrix< T >::NullMatrix(unsigned int size)
{
return MATRIX_PTR(new Matrix< T >(size, size));
}
template < class T >
typename Matrix< T >::MATRIX_PTR Matrix< T >::Minor(unsigned int col, unsigned int row)
{
static MATRIX_PTR tmp;
if(SquareMatrix( ) && (Rows != 0 && Cols != 0))
{
tmp.reset(new Matrix< T >);
int y = 0;
// Move horizontally then down in the matrix
for(int j = 0; j < Rows; j++)
{
if(j != row)
{
// We're on a new line...
tmp->Data.push_back(std::vector< T >( ));
for(int i = 0; i < Cols; i++)
{
if(i != col)
{
(tmp->Data[y]).push_back(Data[j][i]);
}
}
y++;
}
}
tmp->Cols = tmp->Data[0].size();
tmp->Rows = tmp->Data.size();
}
return tmp;
}
template< class T >
Matrix< T >& Matrix< T >::operator = (const Matrix< T >& m)
{
Rows = m.Rows;
Cols = m.Cols;
Data.assign(m.Data.begin(), m.Data.end());
return *this;
}
template< class T >
Matrix< T >& Matrix< T >::operator += (const Matrix< T >& m)
{
*this = *this + m;
return *this;
}
template< class T >
Matrix< T >& Matrix< T >::operator -= (const Matrix< T >& m)
{
*this = *this - m;
return *this;
}
template< class T >
Matrix< T > Matrix< T >::operator * (const Matrix< T >& m) const
{
// A Matrix A(a, b) and a matrix B(c, d) yield a matric C(b, c) when multiplied
Matrix< T > tmp(Rows, m.Cols);
if(Cols == m.Rows && Rows == m.Cols && Rows > 0 && Cols > 0)
{
for(int j_result = 0; j_result < Rows; j_result++)
{
for(int i_result = 0; i_result < m.Cols; i_result++)
{
int sum = 0;
for(int j = 0, i = 0; j < m.Rows && i < Cols; j++, i++)
{
sum += Data[j_result][i] * m.Data[j][i_result];
}
tmp.Data[j_result][i_result] = sum;
}
}
}
return tmp;
}
template< class T >
Matrix< T > Matrix< T >::operator - (const Matrix< T >& m) const
{
Matrix< T > tmp(Cols, Rows);
if(Rows == m.Rows && Cols == m.Cols && Rows > 0 && Cols > 0)
{
for(int j = 0; j < Rows; j++)
{
for(int i = 0; i < Cols; i++)
{
tmp.Data[j][i] = Data[j][i] - m.Data[j][i];
}
}
}
return tmp;
}
template< class T >
Matrix< T > Matrix< T >::operator + (const Matrix< T >& m) const
{
Matrix< T > tmp(Cols, Rows);
if(Rows == m.Rows && Cols == m.Cols && Rows > 0 && Cols > 0)
{
for(int j = 0; j < Rows; j++)
{
for(int i = 0; i < Cols; i++)
{
tmp.Data[j][i] = Data[j][i] + m.Data[j][i];
}
}
}
return tmp;
}
#endif // MATRIX_H_INCLUDED