#ifndef DMATRIX_H_INCLUDED #define DMATRIX_H_INCLUDED //Created on March 5 2011 //Last updated in December 16, 2011 //Update: //June 14 //add two friend function to output dMatrix (overload <<) //December 16, 2011 //add transpose function tr() //Gang Peng //gpeng1@mdanderson.org #include #include template class dMatrix { private: //data Type* data; //row unsigned int m_row; //column unsigned int m_column; public: dMatrix(); dMatrix(unsigned int row, unsigned int column, Type m=0); dMatrix(const dMatrix & dM); virtual ~dMatrix(); unsigned int get_row() const { return m_row;} unsigned int get_column() const { return m_column;} bool setZero(); dMatrix & operator=(const dMatrix & dM); const Type & operator()(unsigned int m, unsigned int n) const; Type & operator()(unsigned int m, unsigned int n); dMatrix tr() const; dMatrix operator * (const dMatrix & ma) const; //sumation of row m Type sum_row(unsigned int m); //sumation of column n Type sum_column(unsigned int n); //sumation of whole matrix Type sumAll(); //output bool output() const; //output template friend std::ostream & operator<< (std::ostream & os, const dMatrix & dM); template friend std::ofstream & operator<<(std::ofstream & of, const dMatrix & dM); }; template dMatrix::dMatrix() { m_row=1; m_column=1; data=new Type[1]; data[0]=0; } template dMatrix::dMatrix(unsigned int row, unsigned int column, Type m) { m_row=row; m_column=column; data=new Type[row*column]; for(unsigned int i=0;i dMatrix::dMatrix(const dMatrix & dM) { m_row=dM.get_row(); m_column=dM.get_column(); data=new Type[m_row*m_column]; for(unsigned int i=0;i dMatrix::~dMatrix() { delete[] data; } template dMatrix & dMatrix::operator=(const dMatrix & dM) { if(this==&dM) { return *this; } delete[] data; m_row=dM.get_row(); m_column=dM.get_column(); data=new Type[m_row*m_column]; for(unsigned int i=0;i const Type & dMatrix::operator()(unsigned int m, unsigned int n) const { return data[m*m_column+n]; } template Type & dMatrix::operator()(unsigned int m, unsigned int n) { return data[m*m_column+n]; } template Type dMatrix::sum_row(unsigned int m) { Type rlt=0; for(unsigned int i=0;i Type dMatrix::sum_column(unsigned int n) { Type rlt=0; for(int i=0;i Type dMatrix::sumAll() { Type rlt=0; for(int i=0;i bool dMatrix::output() const { using namespace std; for(int i=0;i std::ostream & operator<<(std::ostream & os, const dMatrix & dM) { for(unsigned int i=0;i std::ofstream & operator<<(std::ofstream & of, const dMatrix & dM) { for(unsigned int i=0;i bool dMatrix::setZero() { for(int i=0;i dMatrix dMatrix::tr() const { dMatrix rlt(m_column,m_row); for(int i=0;i dMatrix dMatrix::operator *(const dMatrix & ma) const { dMatrix rlt(m_row,ma.get_column(),0); for(int i=0;i