21 #ifndef SELDON_FILE_MATRIX_SYMMETRIC_CXX
23 #include "Matrix_Symmetric.hxx"
34 template <
class T,
class Prop,
class Storage,
class Allocator>
39 #ifdef SELDON_CHECK_MEMORY
44 me_ =
reinterpret_cast<pointer*
>( calloc(i,
sizeof(pointer)) );
46 #ifdef SELDON_CHECK_MEMORY
61 if (me_ == NULL && i != 0)
62 throw NoMemory(
"Matrix_Symmetric::Matrix_Symmetric(int, int)",
63 string(
"Unable to allocate memory for a matrix of size ")
64 +
to_str(
static_cast<long int>(i)
65 *
static_cast<long int>(i)
66 *
static_cast<long int>(
sizeof(T)))
71 #ifdef SELDON_CHECK_MEMORY
76 this->data_ = Allocator::allocate(
long(i) *
long(i),
this);
78 #ifdef SELDON_CHECK_MEMORY
88 if (this->data_ == NULL)
95 if (this->data_ == NULL && i != 0)
96 throw NoMemory(
"Matrix_Symmetric::Matrix_Symmetric(int, int)",
97 string(
"Unable to allocate memory for a matrix of size ")
98 +
to_str(
static_cast<long int>(i)
99 *
static_cast<long int>(i)
100 *
static_cast<long int>(
sizeof(T)))
105 pointer ptr = this->data_;
107 for (
int k = 0; k < i; k++, ptr += lgth)
113 template <
class T,
class Prop,
class Storage,
class Allocator>
132 template <
class T,
class Prop,
class Storage,
class Allocator>
136 #ifdef SELDON_CHECK_MEMORY
141 if (this->data_ != NULL)
143 Allocator::deallocate(this->data_, this->m_ *
long(this->n_));
147 #ifdef SELDON_CHECK_MEMORY
155 #ifdef SELDON_CHECK_MEMORY
166 #ifdef SELDON_CHECK_MEMORY
193 template <
class T,
class Prop,
class Storage,
class Allocator>
203 #ifdef SELDON_CHECK_MEMORY
208 me_ =
reinterpret_cast<pointer*
>( realloc(me_,
209 i *
sizeof(pointer)) );
211 #ifdef SELDON_CHECK_MEMORY
226 if (me_ == NULL && i != 0)
227 throw NoMemory(
"Matrix_Symmetric::Reallocate(int, int)",
228 string(
"Unable to reallocate memory")
229 +
" for a matrix of size "
230 +
to_str(
static_cast<long int>(i)
231 *
static_cast<long int>(i)
232 *
static_cast<long int>(
sizeof(T)))
237 #ifdef SELDON_CHECK_MEMORY
243 reinterpret_cast<pointer
>(Allocator::reallocate(this->data_,
244 long(i)*
long(i),
this) );
246 #ifdef SELDON_CHECK_MEMORY
256 if (this->data_ == NULL)
263 if (this->data_ == NULL && i != 0)
264 throw NoMemory(
"Matrix_Symmetric::Reallocate(int, int)",
265 string(
"Unable to reallocate memory")
266 +
" for a matrix of size "
267 +
to_str(
static_cast<long int>(i)
268 *
static_cast<long int>(i)
269 *
static_cast<long int>(
sizeof(T)))
274 pointer ptr = this->data_;
275 long lgth = Storage::GetSecond(i, i);
276 for (
int k = 0; k < Storage::GetFirst(i, i); k++, ptr += lgth)
298 template <
class T,
class Prop,
class Storage,
class Allocator>
310 #ifdef SELDON_CHECK_MEMORY
315 me_ =
reinterpret_cast<pointer*
>( calloc(i,
sizeof(pointer)) );
317 #ifdef SELDON_CHECK_MEMORY
338 pointer ptr = this->data_;
340 for (
int k = 0; k < i; k++, ptr += lgth)
351 template <
class T,
class Prop,
class Storage,
class Allocator>
357 #ifdef SELDON_CHECK_MEMORY
368 #ifdef SELDON_CHECK_MEMORY
395 template <
class T,
class Prop,
class Storage,
class Allocator>
401 int iold = Storage::GetFirst(this->m_, this->n_);
402 int jold = Storage::GetSecond(this->m_, this->n_);
404 for (
long k = 0; k < this->GetDataSize(); k++)
405 xold(k) = this->data_[k];
408 int inew = Storage::GetFirst(i, j);
409 int jnew = Storage::GetSecond(i, j);
410 this->Reallocate(i, j);
413 int imin = min(iold, inew), jmin = min(jold, jnew);
414 for (
int k = 0; k < imin; k++)
415 for (
int l = 0; l < jmin; l++)
416 this->data_[k*
long(jnew)+l] = xold(l+
long(jold)*k);
430 template <
class T,
class Prop,
class Storage,
class Allocator>
433 Allocator::memoryset(this->data_,
char(0),
434 this->GetDataSize() *
sizeof(value_type));
439 template <
class T,
class Prop,
class Storage,
class Allocator>
443 SetComplexZero(zero);
448 for (
int i = 0; i < min(this->m_, this->n_); i++)
449 this->Val(i, i) = one;
458 template <
class T,
class Prop,
class Storage,
class Allocator>
461 for (
long i = 0; i < this->GetDataSize(); i++)
470 template <
class T,
class Prop,
class Storage,
class Allocator>
476 for (
long i = 0; i < this->GetDataSize(); i++)
485 template <
class T,
class Prop,
class Storage,
class Allocator>
500 template <
class T,
class Prop,
class Storage,
class Allocator>
503 #ifndef SELDON_WITHOUT_REINIT_RANDOM
506 for (
long i = 0; i < this->GetDataSize(); i++)
517 template <
class T,
class Prop,
class Storage,
class Allocator>
520 for (
int i = 0; i < this->m_; i++)
522 for (
int j = 0; j < this->n_; j++)
523 cout << (*
this)(i, j) <<
"\t";
541 template <
class T,
class Prop,
class Storage,
class Allocator>
545 for (
int i = a; i < min(this->m_, a + m); i++)
547 for (
int j = b; j < min(this->n_, b + n); j++)
548 cout << (*
this)(i, j) <<
"\t";
563 template <
class T,
class Prop,
class Storage,
class Allocator>
583 template <
class T,
class Prop,
class Storage,
class Allocator>
588 FileStream.open(FileName.c_str(), ofstream::binary);
590 #ifdef SELDON_CHECK_IO
592 if (!FileStream.is_open())
593 throw IOError(
"Matrix_Symmetric::Write(string FileName)",
594 string(
"Unable to open file \"") + FileName +
"\".");
597 this->Write(FileStream);
611 template <
class T,
class Prop,
class Storage,
class Allocator>
616 #ifdef SELDON_CHECK_IO
618 if (!FileStream.good())
619 throw IOError(
"Matrix_Symmetric::Write(ofstream& FileStream)",
620 "Stream is not ready.");
623 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
625 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
628 FileStream.write(
reinterpret_cast<char*
>(this->data_),
629 long(this->m_) *
long(this->n_) *
sizeof(value_type));
631 #ifdef SELDON_CHECK_IO
633 if (!FileStream.good())
634 throw IOError(
"Matrix_Symmetric::Write(ofstream& FileStream)",
635 string(
"Output operation failed.")
636 +
string(
" The output file may have been removed")
637 +
" or there is no space left on device.");
651 template <
class T,
class Prop,
class Storage,
class Allocator>
656 FileStream.precision(cout.precision());
657 FileStream.flags(cout.flags());
658 FileStream.open(FileName.c_str());
660 #ifdef SELDON_CHECK_IO
662 if (!FileStream.is_open())
663 throw IOError(
"Matrix_Symmetric::WriteText(string FileName)",
664 string(
"Unable to open file \"") + FileName +
"\".");
667 this->WriteText(FileStream);
681 template <
class T,
class Prop,
class Storage,
class Allocator>
686 #ifdef SELDON_CHECK_IO
688 if (!FileStream.good())
689 throw IOError(
"Matrix_Symmetric::WriteText(ofstream& FileStream)",
690 "Stream is not ready.");
694 for (i = 0; i < this->GetM(); i++)
696 for (j = 0; j < this->GetN(); j++)
697 FileStream << (*
this)(i, j) <<
'\t';
701 #ifdef SELDON_CHECK_IO
703 if (!FileStream.good())
704 throw IOError(
"Matrix_Symmetric::WriteText(ofstream& FileStream)",
705 string(
"Output operation failed.")
706 + string(
" The output file may have been removed")
707 +
" or there is no space left on device.");
721 template <
class T,
class Prop,
class Storage,
class Allocator>
725 FileStream.open(FileName.c_str(), ifstream::binary);
727 #ifdef SELDON_CHECK_IO
729 if (!FileStream.is_open())
730 throw IOError(
"Matrix_Symmetric::Read(string FileName)",
731 string(
"Unable to open file \"") + FileName +
"\".");
734 this->Read(FileStream);
748 template <
class T,
class Prop,
class Storage,
class Allocator>
753 #ifdef SELDON_CHECK_IO
755 if (!FileStream.good())
756 throw IOError(
"Matrix_Symmetric::Read(ifstream& FileStream)",
757 "Stream is not ready.");
761 FileStream.read(
reinterpret_cast<char*
>(&new_m),
sizeof(
int));
762 FileStream.read(
reinterpret_cast<char*
>(&new_n),
sizeof(
int));
763 this->Reallocate(new_m, new_n);
765 FileStream.read(
reinterpret_cast<char*
>(this->data_),
766 long(new_m) *
long(new_n) *
sizeof(value_type));
768 #ifdef SELDON_CHECK_IO
770 if (!FileStream.good())
771 throw IOError(
"Matrix_Symmetric::Read(ifstream& FileStream)",
772 string(
"Input operation failed.")
773 +
string(
" The input file may have been removed")
774 +
" or may not contain enough data.");
785 template <
class T,
class Prop,
class Storage,
class Allocator>
789 FileStream.open(FileName.c_str());
791 #ifdef SELDON_CHECK_IO
793 if (!FileStream.is_open())
794 throw IOError(
"Matrix_Pointers::ReadText(string FileName)",
795 string(
"Unable to open file \"") + FileName +
"\".");
798 this->ReadText(FileStream);
809 template <
class T,
class Prop,
class Storage,
class Allocator>
816 #ifdef SELDON_CHECK_IO
818 if (!FileStream.good())
819 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
820 "Stream is not ready.");
825 getline(FileStream, line);
827 if (FileStream.fail())
834 istringstream line_stream(line);
836 first_row.ReadText(line_stream);
840 other_rows.ReadText(FileStream);
843 int n = first_row.GetM();
844 int m = 1 + other_rows.GetM()/n;
846 #ifdef SELDON_CHECK_IO
848 if (other_rows.GetM() != (m-1)*n)
849 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
850 "The file should contain same number of columns.");
853 this->Reallocate(m,n);
855 for (
int j = 0; j < n; j++)
856 this->Val(0, j) = first_row(j);
859 for (
int i = 1; i < m; i++)
861 for (
int j = 0; j < i; j++)
864 for (
int j = i; j < n; j++)
865 this->Val(i, j) = other_rows(nb++);
872 #define SELDON_FILE_MATRIX_SYMMETRIC_CXX