23 #ifndef SELDON_FILE_FUNCTIONS_VECTOR_CXX
24 #define SELDON_FILE_FUNCTIONS_VECTOR_CXX
27 #include "Functions_Vector.hxx"
74 class T1,
class Storage1,
class Allocator1>
92 class T1,
class Storage1,
class Allocator1,
93 class T2,
class Storage2,
class Allocator2>
98 T0 zero; SetComplexZero(zero);
103 #ifdef SELDON_CHECK_DIMENSIONS
107 for (
long i = 0; i < ma; i++)
108 Y(i) += alpha * X(i);
115 class T1,
class Storage1,
class Allocator1,
116 class T2,
class Storage2,
class Allocator2>
122 T0 zero; SetComplexZero(zero);
127 #ifdef SELDON_CHECK_DIMENSIONS
131 for (
long i = 0; i < ma; i++)
132 Y(i) = beta*Y(i) + alpha * X(i);
141 class T1,
class Allocator1,
142 class T2,
class Allocator2>
151 #ifdef SELDON_CHECK_DIMENSIONS
155 VecAXPY(Y.GetPetscVector(), alpha_, X.GetPetscVector());
161 class T1,
class Allocator1,
162 class T2,
class Allocator2>
164 const Vector<T1, PETScPar, Allocator1>& X,
165 Vector<T2, PETScPar, Allocator2>& Y)
171 #ifdef SELDON_CHECK_DIMENSIONS
175 VecAXPY(Y.GetPetscVector(), alpha_, X.GetPetscVector());
182 class T1,
class Allocator1,
183 class T2,
class Allocator2>
189 SetComplexZero(zero);
194 Y.AddInteractionRow(Xalpha.GetSize(),
195 Xalpha.GetIndex(), Xalpha.GetData(),
true);
202 class T1,
class Allocator1,
203 class T2,
class Allocator2>
209 SetComplexZero(zero);
212 for (
int i = 0; i < X.GetM(); i++)
213 Y(X.Index(i)) += alpha * X.Value(i);
220 class T1,
class Allocator1,
221 class T2,
class Allocator2>
227 #ifdef SELDON_CHECK_DIMENSIONS
228 CheckDim(X, Y,
"Add(X, Y)",
"X + Y");
231 for (
int i = 0; i < X.GetNvector(); i++)
232 Add(alpha, X.GetVector(i), Y.GetVector(i));
237 class T1,
template <
class U1>
class Allocator1,
238 class T2,
template <
class U2>
class Allocator2>
241 Vector<FloatDouble, DenseSparseCollection, Allocator1<T1> >& X,
242 Vector<FloatDouble, DenseSparseCollection, Allocator2<T2> >& Y)
245 #ifdef SELDON_CHECK_DIMENSIONS
249 AddVector(alpha, X.GetFloatDense(), Y.GetFloatDense());
250 AdVectord(alpha, X.GetFloatSparse(), Y.GetFloatSparse());
251 AddVector(alpha, X.GetDoubleDense(), Y.GetDoubleDense());
252 AddVector(alpha, X.GetDoubleSparse(), Y.GetDoubleSparse());
258 class T1,
template <
class U1>
class Allocator1,
259 class T2,
class Storage2,
class Allocator2>
267 double alpha_ = alpha;
271 #ifdef SELDON_CHECK_DIMENSIONS
275 for (
int i = 0; i < ma; i++)
276 Y(i) += alpha_ * X(i);
291 template <
class T1,
class Storage1,
class Allocator1,
292 class T2,
class Storage2,
class Allocator2>
301 template <
class T1,
class Allocator1,
302 class T2,
class Allocator2>
307 for (
int i = 0; i < X.GetNvector(); i++)
308 Y.PushBack(X.GetVector(i));
312 template<
class T,
class Alloc1,
class Alloc2>
313 void CopyVector(
const Vector<T, PETScPar, Alloc1>& A,
314 Vector<T, VectFull, Alloc2>& B)
316 B.Reallocate(A.GetLocalM());
318 VecGetArray(A.GetPetscVector(), &local_data);
319 for (
int i = 0; i < A.GetLocalM(); i++)
320 B(i) = local_data[i];
321 VecRestoreArray(A.GetPetscVector(), &local_data);
325 template<
class T,
class Alloc1,
class Alloc2>
326 void CopyVector(
const Vector<T, VectFull, Alloc1>& A,
327 Vector<T, PETScPar, Alloc2>& B)
330 VecGetArray(B.GetPetscVector(), &local_data);
331 for (
int i = 0; i < A.GetM(); i++)
332 local_data[i] = A(i);
333 VecRestoreArray(B.GetPetscVector(), &local_data);
347 template <
class T,
class Storage,
class Allocator>
352 T* data = X.GetData();
354 X.SetData(Y.GetM(), Y.GetData());
361 template <
class T,
class Allocator>
380 T* data = X.GetData();
382 X.SetData(Y.GetM(), Y.GetData());
397 template<
class T1,
class Storage1,
class Allocator1,
398 class T2,
class Storage2,
class Allocator2>
403 SetComplexZero(value);
405 #ifdef SELDON_CHECK_DIMENSIONS
409 for (
long i = 0; i < X.GetM(); i++)
410 value += X(i) * Y(i);
417 template<
class T1,
class Allocator1,
418 class T2,
class Allocator2>
419 typename T1::value_type
423 typename T1::value_type value(0);
425 #ifdef SELDON_CHECK_DIMENSIONS
426 CheckDim(X, Y,
"DotProd(X, Y)",
"<X, Y>");
429 for (
int i = 0; i < X.GetNvector(); i++)
436 template<
class T1,
template <
class U1>
class Allocator1,
437 class T2,
template <
class U2>
class Allocator2>
445 #ifdef SELDON_CHECK_DIMENSIONS
450 value +=
DotProdVector(X.GetFloatDense(), Y.GetFloatDense());
451 value +=
DotProdVector(X.GetFloatSparse(), Y.GetFloatSparse());
452 value +=
DotProdVector(X.GetDoubleDense(), Y.GetDoubleDense());
453 value +=
DotProdVector(X.GetDoubleSparse(), Y.GetDoubleSparse());
459 template<
class T1,
class Storage1,
class Allocator1,
460 class T2,
class Storage2,
class Allocator2>
465 SetComplexZero(value);
467 #ifdef SELDON_CHECK_DIMENSIONS
468 CheckDim(X, Y,
"DotProdConj(X, Y)");
471 for (
long i = 0; i < X.GetM(); i++)
472 value += conjugate(X(i)) * Y(i);
479 template<
class T1,
class Allocator1,
480 class T2,
class Allocator2>
485 SetComplexZero(value);
487 int size_x = X.GetSize();
488 int size_y = Y.GetSize();
489 int kx = 0, ky = 0, pos_x;
493 while (ky < size_y && Y.Index(ky) < pos_x)
496 if (ky < size_y && Y.Index(ky) == pos_x)
497 value += X.Value(kx) * Y.Value(ky);
507 template<
class T1,
class Allocator1,
508 class T2,
class Allocator2>
513 SetComplexZero(value);
514 for (
int i = 0; i < X.GetM(); i++)
515 value += X.Value(i)*Y(X.Index(i));
522 template<
class T1,
class Allocator1,
523 class T2,
class Allocator2>
532 template<
class T1,
class Allocator1,
533 class T2,
class Allocator2>
539 SetComplexZero(value);
541 int size_x = X.GetSize();
542 int size_y = Y.GetSize();
543 int kx = 0, ky = 0, pos_x;
547 while (ky < size_y && Y.Index(ky) < pos_x)
550 if (ky < size_y && Y.Index(ky) == pos_x)
551 value += conjugate(X.Value(kx)) * Y.Value(ky);
561 template<
class T1,
class Allocator1,
562 class T2,
class Allocator2>
568 SetComplexZero(value);
570 for (
int i = 0; i < X.GetM(); i++)
571 value += conjugate(X.Value(i))*Y(X.Index(i));
590 template<
class T1,
class Storage1,
class Allocator1>
591 typename ClassComplexType<T1>::Treal
594 typename ClassComplexType<T1>::Treal value(0);
596 for (
long i = 0; i < X.GetM(); i++)
608 template<
class T1,
class Allocator1>
609 typename ClassComplexType<T1>::Treal
612 typename ClassComplexType<T1>::Treal value(0);
614 for (
int i = 0; i < X.GetM(); i++)
630 template<
class T1,
class Storage1,
class Allocator1>
635 for (
long i = 0; i < X.GetM(); i++)
636 value += X(i) * X(i);
643 template<
class T1,
class Storage1,
class Allocator1>
648 for (
size_t i = 0; i < X.GetSize(); i++)
656 template<
class T1,
class Allocator1>
661 for (
size_t i = 0; i < X.GetSize(); i++)
662 value += X.Value(i) * X.Value(i);
669 template<
class T1,
class Allocator1>
674 for (
size_t i = 0; i < X.GetSize(); i++)
690 template<
class T,
class Storage,
class Allocator>
693 return X.GetNormInfIndex();
707 void GenRot(T& a_in, T& b_in, T& c_, T& s_)
711 if (abs(a_in) > abs(b_in))
716 T scal = abs(a_in) + abs(b_in);
720 T a_scl = a_in / scal;
721 T b_scl = b_in / scal;
722 r = scal * sqrt(a_scl * a_scl + b_scl * b_scl);
729 if (abs(a_in) > abs(b_in))
731 else if (abs(b_in) >= abs(a_in) && c_ != T(0))
748 void GenRot(complex<T>& a_in, complex<T>& b_in, T& c_, complex<T>& s_)
751 T a = abs(a_in), b = abs(b_in);
755 s_ = complex<T>(1, 0);
761 T a_scal = abs(a_in / scale);
762 T b_scal = abs(b_in / scale);
763 T norm = sqrt(a_scal * a_scal + b_scal * b_scal) * scale;
766 complex<T> alpha = a_in / a;
767 s_ = alpha * conjugate(b_in) / norm;
770 b_in = complex<T>(0, 0);
776 void ApplyRot(T& x, T& y,
const T& c_,
const T& s_)
778 T temp = c_ * x + s_ * y;
787 const T& c_,
const complex<T>& s_)
789 complex<T> temp = s_ * y + c_ * x;
790 y = -conjugate(s_) * x + c_ * y;
796 template<
class T,
class Allocator1,
class Allocator2>
799 const T& c,
const T& s)
802 for (
long i = 0; i < X.GetM(); i++)
804 tmp = c*X(i) + s*Y(i);
805 Y(i) = c*Y(i) - s*X(i);
812 template<
class T,
class Allocator1,
class Allocator2>
815 const T& c,
const T& s)
818 for (
int i = 0; i < X.GetM(); i++)
820 tmp = c*X.Value(i) + s*Y(X.Index(i));
821 Y(X.Index(i)) = c*Y(X.Index(i)) - s*X.Value(i);
846 template <
class T0,
class Storage0,
class Allocator0,
847 class T1,
class Storage1,
class Allocator1>
850 string function,
string op)
852 #ifndef SELDON_WITHOUT_TO_STR_CHECKDIM
855 string Xchar(
"X"), Ychar(
"Y");
858 if (X.GetLength() != Y.GetLength())
859 throw WrongDim(
function,
string(
"Operation ") + op
860 +
string(
" not permitted:")
861 +
string(
"\n X (") + Xchar +
string(
") is a ")
862 +
string(
"vector of length ") +
to_str(X.GetLength())
863 +
string(
";\n Y (") + Ychar +
string(
") is a ")
864 +
string(
"vector of length ") +
to_str(Y.GetLength())
880 template <
class T0,
class Allocator0,
881 class T1,
class Allocator1>
884 string function,
string op)
902 template <
class T0,
class Allocator0,
903 class T1,
class Allocator1>
906 string function,
string op)
908 if (X.GetLength() != Y.GetLength())
909 throw WrongDim(
function,
string(
"Operation ") + op
910 +
string(
" not permitted:")
911 +
string(
"\n X (") +
to_str(&X) +
string(
") is a ")
912 +
string(
"vector of length ") +
to_str(X.GetLength())
913 +
string(
";\n Y (") +
to_str(&Y) +
string(
") is a ")
914 +
string(
"vector of length ") +
to_str(Y.GetLength())
917 if (X.GetNvector() != Y.GetNvector())
918 throw WrongDim(
function,
string(
"Operation ") + op
919 +
string(
" not permitted:")
920 +
string(
"\n X (") +
to_str(&X) +
string(
") is a ")
921 +
string(
"vector of length ") +
to_str(X.GetNvector())
922 +
string(
";\n Y (") +
to_str(&Y) +
string(
") is a ")
923 +
string(
"vector of length ") +
to_str(Y.GetNvector())
939 template <
class T0,
class Allocator0,
class Allocator00,
940 class T1,
class Allocator1,
class Allocator11>
945 string function,
string op)
947 #ifndef SELDON_WITHOUT_TO_STR_CHECKDIM
950 string Xchar(
"X"), Ychar(
"Y");
953 if (X.GetNvector() != Y.GetNvector())
954 throw WrongDim(
function,
string(
"Operation ") + op
955 +
string(
" not permitted:")
956 +
string(
"\n X (") + Xchar +
string(
") is a ")
957 +
string(
"vector of length ") +
to_str(X.GetNvector())
958 +
string(
";\n Y (") + Ychar +
string(
") is a ")
959 +
string(
"vector of length ") +
to_str(Y.GetNvector())
975 template <
class T0,
class Allocator0,
976 class T1,
class Allocator1>
979 string function,
string op)
981 #ifndef SELDON_WITHOUT_TO_STR_CHECKDIM
984 string Xchar(
"X"), Ychar(
"Y");
987 if (X.GetLength() != Y.GetM())
988 throw WrongDim(
function,
string(
"Operation ") + op
989 +
string(
" not permitted:")
990 +
string(
"\n X (") + Xchar +
string(
") is a ")
991 +
string(
"vector of length ") +
to_str(X.GetLength())
992 +
string(
";\n Y (") + Ychar +
string(
") is a ")
993 +
string(
"vector of length ") +
to_str(Y.GetM())
1009 template <
class T0,
template <
class U0>
class Allocator0,
1010 class T1,
template <
class U1>
class Allocator1>
1016 string function,
string op)
1018 #ifndef SELDON_WITHOUT_TO_STR_CHECKDIM
1021 string Xchar(
"X"), Ychar(
"Y");
1024 if (X.GetNvector() != Y.GetNvector())
1025 throw WrongDim(
function,
string(
"Operation ") + op
1026 +
string(
" not permitted:")
1027 +
string(
"\n X (") + Xchar +
string(
") is a ")
1028 +
string(
"vector of length ") +
to_str(X.GetNvector())
1029 +
string(
";\n Y (") + Ychar +
string(
") is a ")
1030 +
string(
"vector of length ") +
to_str(Y.GetNvector())
1043 template<
class T,
class Allocator1,
class Allocator2>
1044 void GatherSparseEntry(
const Vector<T, VectFull, Allocator1>& Y,
1045 Vector<T, VectSparse, Allocator2>& X)
1047 for (
int i = 0; i < X.GetM(); i++)
1048 X.Value(i) = Y(X.Index(i));
1051 template<
class T,
class Allocator1,
class Allocator2>
1052 void GatherSparseEntryZero(Vector<T, VectFull, Allocator1>& Y,
1053 Vector<T, VectSparse, Allocator2>& X)
1055 T zero; SetComplexZero(zero);
1056 for (
int i = 0; i < X.GetM(); i++)
1058 X.Value(i) = Y(X.Index(i));
1059 Y(X.Index(i)) = zero;
1064 template<
class T,
class Allocator1,
class Allocator2>
1065 void ScatterSparseEntry(
const Vector<T, VectSparse, Allocator1>& X,
1066 Vector<T, VectFull, Allocator2>& Y)
1068 for (
int i = 0; i < X.GetM(); i++)
1069 Y(X.Index(i)) = X.Value(i);
1082 template<
class T,
class Prop,
class Allocator>
1085 for (
long i = 0; i < X.GetM(); i++)
1086 X(i) = conjugate(X(i));
1091 template<
class T,
class Allocator>
1094 for (
size_t i = 0; i < X.
GetSize(); i++)