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

Generated on Sun Jul 26 08:36:49 2009 for IT++ by Doxygen 1.5.9