21 #ifndef SELDON_FILE_MATRIX_TRIANGULAR_CXX
23 #include "Matrix_Triangular.hxx"
35 template <
class T,
class Prop,
class Storage,
class Allocator>
40 #ifdef SELDON_CHECK_MEMORY
45 me_ =
reinterpret_cast<pointer*
>( calloc(i,
sizeof(pointer)) );
47 #ifdef SELDON_CHECK_MEMORY
62 if (me_ == NULL && i != 0)
63 throw NoMemory(
"Matrix_Triangular::Matrix_Triangular(int, int)",
64 string(
"Unable to allocate memory for a matrix of size ")
65 +
to_str(
static_cast<long int>(i)
66 *
static_cast<long int>(i)
67 *
static_cast<long int>(
sizeof(T)))
72 #ifdef SELDON_CHECK_MEMORY
77 this->data_ = Allocator::allocate(
long(i) *
long(i),
this);
79 #ifdef SELDON_CHECK_MEMORY
89 if (this->data_ == NULL)
96 if (this->data_ == NULL && i != 0)
97 throw NoMemory(
"Matrix_Triangular::Matrix_Triangular(int, int)",
98 string(
"Unable to allocate memory for a matrix of size ")
99 +
to_str(
static_cast<long int>(i)
100 *
static_cast<long int>(i)
101 *
static_cast<long int>(
sizeof(T)))
106 pointer ptr = this->data_;
108 for (
int k = 0; k < i; k++, ptr += lgth)
114 template <
class T,
class Prop,
class Storage,
class Allocator>
117 Storage, Allocator>& A)
134 template <
class T,
class Prop,
class Storage,
class Allocator>
137 #ifdef SELDON_CHECK_MEMORY
142 if (this->data_ != NULL)
144 Allocator::deallocate(this->data_,
long(this->m_) *
long(this->n_));
148 #ifdef SELDON_CHECK_MEMORY
156 #ifdef SELDON_CHECK_MEMORY
167 #ifdef SELDON_CHECK_MEMORY
194 template <
class T,
class Prop,
class Storage,
class Allocator>
204 #ifdef SELDON_CHECK_MEMORY
209 me_ =
reinterpret_cast<pointer*
>( realloc(me_,
210 i *
sizeof(pointer)) );
212 #ifdef SELDON_CHECK_MEMORY
227 if (me_ == NULL && i != 0)
228 throw NoMemory(
"Matrix_Triangular::Reallocate(int, int)",
229 string(
"Unable to reallocate memory")
230 +
" for a matrix of size "
231 +
to_str(
static_cast<long int>(i)
232 *
static_cast<long int>(i)
233 *
static_cast<long int>(
sizeof(T)))
238 #ifdef SELDON_CHECK_MEMORY
244 reinterpret_cast<pointer
>(Allocator::reallocate(this->data_,
245 long(i)*
long(i),
this) );
247 #ifdef SELDON_CHECK_MEMORY
257 if (this->data_ == NULL)
264 if (this->data_ == NULL && i != 0)
265 throw NoMemory(
"Matrix_Triangular::Reallocate(int, int)",
266 string(
"Unable to reallocate memory")
267 +
" for a matrix of size "
268 +
to_str(
static_cast<long int>(i)
269 *
static_cast<long int>(i)
270 *
static_cast<long int>(
sizeof(T)))
275 pointer ptr = this->data_;
276 long lgth = Storage::GetSecond(i, i);
277 for (
int k = 0; k < Storage::GetFirst(i, i); k++, ptr += lgth)
299 template <
class T,
class Prop,
class Storage,
class Allocator>
311 #ifdef SELDON_CHECK_MEMORY
316 me_ =
reinterpret_cast<pointer*
>( calloc(i,
sizeof(pointer)) );
318 #ifdef SELDON_CHECK_MEMORY
339 pointer ptr = this->data_;
341 for (
int k = 0; k < i; k++, ptr += lgth)
352 template <
class T,
class Prop,
class Storage,
class Allocator>
358 #ifdef SELDON_CHECK_MEMORY
369 #ifdef SELDON_CHECK_MEMORY
391 template <
class T,
class Prop,
class Storage,
class Allocator>
401 for (
long k = 0; k < this->GetDataSize(); k++)
402 xold(k) = this->data_[k];
405 this->Reallocate(i, i);
408 int imin = min(iold, i);
409 for (
int k = 0; k < imin; k++)
410 for (
int l = 0; l < imin; l++)
411 this->data_[k*i + l] = xold(k*iold + l);
426 template <
class T,
class Prop,
class Storage,
class Allocator>
429 Allocator::memoryset(this->data_,
char(0),
430 this->GetDataSize() *
sizeof(value_type));
435 template <
class T,
class Prop,
class Storage,
class Allocator>
439 SetComplexZero(zero);
444 for (
int i = 0; i < min(this->m_, this->n_); i++)
445 this->Val(i, i) = one;
454 template <
class T,
class Prop,
class Storage,
class Allocator>
457 for (
long i = 0; i < this->GetDataSize(); i++)
467 template <
class T,
class Prop,
class Storage,
class Allocator>
473 for (
long i = 0; i < this->GetDataSize(); i++)
483 template <
class T,
class Prop,
class Storage,
class Allocator>
498 template <
class T,
class Prop,
class Storage,
class Allocator>
501 #ifndef SELDON_WITHOUT_REINIT_RANDOM
504 for (
long i = 0; i < this->GetDataSize(); i++)
515 template <
class T,
class Prop,
class Storage,
class Allocator>
518 for (
int i = 0; i < this->m_; i++)
520 for (
int j = 0; j < this->n_; j++)
521 cout << (*
this)(i, j) <<
"\t";
539 template <
class T,
class Prop,
class Storage,
class Allocator>
543 for (
int i = a; i < min(this->m_, a + m); i++)
545 for (
int j = b; j < min(this->n_, b + n); j++)
546 cout << (*
this)(i, j) <<
"\t";
561 template <
class T,
class Prop,
class Storage,
class Allocator>
581 template <
class T,
class Prop,
class Storage,
class Allocator>
586 FileStream.open(FileName.c_str(), ofstream::binary);
588 #ifdef SELDON_CHECK_IO
590 if (!FileStream.is_open())
591 throw IOError(
"Matrix_Triangular::Write(string FileName)",
592 string(
"Unable to open file \"") + FileName +
"\".");
595 this->Write(FileStream);
609 template <
class T,
class Prop,
class Storage,
class Allocator>
614 #ifdef SELDON_CHECK_IO
616 if (!FileStream.good())
617 throw IOError(
"Matrix_Triangular::Write(ofstream& FileStream)",
618 "Stream is not ready.");
621 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
623 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
626 FileStream.write(
reinterpret_cast<char*
>(this->data_),
627 this->m_ * this->n_ *
sizeof(value_type));
629 #ifdef SELDON_CHECK_IO
631 if (!FileStream.good())
632 throw IOError(
"Matrix_Triangular::Write(ofstream& FileStream)",
633 string(
"Output operation failed.")
634 +
string(
" The output file may have been removed")
635 +
" or there is no space left on device.");
649 template <
class T,
class Prop,
class Storage,
class Allocator>
654 FileStream.precision(cout.precision());
655 FileStream.flags(cout.flags());
656 FileStream.open(FileName.c_str());
658 #ifdef SELDON_CHECK_IO
660 if (!FileStream.is_open())
661 throw IOError(
"Matrix_Triangular::WriteText(string FileName)",
662 string(
"Unable to open file \"") + FileName +
"\".");
665 this->WriteText(FileStream);
679 template <
class T,
class Prop,
class Storage,
class Allocator>
684 #ifdef SELDON_CHECK_IO
686 if (!FileStream.good())
687 throw IOError(
"Matrix_Triangular::WriteText(ofstream& FileStream)",
688 "Stream is not ready.");
692 for (i = 0; i < this->GetM(); i++)
694 for (j = 0; j < this->GetN(); j++)
695 FileStream << (*
this)(i, j) <<
'\t';
699 #ifdef SELDON_CHECK_IO
701 if (!FileStream.good())
702 throw IOError(
"Matrix_Triangular::WriteText(ofstream& FileStream)",
703 string(
"Output operation failed.")
704 + string(
" The output file may have been removed")
705 +
" or there is no space left on device.");
719 template <
class T,
class Prop,
class Storage,
class Allocator>
723 FileStream.open(FileName.c_str(), ifstream::binary);
725 #ifdef SELDON_CHECK_IO
727 if (!FileStream.is_open())
728 throw IOError(
"Matrix_Triangular::Read(string FileName)",
729 string(
"Unable to open file \"") + FileName +
"\".");
732 this->Read(FileStream);
746 template <
class T,
class Prop,
class Storage,
class Allocator>
751 #ifdef SELDON_CHECK_IO
753 if (!FileStream.good())
754 throw IOError(
"Matrix_Triangular::Read(ifstream& FileStream)",
755 "Stream is not ready.");
759 FileStream.read(
reinterpret_cast<char*
>(&new_m),
sizeof(
int));
760 FileStream.read(
reinterpret_cast<char*
>(&new_n),
sizeof(
int));
761 this->Reallocate(new_m, new_n);
763 FileStream.read(
reinterpret_cast<char*
>(this->data_),
764 long(new_m) *
long(new_n) *
sizeof(value_type));
766 #ifdef SELDON_CHECK_IO
768 if (!FileStream.good())
769 throw IOError(
"Matrix_Triangular::Read(ifstream& FileStream)",
770 string(
"Input operation failed.")
771 +
string(
" The input file may have been removed")
772 +
" or may not contain enough data.");
783 template <
class T,
class Prop,
class Storage,
class Allocator>
787 FileStream.open(FileName.c_str());
789 #ifdef SELDON_CHECK_IO
791 if (!FileStream.is_open())
792 throw IOError(
"Matrix_Pointers::ReadText(string FileName)",
793 string(
"Unable to open file \"") + FileName +
"\".");
796 this->ReadText(FileStream);
807 template <
class T,
class Prop,
class Storage,
class Allocator>
814 #ifdef SELDON_CHECK_IO
816 if (!FileStream.good())
817 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
818 "Stream is not ready.");
823 getline(FileStream, line);
825 if (FileStream.fail())
832 istringstream line_stream(line);
834 first_row.ReadText(line_stream);
838 other_rows.ReadText(FileStream);
841 int n = first_row.GetM();
842 int m = 1 + other_rows.GetM()/n;
844 #ifdef SELDON_CHECK_IO
846 if (other_rows.GetM() != (m-1)*n)
847 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
848 "The file should contain same number of columns.");
851 this->Reallocate(m,n);
854 for (
int j = 0; j < n; j++)
855 this->Val(0, j) = first_row(j);
857 this->Val(0, 0) = first_row(0);
861 for (
int i = 1; i < m; i++)
863 for (
int j = 0; j < i; j++)
866 for (
int j = i; j < n; j++)
867 this->Val(i, j) = other_rows(nb++);
870 for (
int i = 1; i < m; i++)
872 for (
int j = 0; j <= i; j++)
873 this->Val(i, j) = other_rows(nb++);
875 for (
int j = i+1; j < n; j++)
883 #define SELDON_FILE_MATRIX_TRIANGULAR_CXX