00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _MV_MATRIX_H_
00034 #define _MV_MATRIX_H_
00035
00036 #include "mvvtp.h"
00037
00038 struct Matrix_
00039 {
00040 enum ref_type { ref = 1 };
00041 };
00042
00043
00044 #include <iostream>
00045 #ifdef MV_MATRIX_BOUNDS_CHECK
00046 # include <assert.h>
00047 #endif
00048
00049
00050 template <class TYPE>
00051 class MV_ColMat
00052 {
00053 private:
00054 MV_Vector<TYPE> v_;
00055 int dim0_;
00056 int dim1_;
00057 int lda_;
00058 int ref_;
00059
00060
00061 public:
00062
00063
00064
00065
00066
00067 MV_ColMat();
00068 MV_ColMat( int, int);
00069
00070
00071 MV_ColMat( int, int, const TYPE&);
00072
00073
00074
00075
00076 MV_ColMat(TYPE*, int m, int n);
00077 MV_ColMat(TYPE*, int m, int n, int lda);
00078
00079
00080
00081
00082 MV_ColMat(TYPE*, int m, int n, Matrix_::ref_type i);
00083 MV_ColMat(TYPE*, int m, int n, int lda,
00084 Matrix_::ref_type i);
00085
00086 MV_ColMat(const MV_ColMat<TYPE>&);
00087 ~MV_ColMat();
00088
00089
00090
00091
00092
00093 inline TYPE& operator()( int, int);
00094 inline const TYPE& operator()( int, int) const;
00095 MV_ColMat<TYPE> operator()(const MV_VecIndex &I, const MV_VecIndex &J) ;
00096 const MV_ColMat<TYPE> operator()(const MV_VecIndex &I, const MV_VecIndex &J) const;
00097 int size(int i) const;
00098 MV_ColMat<TYPE>& newsize( int, int);
00099 int ref() const { return ref_;}
00100
00101
00102
00103
00104
00105 MV_ColMat<TYPE> & operator=(const MV_ColMat<TYPE>&);
00106 MV_ColMat<TYPE> & operator=(const TYPE&);
00107
00108
00109 friend std::ostream& operator<<(std::ostream &s, const MV_ColMat<TYPE> &A);
00110
00111 };
00112
00113
00114
00115 template<class TYPE>
00116 int MV_ColMat<TYPE>::size(int i) const
00117 {
00118 if (i==0) return dim0_;
00119 if (i==1) return dim1_;
00120 else
00121 {
00122 cerr << "Called MV_ColMat::size(" << i << ") must be 0 or 1 " << endl;
00123 exit(1);
00124 }
00125
00126
00127
00128 return 0;
00129 }
00130
00131
00132
00133 template <class TYPE>
00134 MV_ColMat<TYPE>::MV_ColMat() : v_(), dim0_(0), dim1_(0) , lda_(0), ref_(0){}
00135
00136
00137 template <class TYPE>
00138 MV_ColMat<TYPE>::MV_ColMat( int m, int n) : v_(m*n),
00139 dim0_(m), dim1_(n), lda_(m), ref_(0) {}
00140
00141 template <class TYPE>
00142 MV_ColMat<TYPE>::MV_ColMat( int m, int n, const TYPE &s) : v_(m*n),
00143 dim0_(m), dim1_(n), lda_(m), ref_(0)
00144 {
00145 operator=(s);
00146 }
00147
00148
00149
00150
00151
00152 template <class TYPE>
00153 inline TYPE& MV_ColMat<TYPE>::operator()( int i, int j)
00154 {
00155 #ifdef MV_MATRIX_BOUNDS_CHECK
00156 assert(0<=i && i<size(0));
00157 assert(0<=j && j<size(1));
00158 #endif
00159 return v_(j*lda_ + i);
00160
00161 }
00162
00163 template <class TYPE>
00164 inline const TYPE& MV_ColMat<TYPE>::operator()
00165 ( int i, int j) const
00166 {
00167 #ifdef MV_MATRIX_BOUNDS_CHECK
00168 assert(0<=i && i<size(0));
00169 assert(0<=j && j<size(1));
00170 #endif
00171 return v_(j*lda_ + i);
00172 }
00173
00174
00175 template <class TYPE>
00176 MV_ColMat<TYPE>& MV_ColMat<TYPE>::operator=(const TYPE & s)
00177 {
00178 int M = size(0);
00179 int N = size(1);
00180
00181 if (lda_ == M)
00182 v_ = s;
00183
00184 else
00185 {
00186
00187
00188
00189
00190 MV_VecIndex I(0,M-1);
00191 for (int j=0; j<N; j++)
00192 {
00193 v_(I) = s;
00194 I += lda_;
00195 }
00196 }
00197
00198 return *this;
00199 }
00200
00201 template <class TYPE>
00202 MV_ColMat<TYPE>& MV_ColMat<TYPE>::newsize( int M, int N)
00203 {
00204 v_.newsize(M*N);
00205 dim0_ = M;
00206 dim1_ = N;
00207 lda_ = M;
00208
00209 return *this;
00210 }
00211
00212 template <class TYPE>
00213 MV_ColMat<TYPE>& MV_ColMat<TYPE>::operator=(const MV_ColMat<TYPE> & m)
00214 {
00215
00216 int lM = dim0_;
00217 int lN = dim1_;
00218
00219 int rM = m.dim0_;
00220 int rN = m.dim1_;
00221
00222
00223
00224
00225
00226
00227 if (ref_)
00228 {
00229
00230 if (lM != rM || lN != rN)
00231 {
00232 cerr << "MV_ColMatRef::operator= non-conformant assignment.\n";
00233 exit(1);
00234 }
00235 }
00236 else
00237 {
00238 newsize(rM,rN);
00239 }
00240
00241
00242
00243
00244
00245
00246
00247 if ( lM == lda_ && rM == m.lda_)
00248 {
00249 MV_VecIndex I(0,rM*rN-1);
00250 v_(I) = m.v_(I);
00251 }
00252 else
00253 {
00254
00255
00256 MV_VecIndex I(0,rM-1);
00257 MV_VecIndex K(0,rM-1);
00258 for (int j=0; j<rN; j++)
00259 {
00260 v_(I) = m.v_(K);
00261 I += lda_;
00262 K += m.lda_;
00263 }
00264 }
00265
00266 return *this;
00267 }
00268
00269 template <class TYPE>
00270 MV_ColMat<TYPE>::MV_ColMat(const MV_ColMat<TYPE> & m) :
00271 v_(m.dim0_*m.dim1_), dim0_(m.dim0_),
00272 dim1_(m.dim1_), ref_(0), lda_(m.dim0_)
00273 {
00274
00275 int M = m.dim0_;
00276 int N = m.dim1_;
00277
00278
00279
00280
00281 MV_VecIndex I(0,M-1);
00282 MV_VecIndex K(0,M-1);
00283 for (int j=0; j<N; j++)
00284 {
00285 v_(I) = m.v_(K);
00286 I += lda_;
00287 K += m.lda_;
00288 }
00289 }
00290
00291
00292 template <class TYPE>
00293 inline MV_ColMat<TYPE>::MV_ColMat(TYPE* d, int m, int n,
00294 Matrix_::ref_type i ):
00295 v_(d,m*n, MV_Vector_::ref), dim0_(m), dim1_(n), lda_(m), ref_(i) {}
00296
00297 template <class TYPE>
00298 inline MV_ColMat<TYPE>::MV_ColMat(TYPE* d, int m, int n,
00299 int lda, Matrix_::ref_type i) :
00300 v_(d, lda*n, MV_Vector_::ref), dim0_(m), dim1_(n), lda_(lda),
00301 ref_(i) {}
00302
00303 template <class TYPE>
00304 MV_ColMat<TYPE>::MV_ColMat(TYPE* d, int m, int n) :
00305 v_(m*n), dim0_(m), dim1_(n), lda_(m), ref_(0)
00306 {
00307 int mn = m*n;
00308
00309
00310 for (int i=0; i< mn; i++)
00311 v_[i] = d[i];
00312 }
00313
00314
00315 template <class TYPE>
00316 MV_ColMat<TYPE>::MV_ColMat(TYPE* d, int m, int n,
00317 int lda) :
00318 v_(m*n), dim0_(m), dim1_(n), lda_(lda), ref_(0)
00319 {
00320 for (int j=0; j< n; j++)
00321 for (int i=0; i<m; i++)
00322 operator()(i,j) = d[j*lda + i];
00323 }
00324
00325
00326 template <class TYPE>
00327 MV_ColMat<TYPE> MV_ColMat<TYPE>::operator()(const MV_VecIndex &I, const MV_VecIndex &J)
00328 {
00329
00330
00331 if (I.end() >= dim0_ || J.end() >= dim1_)
00332 {
00333 cerr << "Matrix index: (" << I.start() << ":" << I.end()
00334 << "," << J.start() << ":" << J.end()
00335 << ") not a subset of (0:" << dim0_ - 1 << ", 0:"
00336 << dim1_-1 << ") " << endl;
00337 exit(1);
00338 }
00339
00340
00341
00342 return MV_ColMat<TYPE>(&v_[J.start()*lda_ + I.start()],
00343 I.end() - I.start() + 1,
00344 J.end() - J.start() + 1, lda_, Matrix_::ref);
00345 }
00346
00347 template <class TYPE>
00348 const MV_ColMat<TYPE> MV_ColMat<TYPE>::operator()(const MV_VecIndex &I,
00349 const MV_VecIndex &J) const
00350 {
00351
00352 cerr << "Const operator()(MV_VecIndex, MV_VecIndex) called " << endl;
00353
00354
00355
00356 if (I.end() >= dim0_ || J.end() >= dim1_)
00357 {
00358 cerr << "Matrix index: (" << I.start() << ":" << I.end()
00359 << "," << J.start() << ":" << J.end()
00360 << ") not a subset of (0:" << dim0_ - 1 << ", 0:"
00361 << dim1_-1 << ") " << endl;
00362 exit(1);
00363 }
00364
00365
00366
00367
00368
00369 MV_ColMat<TYPE> *t = (MV_ColMat<TYPE>*) this;
00370 return MV_ColMat<TYPE>(&(t->v_[J.start()*lda_ + I.start()]),
00371 I.end() - I.start() + 1,
00372 J.end() - J.start() + 1, lda_, Matrix_::ref);
00373 }
00374
00375 template <class TYPE>
00376 MV_ColMat<TYPE>::~MV_ColMat() {}
00377
00378 template <class TYPE>
00379 ostream& operator<<(ostream& s, const MV_ColMat<TYPE>& V)
00380 {
00381 int M = V.size(0);
00382 int N = V.size(1);
00383
00384 for (int i=0; i<M; i++)
00385 {
00386 for (int j=0; j<N; j++)
00387 s << V(i,j) << " " ;
00388 s << endl;
00389 }
00390
00391 return s;
00392 }
00393
00394
00395 #endif
00396
00397