00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __Matrix_h__
00011 #define __Matrix_h__
00012
00013 #include <cassert>
00014 #include <vector>
00015 using std::vector;
00016
00017 #include <iostream>
00018 using std::cout;
00019 using std::cerr;
00020 using std::endl;
00021
00022 #include <fstream>
00023 using std::ofstream;
00024
00025 #include <cstdlib>
00026
00027 #include <assert.h>
00028
00029 template<typename T>
00030 class Matrix {
00031 public:
00032
00033 Matrix();
00034
00035 Matrix(unsigned nrows, unsigned ncols);
00036
00037 class BadSize { };
00038
00039 Matrix(T * pointer, unsigned nrows, unsigned ncols);
00040
00041
00042 ~Matrix();
00043 Matrix(const Matrix<T>& m);
00044 Matrix<T>& operator= (const Matrix<T>& m);
00045
00046
00047 T& operator() (unsigned i, unsigned j);
00048 const T& operator() (unsigned i, unsigned j) const;
00049
00050
00051
00052 unsigned get_nrows() const;
00053 unsigned get_ncols() const;
00054
00055 void print(const std::string & filename);
00056 void print(const std::string & filename,
00057 const vector<float> & xvals, const vector<float> & yvals);
00058
00059 private:
00060 unsigned nrows_, ncols_;
00061 T* data_;
00062 bool delete_flag;
00063 };
00064
00065
00066
00067 template<typename T>
00068 inline unsigned Matrix<T>::get_nrows() const
00069 { return nrows_; }
00070
00071 template<typename T>
00072 inline unsigned Matrix<T>::get_ncols() const
00073 { return ncols_; }
00074
00075 template<typename T>
00076 inline T& Matrix<T>::operator() (unsigned row, unsigned col)
00077 {
00078 if (row >= nrows_ || col >= ncols_)
00079 assert(1==0);
00080
00081 return data_[row*ncols_ + col];
00082 }
00083
00084 template<typename T>
00085 inline const T& Matrix<T>::operator() (unsigned row, unsigned col) const
00086 {
00087 if (row >= nrows_ || col >= ncols_)
00088 assert(1==0);
00089
00090 return data_[row*ncols_ + col];
00091 }
00092
00096 template<typename T>
00097 inline Matrix<T>::Matrix(T * pointer, unsigned nrows, unsigned ncols)
00098 : nrows_ (nrows)
00099 , ncols_ (ncols)
00100 {
00101 data_ = pointer;
00102 delete_flag = false;
00103 }
00104
00105 template<typename T>
00106 inline Matrix<T>::Matrix()
00107 {
00108 nrows_ = 1;
00109 ncols_ = 1;
00110 data_ = new T[nrows_ * ncols_];
00111 delete_flag = true;
00112 }
00113
00114 template<typename T>
00115 inline Matrix<T>::Matrix(unsigned nrows, unsigned ncols)
00116 : nrows_ (nrows)
00117 , ncols_ (ncols)
00118
00119 {
00120 if (nrows == 0 || ncols == 0)
00121 {
00122 assert(1==0);
00123 throw BadSize();
00124 }
00125 data_ = new T[nrows * ncols];
00126 delete_flag = true;
00127 }
00128
00129 template<typename T>
00130 inline Matrix<T>::~Matrix()
00131 {
00132 if (delete_flag)
00133 delete[] data_;
00134 }
00135
00136 template<typename T>
00137 Matrix<T>& Matrix<T>::operator= (const Matrix<T>& m)
00138 {
00139 if (this != &m) {
00140 if (delete_flag)
00141 {
00142
00143 delete [] data_;
00144 data_ = new T[m.get_nrows() * m.get_ncols()];
00145
00146
00147 nrows_ = m.get_nrows();
00148 ncols_ = m.get_ncols();
00149 }
00150 else
00151 {
00152
00153 if (this->get_nrows() != m.get_nrows() ||
00154 this->get_ncols() != m.get_ncols())
00155 {
00156 cout << "Trying to copy a matrix to a matrix of a different size"
00157 << ", constructed from a pointer" << endl
00158 << "exiting" << endl;
00159 exit(-1);
00160 }
00161 }
00162
00163 for (unsigned i = 0; i < m.get_nrows(); ++i)
00164 for (unsigned j = 0; j < m.get_nrows(); ++j)
00165 {
00166 (*this)(i,j) = m(i,j);
00167 }
00168 }
00169 return *this;
00170 }
00171
00173 template<typename T>
00174 void Matrix<T>::print (const std::string & filename)
00175 {
00176 ofstream outdata;
00177 outdata.open(filename.c_str());
00178
00179 if( !outdata ) {
00180 cerr << "Error: file could not be opened" << endl;
00181 exit(1);
00182 }
00183
00184 outdata << "# i j (*this)(j,i)" << endl;
00185 for (unsigned j = 0; j < get_nrows(); ++j)
00186 {
00187 for (unsigned i = 0; i < get_ncols(); ++i){
00188 outdata << i << " " << j << " " << (*this)(j,i) << endl;
00189 }
00190
00191 outdata << endl;
00192 }
00193
00194 outdata.close();
00195 }
00196
00197
00202 template<typename T>
00203 void Matrix<T>::print (const std::string & filename,
00204 const vector<float> & xvals, const vector<float> & yvals)
00205 {
00206 ofstream outdata;
00207 outdata.open(filename.c_str());
00208
00209 if( !outdata ) {
00210 cerr << "Error: file " << filename << " could not be opened" << endl;
00211 exit(1);
00212 }
00213
00214 outdata << "# x y val (*this)(j (y),i (x))" << endl;
00215 for (unsigned j = 0; j < get_nrows(); ++j)
00216 {
00217
00218 for (unsigned i = 0; i < get_ncols(); ++i){
00219 outdata << xvals[i] << "\t" << yvals[j] << "\t" << (*this)(j,i) << endl;
00220 }
00221
00222 outdata << endl;
00223 }
00224
00225 outdata.close();
00226 }
00227
00228
00229 #endif // __Matrix_h__