21 #ifndef SELDON_FILE_MATRIX_HERMITIAN_CXX
23 #include "Matrix_Hermitian.hxx"
35 template <
class T,
class Prop,
class Storage,
class Allocator>
41 #ifdef SELDON_CHECK_MEMORY
46 me_ =
reinterpret_cast<pointer*
>(calloc(i,
sizeof(pointer)));
48 #ifdef SELDON_CHECK_MEMORY
63 if ((me_ == NULL) && (i != 0))
64 throw NoMemory(
"Matrix_Hermitian::Matrix_Hermitian(int, int)",
65 string(
"Unable to allocate memory for a matrix of size ")
66 +
to_str(
static_cast<long int>(i)
67 *
static_cast<long int>(i)
68 *
static_cast<long int>(
sizeof(T)))
73 #ifdef SELDON_CHECK_MEMORY
78 this->data_ = Allocator::allocate(
long(i) *
long(i),
this);
80 #ifdef SELDON_CHECK_MEMORY
90 if (this->data_ == NULL)
97 if (this->data_ == NULL && i != 0)
98 throw NoMemory(
"Matrix_Hermitian::Matrix_Hermitian(int, int)",
99 string(
"Unable to allocate memory for a matrix of size ")
100 +
to_str(
static_cast<long int>(i)
101 *
static_cast<long int>(i)
102 *
static_cast<long int>(
sizeof(T)))
107 pointer ptr = this->data_;
109 for (
int k = 0; k < i; k++, ptr += lgth)
115 template <
class T,
class Prop,
class Storage,
class Allocator>
142 template <
class T,
class Prop,
class Storage,
class Allocator>
148 int iold = Storage::GetFirst(this->m_, this->n_);
149 int jold = Storage::GetSecond(this->m_, this->n_);
151 for (
long k = 0; k < this->GetDataSize(); k++)
152 xold(k) = this->data_[k];
155 int inew = Storage::GetFirst(i, j);
156 int jnew = Storage::GetSecond(i, j);
157 this->Reallocate(i, j);
160 int imin = min(iold, inew), jmin = min(jold, jnew);
161 for (
int k = 0; k < imin; k++)
162 for (
int l = 0; l < jmin; l++)
163 this->data_[
long(k)*jnew+l] = xold(
long(l)+
long(jold)*k);
172 template <
class T,
class Prop,
class Storage,
class Allocator>
175 #ifdef SELDON_CHECK_MEMORY
180 if (this->data_ != NULL)
182 Allocator::deallocate(this->data_,
long(this->m_) * this->n_);
186 #ifdef SELDON_CHECK_MEMORY
194 #ifdef SELDON_CHECK_MEMORY
205 #ifdef SELDON_CHECK_MEMORY
227 template <
class T,
class Prop,
class Storage,
class Allocator>
237 #ifdef SELDON_CHECK_MEMORY
242 me_ =
reinterpret_cast<pointer*
>( realloc(me_,
243 i *
sizeof(pointer)) );
245 #ifdef SELDON_CHECK_MEMORY
260 if (me_ == NULL && i != 0)
261 throw NoMemory(
"Matrix_Hermitian::Reallocate(int, int)",
262 string(
"Unable to reallocate memory")
263 +
" for a matrix of size "
264 +
to_str(
static_cast<long int>(i)
265 *
static_cast<long int>(i)
266 *
static_cast<long int>(
sizeof(T)))
271 #ifdef SELDON_CHECK_MEMORY
277 reinterpret_cast<pointer
>(Allocator::reallocate(this->data_,
280 #ifdef SELDON_CHECK_MEMORY
290 if (this->data_ == NULL)
297 if (this->data_ == NULL && i != 0)
298 throw NoMemory(
"Matrix_Hermitian::Reallocate(int, int)",
299 string(
"Unable to reallocate memory")
300 +
" for a matrix of size "
301 +
to_str(
static_cast<long int>(i)
302 *
static_cast<long int>(i)
303 *
static_cast<long int>(
sizeof(T)))
308 pointer ptr = this->data_;
309 long lgth = Storage::GetSecond(i, i);
310 for (
int k = 0; k < Storage::GetFirst(i, i); k++, ptr += lgth)
332 template <
class T,
class Prop,
class Storage,
class Allocator>
343 #ifdef SELDON_CHECK_MEMORY
348 me_ =
reinterpret_cast<pointer*
>(calloc(i,
sizeof(pointer)));
350 #ifdef SELDON_CHECK_MEMORY
371 pointer ptr = this->data_;
373 for (
int k = 0; k < i; k++, ptr += lgth)
384 template <
class T,
class Prop,
class Storage,
class Allocator>
390 #ifdef SELDON_CHECK_MEMORY
401 #ifdef SELDON_CHECK_MEMORY
425 template <
class T,
class Prop,
class Storage,
class Allocator>
428 Allocator::memoryset(this->data_,
char(0),
429 this->GetDataSize() *
sizeof(value_type));
438 template <
class T,
class Prop,
class Storage,
class Allocator>
442 SetComplexZero(zero);
447 for (
int i = 0; i < min(this->m_, this->n_); i++)
448 this->Val(i, i) = one;
457 template <
class T,
class Prop,
class Storage,
class Allocator>
460 for (
long i = 0; i < this->GetDataSize(); i++)
471 template <
class T,
class Prop,
class Storage,
class Allocator>
477 for (
long i = 0; i < this->GetDataSize(); i++)
488 template <
class T,
class Prop,
class Storage,
class Allocator>
503 template <
class T,
class Prop,
class Storage,
class Allocator>
506 #ifndef SELDON_WITHOUT_REINIT_RANDOM
509 for (
long i = 0; i < this->GetDataSize(); i++)
520 template <
class T,
class Prop,
class Storage,
class Allocator>
523 for (
int i = 0; i < this->m_; i++)
525 for (
int j = 0; j < this->n_; j++)
526 cout << (*
this)(i, j) <<
"\t";
544 template <
class T,
class Prop,
class Storage,
class Allocator>
548 for (
int i = a; i < min(this->m_, a+m); i++)
550 for (
int j = b; j < min(this->n_, b+n); j++)
551 cout << (*
this)(i, j) <<
"\t";
566 template <
class T,
class Prop,
class Storage,
class Allocator>
586 template <
class T,
class Prop,
class Storage,
class Allocator>
591 FileStream.open(FileName.c_str(), ofstream::binary);
593 #ifdef SELDON_CHECK_IO
595 if (!FileStream.is_open())
596 throw IOError(
"Matrix_Hermitian::Write(string FileName)",
597 string(
"Unable to open file \"") + FileName +
"\".");
600 this->Write(FileStream);
614 template <
class T,
class Prop,
class Storage,
class Allocator>
619 #ifdef SELDON_CHECK_IO
621 if (!FileStream.good())
622 throw IOError(
"Matrix_Hermitian::Write(ofstream& FileStream)",
623 "Stream is not ready.");
626 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
628 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
631 FileStream.write(
reinterpret_cast<char*
>(this->data_),
632 long(this->m_) *
long(this->n_) *
sizeof(value_type));
634 #ifdef SELDON_CHECK_IO
636 if (!FileStream.good())
637 throw IOError(
"Matrix_Hermitian::Write(ofstream& FileStream)",
638 string(
"Output operation failed.")
639 +
string(
" The output file may have been removed")
640 +
" or there is no space left on device.");
654 template <
class T,
class Prop,
class Storage,
class Allocator>
659 FileStream.precision(cout.precision());
660 FileStream.flags(cout.flags());
661 FileStream.open(FileName.c_str());
663 #ifdef SELDON_CHECK_IO
665 if (!FileStream.is_open())
666 throw IOError(
"Matrix_Hermitian::WriteText(string FileName)",
667 string(
"Unable to open file \"") + FileName +
"\".");
670 this->WriteText(FileStream);
684 template <
class T,
class Prop,
class Storage,
class Allocator>
689 #ifdef SELDON_CHECK_IO
691 if (!FileStream.good())
692 throw IOError(
"Matrix_Hermitian::WriteText(ofstream& FileStream)",
693 "Stream is not ready.");
697 for (i = 0; i < this->GetM(); i++)
699 for (j = 0; j < this->GetN(); j++)
700 FileStream << (*
this)(i, j) <<
'\t';
704 #ifdef SELDON_CHECK_IO
706 if (!FileStream.good())
707 throw IOError(
"Matrix_Hermitian::WriteText(ofstream& FileStream)",
708 string(
"Output operation failed.")
709 + string(
" The output file may have been removed")
710 +
" or there is no space left on device.");
724 template <
class T,
class Prop,
class Storage,
class Allocator>
728 FileStream.open(FileName.c_str(), ifstream::binary);
730 #ifdef SELDON_CHECK_IO
732 if (!FileStream.is_open())
733 throw IOError(
"Matrix_Hermitian::Read(string FileName)",
734 string(
"Unable to open file \"") + FileName +
"\".");
737 this->Read(FileStream);
751 template <
class T,
class Prop,
class Storage,
class Allocator>
756 #ifdef SELDON_CHECK_IO
758 if (!FileStream.good())
759 throw IOError(
"Matrix_Hermitian::Read(ifstream& FileStream)",
760 "Stream is not ready.");
764 FileStream.read(
reinterpret_cast<char*
>(&new_m),
sizeof(
int));
765 FileStream.read(
reinterpret_cast<char*
>(&new_n),
sizeof(
int));
766 this->Reallocate(new_m, new_n);
768 FileStream.read(
reinterpret_cast<char*
>(this->data_),
769 long(new_m) *
long(new_n) *
sizeof(value_type));
771 #ifdef SELDON_CHECK_IO
773 if (!FileStream.good())
774 throw IOError(
"Matrix_Hermitian::Read(ifstream& FileStream)",
775 string(
"Input operation failed.")
776 +
string(
" The input file may have been removed")
777 +
" or may not contain enough data.");
788 template <
class T,
class Prop,
class Storage,
class Allocator>
792 FileStream.open(FileName.c_str());
794 #ifdef SELDON_CHECK_IO
796 if (!FileStream.is_open())
797 throw IOError(
"Matrix_Pointers::ReadText(string FileName)",
798 string(
"Unable to open file \"") + FileName +
"\".");
801 this->ReadText(FileStream);
812 template <
class T,
class Prop,
class Storage,
class Allocator>
819 #ifdef SELDON_CHECK_IO
821 if (!FileStream.good())
822 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
823 "Stream is not ready.");
828 getline(FileStream, line);
830 if (FileStream.fail())
837 istringstream line_stream(line);
839 first_row.ReadText(line_stream);
843 other_rows.ReadText(FileStream);
846 int n = first_row.GetM();
847 int m = 1 + other_rows.GetM()/n;
849 #ifdef SELDON_CHECK_IO
851 if (other_rows.GetM() != (m-1)*n)
852 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
853 "The file should contain same number of columns.");
856 this->Reallocate(m,n);
858 for (
int j = 0; j < n; j++)
859 this->Val(0, j) = first_row(j);
862 for (
int i = 1; i < m; i++)
864 for (
int j = 0; j < i; j++)
867 for (
int j = i; j < n; j++)
868 this->Val(i, j) = other_rows(nb++);
875 #define SELDON_FILE_MATRIX_HERMITIAN_CXX