00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef TBCI_CRMATRIX_H
00016 #define TBCI_CRMATRIX_H
00017
00018 #include "matrix_sig.h"
00019 #include "matrix.h"
00020 #include "vector.h"
00021
00022
00023
00024
00025 #if !defined(NO_GD) && !defined(AUTO_DECL)
00026 # include "crmatrix_gd.h"
00027 #endif
00028
00029 NAMESPACE_TBCI
00030
00031
00032 #ifdef EXCEPT
00033
00034
00035 class CRMatErr : public NumErr
00036 {
00037 public:
00038 CRMatErr()
00039 : NumErr("Error in crmatrix library") {}
00040 CRMatErr(const char* t, const long i = 0)
00041 : NumErr(t, i) {}
00042 CRMatErr(const CRMatErr& ce)
00043 : NumErr(ce) {}
00044 virtual ~CRMatErr() throw() {}
00045 };
00046 #endif
00047
00048
00049 #ifdef PRAGMA_I
00050 # pragma interface "crmatrix.h"
00051 #endif
00052
00053
00054
00055
00056
00057
00062 template <typename T>
00063 class CRMatrix : public Matrix_Sig<T>
00064 {
00065 protected:
00066 unsigned int n_rows, n_cols;
00067 unsigned int* rowSize;
00068 unsigned int** colIndex;
00069 T** comp;
00070 mutable T dummy;
00071
00072 void allocate(unsigned int rows, unsigned int columns);
00073
00074 void destroy();
00075
00076 void copy(const CRMatrix<T>& m);
00077
00078 public:
00079 typedef T value_type;
00080 typedef T element_type;
00081 typedef T aligned_value_type TALIGN(MIN_ALIGN2);
00082
00083 CRMatrix() { allocate(1,1); }
00084 CRMatrix(unsigned int rows, unsigned int columns) { allocate(rows,columns); }
00085 CRMatrix(const CRMatrix<T>& m) { copy(m); }
00086 CRMatrix(const Matrix<T>& mat);
00087 ~CRMatrix() { destroy(); }
00088
00089
00090 static const char* mat_info () { return ("CRMatrix"); }
00091
00093 CRMatrix<T>& clear ();
00095 CRMatrix<T>& fill (const T&);
00097 CRMatrix<T>& setunit (const T& = (T)1);
00098
00100 inline const T& operator () (unsigned int row, unsigned int column) const;
00101 inline const T& get (unsigned int row, unsigned int column) const
00102 { return (*this) (row, column); }
00104 CRMatrix<T>& setval (const T& z, unsigned int row, unsigned int column);
00105 T& setval (unsigned int row, unsigned int column);
00106 T& operator () (unsigned int row, unsigned int column)
00107 { return setval (row, column); }
00108
00109
00110 TVector<T> get_row(unsigned int row) const;
00111 void set_row(const Vector<T>& v, unsigned int row);
00112 void set_row(const TVector<T>& tv, unsigned int row);
00113 void set_row(const TSVector<T>& tsv, unsigned int row);
00114
00115 void set_row( T* value, unsigned int row, unsigned int count, unsigned int* index);
00116
00117
00118 unsigned int rows() const {return n_rows;}
00119 unsigned int columns() const {return n_cols;}
00120 unsigned int size() const;
00121
00122
00123 inline void resize(unsigned int newRows, unsigned int newColumns);
00124
00125
00126 CRMatrix<T>& operator= (const CRMatrix<T>& m);
00127 bool operator == (const CRMatrix<T>& m) const;
00128 bool operator != (const CRMatrix<T>& m) const { return !(this->operator == (m)); }
00129
00130
00131 CRMatrix<T> operator - () const;
00132
00133
00134 TVector<T> operator * (const Vector<T>& v) const;
00135 TVector<T> operator * (const TVector<T>& tv) const;
00136 TVector<T> operator * (TSVector<T>& tsv) const;
00137
00138
00139 void MatVecMult (Vector<T>& res, const Vector<T>& v) const;
00140 void MatVecMult (T* v, T* res);
00141
00142
00143 TVector<T> transMult(const Vector<T>& v) const;
00144 TVector<T> transMult(const TVector<T>& tv) const;
00145 TVector<T> transMult(const TSVector<T>& tsv) const;
00146
00147 CRMatrix<T> operator* (const T& z) const;
00148 CRMatrix<T>& operator*= (const T& z);
00149 CRMatrix<T> operator/ (const T& z) const;
00150 CRMatrix<T>& operator/= (const T& z);
00151
00152
00153 CRMatrix<T> mult (const T& z) const;
00154
00155 CRMatrix<T>& swap (CRMatrix<T>&);
00157 CRMatrix<T> transposed_copy() const;
00158 CRMatrix<T>& transpose();
00159
00160
00161 friend STD__ ostream& operator<< FGD (STD__ ostream& stream, const CRMatrix<T>& m);
00162 };
00163
00164
00165
00166
00167
00168
00169
00170 template <typename T>
00171 CRMatrix<T>::CRMatrix(const Matrix<T>& mat)
00172 {
00173 allocate (mat.rows(), mat.columns());
00174 for (unsigned i = 0; i < n_rows; ++i)
00175 for (unsigned j = 0; j < n_cols; ++j)
00176 (*this) (i, j) = mat( i, j);
00177 }
00178
00179
00180 template <typename T>
00181 inline const T& CRMatrix<T>::operator() (unsigned int row, unsigned int column) const
00182 {
00183 BCHK(row >= n_rows || column >= n_cols, CRMatErr, Illegal index, (row<column ? row : column), dummy = (T)0);
00184 for (unsigned int j=0; j<rowSize[row]; j++)
00185 if (colIndex[row][j] == column)
00186 return comp[row][j];
00187 return (dummy = (T)0);
00188 }
00189
00190
00191
00192 template <typename T>
00193 unsigned int CRMatrix<T>::size() const
00194 {
00195 unsigned int erg=0;
00196 for (unsigned int i=0; i<n_rows; ++i)
00197 erg += rowSize[i];
00198 return erg;
00199 }
00200
00201
00202
00203
00204 template <typename T>
00205 inline void CRMatrix<T>::resize(unsigned int newRows, unsigned int newColumns)
00206 {
00207 if (newRows != n_rows || newColumns != n_cols)
00208 destroy();
00209 allocate(newRows, newColumns);
00210 }
00211
00212
00213
00214 template <typename T>
00215 CRMatrix<T>& CRMatrix<T>::setval(const T& z, unsigned int row, unsigned int column)
00216 {
00217 BCHK(row >= n_rows, CRMatErr, Illegal row index, row, *this);
00218 BCHK(column >= n_cols, CRMatErr, Illegal col index, column, *this);
00219 unsigned int j;
00220 for (j=0; j<rowSize[row]; ++j)
00221 if (colIndex[row][j] == column) {
00222 comp[row][j] = z;
00223 return *this;
00224 }
00225
00226
00227 if (z==0.0)
00228 return *this;
00229
00230 unsigned int* newIndex = new unsigned int[rowSize[row]+1];
00231 BCHK( newIndex==NULL, CRMatErr, Out of Memory, rowSize[row]+1, *this);
00232 T* newComp = new T[rowSize[row]+1];
00233 BCHK( newComp==NULL, CRMatErr, Out of Memory, rowSize[row]+1, *this);
00234
00235 for (j=0; j<rowSize[row]; ++j)
00236 if (colIndex[row][j] < column) {
00237 newIndex[j] = colIndex[row][j];
00238 newComp[j] = comp[row][j];
00239 } else
00240 break;
00241
00242 newIndex[j] = column;
00243 newComp[j] = z;
00244
00245 for (unsigned int i=j; i<rowSize[row]; i++) {
00246 newIndex[i+1] = colIndex[row][i];
00247 newComp[i+1] = comp[row][i];
00248 }
00249
00250 if (rowSize[row] != 0) {
00251 delete[] colIndex[row];
00252 delete[] comp[row];
00253 }
00254 rowSize[row]++;
00255 colIndex[row] = newIndex;
00256 comp[row] = newComp;
00257 return *this;
00258 }
00259
00260 template <typename T>
00261 T& CRMatrix<T>::setval(unsigned int row, unsigned int column)
00262 {
00263 BCHK(row >= n_rows || column >= n_cols, CRMatErr, "Illegal index", (row<column ? row : column), comp[0][0]);
00264 unsigned int j;
00265 for (j=0; j<rowSize[row]; j++)
00266 if (colIndex[row][j] == column)
00267 {
00268 return comp[row][j];
00269 }
00270
00271
00272
00273 unsigned int* newIndex = new unsigned int[rowSize[row]+1];
00274 BCHK( newIndex==NULL, CRMatErr, Out of Memory, rowSize[row]+1, comp[0][0]);
00275 T* newComp = new T[rowSize[row]+1];
00276 BCHK( newComp==NULL, CRMatErr, Out of Memory, rowSize[row]+1, comp[0][0]);
00277
00278 for (j=0; j<rowSize[row]; j++)
00279 if (colIndex[row][j] < column)
00280 {
00281 newIndex[j] = colIndex[row][j];
00282 newComp[j] = comp[row][j];
00283 }
00284 else break;
00285
00286 unsigned int pos_store(j);
00287 newIndex[j] = column;
00288
00289
00290 for (unsigned int i=j; i<rowSize[row]; i++)
00291 {
00292 newIndex[i+1] = colIndex[row][i];
00293 newComp[i+1] = comp[row][i];
00294 }
00295
00296 if (rowSize[row] != 0)
00297 {
00298 delete[] colIndex[row];
00299 delete[] comp[row];
00300 }
00301 rowSize[row]++;
00302 colIndex[row] = newIndex;
00303 comp[row] = newComp;
00304
00305
00306 comp[row][pos_store]=0;
00307 return comp[row][pos_store];
00308 }
00309
00310
00311 template <typename T>
00312 TVector<T> CRMatrix<T>::get_row(unsigned int row) const
00313 {
00314 TVector<T> RowVec(n_cols);
00315 BCHK(row >= n_rows, CRMatErr, Illegal index, row, RowVec);
00316 for (unsigned int j=0; j<n_cols; j++)
00317 RowVec.set((*this)(row,j),j);
00318 return RowVec;
00319 }
00320
00321
00322 template <typename T>
00323 void CRMatrix<T>::set_row( const Vector<T>& v, unsigned int row)
00324 {
00325 BCHKNR(row >= n_rows, CRMatErr, Illegal index, row);
00326 unsigned int count = 0;
00327 unsigned int j;
00328 for (j=0; j<v.size(); j++)
00329 if (v(j) != 0.0) count++;
00330 if (rowSize[row] != count)
00331 {
00332 if (rowSize[row]!=0)
00333 {
00334 delete[] colIndex[row];
00335 delete[] comp[row];
00336 }
00337 if ((rowSize[row]=count) != 0)
00338 {
00339 colIndex[row] = new unsigned int[count];
00340 BCHKNR(colIndex[row]==NULL, CRMatErr, Out of memory, count);
00341 comp[row] = new T[row];
00342 BCHKNR(comp[row]==NULL, CRMatErr, Out of memory, row);
00343 }
00344 else
00345 {
00346 colIndex[row] = NULL;
00347 comp[row] = NULL;
00348 }
00349 }
00350 count = 0;
00351 for (j=0; j<v.size(); j++)
00352 if (v(j)!=0.0)
00353 {
00354 colIndex[row][count] = j;
00355 comp[row][count] = v(j);
00356 count++;
00357 }
00358 }
00359
00360 template <typename T>
00361 void CRMatrix<T>::set_row( const TVector<T>& tv, unsigned int row)
00362 {
00363 Vector<T> v(tv);
00364 set_row(v,row);
00365 }
00366
00367 template <typename T>
00368 void CRMatrix<T>::set_row( const TSVector<T>& tsv, unsigned int row)
00369 {
00370 Vector<T> v(tsv);
00371 set_row(v,row);
00372 }
00373
00374
00375
00376 template <typename T>
00377 void CRMatrix<T>::set_row( T* value,unsigned int row, unsigned int count, unsigned int* index)
00378 {
00379 BCHKNR(row >= n_rows || count >= n_cols, CRMatErr, Illegal index, (row<count ? row : count));
00380 if (rowSize[row] != count)
00381 {
00382 if (rowSize[row]!=0)
00383 {
00384 delete[] colIndex[row];
00385 delete[] comp[row];
00386 }
00387 if ((rowSize[row]=count) != 0)
00388 {
00389 colIndex[row] = new unsigned int[count];
00390 BCHKNR(colIndex[row]==NULL, CRMatErr, Out of memory, count);
00391 comp[row] = new T[row];
00392 BCHKNR(comp[row]==NULL, CRMatErr, Out of memory, row);
00393 }
00394 else
00395 {
00396 colIndex[row] = NULL;
00397 comp[row] = NULL;
00398 }
00399 }
00400 for (unsigned int j=0; j<count; j++)
00401 {
00402 colIndex[row][j] = index[j];
00403 comp[row][j] = value[j];
00404 }
00405 }
00406
00407
00408 template <typename T>
00409 CRMatrix<T>& CRMatrix<T>::operator= (const CRMatrix<T>& m)
00410 {
00411 if (this == &m) return *this;
00412 if (n_rows==m.n_rows)
00413 {
00414 for (unsigned int i=0; i<n_rows; i++)
00415 {
00416 if (rowSize[i]!=m.rowSize[i])
00417 {
00418 if (rowSize[i] != 0)
00419 {
00420 delete[] colIndex[i];
00421 delete[] comp[i];
00422 }
00423 if ((rowSize[i]=m.rowSize[i]) != 0)
00424 {
00425 colIndex[i] = new unsigned int[rowSize[i]];
00426 BCHK(colIndex[i]==NULL, CRMatErr, Out of memory, rowSize[i], *this);
00427 comp[i] = new T[rowSize[i]];
00428 BCHK(comp[i]==NULL, CRMatErr, Out of memory, rowSize[i], *this);
00429 }
00430 else
00431 {
00432 colIndex[i] = NULL;
00433 comp[i] = NULL;
00434 }
00435 }
00436 for (unsigned int j=0; j<rowSize[i]; j++)
00437 {
00438 colIndex[i][j] = m.colIndex[i][j];
00439 comp[i][j] = m.comp[i][j];
00440 }
00441 }
00442 return *this;
00443 }
00444 destroy();
00445 copy(m);
00446 return *this;
00447 }
00448
00449
00450
00451 template <typename T>
00452 bool CRMatrix<T>::operator == (const CRMatrix<T>& cm) const
00453 {
00454 if (n_rows != cm.n_rows || n_cols != cm.n_cols)
00455 return false;
00456 for (unsigned int i=0; i<n_rows; ++i)
00457 for (unsigned int j=0; j<n_cols; ++j)
00458 if ((*this)(i,j) != cm(i,j))
00459 return false;
00460 return true;
00461 }
00462
00463
00464
00465 template <typename T>
00466 CRMatrix<T> CRMatrix<T>::operator - () const
00467 {
00468 CRMatrix<T> res(n_rows, n_cols);
00469 for (unsigned int i=0; i<n_rows; ++i)
00470 for (unsigned int j=0; j<n_cols; ++j)
00471 if ((*this)(i,j) != (T)0)
00472 res.setval(-(*this)(i,j),i,j);
00473 return res;
00474 }
00475
00476
00477
00478 template <typename T>
00479 TVector<T> CRMatrix<T>::operator * (const Vector<T>& v) const
00480 {
00481 TVector<T> res(n_rows);
00482 BCHK(n_cols != v.size(), CRMatErr, Dimension conflict, v.size(), res);
00483 for (unsigned int i=0; i<n_rows; ++i) {
00484 T var = 0.0;
00485 for (unsigned int j=0; j<rowSize[i]; ++j)
00486 var += comp[i][j] * v(colIndex[i][j]);
00487 res.set(var,i);
00488 }
00489 return res;
00490 }
00491
00492
00493 template <typename T>
00494 inline TVector<T> CRMatrix<T>::operator * (const TVector<T>& tv) const
00495 {
00496 Vector<T> v(tv);
00497 return (this->operator * (v));
00498 }
00499
00500
00501 template <typename T>
00502 inline TVector<T> CRMatrix<T>::operator * (TSVector<T>& tsv) const
00503 {
00504 TVector<T> res(n_rows);
00505 BCHK(n_cols != tsv.size(), CRMatErr, Dimension conflict, tsv.size(), res);
00506 for (unsigned int i=0; i<n_rows; ++i) {
00507 T var = 0.0;
00508 for (unsigned int j=0; j<rowSize[i]; ++j)
00509 var += comp[i][j] * tsv.get(colIndex[i][j]);
00510 res.set(var,i);
00511 }
00512 tsv.destroy ();
00513 return res;
00514 }
00515
00516
00517
00518 template <typename T>
00519 void CRMatrix<T>::MatVecMult(Vector<T>& res, const Vector<T>& v) const
00520 {
00521 BCHKNR(n_cols != v.size(), CRMatErr, Dimension conflict, v.size());
00522 for (unsigned int i=0; i<n_rows; ++i) {
00523 T var = 0.0;
00524 for (unsigned int j=0; j<rowSize[i]; ++j)
00525 var += comp[i][j] * v(colIndex[i][j]);
00526 res(i) = var;
00527 }
00528 }
00529
00530 INST(template <typename T> class CRMatrix friend void MatVecMult(Vector<T>&, const CRMatrix<T>&, const Vector<T>&);)
00531 template <typename T>
00532 inline void MatVecMult(Vector<T>& res, const CRMatrix<T>& m, const Vector<T>& v)
00533 { m.MatVecMult (res, v); }
00534
00535 template <typename T>
00536 void CRMatrix<T>::MatVecMult (T* v, T* res)
00537 {
00538
00539 for (unsigned int i=0; i<this->n_rows; ++i) {
00540 T var = 0.0;
00541 for (unsigned int j=0; j<this->rowSize[i]; ++j)
00542 var += this->comp[i][j] * v[this->colIndex[i][j]];
00543 res[i] = var;
00544 }
00545 }
00546
00547
00548
00549 template <typename T>
00550 TVector<T> CRMatrix<T>::transMult(const Vector<T>& v) const
00551 {
00552
00553 TVector<T> res(0, n_cols);
00554 BCHK(n_rows != v.size(), CRMatErr, Dimension conflict, v.size(), res);
00555
00556 unsigned int pos;
00557 for (unsigned int i=0; i<n_rows; ++i) {
00558 for (unsigned int j=0; j<rowSize[i]; ++j) {
00559 pos = colIndex[i][j];
00560 res.setval(pos) += comp[i][j] * v(i);
00561 }
00562 }
00563 return res;
00564 }
00565
00566
00567 template <typename T>
00568 TVector<T> CRMatrix<T>::transMult(const TVector<T>& tv) const
00569 {
00570 Vector<T> v(tv);
00571 return(this->transMult(v));
00572 }
00573
00574
00575 template <typename T>
00576 TVector<T> CRMatrix<T>::transMult(const TSVector<T>& tsv) const
00577 {
00578 Vector<T> v(tsv);
00579 return(this->transMult(v));
00580 }
00581
00582
00583
00584 template <typename T>
00585 CRMatrix<T> CRMatrix<T>:: operator* (const T& z) const
00586 {
00587 CRMatrix<T> res(*this);
00588 for (unsigned int i=0; i<this->n_rows; ++i)
00589 for (unsigned int j=0; j<this->rowSize[i]; ++j)
00590 res.comp[i][j] = this->comp[i][j] * z;
00591 return res;
00592 }
00593
00594
00595
00596 template <typename T>
00597 CRMatrix<T> CRMatrix<T>::mult (const T& z) const
00598 {
00599 CRMatrix<T> res(*this);
00600 for (unsigned int i=0; i<n_rows; ++i)
00601 for (unsigned int j=0; j<rowSize[i]; ++j)
00602 res.comp[i][j] = z * comp[i][j];
00603 return res;
00604 }
00605
00606 INST(template <typename T> class CRMatrix friend CRMatrix<T> operator * (const T&, const CRMatrix<T>&);)
00607 template <typename T>
00608 inline CRMatrix<T> operator* (const T& z, const CRMatrix<T>& m)
00609 { return m.mult (z); }
00610
00611
00612
00613 template <typename T>
00614 CRMatrix<T>& CRMatrix<T>::operator*= (const T& z)
00615 {
00616 for (unsigned int i=0; i<n_rows; ++i)
00617 for (unsigned int j=0; j<rowSize[i]; ++j)
00618 comp[i][j] *= z;
00619 return *this;
00620 }
00621
00622
00623 template <typename T>
00624 CRMatrix<T> CRMatrix<T>::operator/ (const T& z) const
00625 {
00626 CRMatrix<T> res(*this);
00627 for (unsigned int i=0; i<n_rows; ++i)
00628 for (unsigned int j=0; j<this->rowSize[i]; ++j)
00629 res.comp[i][j] = this->comp[i][j] / z;
00630 return res;
00631 }
00632
00633
00634
00635 template <typename T>
00636 CRMatrix<T>& CRMatrix<T>::operator/= (const T& z)
00637 {
00638 for (unsigned int i=0; i<n_rows; i++)
00639 for (unsigned int j=0; j<rowSize[i]; j++)
00640 comp[i][j] /= z;
00641 return *this;
00642 }
00643
00644
00645
00646 template <typename T>
00647 void CRMatrix<T>::allocate(unsigned int rows, unsigned int columns)
00648 {
00649 BCHKNR(rows <= 0 || columns <= 0, CRMatErr, Zero space allocating, 0);
00650 n_rows = rows; dummy = 0;
00651 n_cols = columns;
00652 rowSize = new unsigned int[n_rows];
00653 BCHKNR(rowSize==NULL, CRMatErr, Out of memory, n_rows);
00654 colIndex = new unsigned int*[n_rows];
00655 BCHKNR(colIndex==NULL, CRMatErr, Out of memory, n_rows);
00656 comp = new T*[n_rows];
00657 BCHKNR(comp==NULL, CRMatErr, Out of memory, n_rows);
00658 for (unsigned int i=0; i<n_rows; i++)
00659 {
00660 rowSize[i] = 0;
00661 colIndex[i] = NULL;
00662 comp[i] = NULL;
00663 }
00664 }
00665
00666
00667
00668 template <typename T>
00669 void CRMatrix<T>::destroy()
00670 {
00671 for (unsigned int i=0; i<n_rows; ++i) {
00672 if (colIndex[i]!=NULL) delete[] colIndex[i];
00673 if (comp[i]!=NULL) delete[] comp[i];
00674 }
00675 if (rowSize != NULL)
00676 delete[] rowSize;
00677 if (comp != NULL)
00678 delete[] comp;
00679 if (colIndex != NULL)
00680 delete[] colIndex;
00681 }
00682
00683
00684
00685 template <typename T>
00686 void CRMatrix<T>::copy(const CRMatrix<T>& m)
00687 {
00688 n_rows = m.n_rows;
00689 n_cols = m.n_cols; dummy = 0;
00690 rowSize = new unsigned int[n_rows];
00691 BCHKNR(rowSize==NULL, CRMatErr, Out of memory, n_rows);
00692 colIndex = new unsigned int*[n_rows];
00693 BCHKNR(colIndex==NULL, CRMatErr, Out of memory, n_rows);
00694 comp = new T*[n_rows];
00695 BCHKNR(comp==NULL, CRMatErr, Out of memory, n_rows);
00696 for (unsigned int i=0; i<n_rows; ++i) {
00697 if ((rowSize[i]=m.rowSize[i]) != 0) {
00698 colIndex[i] = new unsigned int[rowSize[i]];
00699 BCHKNR(colIndex[i]==NULL, CRMatErr, Out of memory, rowSize[i]);
00700 comp[i] = new T[rowSize[i]];
00701 BCHKNR(comp[i]==NULL, CRMatErr, Out of memory, rowSize[i]);
00702 for (unsigned int j=0; j<rowSize[i]; ++j) {
00703 colIndex[i][j] = m.colIndex[i][j];
00704 comp[i][j] = m.comp[i][j];
00705 }
00706 } else {
00707 colIndex[i] = NULL;
00708 comp[i] = NULL;
00709 }
00710 }
00711 }
00712
00713
00714
00715 template <typename T>
00716 inline CRMatrix<T>& CRMatrix<T>::fill (const T& val)
00717 {
00718 for (unsigned int i=0; i<n_rows; ++i)
00719 for (unsigned int j=0; j<rowSize[i]; ++j)
00720 comp[i][j] = val;
00721 return *this;
00722 }
00723
00724 template <typename T>
00725 inline CRMatrix<T>& CRMatrix<T>::clear ()
00726 { return fill ((T)0); }
00727
00728 template <typename T>
00729 inline CRMatrix<T>& CRMatrix<T>::setunit (const T& val)
00730 {
00731 BCHK (n_rows != n_cols, CRMatErr, setunit is meaningless for non-square matrices, n_cols, *this);
00732 clear ();
00733 for (unsigned int i = 0; i < n_cols; ++i)
00734 setval (val, i, i);
00735 return *this;
00736 }
00737
00738 template <typename T>
00739 CRMatrix<T>& CRMatrix<T>::swap(CRMatrix<T>& m)
00740 {
00741 SWAP(n_rows, m.n_rows); SWAP(n_cols, m.n_cols);
00742 SWAP(rowSize, m.rowSize); SWAP(colIndex, m.colIndex);
00743 SWAP(comp, m.comp);
00744 return *this;
00745 }
00746
00747 template <typename T>
00748 CRMatrix<T> CRMatrix<T>::transposed_copy() const
00749 {
00750 CRMatrix<T> m(columns(), rows());
00751 for (unsigned r = 0; r < rows(); ++r)
00752 for (unsigned c = 0; c < columns(); ++c)
00753 m(c,r) = (*this)(r,c);
00754 return m;
00755 }
00756
00757 template <typename T>
00758 inline CRMatrix<T>& CRMatrix<T>::transpose()
00759 {
00760 CRMatrix<T> tr(this->transposed_copy());
00761 return swap(tr);
00762 }
00763
00764 INST(template <typename T> class CRMatrix friend CRMatrix<T> transpose(const CRMatrix<T>& crm);)
00765 template <typename T>
00766 inline CRMatrix<T> transpose(const CRMatrix<T>& crm)
00767 {
00768 return crm.transposed_copy();
00769 }
00770
00771
00772 template <typename T>
00773 STD__ ostream& operator<< (STD__ ostream& stream, const CRMatrix<T>& m)
00774 {
00775 for (unsigned int i = 0; i < m.rows(); ++i) {
00776 stream << "|\t";
00777 for (unsigned int j = 0; j < m.columns(); ++j)
00778 stream << m(i,j) << "\t";
00779 stream << "|\n";
00780 }
00781 return stream << STD__ flush;
00782 }
00783
00784 NAMESPACE_END
00785
00786 #endif