#include <vector>
#include <iostream>
#include <cmath>
#include "matrix.h"
using namespace std;
template <class T>
Matrix<T>::Matrix() : nRows(0), nCols(0) {}
template <class T>
Matrix<T>::Matrix(int rows, int cols)
{
this->nRows = rows;
this->nCols = cols;
for (int i=0; i < nRows; i++)
{
vector<T> col(nCols);
m.push_back(col);
}
}
template <class T>
Matrix<T>::Matrix(const Matrix<T> &M)
{
this->nRows = M.nRows;
this->nCols = M.nCols;
this->m = M.m;
}
template <class T>
Matrix<T> &Matrix<T>::operator= (const Matrix<T> &M)
{
this->nRows = M.nRows;
this->nCols = M.nCols;
this->m = M.m;
return *this;
}
template <class T>
vector<T> &Matrix<T>::operator[] (int row)
{
return m[row];
}
template <class T>
void Matrix<T>::Dump(void)
{
cout << "\t[\n";
for (int row=0; row<nRows; row++)
{
for (int col=0; col<nCols; col++)
cout << "\t\t" << m[row][col] << " ";
cout << "\n";
}
cout << "\t]\n";
}
template <class T>
int Matrix<T>::Rows(void)
{
return nRows;
}
template <class T>
int Matrix<T>::Cols(void)
{
return nCols;
}
template <class T>
vector<T> Matrix<T>::Row(int num)
{
vector<T> Result(this->nCols);
for(int x=0; x<this->nCols; x++)
Result[x] = (this->m[num][x]);
return Result;
}
template <class T>
vector<T> Matrix<T>::Col(int num)
{
vector<T> Result(nRows);
for(int y=0; y<this->nRows; y++)
Result[y] = (this->m[y][num]);
return Result;
}
template <class T>
Matrix<T> Matrix<T>::Transpose(void)
{
Matrix<T> M_Trans(this->nCols, this->nRows);
for(int row=0; row<M_Trans.Rows(); row++)
for(int mrow=0; mrow<this->nRows; mrow++)
M_Trans[row][mrow] = m[mrow][row];
return M_Trans;
}
template <class T>
Matrix<T> Matrix<T>::Scale(T factor)
{
Matrix Result(this->nRows, this->nCols);
for(int row=0; row<Result.Rows(); row++)
for(int col=0; col<Result.Cols(); col++)
Result[row][col] = factor*(this->m[row][col]);
return Result;
}
template <class T>
Matrix<T> Matrix<T>::operator+ (Matrix<T> &M)
{
Matrix<T> M2 = *this;
Matrix<T> Result(M2.Rows(), M2.Cols());
for(int row=0; row<M2.Rows(); row++)
for(int col=0; col<M2.Cols(); col++)
Result[row][col] = M2[row][col] + M[row][col];
return Result;
}
template <class T>
Matrix<T> Matrix<T>::operator- (Matrix<T> &M)
{
Matrix<T> M2 = *this;
Matrix<T> Result(M2.Rows(), M2.Cols());
for(int row=0; row<M2.Rows(); row++)
for(int col=0; col<M2.Cols(); col++)
Result[row][col] = M2[row][col] - M[row][col];
return Result;
}
template <class T>
Matrix<T> Matrix<T>::operator* (Matrix<T> &M)
{
Matrix<T> M2 = *this;
Matrix<T> Result(M2.Rows(),M.Cols());
T Temp;
for(int row=0; row<Result.Rows(); row++)
for(int col=0; col<Result.Cols(); col++)
{
Temp = 0;
vector<T> row_vector = M2.Row(row);
vector<T> col_vector = M.Col(col);
for(int x=0; x<row_vector.size(); x++)
Temp += (row_vector[x]*col_vector[x]);
Result[row][col] = Temp;
}
return Result;
}
template <class T>
T Matrix<T>::CofactorSign(int row, int col)
{
return(pow(-1.0,row) * pow(-1.0,col));
}
template <class T>
Matrix<T> Matrix<T>::Minor(int row, int col)
{
Matrix<T> result((this->Rows()-1), (this->Cols()-1));
int xpt=0, ypt=0;
for(int y=0; y<(this->Rows()); y++)
{
if(y != row)
{
for(int x=0; x<(this->Cols()); x++)
{
if(x != col)
{
result[ypt][xpt] = this->m[y][x];
xpt++;
}
}
ypt++;
}
xpt=0;
}
return result;
}
template <class T>
T Matrix<T>::Det(void)
{
T result = 0; Matrix<T> Temp;
if(this->Rows() == 2)
{
result = (this->m[0][0]*this->m[1][1] - this->m[0][1]*this->m[1][0]);
}
else
{
for(int col=0; col < this->Cols(); col++)
{
Temp = this->Minor(0, col);
result += (Temp.Det() * this->m[0][col] * this->CofactorSign(0, col));
}
}
return result;
}
//Bug fix for G++ to properly instantiate templates
//see http://lists.debian.org/debian-user/1997/debian-user-199709/msg00102.html
template class Matrix<int>;
template class Matrix<double>;
template class Matrix<bool>;
syntax highlighted by Code2HTML, v. 0.9.1