#ifndef MATRIX_H #define MATRIX_H /////////////////////////////////////////////////////////////// // matrix.h - declares an nxm Dimensional matrix // // Exam2:Q10 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 05 // /////////////////////////////////////////////////////////////// #include #include // namespace Exam2 { ///////////////////////////////////////////////////// // This is the required part of the answer template class matrix { public: matrix(int m, int n, const T& t = T(0)); // matrix(const matrix& m); // correct compiler generated // matrix& operator=(const matrix& m); // " " " T& operator()(int n, int m); // m(1,2) = t; T operator()(int n, int m) const; // t = m(3,1); matrix& operator+=(const matrix& m); // m2 += m1; matrix& operator-=(const matrix& m); // m2 -= m1; matrix& operator*=(const T &t); // m1 *= t; int rows() const { return data.size(); } int cols() const { return data[0].size(); } private: std::vector< std::vector > data; }; /////////////////////////////////////////////////////// // global operators template matrix operator+(const matrix& m1, const matrix& m2); // m3 = m1 + m2; template matrix operator-(const matrix& m1, const matrix& m2); // m3 = m1 - m2; template matrix operator*(const matrix& m1, const matrix& m2); // m3 = m1 * m2; template matrix operator*(const matrix& m, const T& t); // m2 = m1*t; template matrix operator*(const T& t, const matrix& m); // m2 = t*m1; // end of required part of answer /////////////////////////////////////////////////////// //----< size validation >------------------------------------ template void checkSize(const matrix& m1, const matrix& m2) { if(m1.rows() != m2.rows() || m1.cols() != m2.cols()) throw exception("incompatible matrix sizes"); } //---< construct vector with n elements >-------------------- template matrix::matrix(int m, int n, const T& t) { std::vector row; for(int i=0; i--------------------------------- template matrix& matrix::operator+=(const matrix& m) { checkSize(*this,m); for(int i=0; i--------------------------- template matrix operator+(const matrix& m1, const matrix& m2) { checkSize(m1,m2); matrix temp = m1; return (temp += m2); } // //----< subtractive assignment >----------------------------- template matrix& matrix::operator-=(const matrix& m) { checkSize(*this,m); for(int i=0; i------------------------ template matrix operator-(const matrix& m1, const matrix& m2) { checkSize(m1,m2); matrix temp = m1; return (temp -= m2); } //----< scalar multiplicative assignment >------------------- template matrix& matrix::operator*=(const T &t) { for(int i=0; i-------------- template matrix operator*(const T &t, const matrix& m) { matrix temp = m; temp *= t; return temp; } //----< global scalar multiplication operator >-------------- template matrix operator*(const matrix& m, const T &t) { matrix temp = m; temp *= t; return temp; } //----< global multiplication operator >--------------------- template matrix operator*(const matrix& m1, const matrix& m2) { if(m1.cols() != m2.rows()) throw exception("matrices not comformable in multiplication"); matrix temp(m1.rows(),m2.cols()); for(int i=0; i------------------------------------- template T& matrix::operator()(int m, int n) { if(m<0 || rows()<=m || n<0 || cols()<=n) throw exception("index out of range in operator()"); return data[m][n]; } //----< index operator >------------------------------------- template T matrix::operator()(int m, int n) const { if(m<0 || rows()<=m || n<0 || cols()<=n) throw exception("index out of range in operator()"); return data[m][n]; } //----< global display of vector contents >------------------ template void show(const matrix& m, const std::string& name="") { std::cout << "\n "; if(name.size() != 0) std::cout << "\n " << name.c_str(); for(int i=0; i