added: allow specifying offset and stride for vector in add

This commit is contained in:
Arne Morten Kvarving 2023-09-14 11:55:21 +02:00
parent d50328df50
commit 4865cc317b

View File

@ -128,7 +128,8 @@ namespace utl //! General utility classes and functions.
//! \brief Subtract the given vector \b X from \a *this.
vector<T>& operator-=(const vector<T>& X) { return this->add(X,T(-1)); }
//! \brief Add the given vector \b X scaled by \a alfa to \a *this.
vector<T>& add(const std::vector<T>& X, const T& alfa = T(1));
vector<T>& add(const std::vector<T>& X, const T& alfa = T(1),
int ofsx = 0, int stridex = 1);
//! \brief Perform \f${\bf Y} = \alpha{\bf Y} + (1-\alpha){\bf X} \f$
//! where \b Y = \a *this.
@ -963,19 +964,21 @@ namespace utl //! General utility classes and functions.
template<> inline
vector<float>& vector<float>::add(const std::vector<float>& X,
const float& alfa)
const float& alfa,
int ofsx, int stridex)
{
size_t n = this->size() < X.size() ? this->size() : X.size();
cblas_saxpy(n,alfa,&X.front(),1,this->ptr(),1);
cblas_saxpy(n,alfa,X.data()+ofsx,stridex,this->data(),1);
return *this;
}
template<> inline
vector<double>& vector<double>::add(const std::vector<double>& X,
const double& alfa)
const double& alfa,
int ofsx, int stridex)
{
size_t n = this->size() < X.size() ? this->size() : X.size();
cblas_daxpy(n,alfa,&X.front(),1,this->ptr(),1);
cblas_daxpy(n,alfa,X.data()+ofsx,stridex,this->data(),1);
return *this;
}
@ -1345,11 +1348,12 @@ namespace utl //! General utility classes and functions.
}
template<class T> inline
vector<T>& vector<T>::add(const std::vector<T>& X, const T& alfa)
vector<T>& vector<T>::add(const std::vector<T>& X, const T& alfa,
int ofsx, int stridex)
{
T* p = this->ptr();
const T* q = &X.front();
for (size_t i = 0; i < this->size() && i < X.size(); i++, p++, q++)
const T* q = X.data() + ofsx;
for (size_t i = 0; i < this->size() && i < X.size(); i++, p++, q += stridex)
*p += alfa*(*q);
return *this;
}