21 #ifndef SELDON_FILE_MATRIX_ARRAY_COMPLEX_SPARSE_CXX
23 #include "Matrix_ArrayComplexSparse.hxx"
33 template <
class T,
class Prop,
class Storage,
class Allocator>
38 for (
int i = 0; i < this->val_real_.GetM(); i++)
39 nnz += this->val_real_(i).GetM();
49 template <
class T,
class Prop,
class Storage,
class Allocator>
54 for (
int i = 0; i < this->val_imag_.GetM(); i++)
55 nnz += this->val_imag_(i).GetM();
62 template<
class T,
class Prop,
class Storage,
class Allocator>
66 size_t taille =
sizeof(*this) +
sizeof(pointer)*this->val_real_.GetM();
67 taille +=
sizeof(pointer)*this->val_imag_.GetM();
68 for (
int i = 0; i < val_real_.GetM(); i++)
69 taille += val_real_(i).GetMemorySize();
71 for (
int i = 0; i < val_imag_.GetM(); i++)
72 taille += val_imag_(i).GetMemorySize();
90 template <
class T,
class Prop,
class Storage,
class Allocator>
100 int n = Storage::GetFirst(i,j);
101 val_real_.Reallocate(n);
102 val_imag_.Reallocate(n);
113 template <
class T,
class Prop,
class Storage,
class Allocator>
117 int n = Storage::GetFirst(this->m_, this->n_);
118 int new_n = Storage::GetFirst(i, j);
127 new_val_real.Reallocate(new_n);
128 new_val_imag.Reallocate(new_n);
130 for (
int k = 0 ; k < min(n, new_n) ; k++)
132 Swap(new_val_real(k), this->val_real_(k));
133 Swap(new_val_imag(k), this->val_imag_(k));
136 val_real_.SetData(new_n, new_val_real.GetData());
137 val_imag_.SetData(new_n, new_val_imag.GetData());
138 new_val_real.Nullify();
139 new_val_imag.Nullify();
159 template <
class T,
class Prop,
class Storage,
class Allocator>
162 ::Set(
int i,
int j,
const entry_type& x)
164 if (real(x) != value_type(0))
165 val_real_(Storage::GetFirst(i, j)).Get(Storage::GetSecond(i, j)) = real(x);
168 if (val_real_(Storage::GetFirst(i, j))
169 (Storage::GetSecond(i, j)) != value_type(0))
170 val_real_(Storage::GetFirst(i, j)).
171 Get(Storage::GetSecond(i, j)) = value_type(0);
174 if (imag(x) != value_type(0))
175 val_imag_(Storage::GetFirst(i, j)).
176 Get(Storage::GetSecond(i, j)) = imag(x);
179 if (val_imag_(Storage::GetFirst(i, j))
180 (Storage::GetSecond(i, j)) != value_type(0))
181 val_imag_(Storage::GetFirst(i, j)).
182 Get(Storage::GetSecond(i, j)) = value_type(0);
194 template <
class T,
class Prop,
class Storage,
class Allocator>
197 if (Storage::GetFirst(1, 0) == 1)
198 for (
int i = 0; i < this->m_; i++)
200 for (
int j = 0; j < this->val_real_(i).GetM(); j++)
201 cout << (i+1) <<
" " << this->val_real_(i).Index(j)+1
202 <<
" " << this->val_real_(i).Value(j) << endl;
204 for (
int j = 0; j < this->val_imag_(i).GetM(); j++)
205 cout << (i+1) <<
" " << this->val_imag_(i).Index(j)+1
206 <<
" (0, " << this->val_imag_(i).Value(j) <<
")"<<endl;
209 for (
int i = 0; i < this->n_; i++)
211 for (
int j = 0; j < this->val_real_(i).GetM(); j++)
212 cout << this->val_real_(i).Index(j)+1 <<
" " << i+1
213 <<
" " << this->val_real_(i).Value(j) << endl;
215 for (
int j = 0; j < this->val_imag_(i).GetM(); j++)
216 cout << this->val_imag_(i).Index(j)+1 <<
" " << i+1
217 <<
" (0, " << this->val_imag_(i).Value(j) <<
")"<<endl;
230 template <
class T,
class Prop,
class Storage,
class Allocator>
235 FileStream.open(FileName.c_str(), ofstream::binary);
237 #ifdef SELDON_CHECK_IO
239 if (!FileStream.is_open())
240 throw IOError(
"Matrix_ArrayComplexSparse::Write(string FileName)",
241 string(
"Unable to open file \"") + FileName +
"\".");
244 this->Write(FileStream);
258 template <
class T,
class Prop,
class Storage,
class Allocator>
263 #ifdef SELDON_CHECK_IO
265 if (!FileStream.good())
266 throw IOError(
"Matrix_ArrayComplexSparse::Write(ofstream& FileStream)",
267 "Stream is not ready.");
270 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
273 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
276 for (
int i = 0; i < val_real_.GetM(); i++)
278 val_real_(i).Write(FileStream);
279 val_imag_(i).Write(FileStream);
289 template <
class T,
class Prop,
class Storage,
class Allocator>
293 ofstream FileStream; FileStream.precision(14);
294 FileStream.open(FileName.c_str());
297 FileStream.precision(cout.precision());
299 #ifdef SELDON_CHECK_IO
301 if (!FileStream.is_open())
302 throw IOError(
"Matrix_ArrayComplexSparse::WriteText(string FileName)",
303 string(
"Unable to open file \"") + FileName +
"\".");
306 this->WriteText(FileStream, cplx);
317 template <
class T,
class Prop,
class Storage,
class Allocator>
322 #ifdef SELDON_CHECK_IO
324 if (!FileStream.good())
325 throw IOError(
"Matrix_ArrayComplexSparse::"
326 "WriteText(ofstream& FileStream)",
327 "Stream is not ready.");
334 entry_type zero;
int index = 1;
335 WriteCoordinateMatrix(leaf_class, FileStream, zero, index, cplx);
344 template <
class T,
class Prop,
class Storage,
class Allocator>
349 FileStream.open(FileName.c_str(), ifstream::binary);
351 #ifdef SELDON_CHECK_IO
353 if (!FileStream.is_open())
354 throw IOError(
"Matrix_ArrayComplexSparse::Read(string FileName)",
355 string(
"Unable to open file \"") + FileName +
"\".");
358 this->Read(FileStream);
369 template <
class T,
class Prop,
class Storage,
class Allocator>
374 #ifdef SELDON_CHECK_IO
376 if (!FileStream.good())
377 throw IOError(
"Matrix_ArraySparse::Read(ofstream& FileStream)",
378 "Stream is not ready.");
381 FileStream.read(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
384 FileStream.read(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
387 val_real_.Reallocate(Storage::GetFirst(this->m_, this->n_));
388 val_imag_.Reallocate(Storage::GetFirst(this->m_, this->n_));
389 for (
int i = 0; i < val_real_.GetM(); i++)
391 val_real_(i).Read(FileStream);
392 val_imag_(i).Read(FileStream);
395 #ifdef SELDON_CHECK_IO
397 if (!FileStream.good())
398 throw IOError(
"Matrix_ArraySparse::Read(istream& FileStream)",
399 string(
"Input operation failed.")
400 +
string(
" The input file may have been removed")
401 +
" or may not contain enough data.");
412 template <
class T,
class Prop,
class Storage,
class Allocator>
417 FileStream.open(FileName.c_str());
419 #ifdef SELDON_CHECK_IO
421 if (!FileStream.is_open())
422 throw IOError(
"Matrix_ArraySparse::ReadText(string FileName)",
423 string(
"Unable to open file \"") + FileName +
"\".");
426 this->ReadText(FileStream, cplx);
437 template <
class T,
class Prop,
class Storage,
class Allocator>
444 entry_type zero;
int index = 1;
445 ReadCoordinateMatrix(leaf_class, FileStream, zero, index, -1, cplx);
456 template <
class T,
class Prop,
class Storage,
class Allocator>
459 for (
int i = 0; i < Storage::GetFirst(this->m_, this->n_); i++)
461 val_real_(i).Assemble();
462 val_imag_(i).Assemble();
468 template <
class T,
class Prop,
class Storage,
class Allocator>
template<
class T0>
472 for (
int i = 0; i < val_real_.GetM(); i++)
473 val_real_(i).RemoveSmallEntry(epsilon);
475 for (
int i = 0; i < val_imag_.GetM(); i++)
476 val_imag_(i).RemoveSmallEntry(epsilon);
481 template <
class T,
class Prop,
class Storage,
class Allocator>
486 for (
int i = 0; i < this->m_; i++)
488 val_imag_(i).Clear();
489 val_real_(i).Reallocate(1);
490 val_real_(i).Index(0) = i;
491 val_real_(i).Value(0) = value_type(1);
497 template <
class T,
class Prop,
class Storage,
class Allocator>
500 for (
int i = 0; i < Storage::GetFirst(this->m_, this->n_); i++)
509 template <
class T,
class Prop,
class Storage,
class Allocator>
513 for (
int i = 0; i < Storage::GetFirst(this->m_, this->n_); i++)
515 for (
int j = 0; j < val_real_(i).GetM(); j++)
518 for (
int j = 0; j < val_imag_(i).GetM(); j++)
529 template <
class T,
class Prop,
class Storage,
class Allo>
template<
class T0>
533 for (
int i = 0; i < Storage::GetFirst(this->m_, this->n_); i++)
535 val_real_(i).Fill(real(x));
536 val_imag_(i).Fill(imag(x));
542 template <
class T,
class Prop,
class Storage,
class Allocator>
546 (
const complex<T0>& x)
553 template <
class T,
class Prop,
class Storage,
class Allocator>
556 #ifndef SELDON_WITHOUT_REINIT_RANDOM
559 for (
int i = 0; i < Storage::GetFirst(this->m_, this->n_); i++)
561 val_real_(i).FillRand();
562 val_imag_(i).FillRand();
579 template <
class T,
class Prop,
class Allocator>
584 for (
int j = 0; j < nb; j++)
585 AddInteraction(i, col(j), val(j));
596 template <
class T,
class Prop,
class Allocator>
603 IVect row_real(nb), row_imag(nb);
605 for (
int j = 0; j < nb; j++)
607 if (real(val(j)) != value_type(0))
609 row_real(nb_real) = row(j);
610 val_real(nb_real) = real(val(j));
614 if (imag(val(j)) != value_type(0))
616 row_imag(nb_imag) = row(j);
617 val_imag(nb_imag) = imag(val(j));
622 this->val_real_(i).AddInteractionRow(nb_real, row_real, val_real, sorted);
623 this->val_imag_(i).AddInteractionRow(nb_imag, row_imag, val_imag, sorted);
639 template <
class T,
class Prop,
class Allocator>
649 IVect col_real(nb), col_imag(nb);
651 for (
int j = 0; j < nb; j++)
653 if (real(val(j)) != value_type(0))
655 col_real(nb_real) = col(j);
656 val_real(nb_real) = real(val(j));
660 if (imag(val(j)) != value_type(0))
662 col_imag(nb_imag) = col(j);
663 val_imag(nb_imag) = imag(val(j));
668 this->val_real_(i).AddInteractionRow(nb_real, col_real, val_real, sorted);
669 this->val_imag_(i).AddInteractionRow(nb_imag, col_imag, val_imag, sorted);
680 template <
class T,
class Prop,
class Allocator>
685 for (
int j = 0; j < nb; j++)
686 AddInteraction(row(j), i, val(j));
701 template <
class T,
class Prop,
class Allocator>
703 ::Set(
int i,
int j,
const entry_type& x)
707 if (real(x) != value_type(0))
708 this->val_real_(j).Get(i) = real(x);
711 if (this->val_real_(j)(i) != value_type(0))
712 this->val_real_(j).Get(i) = value_type(0);
715 if (imag(x) != value_type(0))
716 this->val_imag_(j).Get(i) = imag(x);
719 if (this->val_imag_(j)(i) != value_type(0))
720 this->val_imag_(j).Get(i) = value_type(0);
725 if (real(x) != value_type(0))
726 this->val_real_(i).Get(j) = real(x);
729 if (this->val_real_(i)(j) != value_type(0))
730 this->val_real_(i).Get(j) = value_type(0);
733 if (imag(x) != value_type(0))
734 this->val_imag_(i).Get(j) = imag(x);
737 if (this->val_imag_(i)(j) != value_type(0))
738 this->val_imag_(i).Get(j) = value_type(0);
750 template <
class T,
class Prop,
class Allocator>
756 if (real(val) != value_type(0))
757 this->val_real_(j).AddInteraction(i, real(val));
759 if (imag(val) != value_type(0))
760 this->val_imag_(j).AddInteraction(i, imag(val));
772 template <
class T,
class Prop,
class Allocator>
777 for (
int j = 0; j < nb; j++)
778 AddInteraction(i, col(j), val(j));
789 template <
class T,
class Prop,
class Allocator>
796 IVect row_real(nb), row_imag(nb);
798 for (
int j = 0; j < nb; j++)
801 if (real(val(j)) != value_type(0))
803 row_real(nb_real) = row(j);
804 val_real(nb_real) = real(val(j));
808 if (imag(val(j)) != value_type(0))
810 row_imag(nb_imag) = row(j);
811 val_imag(nb_imag) = imag(val(j));
816 this->val_real_(i).AddInteractionRow(nb_real, row_real, val_real, sorted);
817 this->val_imag_(i).AddInteractionRow(nb_imag, row_imag, val_imag, sorted);
832 template <
class T,
class Prop,
class Allocator>
834 ::Set(
int i,
int j,
const entry_type& x)
838 if (real(x) != value_type(0))
839 this->val_real_(i).Get(j) = real(x);
842 if (this->val_real_(i)(j) != value_type(0))
843 this->val_real_(i).Get(j) = value_type(0);
846 if (imag(x) != value_type(0))
847 this->val_imag_(i).Get(j) = imag(x);
850 if (this->val_imag_(i)(j) != value_type(0))
851 this->val_imag_(i).Get(j) = value_type(0);
856 if (real(x) != value_type(0))
857 this->val_real_(j).Get(i) = real(x);
860 if (this->val_real_(j)(i) != value_type(0))
861 this->val_real_(j).Get(i) = value_type(0);
864 if (imag(x) != value_type(0))
865 this->val_imag_(j).Get(i) = imag(x);
868 if (this->val_imag_(j)(i) != value_type(0))
869 this->val_imag_(j).Get(i) = value_type(0);
881 template <
class T,
class Prop,
class Allocator>
887 if (real(val) != value_type(0))
888 this->val_real_(i).AddInteraction(j, real(val));
890 if (imag(val) != value_type(0))
891 this->val_imag_(i).AddInteraction(j, imag(val));
903 template <
class T,
class Prop,
class Allocator>
910 IVect col_real(nb), col_imag(nb);
912 for (
int j = 0; j < nb; j++)
915 if (real(val(j)) != value_type(0))
917 col_real(nb_real) = col(j);
918 val_real(nb_real) = real(val(j));
922 if (imag(val(j)) != value_type(0))
924 col_imag(nb_imag) = col(j);
925 val_imag(nb_imag) = imag(val(j));
930 this->val_real_(i).AddInteractionRow(nb_real, col_real, val_real, sorted);
931 this->val_imag_(i).AddInteractionRow(nb_imag, col_imag, val_imag, sorted);
942 template <
class T,
class Prop,
class Allocator>
947 for (
int j = 0; j < nb; j++)
948 AddInteraction(row(j), i, val(j));
953 #define SELDON_FILE_MATRIX_ARRAY_COMPLEX_SPARSE_CXX