21 #ifndef SELDON_FILE_MATRIX_COLLECTION_CXX
23 #include "MatrixCollection.hxx"
44 template <
class T,
class Prop,
class Storage,
class Allocator>
46 Matrix_Base<T, Allocator>(), Mlocal_(), Mlocal_sum_(1),
47 Nlocal_(), Nlocal_sum_(1), matrix_()
62 template <
class T,
class Prop,
class Storage,
class Allocator>
65 Mlocal_(i), Mlocal_sum_(i + 1),
66 Nlocal_(j), Nlocal_sum_(j + 1),
84 template <
class T,
class Prop,
class Storage,
class Allocator>
87 for (
int i = 0; i < Mmatrix_; i++)
88 for (
int j = 0; j < Nmatrix_; j++)
108 template <
class T,
class Prop,
class Storage,
class Allocator>
111 for (
int i = 0; i < Mmatrix_; i++)
112 for (
int j = 0; j < Nmatrix_; j++)
130 template <
class T,
class Prop,
class Storage,
class Allocator>
135 matrix_(i, j).Nullify();
140 template <
class T,
class Prop,
class Storage,
class Allocator>
143 for (
int i = 0; i < Mmatrix_; i++)
144 for (
int j = 0; j < Nmatrix_; j++)
145 GetMatrix(i, j).
Clear();
162 template <
class T,
class Prop,
class Storage,
class Allocator>
169 Mlocal_.Reallocate(i);
170 Nlocal_.Reallocate(j);
171 Mlocal_sum_.Reallocate(i + 1);
172 Nlocal_sum_.Reallocate(j + 1);
175 Mlocal_sum_.Fill(0.);
176 Nlocal_sum_.Fill(0.);
177 matrix_.Reallocate(i, j);
187 template <
class T,
class Prop,
class Storage,
class Allocator>
188 template <
class T0,
class Prop0,
class Storage0,
class Allocator0>
192 #ifdef SELDON_CHECK_BOUNDS
193 if (i < 0 || i >= Mmatrix_)
194 throw WrongRow(
"MatrixCollection::SetMatrix()",
195 string(
"Line index should be in [0, ")
196 +
to_str(Mmatrix_ - 1) +
"], but is equal to "
198 if (j < 0 || j >= Nmatrix_)
199 throw WrongCol(
"MatrixCollection::SetMatrix()",
200 string(
"Column index should be in [0, ")
201 +
to_str(Nmatrix_ - 1) +
"], but is equal to "
203 if ((Mlocal_(i) != 0) && (Mlocal_(i) != A.GetM()))
204 throw WrongDim(
"MatrixCollection::SetMatrix()",
205 string(
"The matrix expected should have ")
206 +
to_str(this->Mlocal_(i)) +
" rows, but has "
207 +
to_str(A.GetM()) +
" rows.");
208 if ((Nlocal_(j) != 0) && (Nlocal_(j) != A.GetN()))
209 throw WrongDim(
"MatrixCollection::SetMatrix()",
210 string(
"The matrix expected should have ")
211 +
to_str(this->Nlocal_(j)) +
" columns, but has "
212 +
to_str(A.GetN()) +
" columns.");
215 nz_ = A.GetDataSize() - matrix_(i, j).GetDataSize();
217 int Mdiff = A.GetM() - Mlocal_(i);
218 int Ndiff = A.GetN() - Nlocal_(j);
220 Mlocal_(i) = A.GetM();
221 Nlocal_(j) = A.GetN();
223 for (
int k = i + 1; k < Mmatrix_ + 1; k++)
224 Mlocal_sum_(k) += Mdiff;
226 for (
int k = j + 1; k < Nmatrix_ + 1; k++)
227 Nlocal_sum_(k) += Ndiff;
229 this->m_ = Mlocal_sum_(Mmatrix_);
230 this->n_ = Nlocal_sum_(Nmatrix_);
232 matrix_(i, j).SetData(A.GetM(), A.GetN(), A.GetData());
242 template <
class T,
class Prop,
class Storage,
class Allocator>
243 template <
class T0,
class Prop0,
class Allocator0>
247 #ifdef SELDON_CHECK_BOUNDS
248 if (i < 0 || i >= Mmatrix_)
249 throw WrongRow(
"MatrixCollection::SetMatrix()",
250 string(
"Index should be in [0, ")
251 +
to_str(Mmatrix_ - 1) +
"], but is equal to "
253 if (j < 0 || j >= Nmatrix_)
254 throw WrongCol(
"MatrixCollection::SetMatrix()",
255 string(
"Index should be in [0, ")
256 +
to_str(Nmatrix_ - 1) +
"], but is equal to "
258 if ((Mlocal_(i) != 0) && (Mlocal_(i) != A.GetM()))
259 throw WrongDim(
"MatrixCollection::SetMatrix()",
260 string(
"The matrix expected should have ")
261 +
to_str(this->Mlocal_(i)) +
" rows, but has "
262 +
to_str(A.GetM()) +
" rows.");
263 if ((Nlocal_(j) != 0) && (Nlocal_(j) != A.GetN()))
264 throw WrongDim(
"MatrixCollection::SetMatrix()",
265 string(
"The matrix expected should have ")
266 +
to_str(this->Nlocal_(j)) +
" columns, but has "
267 +
to_str(A.GetN()) +
" columns.");
270 nz_ = A.GetDataSize() - matrix_(i, j).GetDataSize();
272 int Mdiff = A.GetM() - Mlocal_(i);
273 int Ndiff = A.GetN() - Nlocal_(j);
275 Mlocal_(i) = A.GetM();
276 Nlocal_(j) = A.GetN();
278 for (
int k = i + 1; k < Mmatrix_ + 1; k++)
279 Mlocal_sum_(k) += Mdiff;
281 for (
int k = j + 1; k < Nmatrix_ + 1; k++)
282 Nlocal_sum_(k) += Ndiff;
284 this->m_ = Mlocal_sum_(Mmatrix_);
285 this->n_ = Nlocal_sum_(Nmatrix_);
287 matrix_(i, j).SetData(A.GetM(), A.GetN(), A.GetNonZeros(), A.GetData(),
288 A.GetPtr(), A.GetInd());
300 template <
class T,
class Prop,
class Storage,
class Allocator>
306 #ifdef SELDON_CHECK_BOUNDS
307 if (i < 0 || i >= this->Mlocal_sum_(Mmatrix_))
308 throw WrongRow(
"MatrixCollection::operator(int, int)",
309 string(
"Row index should be in [0, ")
310 +
to_str(this->Mlocal_sum_(Mmatrix_) - 1)
311 +
"], but is equal to " +
to_str(i) +
".");
312 if (j < 0 || j >= this->Nlocal_sum_(Nmatrix_))
313 throw WrongCol(
"MatrixCollection::operator(int, int)",
314 string(
"Column index should be in [0, ")
315 +
to_str(this->Nlocal_sum_(Nmatrix_) - 1)
316 +
"], but is equal to " +
to_str(j) +
".");
320 while (i >= Mlocal_sum_(i_global))
325 while (j >= Nlocal_sum_(j_global))
329 return matrix_(i_global, j_global)(i - Mlocal_sum_(i_global),
330 j - Nlocal_sum_(j_global));
340 template <
class T,
class Prop,
class Storage,
class Allocator>
356 template <
class T,
class Prop,
class Storage,
class Allocator>
372 matrix_.Reallocate(Mmatrix_, Nmatrix_);
374 for (
int i = 0; i < Mmatrix_; i++)
375 for (
int j = 0; j < Nmatrix_; j++)
389 template <
class T,
class Prop,
class Storage,
class Allocator>
392 for (
int i = 0; i < Mmatrix_; i++)
394 for (
int j = 0; j < Nmatrix_; j++)
395 cout << GetMatrix(i, j) << endl;
406 template <
class T,
class Prop,
class Storage,
class Allocator>
410 #ifdef SELDON_CHECK_BOUNDS
411 if (i < 0 || i >= Mmatrix_)
412 throw WrongRow(
"MatrixCollection::Print(int, int)",
413 string(
"Row index should be in [0, ")
414 +
to_str(Mmatrix_ - 1) +
"], but is equal to "
416 if (j < 0 || j >= Nmatrix_)
417 throw WrongCol(
"MatrixCollection::Print(int, int)",
418 string(
"Column index should be in [0, ")
419 +
to_str(Nmatrix_ - 1) +
"], but is equal to "
423 cout << matrix_(i, j) << endl;
436 template <
class T,
class Prop,
class Storage,
class Allocator>
438 ::Write(
string FileName,
bool with_size)
const
441 FileStream.open(FileName.c_str(), ofstream::binary);
443 #ifdef SELDON_CHECK_IO
445 if (!FileStream.is_open())
446 throw IOError(
"MatrixCollection::Write(string FileName, bool)",
447 string(
"Unable to open file \"") + FileName +
"\".");
450 this->Write(FileStream, with_size);
465 template <
class T,
class Prop,
class Storage,
class Allocator>
467 ::Write(ostream& FileStream,
bool with_size =
true)
const
470 #ifdef SELDON_CHECK_IO
472 if (!FileStream.good())
473 throw IOError(
"MatrixCollection::Write(ostream& FileStream, bool)",
474 "The stream is not ready.");
479 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&Mmatrix_)),
481 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&Nmatrix_)),
486 for (i = 0; i < this->GetMmatrix(); i++)
488 for (j = 0; j < this->GetNmatrix(); j++)
489 GetMatrix(i, j).Write(FileStream, with_size);
492 #ifdef SELDON_CHECK_IO
494 if (!FileStream.good())
495 throw IOError(
"MatrixCollection::Write(ostream& FileStream, bool)",
496 "Output operation failed.");
508 template <
class T,
class Prop,
class Storage,
class Allocator>
513 FileStream.precision(cout.precision());
514 FileStream.flags(cout.flags());
515 FileStream.open(FileName.c_str());
517 #ifdef SELDON_CHECK_IO
519 if (!FileStream.is_open())
520 throw IOError(
"MatrixCollection::WriteText(string FileName)",
521 string(
"Unable to open file \"") + FileName +
"\".");
524 this->WriteText(FileStream);
537 template <
class T,
class Prop,
class Storage,
class Allocator>
542 #ifdef SELDON_CHECK_IO
544 if (!FileStream.good())
545 throw IOError(
"MatrixCollection::WriteText(ostream& FileStream)",
546 "The stream is not ready.");
550 for (i = 0; i < this->GetMmatrix(); i++)
552 for (j = 0; j < this->GetNmatrix(); j++)
553 GetMatrix(i, j).WriteText(FileStream);
557 #ifdef SELDON_CHECK_IO
559 if (!FileStream.good())
560 throw IOError(
"MatrixCollection::WriteText(ostream& FileStream)",
561 "Output operation failed.");
574 template <
class T,
class Prop,
class Storage,
class Allocator>
578 FileStream.open(FileName.c_str(), ifstream::binary);
580 #ifdef SELDON_CHECK_IO
582 if (!FileStream.is_open())
583 throw IOError(
"MatrixCollection::Read(string FileName)",
584 string(
"Unable to open file \"") + FileName +
"\".");
587 this->Read(FileStream);
600 template <
class T,
class Prop,
class Storage,
class Allocator>
605 #ifdef SELDON_CHECK_IO
607 if (!FileStream.good())
608 throw IOError(
"MatrixCollection::Read(istream& FileStream)",
609 "The stream is not ready.");
616 FileStream.read(
reinterpret_cast<char*
>(new_m),
sizeof(
int));
617 FileStream.read(
reinterpret_cast<char*
>(new_n),
sizeof(
int));
619 this->Reallocate(*new_m, *new_n);
623 for (i = 0; i < *new_m; i++)
624 for (j = 0; j < *new_n; j++)
626 working_matrix.Read(FileStream);
627 SetMatrix(i, j, working_matrix);
628 working_matrix.Nullify();
635 #ifdef SELDON_CHECK_IO
637 if (!FileStream.good())
638 throw IOError(
"MatrixCollection::Read(istream& FileStream)",
639 "Input operation failed.");
648 #define SELDON_FILE_MATRIX_COLLECTION_CXX