IT++ Logo Newcom Logo

copy_vector.h

Go to the documentation of this file.
00001 
00033 #ifndef COPY_VECTOR_H
00034 #define COPY_VECTOR_H
00035 
00036 #ifndef _MSC_VER
00037 #  include <itpp/config.h>
00038 #else
00039 #  include <itpp/config_msvc.h>
00040 #endif
00041 
00042 #if defined (HAVE_CBLAS)
00043 #  include <itpp/base/cblas.h>
00044 #endif
00045 
00046 #include <itpp/base/binary.h>
00047 #include <complex>
00048 
00049 
00050 namespace itpp {
00051 
00052 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00053 
00054   /*
00055     Copy vector x to vector y. Both vectors are of size n
00056   */
00057   inline void copy_vector(const int n, const int *x, int *y) { memcpy(y, x, (unsigned int)n*sizeof(int)); }
00058   inline void copy_vector(const int n, const short *x, short *y) { memcpy(y, x, (unsigned int)n*sizeof(short)); }
00059   inline void copy_vector(const int n, const bin *x, bin *y) { memcpy(y, x, (unsigned int)n*sizeof(bin)); }
00060   inline void copy_vector(const int n, const float *x, float *y) { memcpy(y, x, (unsigned int)n*sizeof(float)); }
00061   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>)); }
00062 
00063 #if defined (HAVE_CBLAS)
00064   inline void copy_vector(const int n, const double *x, double *y) { cblas_dcopy(n, x, 1, y, 1); }
00065   inline void copy_vector(const int n, const std::complex<double> *x, std::complex<double> *y) { cblas_zcopy(n, x, 1, y, 1); }
00066 #else
00067   inline void copy_vector(const int n, const double *x, double *y) { memcpy(y, x, (unsigned int)n*sizeof(double)); }
00068   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>)); }
00069 #endif
00070 
00071   template<class T> inline
00072   void copy_vector(const int n, const T *x, T *y)
00073   {
00074     for (int i=0; i<n; i++)
00075       y[i] = x[i];
00076   }
00077 
00078 
00079 
00080 
00081   /*
00082     Copy vector x to vector y. Both vectors are of size n
00083     vector x elements are stored linearly with element increament incx
00084     vector y elements are stored linearly with element increament incx
00085   */
00086 #if defined (HAVE_CBLAS)
00087   inline void copy_vector(const int n, const double *x, const int incx, double *y, const int incy) { cblas_dcopy(n, x, incx, y, incy); }
00088   inline void copy_vector(const int n, const std::complex<double> *x, const int incx, std::complex<double> *y, const int incy) { cblas_zcopy(n, x, incx, y, incy); }
00089 #endif
00090 
00091   template<class T> inline
00092   void copy_vector(const int n, const T *x, const int incx, T *y, const int incy)
00093   {
00094     for (int i=0;i<n; i++)
00095       y[i*incy] = x[i*incx];
00096   }
00097 
00098 
00099   /*
00100     Swap vector x to vector y. Both vectors are of size n
00101   */
00102   inline void swap_vector(const int n, int *x, int *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00103   inline void swap_vector(const int n, short *x, short *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00104   inline void swap_vector(const int n, bin *x, bin *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00105   inline void swap_vector(const int n, float *x, float *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00106   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]); }
00107 
00108 #if defined (HAVE_CBLAS)
00109   inline void swap_vector(const int n, double *x, double *y) { cblas_dswap(n, x, 1, y, 1); }
00110   inline void swap_vector(const int n, std::complex<double> *x, std::complex<double> *y) { cblas_zswap(n, x, 1, y, 1); }
00111 #else
00112   inline void swap_vector(const int n, double *x, double *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); }
00113   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]); }
00114 #endif
00115 
00116   template<class T> inline
00117   void swap_vector(const int n, T *x, T *y)
00118   {
00119     T tmp;
00120     for (int i=0; i<n; i++) {
00121       tmp = y[i];
00122       y[i] = x[i];
00123       x[i] = tmp;
00124     }
00125   }
00126 
00127 
00128   /*
00129     Swap vector x to vector y. Both vectors are of size n
00130     vector x elements are stored linearly with element increament incx
00131     vector y elements are stored linearly with element increament incx
00132   */
00133   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]); }
00134   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]); }
00135   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]); }
00136   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]); }
00137   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]); }
00138 
00139 #if defined (HAVE_CBLAS)
00140   inline void swap_vector(const int n, double *x, const int incx, double *y, const int incy) { cblas_dswap(n, x, incx, y, incy); }
00141   inline void swap_vector(const int n, std::complex<double> *x, const int incx, std::complex<double> *y, const int incy) { cblas_zswap(n, x, incx, y, incy); }
00142 #else
00143   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]); }
00144   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]); }
00145 #endif
00146 
00147   template<class T> inline
00148   void swap_vector(const int n, T *x, const int incx, T *y, const int incy)
00149   {
00150     T tmp;
00151     for (int i=0; i<n; i++) {
00152       tmp = y[i*incy];
00153       y[i*incy] = x[i*incx];
00154       x[i*incx] = tmp;
00155     }
00156   }
00157 
00158 
00159 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
00160 
00161 } // namespace itpp
00162 
00163 #endif // #ifndef COPY_VECTOR_H
SourceForge Logo

Generated on Fri Jun 8 02:08:50 2007 for IT++ by Doxygen 1.5.2