matrix/Matrix_HermPacked.cxx

00001 // Copyright (C) 2001-2009 Vivien Mallet
00002 // Copyright (C) 2003-2009 Marc Duruflé
00003 //
00004 // This file is part of the linear-algebra library Seldon,
00005 // http://seldon.sourceforge.net/.
00006 //
00007 // Seldon is free software; you can redistribute it and/or modify it under the
00008 // terms of the GNU Lesser General Public License as published by the Free
00009 // Software Foundation; either version 2.1 of the License, or (at your option)
00010 // any later version.
00011 //
00012 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00015 // more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public License
00018 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00019 
00020 
00021 #ifndef SELDON_FILE_MATRIX_HERMPACKED_CXX
00022 
00023 #include "Matrix_HermPacked.hxx"
00024 
00025 namespace Seldon
00026 {
00027 
00028 
00029   /****************
00030    * CONSTRUCTORS *
00031    ****************/
00032 
00033 
00035 
00038   template <class T, class Prop, class Storage, class Allocator>
00039   inline Matrix_HermPacked<T, Prop, Storage, Allocator>::Matrix_HermPacked():
00040     Matrix_Base<T, Allocator>()
00041   {
00042   }
00043 
00044 
00046 
00051   template <class T, class Prop, class Storage, class Allocator>
00052   inline Matrix_HermPacked<T, Prop, Storage, Allocator>
00053   ::Matrix_HermPacked(int i, int j):
00054     Matrix_Base<T, Allocator>(i, i)
00055   {
00056 
00057 #ifdef SELDON_CHECK_MEMORY
00058     try
00059       {
00060 #endif
00061 
00062         this->data_ = this->allocator_.allocate((i * (i + 1)) / 2, this);
00063 
00064 #ifdef SELDON_CHECK_MEMORY
00065       }
00066     catch (...)
00067       {
00068         this->m_ = 0;
00069         this->n_ = 0;
00070         this->data_ = NULL;
00071         return;
00072       }
00073     if (this->data_ == NULL)
00074       {
00075         this->m_ = 0;
00076         this->n_ = 0;
00077         return;
00078       }
00079 #endif
00080 
00081   }
00082 
00083 
00085   template <class T, class Prop, class Storage, class Allocator>
00086   inline Matrix_HermPacked<T, Prop, Storage, Allocator>
00087   ::Matrix_HermPacked(const Matrix_HermPacked<T, Prop,
00088                       Storage, Allocator>& A):
00089     Matrix_Base<T, Allocator>()
00090   {
00091     this->m_ = 0;
00092     this->n_ = 0;
00093     this->data_ = NULL;
00094 
00095     this->Copy(A);
00096   }
00097 
00098 
00099   /**************
00100    * DESTRUCTOR *
00101    **************/
00102 
00103 
00105   template <class T, class Prop, class Storage, class Allocator>
00106   inline Matrix_HermPacked<T, Prop, Storage, Allocator>::~Matrix_HermPacked()
00107   {
00108 
00109 #ifdef SELDON_CHECK_MEMORY
00110     try
00111       {
00112 #endif
00113 
00114         if (this->data_ != NULL)
00115           {
00116             this->allocator_.deallocate(this->data_,
00117                                         (this->m_ * (this->m_ + 1)) / 2);
00118             this->data_ = NULL;
00119           }
00120 
00121 #ifdef SELDON_CHECK_MEMORY
00122       }
00123     catch (...)
00124       {
00125         this->m_ = 0;
00126         this->n_ = 0;
00127         this->data_ = NULL;
00128       }
00129 #endif
00130 
00131   }
00132 
00133 
00135 
00139   template <class T, class Prop, class Storage, class Allocator>
00140   inline void Matrix_HermPacked<T, Prop, Storage, Allocator>::Clear()
00141   {
00142     this->~Matrix_HermPacked();
00143     this->m_ = 0;
00144     this->n_ = 0;
00145   }
00146 
00147 
00148   /*******************
00149    * BASIC FUNCTIONS *
00150    *******************/
00151 
00152 
00154 
00157   template <class T, class Prop, class Storage, class Allocator>
00158   int Matrix_HermPacked<T, Prop, Storage, Allocator>::GetDataSize() const
00159   {
00160     return (this->m_ * (this->m_ + 1)) / 2;
00161   }
00162 
00163 
00164   /*********************
00165    * MEMORY MANAGEMENT *
00166    *********************/
00167 
00168 
00170 
00176   template <class T, class Prop, class Storage, class Allocator>
00177   inline void Matrix_HermPacked<T, Prop, Storage, Allocator>
00178   ::Reallocate(int i, int j)
00179   {
00180 
00181     if (i != this->m_)
00182       {
00183         this->m_ = i;
00184         this->n_ = i;
00185 
00186 #ifdef SELDON_CHECK_MEMORY
00187         try
00188           {
00189 #endif
00190 
00191             this->data_ =
00192               reinterpret_cast<pointer>(this->allocator_.reallocate(this->data_,
00193                                                                     (i*(i + 1)) / 2,
00194                                                                     this));
00195 
00196 #ifdef SELDON_CHECK_MEMORY
00197           }
00198         catch (...)
00199           {
00200             this->m_ = 0;
00201             this->n_ = 0;
00202             this->data_ = NULL;
00203             throw NoMemory("Matrix_HermPacked::Reallocate(int, int)",
00204                            "Unable to reallocate memory for data_.");
00205           }
00206         if (this->data_ == NULL)
00207           {
00208             this->m_ = 0;
00209             this->n_ = 0;
00210             throw NoMemory("Matrix_HermPacked::Reallocate(int, int)",
00211                            "Unable to reallocate memory for data_.");
00212           }
00213 #endif
00214 
00215       }
00216   }
00217 
00218 
00221 
00235   template <class T, class Prop, class Storage, class Allocator>
00236   inline void Matrix_HermPacked<T, Prop, Storage, Allocator>
00237   ::SetData(int i, int j,
00238             typename Matrix_HermPacked<T, Prop, Storage, Allocator>
00239             ::pointer data)
00240   {
00241     this->Clear();
00242 
00243     this->m_ = i;
00244     this->n_ = i;
00245 
00246     this->data_ = data;
00247   }
00248 
00249 
00251 
00255   template <class T, class Prop, class Storage, class Allocator>
00256   inline void Matrix_HermPacked<T, Prop, Storage, Allocator>::Nullify()
00257   {
00258     this->data_ = NULL;
00259     this->m_ = 0;
00260     this->n_ = 0;
00261   }
00262 
00263 
00264   /**********************************
00265    * ELEMENT ACCESS AND AFFECTATION *
00266    **********************************/
00267 
00268 
00270 
00276   template <class T, class Prop, class Storage, class Allocator>
00277   inline typename Matrix_HermPacked<T, Prop, Storage, Allocator>::reference
00278   Matrix_HermPacked<T, Prop, Storage, Allocator>::operator() (int i, int j)
00279   {
00280 
00281 #ifdef SELDON_CHECK_BOUNDS
00282     if (i < 0 || i >= this->m_)
00283       throw WrongRow("Matrix_HermPacked::operator()",
00284                      string("Index should be in [0, ") + to_str(this->m_-1)
00285                      + "], but is equal to " + to_str(i) + ".");
00286     if (j < 0 || j >= this->n_)
00287       throw WrongCol("Matrix_HermPacked::operator()",
00288                      string("Index should be in [0, ") + to_str(this->n_-1)
00289                      + "], but is equal to " + to_str(j) + ".");
00290 
00291     if (i > j)
00292       throw WrongRow("Matrix_HermPacked::operator()",
00293                      string("Attempted to access to element (")
00294                      + to_str(i) + ", " + to_str(j)
00295                      + ") but row index should not be strictly"
00296                      + " more than column index.");
00297 #endif
00298 
00299     return this->data_[Storage::GetFirst(i * this->n_ - (i*(i+1)) / 2 + j,
00300                                          (j*(j+1)) / 2 + i)];
00301   }
00302 
00303 
00305 
00311   template <class T, class Prop, class Storage, class Allocator>
00312   inline typename Matrix_HermPacked<T, Prop, Storage, Allocator>::value_type
00313   Matrix_HermPacked<T, Prop, Storage, Allocator>
00314   ::operator() (int i, int j) const
00315   {
00316 
00317 #ifdef SELDON_CHECK_BOUNDS
00318     if (i < 0 || i >= this->m_)
00319       throw WrongRow("Matrix_HermPacked::operator()",
00320                      string("Index should be in [0, ") + to_str(this->m_-1)
00321                      + "], but is equal to " + to_str(i) + ".");
00322     if (j < 0 || j >= this->n_)
00323       throw WrongCol("Matrix_HermPacked::operator()",
00324                      string("Index should be in [0, ") + to_str(this->n_-1)
00325                      + "], but is equal to " + to_str(j) + ".");
00326 #endif
00327 
00328     if (i > j)
00329       return conj(this->data_[Storage::GetFirst(j * this->m_
00330                                                 - (j*(j+1)) / 2 + i,
00331                                                 (i*(i+1)) / 2 + j)]);
00332     else
00333       return this->data_[Storage::GetFirst(i * this->n_ - (i*(i+1)) / 2 + j,
00334                                            (j*(j+1)) / 2 + i)];
00335   }
00336 
00337 
00339 
00346   template <class T, class Prop, class Storage, class Allocator>
00347   inline typename Matrix_HermPacked<T, Prop, Storage, Allocator>::reference
00348   Matrix_HermPacked<T, Prop, Storage, Allocator>::Val(int i, int j)
00349   {
00350 
00351 #ifdef SELDON_CHECK_BOUNDS
00352     if (i < 0 || i >= this->m_)
00353       throw WrongRow("Matrix_HermPacked::Val(int, int)",
00354                      string("Index should be in [0, ") + to_str(this->m_-1)
00355                      + "], but is equal to " + to_str(i) + ".");
00356     if (j < 0 || j >= this->n_)
00357       throw WrongCol("Matrix_HermPacked::Val(int, int)",
00358                      string("Index should be in [0, ") + to_str(this->n_-1)
00359                      + "], but is equal to " + to_str(j) + ".");
00360     if (i > j)
00361       throw WrongRow("Matrix_HermPacked::Val(int, int)",
00362                      string("Attempted to access to element (")
00363                      + to_str(i) + ", " + to_str(j)
00364                      + ") but row index should not be strictly"
00365                      + " more than column index.");
00366 #endif
00367 
00368     return this->data_[Storage::GetFirst(i * this->n_ - (i*(i+1)) / 2 + j,
00369                                          (j*(j+1)) / 2 + i)];
00370   }
00371 
00372 
00374 
00381   template <class T, class Prop, class Storage, class Allocator>
00382   inline typename Matrix_HermPacked<T, Prop, Storage, Allocator>
00383   ::const_reference
00384   Matrix_HermPacked<T, Prop, Storage, Allocator>::Val(int i, int j) const
00385   {
00386 
00387 #ifdef SELDON_CHECK_BOUNDS
00388     if (i < 0 || i >= this->m_)
00389       throw WrongRow("Matrix_HermPacked::Val(int, int) const",
00390                      string("Index should be in [0, ") + to_str(this->m_-1)
00391                      + "], but is equal to " + to_str(i) + ".");
00392     if (j < 0 || j >= this->n_)
00393       throw WrongCol("Matrix_HermPacked::Val(int, int) cont",
00394                      string("Index should be in [0, ") + to_str(this->n_-1)
00395                      + "], but is equal to " + to_str(j) + ".");
00396     if (i > j)
00397       throw WrongRow("Matrix_HermPacked::Val(int, int) const",
00398                      string("Attempted to access to element (")
00399                      + to_str(i) + ", " + to_str(j)
00400                      + string(") but row index should not be strictly")
00401                      + " more than column index.");
00402 #endif
00403 
00404     return this->data_[Storage::GetFirst(i * this->n_ - (i*(i+1)) / 2 + j,
00405                                          (j*(j+1)) / 2 + i)];
00406   }
00407 
00408 
00410 
00415   template <class T, class Prop, class Storage, class Allocator>
00416   inline typename Matrix_HermPacked<T, Prop, Storage, Allocator>::reference
00417   Matrix_HermPacked<T, Prop, Storage, Allocator>::operator[] (int i)
00418   {
00419 
00420 #ifdef SELDON_CHECK_BOUNDS
00421     if (i < 0 || i >= this->GetDataSize())
00422       throw WrongIndex("Matrix_HermPacked::operator[] (int)",
00423                        string("Index should be in [0, ")
00424                        + to_str(this->GetDataSize()-1) + "], but is equal to "
00425                        + to_str(i) + ".");
00426 #endif
00427 
00428     return this->data_[i];
00429   }
00430 
00431 
00433 
00438   template <class T, class Prop, class Storage, class Allocator>
00439   inline typename Matrix_HermPacked<T, Prop, Storage, Allocator>
00440   ::const_reference
00441   Matrix_HermPacked<T, Prop, Storage, Allocator>::operator[] (int i) const
00442   {
00443 
00444 #ifdef SELDON_CHECK_BOUNDS
00445     if (i < 0 || i >= this->GetDataSize())
00446       throw WrongIndex("Matrix_HermPacked::operator[] (int) const",
00447                        string("Index should be in [0, ")
00448                        + to_str(this->GetDataSize()-1) + "], but is equal to "
00449                        + to_str(i) + ".");
00450 #endif
00451 
00452     return this->data_[i];
00453   }
00454 
00455 
00457 
00462   template <class T, class Prop, class Storage, class Allocator>
00463   inline Matrix_HermPacked<T, Prop, Storage, Allocator>&
00464   Matrix_HermPacked<T, Prop, Storage, Allocator>
00465   ::operator= (const Matrix_HermPacked<T, Prop, Storage, Allocator>& A)
00466   {
00467     this->Copy(A);
00468 
00469     return *this;
00470   }
00471 
00472 
00474 
00479   template <class T, class Prop, class Storage, class Allocator>
00480   inline void Matrix_HermPacked<T, Prop, Storage, Allocator>
00481   ::Copy(const Matrix_HermPacked<T, Prop, Storage, Allocator>& A)
00482   {
00483     this->Reallocate(A.GetM(), A.GetN());
00484 
00485     this->allocator_.memorycpy(this->data_, A.GetData(), this->GetDataSize());
00486   }
00487 
00488 
00489   /************************
00490    * CONVENIENT FUNCTIONS *
00491    ************************/
00492 
00493 
00495 
00499   template <class T, class Prop, class Storage, class Allocator>
00500   void Matrix_HermPacked<T, Prop, Storage, Allocator>::Zero()
00501   {
00502     this->allocator_.memoryset(this->data_, char(0),
00503                                this->GetDataSize() * sizeof(value_type));
00504   }
00505 
00506 
00508   template <class T, class Prop, class Storage, class Allocator>
00509   void Matrix_HermPacked<T, Prop, Storage, Allocator>::SetIdentity()
00510   {
00511     this->Fill(T(0));
00512 
00513     T one(1);
00514     for (int i = 0; i < min(this->m_, this->n_); i++)
00515       (*this)(i,i) = one;
00516   }
00517 
00518 
00520 
00524   template <class T, class Prop, class Storage, class Allocator>
00525   void Matrix_HermPacked<T, Prop, Storage, Allocator>::Fill()
00526   {
00527     for (int i = 0; i < this->GetDataSize(); i++)
00528       this->data_[i] = i;
00529   }
00530 
00531 
00533 
00536   template <class T, class Prop, class Storage, class Allocator>
00537   template <class T0>
00538   void Matrix_HermPacked<T, Prop, Storage, Allocator>::Fill(const T0& x)
00539   {
00540     for (int i = 0; i < this->GetDataSize(); i++)
00541       this->data_[i] = x;
00542   }
00543 
00544 
00546 
00549   template <class T, class Prop, class Storage, class Allocator>
00550   template <class T0>
00551   Matrix_HermPacked<T, Prop, Storage, Allocator>&
00552   Matrix_HermPacked<T, Prop, Storage, Allocator>::operator= (const T0& x)
00553   {
00554     this->Fill(x);
00555 
00556     return *this;
00557   }
00558 
00559 
00561 
00564   template <class T, class Prop, class Storage, class Allocator>
00565   void Matrix_HermPacked<T, Prop, Storage, Allocator>::FillRand()
00566   {
00567     srand(time(NULL));
00568     for (int i = 0; i < this->GetDataSize(); i++)
00569       this->data_[i] = rand();
00570   }
00571 
00572 
00574 
00579   template <class T, class Prop, class Storage, class Allocator>
00580   void Matrix_HermPacked<T, Prop, Storage, Allocator>::Print() const
00581   {
00582     for (int i = 0; i < this->m_; i++)
00583       {
00584         for (int j = 0; j < this->n_; j++)
00585           cout << (*this)(i, j) << "\t";
00586         cout << endl;
00587       }
00588   }
00589 
00590 
00592 
00603   template <class T, class Prop, class Storage, class Allocator>
00604   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00605   ::Print(int a, int b, int m, int n) const
00606   {
00607     for (int i = a; i < min(this->m_, a+m); i++)
00608       {
00609         for (int j = b; j < min(this->n_, b+n); j++)
00610           cout << (*this)(i, j) << "\t";
00611         cout << endl;
00612       }
00613   }
00614 
00615 
00617 
00625   template <class T, class Prop, class Storage, class Allocator>
00626   void Matrix_HermPacked<T, Prop, Storage, Allocator>::Print(int l) const
00627   {
00628     Print(0, 0, l, l);
00629   }
00630 
00631 
00632   /**************************
00633    * INPUT/OUTPUT FUNCTIONS *
00634    **************************/
00635 
00636 
00638 
00645   template <class T, class Prop, class Storage, class Allocator>
00646   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00647   ::Write(string FileName) const
00648   {
00649 
00650     ofstream FileStream;
00651     FileStream.open(FileName.c_str());
00652 
00653 #ifdef SELDON_CHECK_IO
00654     // Checks if the file was opened.
00655     if (!FileStream.is_open())
00656       throw IOError("Matrix_HermPacked::Write(string FileName)",
00657                     string("Unable to open file \"") + FileName + "\".");
00658 #endif
00659 
00660     this->Write(FileStream);
00661 
00662     FileStream.close();
00663 
00664   }
00665 
00666 
00668 
00675   template <class T, class Prop, class Storage, class Allocator>
00676   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00677   ::Write(ostream& FileStream) const
00678   {
00679 
00680 #ifdef SELDON_CHECK_IO
00681     // Checks if the file is ready.
00682     if (!FileStream.good())
00683       throw IOError("Matrix_HermPacked::Write(ofstream& FileStream)",
00684                     "Stream is not ready.");
00685 #endif
00686 
00687     FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->m_)),
00688                      sizeof(int));
00689     FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->n_)),
00690                      sizeof(int));
00691 
00692     FileStream.write(reinterpret_cast<char*>(this->data_),
00693                      this->GetDataSize() * sizeof(value_type));
00694 
00695 #ifdef SELDON_CHECK_IO
00696     // Checks if data was written.
00697     if (!FileStream.good())
00698       throw IOError("Matrix_HermPacked::Write(ofstream& FileStream)",
00699                     string("Output operation failed.")
00700                     + string(" The output file may have been removed")
00701                     + " or there is no space left on device.");
00702 #endif
00703 
00704   }
00705 
00706 
00708 
00715   template <class T, class Prop, class Storage, class Allocator>
00716   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00717   ::WriteText(string FileName) const
00718   {
00719     ofstream FileStream;
00720     FileStream.precision(cout.precision());
00721     FileStream.flags(cout.flags());
00722     FileStream.open(FileName.c_str());
00723 
00724 #ifdef SELDON_CHECK_IO
00725     // Checks if the file was opened.
00726     if (!FileStream.is_open())
00727       throw IOError("Matrix_HermPacked::WriteText(string FileName)",
00728                     string("Unable to open file \"") + FileName + "\".");
00729 #endif
00730 
00731     this->WriteText(FileStream);
00732 
00733     FileStream.close();
00734   }
00735 
00736 
00738 
00745   template <class T, class Prop, class Storage, class Allocator>
00746   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00747   ::WriteText(ostream& FileStream) const
00748   {
00749 
00750 #ifdef SELDON_CHECK_IO
00751     // Checks if the stream is ready.
00752     if (!FileStream.good())
00753       throw IOError("Matrix_HermPacked::WriteText(ofstream& FileStream)",
00754                     "Stream is not ready.");
00755 #endif
00756 
00757     int i, j;
00758     for (i = 0; i < this->GetM(); i++)
00759       {
00760         for (j = 0; j < this->GetN(); j++)
00761           FileStream << (*this)(i, j) << '\t';
00762         FileStream << endl;
00763       }
00764 
00765 #ifdef SELDON_CHECK_IO
00766     // Checks if data was written.
00767     if (!FileStream.good())
00768       throw IOError("Matrix_HermPacked::WriteText(ofstream& FileStream)",
00769                     string("Output operation failed.")
00770                     + string(" The output file may have been removed")
00771                     + " or there is no space left on device.");
00772 #endif
00773 
00774   }
00775 
00776 
00778 
00785   template <class T, class Prop, class Storage, class Allocator>
00786   void Matrix_HermPacked<T, Prop, Storage, Allocator>::Read(string FileName)
00787   {
00788     ifstream FileStream;
00789     FileStream.open(FileName.c_str());
00790 
00791 #ifdef SELDON_CHECK_IO
00792     // Checks if the file was opened.
00793     if (!FileStream.good())
00794       throw IOError("Matrix_HermPacked::Read(string FileName)",
00795                     string("Unable to open file \"") + FileName + "\".");
00796 #endif
00797 
00798     this->Read(FileStream);
00799 
00800     FileStream.close();
00801   }
00802 
00803 
00805 
00812   template <class T, class Prop, class Storage, class Allocator>
00813   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00814   ::Read(istream& FileStream)
00815   {
00816 
00817 #ifdef SELDON_CHECK_IO
00818     // Checks if the stream is ready.
00819     if (!FileStream.good())
00820       throw IOError("Matrix_HermPacked::Read(ifstream& FileStream)",
00821                     "Stream is not ready.");
00822 #endif
00823 
00824     int new_m, new_n;
00825     FileStream.read(reinterpret_cast<char*>(&new_m), sizeof(int));
00826     FileStream.read(reinterpret_cast<char*>(&new_n), sizeof(int));
00827     this->Reallocate(new_m, new_n);
00828 
00829     FileStream.read(reinterpret_cast<char*>(this->data_),
00830                     this->GetDataSize() * sizeof(value_type));
00831 
00832 #ifdef SELDON_CHECK_IO
00833     // Checks if data was read.
00834     if (!FileStream.good())
00835       throw IOError("Matrix_HermPacked::Read(ifstream& FileStream)",
00836                     string("Output operation failed.")
00837                     + string(" The intput file may have been removed")
00838                     + " or may not contain enough data.");
00839 #endif
00840 
00841   }
00842 
00843 
00845 
00849   template <class T, class Prop, class Storage, class Allocator>
00850   void Matrix_HermPacked<T, Prop, Storage, Allocator>::ReadText(string FileName)
00851   {
00852     ifstream FileStream;
00853     FileStream.open(FileName.c_str());
00854 
00855 #ifdef SELDON_CHECK_IO
00856     // Checks if the file was opened.
00857     if (!FileStream.is_open())
00858       throw IOError("Matrix_Pointers::ReadText(string FileName)",
00859                     string("Unable to open file \"") + FileName + "\".");
00860 #endif
00861 
00862     this->ReadText(FileStream);
00863 
00864     FileStream.close();
00865   }
00866 
00867 
00869 
00873   template <class T, class Prop, class Storage, class Allocator>
00874   void Matrix_HermPacked<T, Prop, Storage, Allocator>
00875   ::ReadText(istream& FileStream)
00876   {
00877     // clears previous matrix
00878     Clear();
00879 
00880 #ifdef SELDON_CHECK_IO
00881     // Checks if the stream is ready.
00882     if (!FileStream.good())
00883       throw IOError("Matrix_Pointers::ReadText(ifstream& FileStream)",
00884                     "Stream is not ready.");
00885 #endif
00886 
00887     // we read first line
00888     string line;
00889     getline(FileStream, line);
00890 
00891     if (FileStream.fail())
00892       {
00893         // empty file ?
00894         return;
00895       }
00896 
00897     // converting first line into a vector
00898     istringstream line_stream(line);
00899     Vector<T> first_row;
00900     first_row.ReadText(line_stream);
00901 
00902     // and now the other rows
00903     Vector<T> other_rows;
00904     other_rows.ReadText(FileStream);
00905 
00906     // number of rows and columns
00907     int n = first_row.GetM();
00908     int m = 1 + other_rows.GetM()/n;
00909 
00910 #ifdef SELDON_CHECK_IO
00911     // Checking number of elements
00912     if (other_rows.GetM() != (m-1)*n)
00913       throw IOError("Matrix_Pointers::ReadText(ifstream& FileStream)",
00914                     "The file should contain same number of columns.");
00915 #endif
00916 
00917     this->Reallocate(m,n);
00918     // filling matrix
00919     for (int j = 0; j < n; j++)
00920       this->Val(0, j) = first_row(j);
00921 
00922     int nb = 0;
00923     for (int i = 1; i < m; i++)
00924       {
00925         for (int j = 0; j < i; j++)
00926           nb++;
00927 
00928         for (int j = i; j < n; j++)
00929           this->Val(i, j) = other_rows(nb++);
00930       }
00931   }
00932 
00933 
00934 
00936   // MATRIX<COLHERMPACKED> //
00938 
00939 
00940   /****************
00941    * CONSTRUCTORS *
00942    ****************/
00943 
00944 
00946 
00949   template <class T, class Prop, class Allocator>
00950   Matrix<T, Prop, ColHermPacked, Allocator>::Matrix():
00951     Matrix_HermPacked<T, Prop, ColHermPacked, Allocator>()
00952   {
00953   }
00954 
00955 
00957 
00962   template <class T, class Prop, class Allocator>
00963   Matrix<T, Prop, ColHermPacked, Allocator>::Matrix(int i, int j):
00964     Matrix_HermPacked<T, Prop, ColHermPacked, Allocator>(i, j)
00965   {
00966   }
00967 
00968 
00969   /*******************
00970    * OTHER FUNCTIONS *
00971    *******************/
00972 
00973 
00975 
00978   template <class T, class Prop, class Allocator>
00979   template <class T0>
00980   inline Matrix<T, Prop, ColHermPacked, Allocator>&
00981   Matrix<T, Prop, ColHermPacked, Allocator>
00982   ::operator= (const T0& x)
00983   {
00984     this->Fill(x);
00985 
00986     return *this;
00987   }
00988 
00989 
00991 
00994   template <class T, class Prop, class Allocator>
00995   template <class T0>
00996   Matrix<T, Prop, ColHermPacked, Allocator>&
00997   Matrix<T, Prop, ColHermPacked, Allocator>::operator*= (const T0& x)
00998   {
00999     for (int i = 0; i < this->GetDataSize();i++)
01000       this->data_[i] *= x;
01001 
01002     return *this;
01003   }
01004 
01005 
01007 
01014   template <class T, class Prop, class Allocator>
01015   inline void Matrix<T, Prop, ColHermPacked, Allocator>
01016   ::Resize(int i, int j)
01017   {
01018 
01019     // Storing the old values of the matrix.
01020     int nold = this->GetDataSize();
01021     Vector<T, VectFull, Allocator> xold(nold);
01022     for (int k = 0; k < nold; k++)
01023       xold(k) = this->data_[k];
01024 
01025     // Reallocation.
01026     this->Reallocate(i, j);
01027 
01028     // Filling the matrix with its old values.
01029     int nmin = min(nold, this->GetDataSize());
01030     for (int k = 0; k < nmin; k++)
01031       this->data_[k] = xold(k);
01032   }
01033 
01034 
01036   // MATRIX<ROWHERMPACKED> //
01038 
01039 
01040   /****************
01041    * CONSTRUCTORS *
01042    ****************/
01043 
01044 
01046 
01049   template <class T, class Prop, class Allocator>
01050   Matrix<T, Prop, RowHermPacked, Allocator>::Matrix():
01051     Matrix_HermPacked<T, Prop, RowHermPacked, Allocator>()
01052   {
01053   }
01054 
01055 
01057 
01062   template <class T, class Prop, class Allocator>
01063   Matrix<T, Prop, RowHermPacked, Allocator>::Matrix(int i, int j):
01064     Matrix_HermPacked<T, Prop, RowHermPacked, Allocator>(i, j)
01065   {
01066   }
01067 
01068 
01069   /*******************
01070    * OTHER FUNCTIONS *
01071    *******************/
01072 
01073 
01075 
01078   template <class T, class Prop, class Allocator>
01079   template <class T0>
01080   inline Matrix<T, Prop, RowHermPacked, Allocator>&
01081   Matrix<T, Prop, RowHermPacked, Allocator>
01082   ::operator= (const T0& x)
01083   {
01084     this->Fill(x);
01085 
01086     return *this;
01087   }
01088 
01089 
01091 
01094   template <class T, class Prop, class Allocator>
01095   template <class T0>
01096   Matrix<T, Prop, RowHermPacked, Allocator>&
01097   Matrix<T, Prop, RowHermPacked, Allocator>::operator*= (const T0& x)
01098   {
01099     for (int i = 0; i < this->GetDataSize();i++)
01100       this->data_[i] *= x;
01101 
01102     return *this;
01103   }
01104 
01105 
01107 
01114   template <class T, class Prop, class Allocator>
01115   inline void Matrix<T, Prop, RowHermPacked, Allocator>
01116   ::Resize(int i, int j)
01117   {
01118     // Storing the old values of the matrix.
01119     int nold = this->GetDataSize(), iold = this->m_;
01120     Vector<T, VectFull, Allocator> xold(nold);
01121     for (int k = 0; k < nold; k++)
01122       xold(k) = this->data_[k];
01123 
01124     // Reallocation.
01125     this->Reallocate(i, j);
01126 
01127     // Filling the matrix with its old values.
01128     int imin = min(iold, i);
01129     nold = 0;
01130     int n = 0;
01131     for (int k = 0; k < imin; k++)
01132       {
01133         for (int l = k; l < imin; l++)
01134           this->data_[n+l-k] = xold(nold+l-k);
01135 
01136         n += i - k;
01137         nold += iold - k;
01138       }
01139   }
01140 
01141 
01142 } // namespace Seldon.
01143 
01144 #define SELDON_FILE_MATRIX_HERMPACKED_CXX
01145 #endif