Matrix_HermPacked.cxx
1 // Copyright (C) 2001-2011 Vivien Mallet
2 // Copyright (C) 2003-2011 Marc DuruflĂ©
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_MATRIX_HERMPACKED_CXX
22 
23 #include "Matrix_HermPacked.hxx"
24 
25 namespace Seldon
26 {
27 
28 
30 
35  template <class T, class Prop, class Storage, class Allocator>
37  ::Matrix_HermPacked(int i, int j):
38  Matrix_Base<T, Allocator>(i, i)
39  {
40 
41 #ifdef SELDON_CHECK_MEMORY
42  try
43  {
44 #endif
45 
46  this->data_ = Allocator::allocate((long(i) * long(i + 1)) / 2, this);
47 
48 #ifdef SELDON_CHECK_MEMORY
49  }
50  catch (...)
51  {
52  this->m_ = 0;
53  this->n_ = 0;
54  this->data_ = NULL;
55  return;
56  }
57  if (this->data_ == NULL)
58  {
59  this->m_ = 0;
60  this->n_ = 0;
61  return;
62  }
63 #endif
64 
65  }
66 
67 
69  template <class T, class Prop, class Storage, class Allocator>
72  Storage, Allocator>& A):
73  Matrix_Base<T, Allocator>()
74  {
75  this->m_ = 0;
76  this->n_ = 0;
77  this->data_ = NULL;
78 
79  this->Copy(A);
80  }
81 
82 
84 
88  template <class T, class Prop, class Storage, class Allocator>
90  {
91 
92 #ifdef SELDON_CHECK_MEMORY
93  try
94  {
95 #endif
96 
97  if (this->data_ != NULL)
98  {
99  Allocator::deallocate(this->data_,
100  (long(this->m_) * long(this->m_ + 1)) / 2);
101  this->data_ = NULL;
102  }
103 
104 #ifdef SELDON_CHECK_MEMORY
105  }
106  catch (...)
107  {
108  this->m_ = 0;
109  this->n_ = 0;
110  this->data_ = NULL;
111  }
112 #endif
113 
114  this->m_ = 0;
115  this->n_ = 0;
116  }
117 
118 
119  /*********************
120  * MEMORY MANAGEMENT *
121  *********************/
122 
123 
125 
131  template <class T, class Prop, class Storage, class Allocator>
133  ::Reallocate(int i, int j)
134  {
135 
136  if (i != this->m_)
137  {
138  this->m_ = i;
139  this->n_ = i;
140 
141 #ifdef SELDON_CHECK_MEMORY
142  try
143  {
144 #endif
145 
146  this->data_ =
147  reinterpret_cast<pointer>(Allocator
148  ::reallocate(this->data_,
149  (long(i)*long(i + 1)) / 2, this));
150 
151 #ifdef SELDON_CHECK_MEMORY
152  }
153  catch (...)
154  {
155  this->m_ = 0;
156  this->n_ = 0;
157  this->data_ = NULL;
158  throw NoMemory("Matrix_HermPacked::Reallocate(int, int)",
159  "Unable to reallocate memory for data_.");
160  }
161  if (this->data_ == NULL)
162  {
163  this->m_ = 0;
164  this->n_ = 0;
165  throw NoMemory("Matrix_HermPacked::Reallocate(int, int)",
166  "Unable to reallocate memory for data_.");
167  }
168 #endif
169 
170  }
171  }
172 
173 
176 
190  template <class T, class Prop, class Storage, class Allocator>
192  ::SetData(int i, int j,
194  ::pointer data)
195  {
196  this->Clear();
197 
198  this->m_ = i;
199  this->n_ = i;
200 
201  this->data_ = data;
202  }
203 
204 
206 
210  template <class T, class Prop, class Storage, class Allocator>
212  {
213  this->data_ = NULL;
214  this->m_ = 0;
215  this->n_ = 0;
216  }
217 
218 
219  /************************
220  * CONVENIENT FUNCTIONS *
221  ************************/
222 
223 
225 
229  template <class T, class Prop, class Storage, class Allocator>
231  {
232  Allocator::memoryset(this->data_, char(0),
233  this->GetDataSize() * sizeof(value_type));
234  }
235 
236 
238  template <class T, class Prop, class Storage, class Allocator>
240  {
241  T one, zero;
242  SetComplexOne(one);
243  SetComplexZero(zero);
244 
245  this->Fill(zero);
246 
247  for (int i = 0; i < min(this->m_, this->n_); i++)
248  this->Val(i,i) = one;
249  }
250 
251 
253 
257  template <class T, class Prop, class Storage, class Allocator>
259  {
260  long taille = this->GetDataSize();
261  for (long i = 0; i < taille; i++)
262  SetComplexReal(i, this->data_[i]);
263  }
264 
265 
267 
272  template <class T, class Prop, class Storage, class Allocator>
273  template <class T0>
275  {
276  T x_; long taille = this->GetDataSize();
277  SetComplexReal(x, x_);
278  for (long i = 0; i < taille; i++)
279  this->data_[i] = x_;
280  }
281 
282 
284 
289  template <class T, class Prop, class Storage, class Allocator>
290  template <class T0>
293  {
294  this->Fill(x);
295 
296  return *this;
297  }
298 
299 
301 
304  template <class T, class Prop, class Storage, class Allocator>
306  {
307 #ifndef SELDON_WITHOUT_REINIT_RANDOM
308  srand(time(NULL));
309 #endif
310  long taille = this->GetDataSize();
311  for (long i = 0; i < taille; i++)
312  SetComplexReal(rand(), this->data_[i]);
313  }
314 
315 
317 
322  template <class T, class Prop, class Storage, class Allocator>
324  {
325  for (int i = 0; i < this->m_; i++)
326  {
327  for (int j = 0; j < this->n_; j++)
328  cout << (*this)(i, j) << "\t";
329  cout << endl;
330  }
331  }
332 
333 
335 
346  template <class T, class Prop, class Storage, class Allocator>
348  ::Print(int a, int b, int m, int n) const
349  {
350  for (int i = a; i < min(this->m_, a+m); i++)
351  {
352  for (int j = b; j < min(this->n_, b+n); j++)
353  cout << (*this)(i, j) << "\t";
354  cout << endl;
355  }
356  }
357 
358 
360 
368  template <class T, class Prop, class Storage, class Allocator>
370  {
371  Print(0, 0, l, l);
372  }
373 
374 
375  /**************************
376  * INPUT/OUTPUT FUNCTIONS *
377  **************************/
378 
379 
381 
388  template <class T, class Prop, class Storage, class Allocator>
390  ::Write(string FileName) const
391  {
392 
393  ofstream FileStream;
394  FileStream.open(FileName.c_str(), ofstream::binary);
395 
396 #ifdef SELDON_CHECK_IO
397  // Checks if the file was opened.
398  if (!FileStream.is_open())
399  throw IOError("Matrix_HermPacked::Write(string FileName)",
400  string("Unable to open file \"") + FileName + "\".");
401 #endif
402 
403  this->Write(FileStream);
404 
405  FileStream.close();
406 
407  }
408 
409 
411 
418  template <class T, class Prop, class Storage, class Allocator>
420  ::Write(ostream& FileStream) const
421  {
422 
423 #ifdef SELDON_CHECK_IO
424  // Checks if the file is ready.
425  if (!FileStream.good())
426  throw IOError("Matrix_HermPacked::Write(ofstream& FileStream)",
427  "Stream is not ready.");
428 #endif
429 
430  FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->m_)),
431  sizeof(int));
432  FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->n_)),
433  sizeof(int));
434 
435  FileStream.write(reinterpret_cast<char*>(this->data_),
436  this->GetDataSize() * sizeof(value_type));
437 
438 #ifdef SELDON_CHECK_IO
439  // Checks if data was written.
440  if (!FileStream.good())
441  throw IOError("Matrix_HermPacked::Write(ofstream& FileStream)",
442  string("Output operation failed.")
443  + string(" The output file may have been removed")
444  + " or there is no space left on device.");
445 #endif
446 
447  }
448 
449 
451 
458  template <class T, class Prop, class Storage, class Allocator>
460  ::WriteText(string FileName) const
461  {
462  ofstream FileStream;
463  FileStream.precision(cout.precision());
464  FileStream.flags(cout.flags());
465  FileStream.open(FileName.c_str());
466 
467 #ifdef SELDON_CHECK_IO
468  // Checks if the file was opened.
469  if (!FileStream.is_open())
470  throw IOError("Matrix_HermPacked::WriteText(string FileName)",
471  string("Unable to open file \"") + FileName + "\".");
472 #endif
473 
474  this->WriteText(FileStream);
475 
476  FileStream.close();
477  }
478 
479 
481 
488  template <class T, class Prop, class Storage, class Allocator>
490  ::WriteText(ostream& FileStream) const
491  {
492 
493 #ifdef SELDON_CHECK_IO
494  // Checks if the stream is ready.
495  if (!FileStream.good())
496  throw IOError("Matrix_HermPacked::WriteText(ofstream& FileStream)",
497  "Stream is not ready.");
498 #endif
499 
500  int i, j;
501  for (i = 0; i < this->GetM(); i++)
502  {
503  for (j = 0; j < this->GetN(); j++)
504  FileStream << (*this)(i, j) << '\t';
505  FileStream << endl;
506  }
507 
508 #ifdef SELDON_CHECK_IO
509  // Checks if data was written.
510  if (!FileStream.good())
511  throw IOError("Matrix_HermPacked::WriteText(ofstream& FileStream)",
512  string("Output operation failed.")
513  + string(" The output file may have been removed")
514  + " or there is no space left on device.");
515 #endif
516 
517  }
518 
519 
521 
528  template <class T, class Prop, class Storage, class Allocator>
530  {
531  ifstream FileStream;
532  FileStream.open(FileName.c_str(), ifstream::binary);
533 
534 #ifdef SELDON_CHECK_IO
535  // Checks if the file was opened.
536  if (!FileStream.good())
537  throw IOError("Matrix_HermPacked::Read(string FileName)",
538  string("Unable to open file \"") + FileName + "\".");
539 #endif
540 
541  this->Read(FileStream);
542 
543  FileStream.close();
544  }
545 
546 
548 
555  template <class T, class Prop, class Storage, class Allocator>
557  ::Read(istream& FileStream)
558  {
559 
560 #ifdef SELDON_CHECK_IO
561  // Checks if the stream is ready.
562  if (!FileStream.good())
563  throw IOError("Matrix_HermPacked::Read(ifstream& FileStream)",
564  "Stream is not ready.");
565 #endif
566 
567  int new_m, new_n;
568  FileStream.read(reinterpret_cast<char*>(&new_m), sizeof(int));
569  FileStream.read(reinterpret_cast<char*>(&new_n), sizeof(int));
570  this->Reallocate(new_m, new_n);
571 
572  FileStream.read(reinterpret_cast<char*>(this->data_),
573  this->GetDataSize() * sizeof(value_type));
574 
575 #ifdef SELDON_CHECK_IO
576  // Checks if data was read.
577  if (!FileStream.good())
578  throw IOError("Matrix_HermPacked::Read(ifstream& FileStream)",
579  string("Input operation failed.")
580  + string(" The input file may have been removed")
581  + " or may not contain enough data.");
582 #endif
583 
584  }
585 
586 
588 
592  template <class T, class Prop, class Storage, class Allocator>
594  {
595  ifstream FileStream;
596  FileStream.open(FileName.c_str());
597 
598 #ifdef SELDON_CHECK_IO
599  // Checks if the file was opened.
600  if (!FileStream.is_open())
601  throw IOError("Matrix_Pointers::ReadText(string FileName)",
602  string("Unable to open file \"") + FileName + "\".");
603 #endif
604 
605  this->ReadText(FileStream);
606 
607  FileStream.close();
608  }
609 
610 
612 
616  template <class T, class Prop, class Storage, class Allocator>
618  ::ReadText(istream& FileStream)
619  {
620  // clears previous matrix
621  Clear();
622 
623 #ifdef SELDON_CHECK_IO
624  // Checks if the stream is ready.
625  if (!FileStream.good())
626  throw IOError("Matrix_Pointers::ReadText(ifstream& FileStream)",
627  "Stream is not ready.");
628 #endif
629 
630  // we read first line
631  string line;
632  getline(FileStream, line);
633 
634  if (FileStream.fail())
635  {
636  // empty file ?
637  return;
638  }
639 
640  // converting first line into a vector
641  istringstream line_stream(line);
642  Vector<T> first_row;
643  first_row.ReadText(line_stream);
644 
645  // and now the other rows
646  Vector<T> other_rows;
647  other_rows.ReadText(FileStream);
648 
649  // number of rows and columns
650  int n = first_row.GetM();
651  int m = 1 + other_rows.GetM()/n;
652 
653 #ifdef SELDON_CHECK_IO
654  // Checking number of elements
655  if (other_rows.GetM() != (m-1)*n)
656  throw IOError("Matrix_Pointers::ReadText(ifstream& FileStream)",
657  "The file should contain same number of columns.");
658 #endif
659 
660  this->Reallocate(m,n);
661  // filling matrix
662  for (int j = 0; j < n; j++)
663  this->Val(0, j) = first_row(j);
664 
665  long nb = 0;
666  for (int i = 1; i < m; i++)
667  {
668  for (int j = 0; j < i; j++)
669  nb++;
670 
671  for (int j = i; j < n; j++)
672  this->Val(i, j) = other_rows(nb++);
673  }
674  }
675 
676 
678  // MATRIX<COLHERMPACKED> //
680 
681 
682  /*******************
683  * OTHER FUNCTIONS *
684  *******************/
685 
686 
688 
692  template <class T, class Prop, class Allocator>
693  template <class T0>
696  {
697  long taille = this->GetDataSize();
698  for (long i = 0; i < taille;i++)
699  this->data_[i] *= x;
700 
701  return *this;
702  }
703 
704 
706 
713  template <class T, class Prop, class Allocator>
715  ::Resize(int i, int j)
716  {
717 
718  // Storing the old values of the matrix.
719  long nold = this->GetDataSize();
721  for (long k = 0; k < nold; k++)
722  xold(k) = this->data_[k];
723 
724  // Reallocation.
725  this->Reallocate(i, j);
726 
727  // Filling the matrix with its old values.
728  long nmin = min(nold, this->GetDataSize());
729  for (long k = 0; k < nmin; k++)
730  this->data_[k] = xold(k);
731  }
732 
733 
735  // MATRIX<ROWHERMPACKED> //
737 
738 
739  /*******************
740  * OTHER FUNCTIONS *
741  *******************/
742 
743 
745 
748  template <class T, class Prop, class Allocator>
749  template <class T0>
752  {
753  long taille = this->GetDataSize();
754  for (long i = 0; i < taille;i++)
755  this->data_[i] *= x;
756 
757  return *this;
758  }
759 
760 
762 
769  template <class T, class Prop, class Allocator>
771  ::Resize(int i, int j)
772  {
773  // Storing the old values of the matrix.
774  long nold = this->GetDataSize(), iold = this->m_;
776  for (long k = 0; k < nold; k++)
777  xold(k) = this->data_[k];
778 
779  // Reallocation.
780  this->Reallocate(i, j);
781 
782  // Filling the matrix with its old values.
783  long imin = min(iold, long(i));
784  nold = 0;
785  long n = 0;
786  for (long k = 0; k < imin; k++)
787  {
788  for (long l = k; l < imin; l++)
789  this->data_[n+l-k] = xold(nold+l-k);
790 
791  n += i - k;
792  nold += iold - k;
793  }
794  }
795 
796 
797 } // namespace Seldon.
798 
799 #define SELDON_FILE_MATRIX_HERMPACKED_CXX
800 #endif
Seldon::Matrix_HermPacked::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_HermPacked.cxx:211
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_HermPacked::Zero
void Zero()
Sets all elements to zero.
Definition: Matrix_HermPacked.cxx:230
Seldon::Vector< T >
Seldon::Matrix_HermPacked::Matrix_HermPacked
Matrix_HermPacked()
Default constructor.
Definition: Matrix_HermPackedInline.cxx:39
Seldon::Matrix_HermPacked::Reallocate
void Reallocate(int i, int j)
Reallocates memory to resize the matrix.
Definition: Matrix_HermPacked.cxx:133
Seldon::Matrix_HermPacked
Hermitian packed matrix class.
Definition: Matrix_HermPacked.hxx:38
Seldon::Matrix_HermPacked::operator=
Matrix_HermPacked< T, Prop, Storage, Allocator > & operator=(const Matrix_HermPacked< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_HermPackedInline.cxx:238
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix< T, Prop, ColHermPacked, Allocator >
Column-major hermitian packed matrix class.
Definition: Matrix_HermPacked.hxx:160
Seldon::Matrix_HermPacked::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_HermPacked.cxx:323
Seldon::Matrix< T, Prop, RowHermPacked, Allocator >
Row-major hermitian packed matrix class.
Definition: Matrix_HermPacked.hxx:189
Seldon::Matrix_HermPacked::Clear
void Clear()
Clears the matrix.
Definition: Matrix_HermPacked.cxx:89
Seldon::Matrix_HermPacked::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_HermPacked.cxx:529
Seldon::Vector< T, VectFull, Allocator >
Full vector class.
Definition: Vector.hxx:88
Seldon::Matrix_HermPacked::SetData
void SetData(int i, int j, pointer data)
Changes the size of the matrix and sets its data array (low level method).
Definition: Matrix_HermPacked.cxx:192
Seldon::Matrix_HermPacked::SetIdentity
void SetIdentity()
Sets the matrix to the identity.
Definition: Matrix_HermPacked.cxx:239
Seldon::Matrix_HermPacked::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: Matrix_HermPacked.cxx:460
Seldon::Matrix_HermPacked::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: Matrix_HermPacked.cxx:593
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_HermPacked::FillRand
void FillRand()
Fills the matrix randomly.
Definition: Matrix_HermPacked.cxx:305
Seldon::IOError
Definition: Errors.hxx:150
Seldon::Matrix_HermPacked::Fill
void Fill()
Fills the matrix with 0, 1, 2, ...
Definition: Matrix_HermPacked.cxx:258
Seldon::NoMemory
Definition: Errors.hxx:90
Seldon::Matrix_HermPacked::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_HermPacked.cxx:390
Seldon::SetComplexReal
void SetComplexReal(int n, std::complex< T > &number)
Sets a complex number to (n, 0).
Definition: CommonInline.cxx:234