00001 #ifndef MATRIX_H 00002 #define MATRIX_H 00003 00004 #include <iostream> 00005 #include <vector> 00006 00037 namespace csci146 { 00038 00050 class matrix { 00051 private: 00053 int _nr; 00054 00056 int _nc; 00057 00059 std::vector<double> _data; 00060 00061 public: 00067 matrix(); 00068 00079 matrix(int numberRows, int numberColumns, double value=0); 00080 00084 int numColumns() const; // number of columns 00085 00089 int numRows() const; // number of rows 00090 00105 matrix size() const; // returns vector designating the size 00106 00115 void reshape(int r, int c); 00116 00162 matrix diag() const; 00163 00186 matrix transpose() const; // equivalent to M' in matlab 00187 00193 bool operator==(const matrix &other) const; 00194 00200 bool operator!=(const matrix &other) const; 00201 00202 00222 double operator()(int r, int c) const; 00223 00264 double& operator()(int r, int c); 00265 00285 double operator()(int k) const; 00286 00315 double& operator()(int k); 00316 00337 matrix operator-() const; // negate all entries 00338 00339 // addition 00372 matrix operator+(const matrix& other) const; // produce sum of two matrices 00373 00400 matrix& operator+=(const matrix& other); // add other matrix to this (mutate) 00401 00421 matrix operator+(double scalar) const; // add scalar to each element 00422 00441 matrix& operator+=(double scalar); // add scalar to each element (mutate) 00442 00443 // subtraction 00476 matrix operator-(const matrix& other) const; // produce difference of two matrices 00477 00504 matrix& operator-=(const matrix& other); // subtract other matrix from this (mutate) 00505 00525 matrix operator-(double scalar) const; // subtract scalar from each element 00526 00545 matrix& operator-=(double scalar); // subract scalar to each element (mutate) 00546 00547 // multiplicaiton 00578 matrix operator*(const matrix& other) const; 00579 00612 matrix& operator*=(const matrix& other); 00613 00633 matrix operator*(double scalar) const; // multiply each element by scalar 00634 00653 matrix& operator*=(double scalar); 00654 }; 00655 00656 // Support for commutative operations with scalar 00662 matrix operator+(double scalar, const matrix& m); 00663 00669 matrix operator*(double scalar, const matrix& m); 00670 00671 // Support for extracting to output stream 00675 std::ostream& operator<<(std::ostream& out, const matrix& m); 00676 00683 std::istream& operator>>(std::istream& in, matrix& m); 00684 00685 } // end of csci146 namespace 00686 00687 00688 #endif