00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef MATRIX_H
00019 #define MATRIX_H
00020
00021 #include "vector.h"
00022
00023 namespace colarray {
00024
00025
00026 template <typename T> class Matrix;
00027 template <typename T>
00028 std::ostream& operator << (std::ostream& , const Matrix<T> &);
00029
00033 template <typename T>
00034 class Matrix {
00035 public:
00036 int_t _m, _n, _ld;
00037 T* _v;
00038 protected:
00039 T* _data;
00040 int* _refCount;
00041
00042 public:
00043
00044
00047 Matrix() : _m(0), _n(0), _ld(0), _v(NULL), _data(NULL), _refCount(new int(1))
00048 {}
00049
00053 Matrix(int_t m, int_t n);
00054
00057 inline Matrix(const Matrix&);
00058
00062 inline Matrix(const Matrix<T>& A,
00063 int_t i_start, int_t i_end,
00064 int_t j_start, int_t j_end);
00065
00066
00067 ~Matrix();
00068
00069
00070 inline const T& operator () (int_t, int_t) const;
00071 inline T& operator () (int_t, int_t);
00072
00073
00074 inline Matrix& operator = (const Matrix&);
00075 inline T operator = (const T);
00078 inline Matrix& operator *= (T val);
00079
00080
00081 void fill(const T v) {
00082 for(int_t j = 0; j < _n; j ++)
00083 for (int_t i = 0; i < _m; i ++)
00084 this->operator()(i,j) = v;
00085 }
00086 void fill(const T v1, const T v2) {
00087 for(int_t j = 0; j < _n; j ++)
00088 for (int_t i = 0; i < _m; i ++)
00089 this->operator()(i,j) = i == j ? v2 : v1;
00090 }
00091 void inject(const Matrix<T> &A);
00092
00093 friend std::ostream& operator << <T> (std::ostream&, const Matrix<T> &);
00094
00095 friend class RowVector<T>;
00096 friend class ColumnVector<T>;
00097
00099 const int& get_m() const
00100 { return _m; }
00102 const int& get_n() const
00103 { return _n; }
00104 };
00105
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 #include "matrix.h"
00129
00130 namespace colarray {
00131
00132 template <typename T>
00133 inline Matrix<T>::Matrix(int_t rows, int_t cols) :
00134 _m(rows), _n(cols), _ld(rows)
00135 {
00136 _refCount = new int(1);
00137 _v = _data = new T[rows*cols];
00138 #if 0
00139 std::cout << std::showbase << std::hex << this->v << std::endl << std::dec;
00140 #endif
00141 }
00142
00143 template <typename T>
00144 inline Matrix<T>::Matrix(const Matrix& a) :
00145 _m(a._m), _n(a._n), _ld(a._ld), _v(a._v), _data(a._data), _refCount(a._refCount)
00146 { *_refCount += 1; }
00147
00148 template <typename T>
00149 inline Matrix<T>::Matrix(const Matrix<T>& a,
00150 int_t i_start, int_t i_end,
00151 int_t j_start, int_t j_end) {
00152 _m = i_end - i_start;
00153 _n = j_end - j_start;
00154 _ld = a._ld;
00155 _v = a._v + i_start + j_start * _ld;
00156 _data = a._data;
00157 _refCount = a._refCount;
00158 *_refCount += 1;
00159 }
00160
00161 template <typename T>
00162 inline Matrix<T>::~Matrix() {
00163 *_refCount -= 1;
00164 if (*_refCount < 1) {
00165 delete [] _data;
00166 delete _refCount;
00167 }
00168 }
00169
00170 template <typename T>
00171 inline const T& Matrix<T>::operator () (int_t i, int_t j) const {
00172 return _v[i + j*_ld];
00173 }
00174
00175 template <typename T>
00176 inline T& Matrix<T>::operator () (int_t i, int_t j) {
00177 return _v[i + j*_ld];
00178 }
00179
00180 template <typename T>
00181 inline Matrix<T>& Matrix<T>::operator = (const Matrix<T>& a) {
00182 if (&a != this) {
00183
00184 *_refCount -= 1;
00185 if (*_refCount < 1) {
00186 delete [] _data;
00187 delete _refCount;
00188 }
00189
00190 _m = a._m;
00191 _n = a._n;
00192 _ld = a._ld;
00193 _v = a._v;
00194 _data = a._data;
00195 _refCount = a._refCount;
00196 *_refCount += 1;
00197 }
00198 return *this;
00199 }
00200
00201 template <typename T>
00202 inline T Matrix<T>::operator = (const T val) {
00203 for(int_t j = 0; j < _n; j ++)
00204 for (int_t i = 0; i < _m; i ++)
00205 this->operator()(i,j) = val;
00206 return val;
00207 }
00208
00209 template <typename T>
00210 inline Matrix<T>& Matrix<T>::operator *= (T val) {
00211 for(int_t j = 0; j < _n; ++ j)
00212 for (int_t i = 0; i < _m; ++ i)
00213 this->operator()(i,j) *= val;
00214 return *this;
00215 }
00216
00217 template <typename T>
00218 void Matrix<T>::inject(const Matrix<T> &A) {
00219 assert(_m == A._m && _n == A._n);
00220 for (int_t i = 0; i < _m; i ++)
00221 for (int_t j = 0; j < _n; j ++)
00222 this->operator()(i,j) = A(i,j);
00223 }
00224
00225
00226 template <typename T>
00227 std::ostream& operator << (std::ostream& ostr, const Matrix<T>& A)
00228 {
00229 ostr << "Matrix(" << A._m << "," << A._n << ") " ;
00230 ostr << "{";
00231 for (int_t i = 0; i < A._m; i ++) {
00232 if (i != 0)
00233 ostr << ", ";
00234 ostr << "{";
00235 for (int_t j = 0; j < A._n; j ++) {
00236 if (j != 0)
00237 ostr << ", ";
00238 ostr << A(i,j);
00239 }
00240 ostr << "}";
00241 }
00242 ostr << "}";
00243
00244 return ostr;
00245 }
00246
00247 }
00248
00249
00250 #endif