IT++ Logo

copy_vector.h

Go to the documentation of this file.
00001 
00030 #ifndef COPY_VECTOR_H
00031 #define COPY_VECTOR_H
00032 
00033 #ifndef _MSC_VER
00034 #  include <itpp/config.h>
00035 #else
00036 #  include <itpp/config_msvc.h>
00037 #endif
00038 
00039 #if defined (HAVE_BLAS)
00040 #  include <itpp/base/blas.h>
00041 #endif
00042 
00043 #include <itpp/base/binary.h>
00044 
00045 
00047 
00048 namespace itpp {
00049 
00050 
00051   /*
00052     Copy vector x to vector y. Both vectors are of size n
00053   */
00054   inline void copy_vector(const int n, const int *x, int *y) { memcpy(y, x, (unsigned int)n*sizeof(int)); }
00055   inline void copy_vector(const int n, const short *x, short *y) { memcpy(y, x, (unsigned int)n*sizeof(short)); }
00056   inline void copy_vector(const int n, const bin *x, bin *y) { memcpy(y, x, (unsigned int)n*sizeof(bin)); }
00057   inline void copy_vector(const int n, const float *x, float *y) { memcpy(y, x, (unsigned int)n*sizeof(float)); }
00058   inline void copy_vector(const int n, const std::complex<float> *x, std::complex<float> *y) { memcpy(y, x, (unsigned int)n*sizeof(std::complex<float>)); }
00059 
00060 #if defined (HAVE_BLAS)
00061   inline void copy_vector(const int n, const double *x, double *y)
00062   {
00063     int incr = 1;
00064     blas::dcopy_(&n, x, &incr, y, &incr);
00065   }
00066   inline void copy_vector(const int n, const std::complex<double> *x,
00067         std::complex<double> *y)
00068   {
00069     int incr = 1;
00070     blas::zcopy_(&n, x, &incr, y, &incr);
00071   }
00072 #else
00073   inline void copy_vector(const int n, const double *x, double *y) { memcpy(y, x, (unsigned int)n*sizeof(double)); }
00074   inline void copy_vector(const int n, const std::complex<double> *x, std::complex<double> *y) { memcpy(y, x, (unsigned int)n*sizeof(std::complex<double>)); }
00075 #endif
00076 
00077   template<class T> inline
00078   void copy_vector(const int n, const T *x, T *y)
00079   {
00080     for (int i=0; i<n; i++)
00081       y[i] = x[i];
00082   }
00083 
00084 
00085 
00086 
00087   /*
00088     Copy vector x to vector y. Both vectors are of size n
00089     vector x elements are stored linearly with element increament incx
00090     vector y elements are stored linearly with element increament incx
00091   */
00092 #if defined (HAVE_BLAS)
00093   inline void copy_vector(const int n, const double *x, const int incx,
00094         double *y, const int incy)
00095   {
00096     blas::dcopy_(&n, x, &incx, y, &incy);
00097   }
00098   inline void copy_vector(const int n, const std::complex<double> *x,
00099         const int incx, std::complex<double> *y,
00100         const int incy)
00101   {
00102     blas::zcopy_(&n, x, &incx, y, &incy);
00103   }
00104 #endif
00105 
00106   template<class T> inline
00107   void copy_vector(const int n, const T *x, const int incx, T *y, const int incy)
00108   {
00109     for (int i=0;i<n; i++)
00110       y[i*incy] = x[i*incx];
00111   }
00112 
00113 
00114   /*
00115     Swap vector x to vector y. Both vectors are of size n
00116   */
00117   inline void swap_vector(const int n, int *x, int *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00118   inline void swap_vector(const int n, short *x, short *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00119   inline void swap_vector(const int n, bin *x, bin *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00120   inline void swap_vector(const int n, float *x, float *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00121   inline void swap_vector(const int n, std::complex<float> *x, std::complex<float> *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00122 
00123 #if defined (HAVE_BLAS)
00124   inline void swap_vector(const int n, double *x, double *y)
00125   {
00126     int incr = 1;
00127     blas::dswap_(&n, x, &incr, y, &incr);
00128   }
00129   inline void swap_vector(const int n, std::complex<double> *x,
00130         std::complex<double> *y)
00131   {
00132     int incr = 1;
00133     blas::zswap_(&n, x, &incr, y, &incr);
00134   }
00135 #else
00136   inline void swap_vector(const int n, double *x, double *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00137   inline void swap_vector(const int n, std::complex<double> *x, std::complex<double> *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00138 #endif
00139 
00140   template<class T> inline
00141   void swap_vector(const int n, T *x, T *y)
00142   {
00143     T tmp;
00144     for (int i=0; i<n; i++) {
00145       tmp = y[i];
00146       y[i] = x[i];
00147       x[i] = tmp;
00148     }
00149   }
00150 
00151 
00152   /*
00153     Swap vector x to vector y. Both vectors are of size n
00154     vector x elements are stored linearly with element increament incx
00155     vector y elements are stored linearly with element increament incx
00156   */
00157   inline void swap_vector(const int n, int *x, const int incx, int *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00158   inline void swap_vector(const int n, short *x, const int incx, short *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00159   inline void swap_vector(const int n, bin *x, const int incx, bin *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00160   inline void swap_vector(const int n, float *x, const int incx, float *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00161   inline void swap_vector(const int n, std::complex<float> *x, const int incx, std::complex<float> *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00162 
00163 #if defined (HAVE_BLAS)
00164   inline void swap_vector(const int n, double *x, const int incx, double *y,
00165         const int incy)
00166   {
00167     blas::dswap_(&n, x, &incx, y, &incy);
00168   }
00169   inline void swap_vector(const int n, std::complex<double> *x, const int incx,
00170         std::complex<double> *y, const int incy)
00171   {
00172     blas::zswap_(&n, x, &incx, y, &incy);
00173   }
00174 #else
00175   inline void swap_vector(const int n, double *x, const int incx, double *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00176   inline void swap_vector(const int n, std::complex<double> *x, const int incx, std::complex<double> *y, const int incy) { for (int i=0; i<n; i++) std::swap(x[i*incx], y[i*incy]); }
00177 #endif
00178 
00179   template<class T> inline
00180   void swap_vector(const int n, T *x, const int incx, T *y, const int incy)
00181   {
00182     T tmp;
00183     for (int i=0; i<n; i++) {
00184       tmp = y[i*incy];
00185       y[i*incy] = x[i*incx];
00186       x[i*incx] = tmp;
00187     }
00188   }
00189 
00190 
00191   /*
00192    * Realise scaling operation: x = alpha*x
00193    */
00194 #if defined(HAVE_BLAS)
00195   inline void scal_vector(int n, double alpha, double *x)
00196   {
00197     int incr = 1;
00198     blas::dscal_(&n, &alpha, x, &incr);
00199   }
00200   inline void scal_vector(int n, std::complex<double> alpha,
00201         std::complex<double> *x)
00202   {
00203     int incr = 1;
00204     blas::zscal_(&n, &alpha, x, &incr);
00205   }
00206 #endif
00207 
00208   template<typename T> inline
00209   void scal_vector(int n, T alpha, T *x)
00210   {
00211     if (alpha != T(1)) {
00212       for (int i = 0; i < n; ++i) {
00213   x[i] *= alpha;
00214       }
00215     }
00216   }
00217 
00218 
00219   /*
00220    * Realise scaling operation: x = alpha*x
00221    * Elements of x are stored linearly with increament incx
00222    */
00223 #if defined(HAVE_BLAS)
00224   inline void scal_vector(int n, double alpha, double *x, int incx)
00225   {
00226     blas::dscal_(&n, &alpha, x, &incx);
00227   }
00228   inline void scal_vector(int n, std::complex<double> alpha,
00229         std::complex<double> *x, int incx)
00230   {
00231     blas::zscal_(&n, &alpha, x, &incx);
00232   }
00233 #endif
00234 
00235   template<typename T> inline
00236   void scal_vector(int n, T alpha, T *x, int incx)
00237   {
00238     if (alpha != T(1)) {
00239       for (int i = 0; i < n; ++i) {
00240   x[i*incx] *= alpha;
00241       }
00242     }
00243   }
00244 
00245 
00246   /*
00247    * Realise the following equation on vectors: y = alpha*x + y
00248    */
00249 #if defined(HAVE_BLAS)
00250   inline void axpy_vector(int n, double alpha, const double *x, double *y)
00251   {
00252     int incr = 1;
00253     blas::daxpy_(&n, &alpha, x, &incr, y, &incr);
00254   }
00255   inline void axpy_vector(int n, std::complex<double> alpha,
00256         const std::complex<double> *x,
00257         std::complex<double> *y)
00258   {
00259     int incr = 1;
00260     blas::zaxpy_(&n, &alpha, x, &incr, y, &incr);
00261   }
00262 #endif
00263 
00264   template<typename T> inline
00265   void axpy_vector(int n, T alpha, const T *x, T *y)
00266   {
00267     if (alpha != T(1)) {
00268       for (int i = 0; i < n; ++i) {
00269   y[i] += alpha * x[i];
00270       }
00271     }
00272     else {
00273       for (int i = 0; i < n; ++i) {
00274   y[i] += x[i];
00275       }
00276     }
00277   }
00278 
00279 
00280   /*
00281    * Realise the following equation on vectors: y = alpha*x + y
00282    * Elements of x are stored linearly with increament incx
00283    * and elements of y are stored linearly with increament incx
00284    */
00285 #if defined(HAVE_BLAS)
00286   inline void axpy_vector(int n, double alpha, const double *x, int incx,
00287         double *y, int incy)
00288   {
00289     blas::daxpy_(&n, &alpha, x, &incx, y, &incy);
00290   }
00291   inline void axpy_vector(int n, std::complex<double> alpha,
00292         const std::complex<double> *x, int incx,
00293         std::complex<double> *y, int incy)
00294   {
00295     blas::zaxpy_(&n, &alpha, x, &incx, y, &incy);
00296   }
00297 #endif
00298 
00299   template<typename T> inline
00300   void axpy_vector(int n, T alpha, const T *x, int incx, T *y, int incy)
00301   {
00302     if (alpha != T(1)) {
00303       for (int i = 0; i < n; ++i) {
00304   y[i*incy] += alpha * x[i*incx];
00305       }
00306     }
00307     else {
00308       for (int i = 0; i < n; ++i) {
00309   y[i*incy] += x[i*incx];
00310       }
00311     }
00312   }
00313 
00314 
00315 } // namespace itpp
00316 
00318 
00319 #endif // #ifndef COPY_VECTOR_H
SourceForge Logo

Generated on Sun Dec 9 17:38:40 2007 for IT++ by Doxygen 1.5.4