56 #ifndef SPARSE_VECTOR_OP_DEF_H
57 #define SPARSE_VECTOR_OP_DEF_H
59 #include "AbstractLinAlgPack_Types.hpp"
60 #include "AbstractLinAlgPack_SparseVectorClass.hpp"
61 #include "DenseLinAlgPack_DVectorOp.hpp"
62 #include "DenseLinAlgPack_DMatrixAsTriSym.hpp"
63 #include "DenseLinAlgPack_DMatrixClass.hpp"
64 #include "DenseLinAlgPack_AssertOp.hpp"
69 T my_my_max(
const T& v1,
const T& v2 ) {
return v1 > v2 ? v1 : v2; }
72 T my_my_min(
const T& v1,
const T& v2 ) {
return v1 < v2 ? v1 : v2; }
75 namespace AbstractLinAlgPack {
77 using DenseLinAlgPack::VopV_assert_sizes;
78 using DenseLinAlgPack::Vp_V_assert_sizes;
79 using DenseLinAlgPack::Vp_MtV_assert_sizes;
81 using DenseLinAlgPack::row;
82 using DenseLinAlgPack::col;
84 namespace SparseVectorUtilityPack {
85 template<
class T_SpVec>
86 value_type imp_dot2_V_V_SV(
const DVectorSlice& vs1,
const DVectorSlice& vs2,
const T_SpVec& sv);
90 template<
class T_SpVec>
91 value_type dot_V_SV(
const DVectorSlice& vs_rhs1,
const T_SpVec& sv_rhs2) {
92 VopV_assert_sizes(vs_rhs1.dim(),sv_rhs2.dim());
93 value_type result = 0.0;
94 typename T_SpVec::difference_type offset = sv_rhs2.offset();
95 for(
typename T_SpVec::const_iterator iter = sv_rhs2.begin(); iter != sv_rhs2.end(); ++iter)
96 result += vs_rhs1(iter->index()+offset) * iter->value();
101 template<
class T_SpVec>
102 value_type dot_SV_V(
const T_SpVec& sv_rhs1,
const DVectorSlice& vs_rhs2) {
103 return dot_V_SV(vs_rhs2,sv_rhs1);
107 template<
class T_SpVec>
108 value_type norm_1_SV(
const T_SpVec& sv_rhs) {
109 typename T_SpVec::element_type::value_type result = 0.0;
110 for(
typename T_SpVec::const_iterator iter = sv_rhs.begin(); iter != sv_rhs.end(); ++iter)
111 result += ::fabs(iter->value());
116 template<
class T_SpVec>
117 value_type norm_2_SV(
const T_SpVec& sv_rhs) {
118 typename T_SpVec::element_type::value_type result = 0.0;
119 for(
typename T_SpVec::const_iterator iter = sv_rhs.begin(); iter != sv_rhs.end(); ++iter)
120 result += (iter->value()) * (iter->value());
125 template<
class T_SpVec>
126 value_type norm_inf_SV(
const T_SpVec& sv_rhs) {
127 typename T_SpVec::element_type::value_type result = 0.0;
128 for(
typename T_SpVec::const_iterator iter = sv_rhs.begin(); iter != sv_rhs.end(); ++iter)
129 result = my_my_max(result,std::fabs(iter->value()));
134 template<
class T_SpVec>
135 value_type max_SV(
const T_SpVec& sv_rhs) {
136 typename T_SpVec::element_type::value_type result = 0.0;
137 for(
typename T_SpVec::const_iterator iter = sv_rhs.begin(); iter != sv_rhs.end(); ++iter)
138 result = my_my_max(iter->value(),result);
143 template<
class T_SpVec>
144 value_type min_SV(
const T_SpVec& sv_rhs) {
145 typename T_SpVec::element_type::value_type result = 0.0;
146 for(
typename T_SpVec::const_iterator iter = sv_rhs.begin(); iter != sv_rhs.end(); ++iter)
147 result = my_my_min(result,iter->value());
152 template<
class T_SpVec>
153 void Vt_S( T_SpVec* sv_lhs, value_type alpha )
155 if( alpha == 1.0 )
return;
156 for(
typename T_SpVec::iterator iter = sv_lhs->begin(); iter != sv_lhs->end(); ++iter)
157 iter->value() *= alpha;
161 template<
class T_SpVec>
162 void Vp_StSV(DVectorSlice* vs_lhs, value_type alpha,
const T_SpVec& sv_rhs)
164 Vp_V_assert_sizes(vs_lhs->dim(),sv_rhs.dim());
165 typename T_SpVec::difference_type offset = sv_rhs.offset();
166 for(
typename T_SpVec::const_iterator iter = sv_rhs.begin(); iter != sv_rhs.end(); ++iter)
167 (*vs_lhs)(iter->index() + offset) += alpha * iter->value();
171 template<
class T_SpVec>
172 void Vp_StMtSV(DVectorSlice* pvs_lhs, value_type alpha,
const DMatrixSlice& gms_rhs1
178 DVectorSlice& vs_lhs = *pvs_lhs;
180 Vp_MtV_assert_sizes(vs_lhs.dim(),gms_rhs1.rows(),gms_rhs1.cols(),trans_rhs1
190 typename T_SpVec::difference_type offset = sv_rhs2.offset();
192 for(
typename T_SpVec::const_iterator sv_rhs2_itr = sv_rhs2.begin(); sv_rhs2_itr != sv_rhs2.end(); ++sv_rhs2_itr)
194 , col( gms_rhs1, trans_rhs1, sv_rhs2_itr->index() + offset ) );
198 template<
class T_SpVec>
199 void Vp_StMtSV(DVectorSlice* pvs_lhs, value_type alpha,
const DMatrixSliceTri& tri_rhs1
202 DVectorSlice &vs_lhs = *pvs_lhs;
204 Vp_MtV_assert_sizes(vs_lhs.dim(),tri_rhs1.rows(),tri_rhs1.cols(),trans_rhs1
210 (tri_rhs1.uplo() == BLAS_Cpp::upper && trans_rhs1 ==
BLAS_Cpp::trans) )
212 effective_uplo = BLAS_Cpp::lower;
215 effective_uplo = BLAS_Cpp::upper;
222 typename T_SpVec::difference_type offset = sv_rhs2.offset();
223 for(
typename T_SpVec::const_iterator sv_itr = sv_rhs2.begin(); sv_itr != sv_rhs2.end(); ++sv_itr)
251 switch(effective_uplo) {
252 case BLAS_Cpp::lower: {
253 if(tri_rhs1.diag() == BLAS_Cpp::unit)
257 vs_lhs(j) += alpha * sv_itr->value();
263 ,col(tri_rhs1.gms(),trans_rhs1,j)(j_adjusted,n) );
267 case BLAS_Cpp::upper: {
268 if(tri_rhs1.diag() == BLAS_Cpp::unit)
272 vs_lhs(j) += alpha * sv_itr->value();
278 ,col(tri_rhs1.gms(),trans_rhs1,j)(1,j_adjusted) );
287 template<
class T_SpVec>
288 void Vp_StMtSV(DVectorSlice* pvs_lhs, value_type alpha,
const DMatrixSliceSym& sym_rhs1
291 DVectorSlice& vs_lhs = *pvs_lhs;
293 Vp_MtV_assert_sizes(vs_lhs.dim(),sym_rhs1.rows(),sym_rhs1.cols(),trans_rhs1
297 switch(sym_rhs1.uplo()) {
298 case BLAS_Cpp::lower: {
299 DVectorSlice::iterator vs_lhs_itr;
size_type i;
300 for(vs_lhs_itr = vs_lhs.begin(), i = 1; i <= size; ++i)
305 SparseVectorUtilityPack::imp_dot2_V_V_SV(
306 sym_rhs1.gms().row(i)(1,i)
307 ,sym_rhs1.gms().col(i)(i+1,size)
311 *vs_lhs_itr++ += alpha *
312 dot_V_SV(sym_rhs1.gms().row(i),sv_rhs2);
316 case BLAS_Cpp::upper: {
317 DVectorSlice::iterator vs_lhs_itr;
size_type i;
318 for(vs_lhs_itr = vs_lhs.begin(), i = 1; i <= size; ++i)
323 SparseVectorUtilityPack::imp_dot2_V_V_SV(
324 sym_rhs1.gms().col(i)(1,i-1)
325 ,sym_rhs1.gms().row(i)(i,size)
329 *vs_lhs_itr++ += alpha * dot_V_SV(sym_rhs1.gms().row(i),sv_rhs2);
336 namespace SparseVectorUtilityPack {
345 template<
class T_SpVec>
346 value_type imp_dot2_V_V_SV(
const DVectorSlice& vs1,
const DVectorSlice& vs2,
const T_SpVec& sv)
349 value_type result = 0;
350 typename T_SpVec::difference_type offset = sv.offset();
351 for(
typename T_SpVec::const_iterator sv_itr = sv.begin(); sv_itr != sv.end(); ++sv_itr) {
352 typename T_SpVec::element_type::indice_type curr_indice = sv_itr->index()+offset;
353 if(curr_indice <= split)
354 result += vs1(curr_indice) * sv_itr->value();
356 result += vs2(curr_indice - split) * sv_itr->value();
365 #endif // SPARSE_VECTOR_OP_DEF_H
void Vt_S(VectorMutable *v_lhs, const value_type &alpha)
v_lhs *= alpha
void Vp_StV(VectorMutable *v_lhs, const value_type &alpha, const Vector &v_rhs)
v_lhs = alpha * v_rhs + v_lhs