21 #ifndef SELDON_FILE_MATRIX_TRIANGPACKED_CXX
23 #include "Matrix_TriangPacked.hxx"
35 template <
class T,
class Prop,
class Storage,
class Allocator>
40 #ifdef SELDON_CHECK_MEMORY
45 this->data_ = Allocator::allocate((i *
long(i + 1)) / 2,
this);
47 #ifdef SELDON_CHECK_MEMORY
56 if (this->data_ == NULL)
68 template <
class T,
class Prop,
class Storage,
class Allocator>
71 Storage, Allocator>& A)
87 template <
class T,
class Prop,
class Storage,
class Allocator>
91 #ifdef SELDON_CHECK_MEMORY
96 if (this->data_ != NULL)
98 Allocator::deallocate(this->data_,
99 (this->m_ *
long(this->m_ + 1)) / 2);
103 #ifdef SELDON_CHECK_MEMORY
130 template <
class T,
class Prop,
class Storage,
class Allocator>
140 #ifdef SELDON_CHECK_MEMORY
146 reinterpret_cast<pointer
>(Allocator::reallocate(this->data_,
147 (i*
long(i+1))/2,
this) );
149 #ifdef SELDON_CHECK_MEMORY
156 throw NoMemory(
"Matrix_TriangPacked::Reallocate(int, int)",
157 "Unable to reallocate memory for data_.");
159 if (this->data_ == NULL)
163 throw NoMemory(
"Matrix_TriangPacked::Reallocate(int, int)",
164 "Unable to reallocate memory for data_.");
188 template <
class T,
class Prop,
class Storage,
class Allocator>
208 template <
class T,
class Prop,
class Storage,
class Allocator>
227 template <
class T,
class Prop,
class Storage,
class Allocator>
230 Allocator::memoryset(this->data_,
char(0),
231 this->GetDataSize() *
sizeof(value_type));
236 template <
class T,
class Prop,
class Storage,
class Allocator>
240 SetComplexZero(zero);
245 bool storage_col = (Storage::GetFirst(1,0) == 0);
246 if ((Storage::UpLo() && storage_col)||(!Storage::UpLo() && !storage_col))
249 for (
int i = 0; i < min(this->m_, this->n_); i++)
252 this->data_[index] = one;
255 else if (Storage::UpLo() && !storage_col)
258 for (
int i = 0; i < min(this->m_, this->n_); i++)
260 this->data_[index] = one;
264 else if (!Storage::UpLo() && storage_col)
267 for (
int i = 0; i < min(this->m_, this->n_); i++)
269 this->data_[index] = one;
281 template <
class T,
class Prop,
class Storage,
class Allocator>
284 for (
long i = 0; i < this->GetDataSize(); i++)
293 template <
class T,
class Prop,
class Storage,
class Allocator>
299 for (
long i = 0; i < this->GetDataSize(); i++)
308 template <
class T,
class Prop,
class Storage,
class Allocator>
323 template <
class T,
class Prop,
class Storage,
class Allocator>
326 #ifndef SELDON_WITHOUT_REINIT_RANDOM
329 for (
long i = 0; i < this->GetDataSize(); i++)
340 template <
class T,
class Prop,
class Storage,
class Allocator>
343 for (
int i = 0; i < this->m_; i++)
345 for (
int j = 0; j < this->n_; j++)
346 cout << (*
this)(i, j) <<
"\t";
364 template <
class T,
class Prop,
class Storage,
class Allocator>
368 for (
int i = a; i < min(this->m_, a + m); i++)
370 for (
int j = b; j < min(this->n_, b + n); j++)
371 cout << (*
this)(i, j) <<
"\t";
386 template <
class T,
class Prop,
class Storage,
class Allocator>
406 template <
class T,
class Prop,
class Storage,
class Allocator>
411 FileStream.open(FileName.c_str(), ofstream::binary);
413 #ifdef SELDON_CHECK_IO
415 if (!FileStream.is_open())
416 throw IOError(
"Matrix_TriangPacked::Write(string FileName)",
417 string(
"Unable to open file \"") + FileName +
"\".");
420 this->Write(FileStream);
434 template <
class T,
class Prop,
class Storage,
class Allocator>
439 #ifdef SELDON_CHECK_IO
441 if (!FileStream.good())
442 throw IOError(
"Matrix_TriangPacked::Write(ofstream& FileStream)",
443 "Stream is not ready.");
446 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
448 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
451 FileStream.write(
reinterpret_cast<char*
>(this->data_),
452 this->GetDataSize() *
sizeof(value_type));
454 #ifdef SELDON_CHECK_IO
456 if (!FileStream.good())
457 throw IOError(
"Matrix_TriangPacked::Write(ofstream& FileStream)",
458 string(
"Output operation failed.")
459 +
string(
" The output file may have been removed")
460 +
" or there is no space left on device.");
474 template <
class T,
class Prop,
class Storage,
class Allocator>
479 FileStream.precision(cout.precision());
480 FileStream.flags(cout.flags());
481 FileStream.open(FileName.c_str());
483 #ifdef SELDON_CHECK_IO
485 if (!FileStream.is_open())
486 throw IOError(
"Matrix_TriangPacked::WriteText(string FileName)",
487 string(
"Unable to open file \"") + FileName +
"\".");
490 this->WriteText(FileStream);
504 template <
class T,
class Prop,
class Storage,
class Allocator>
509 #ifdef SELDON_CHECK_IO
511 if (!FileStream.good())
512 throw IOError(
"Matrix_TriangPacked::WriteText(ofstream& FileStream)",
513 "Stream is not ready.");
517 for (i = 0; i < this->GetM(); i++)
519 for (j = 0; j < this->GetN(); j++)
520 FileStream << (*
this)(i, j) <<
'\t';
524 #ifdef SELDON_CHECK_IO
526 if (!FileStream.good())
527 throw IOError(
"Matrix_TriangPacked::WriteText(ofstream& FileStream)",
528 string(
"Output operation failed.")
529 + string(
" The output file may have been removed")
530 +
" or there is no space left on device.");
544 template <
class T,
class Prop,
class Storage,
class Allocator>
548 FileStream.open(FileName.c_str(), ifstream::binary);
550 #ifdef SELDON_CHECK_IO
552 if (!FileStream.good())
553 throw IOError(
"Matrix_TriangPacked::Read(string FileName)",
554 string(
"Unable to open file \"") + FileName +
"\".");
557 this->Read(FileStream);
571 template <
class T,
class Prop,
class Storage,
class Allocator>
576 #ifdef SELDON_CHECK_IO
578 if (!FileStream.good())
579 throw IOError(
"Matrix_TriangPacked::Read(ifstream& FileStream)",
580 "Stream is not ready.");
584 FileStream.read(
reinterpret_cast<char*
>(&new_m),
sizeof(
int));
585 FileStream.read(
reinterpret_cast<char*
>(&new_n),
sizeof(
int));
586 this->Reallocate(new_m, new_n);
588 FileStream.read(
reinterpret_cast<char*
>(this->data_),
589 this->GetDataSize() *
sizeof(value_type));
591 #ifdef SELDON_CHECK_IO
593 if (!FileStream.good())
594 throw IOError(
"Matrix_TriangPacked::Read(ifstream& FileStream)",
595 string(
"Input operation failed.")
596 +
string(
" The input file may have been removed")
597 +
" or may not contain enough data.");
608 template <
class T,
class Prop,
class Storage,
class Allocator>
612 FileStream.open(FileName.c_str());
614 #ifdef SELDON_CHECK_IO
616 if (!FileStream.is_open())
617 throw IOError(
"Matrix_Pointers::ReadText(string FileName)",
618 string(
"Unable to open file \"") + FileName +
"\".");
621 this->ReadText(FileStream);
632 template <
class T,
class Prop,
class Storage,
class Allocator>
639 #ifdef SELDON_CHECK_IO
641 if (!FileStream.good())
642 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
643 "Stream is not ready.");
648 getline(FileStream, line);
650 if (FileStream.fail())
657 istringstream line_stream(line);
659 first_row.ReadText(line_stream);
663 other_rows.ReadText(FileStream);
666 int n = first_row.GetM();
667 int m = 1 + other_rows.GetM()/n;
669 #ifdef SELDON_CHECK_IO
671 if (other_rows.GetM() != (m-1)*n)
672 throw IOError(
"Matrix_Pointers::ReadText(ifstream& FileStream)",
673 "The file should contain same number of columns.");
676 this->Reallocate(m,n);
679 for (
int j = 0; j < n; j++)
680 this->Val(0, j) = first_row(j);
682 this->Val(0, 0) = first_row(0);
686 for (
int i = 1; i < m; i++)
688 for (
int j = 0; j < i; j++)
691 for (
int j = i; j < n; j++)
692 this->Val(i, j) = other_rows(nb++);
695 for (
int i = 1; i < m; i++)
697 for (
int j = 0; j <= i; j++)
698 this->Val(i, j) = other_rows(nb++);
700 for (
int j = i+1; j < n; j++)
725 template <
class T,
class Prop,
class Allocator>
731 long nold = this->GetDataSize();
733 for (
long k = 0; k < nold; k++)
734 xold(k) = this->data_[k];
737 this->Reallocate(i, j);
740 long nmin = min(nold, this->GetDataSize());
741 for (
long k = 0; k < nmin; k++)
742 this->data_[k] = xold(k);
764 template <
class T,
class Prop,
class Allocator>
770 long nold = this->GetDataSize(), iold = this->m_;
772 for (
long k = 0; k < nold; k++)
773 xold(k) = this->data_[k];
776 this->Reallocate(i, j);
779 long imin = min(iold,
long(i));
782 for (
long k = 0; k < imin; k++)
784 for (
long l = k; l < imin; l++)
785 this->data_[n+l-k] = xold(nold+l-k);
811 template <
class T,
class Prop,
class Allocator>
817 long nold = this->GetDataSize(), iold = this->m_;
819 for (
long k = 0; k < nold; k++)
820 xold(k) = this->data_[k];
823 this->Reallocate(i, j);
826 long imin = min(iold,
long(i));
829 for (
long k = 0; k < imin; k++)
831 for (
long l = k; l < imin; l++)
832 this->data_[n+l-k] = xold(nold+l-k);
859 template <
class T,
class Prop,
class Allocator>
865 long nold = this->GetDataSize();
867 for (
long k = 0; k < nold; k++)
868 xold(k) = this->data_[k];
871 this->Reallocate(i, j);
874 long nmin = min(nold, this->GetDataSize());
875 for (
long k = 0; k < nmin; k++)
876 this->data_[k] = xold(k);
882 #define SELDON_FILE_MATRIX_TRIANGPACKED_CXX