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 numRows() const; // number of rows 00085 00089 int numColumns() const; // number of columns 00090 00105 matrix size() const; // returns vector designating the size 00106 00112 bool operator==(const matrix &other) const; 00113 00119 bool operator!=(const matrix &other) const; 00120 00121 00139 double operator()(int r, int c) const; 00140 00179 double& operator()(int r, int c); 00180 00198 double operator()(int k) const; 00199 00226 double& operator()(int k); 00227 00248 matrix operator-() const; // negate all entries 00249 00272 matrix operator~() const; // use ~M for transpose (M' in matlab) 00273 00274 // addition 00307 matrix operator+(const matrix& other) const; // produce sum of two matrices 00308 00335 matrix& operator+=(const matrix& other); // add other matrix to this (mutate) 00336 00356 matrix operator+(double scalar) const; // add scalar to each element 00357 00376 matrix& operator+=(double scalar); // add scalar to each element (mutate) 00377 00378 // subtraction 00411 matrix operator-(const matrix& other) const; // produce difference of two matrices 00412 00439 matrix& operator-=(const matrix& other); // subtract other matrix from this (mutate) 00440 00460 matrix operator-(double scalar) const; // subtract scalar from each element 00461 00480 matrix& operator-=(double scalar); // subract scalar to each element (mutate) 00481 00482 // multiplicaiton 00515 matrix operator*(const matrix& other) const; 00516 00549 matrix& operator*=(const matrix& other); 00550 00570 matrix operator*(double scalar) const; // multiply each element by scalar 00571 00590 matrix& operator*=(double scalar); 00591 }; 00592 00593 // Support for commutative operations with scalar 00599 matrix operator+(double scalar, const matrix& m); 00600 00606 matrix operator*(double scalar, const matrix& m); 00607 00608 // Support for extracting to output stream 00612 std::ostream& operator<<(std::ostream& out, const matrix& m); 00613 00614 } // end of csci146 namespace 00615 00616 00617 #endif