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

Generated on Sat Apr 19 10:41:10 2008 for IT++ by Doxygen 1.5.5