IT++ Logo
copy_vector.h
Go to the documentation of this file.
00001 
00029 #ifndef COPY_VECTOR_H
00030 #define COPY_VECTOR_H
00031 
00032 #include <itpp/base/binary.h>
00033 #include <complex>
00034 #include <cstring>
00035 
00036 
00038 
00039 namespace itpp
00040 {
00041 
00042 
00043 /*
00044   Copy vector x to vector y. Both vectors are of size n
00045 */
00046 inline void copy_vector(int n, const int *x, int *y)
00047 {
00048   memcpy(y, x, n * sizeof(int));
00049 }
00050 inline void copy_vector(int n, const short *x, short *y)
00051 {
00052   memcpy(y, x, n * sizeof(short));
00053 }
00054 inline void copy_vector(int n, const bin *x, bin *y)
00055 {
00056   memcpy(y, x, n * sizeof(bin));
00057 }
00058 
00059 void copy_vector(int n, const double *x, double *y);
00060 void copy_vector(int n, const std::complex<double> *x,
00061                  std::complex<double> *y);
00062 
00063 template<class T> inline
00064 void copy_vector(int n, const T *x, T *y)
00065 {
00066   for (int i = 0; i < n; i++)
00067     y[i] = x[i];
00068 }
00069 
00070 
00071 /*
00072   Copy vector x to vector y. Both vectors are of size n
00073   vector x elements are stored linearly with element increament incx
00074   vector y elements are stored linearly with element increament incx
00075 */
00076 void copy_vector(int n, const double *x, int incx, double *y, int incy);
00077 void copy_vector(int n, const std::complex<double> *x, int incx,
00078                  std::complex<double> *y, int incy);
00079 
00080 template<class T> inline
00081 void copy_vector(int n, const T *x, int incx, T *y, int incy)
00082 {
00083   for (int i = 0; i < n; i++)
00084     y[i*incy] = x[i*incx];
00085 }
00086 
00087 
00088 /*
00089   Swap vector x and vector y. Both vectors are of size n
00090 */
00091 inline void swap_vector(int n, int *x, int *y)
00092 {
00093   for (int i = 0; i < n; i++)
00094     std::swap(x[i], y[i]);
00095 }
00096 inline void swap_vector(int n, short *x, short *y)
00097 {
00098   for (int i = 0; i < n; i++)
00099     std::swap(x[i], y[i]);
00100 }
00101 inline void swap_vector(int n, bin *x, bin *y)
00102 {
00103   for (int i = 0; i < n; i++)
00104     std::swap(x[i], y[i]);
00105 }
00106 
00107 void swap_vector(int n, double *x, double *y);
00108 void swap_vector(int n, std::complex<double> *x, std::complex<double> *y);
00109 
00110 template<class T> inline
00111 void swap_vector(int n, T *x, T *y)
00112 {
00113   T tmp;
00114   for (int i = 0; i < n; i++) {
00115     tmp = y[i];
00116     y[i] = x[i];
00117     x[i] = tmp;
00118   }
00119 }
00120 
00121 
00122 /*
00123   Swap vector x and vector y. Both vectors are of size n
00124   vector x elements are stored linearly with element increament incx
00125   vector y elements are stored linearly with element increament incx
00126 */
00127 inline void swap_vector(int n, int *x, int incx, int *y, int incy)
00128 {
00129   for (int i = 0; i < n; i++)
00130     std::swap(x[i*incx], y[i*incy]);
00131 }
00132 inline void swap_vector(int n, short *x, int incx, short *y, int incy)
00133 {
00134   for (int i = 0; i < n; i++)
00135     std::swap(x[i*incx], y[i*incy]);
00136 }
00137 inline void swap_vector(int n, bin *x, int incx, bin *y, int incy)
00138 {
00139   for (int i = 0; i < n; i++)
00140     std::swap(x[i*incx], y[i*incy]);
00141 }
00142 
00143 void swap_vector(int n, double *x, int incx, double *y, int incy);
00144 void swap_vector(int n, std::complex<double> *x, int incx,
00145                  std::complex<double> *y, int incy);
00146 
00147 template<class T> inline
00148 void swap_vector(int n, T *x, int incx, T *y, 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 /*
00160  * Realise scaling operation: x = alpha*x
00161  */
00162 void scal_vector(int n, double alpha, double *x);
00163 void scal_vector(int n, std::complex<double> alpha, std::complex<double> *x);
00164 
00165 template<typename T> inline
00166 void scal_vector(int n, T alpha, T *x)
00167 {
00168   if (alpha != T(1)) {
00169     for (int i = 0; i < n; ++i) {
00170       x[i] *= alpha;
00171     }
00172   }
00173 }
00174 
00175 
00176 /*
00177  * Realise scaling operation: x = alpha*x
00178  * Elements of x are stored linearly with increament incx
00179  */
00180 void scal_vector(int n, double alpha, double *x, int incx);
00181 void scal_vector(int n, std::complex<double> alpha, std::complex<double> *x,
00182                  int incx);
00183 
00184 template<typename T> inline
00185 void scal_vector(int n, T alpha, T *x, int incx)
00186 {
00187   if (alpha != T(1)) {
00188     for (int i = 0; i < n; ++i) {
00189       x[i*incx] *= alpha;
00190     }
00191   }
00192 }
00193 
00194 
00195 /*
00196  * Realise the following equation on vectors: y = alpha*x + y
00197  */
00198 void axpy_vector(int n, double alpha, const double *x, double *y);
00199 
00200 void axpy_vector(int n, std::complex<double> alpha,
00201                  const std::complex<double> *x, std::complex<double> *y);
00202 
00203 template<typename T> inline
00204 void axpy_vector(int n, T alpha, const T *x, T *y)
00205 {
00206   if (alpha != T(1)) {
00207     for (int i = 0; i < n; ++i) {
00208       y[i] += alpha * x[i];
00209     }
00210   }
00211   else {
00212     for (int i = 0; i < n; ++i) {
00213       y[i] += x[i];
00214     }
00215   }
00216 }
00217 
00218 
00219 /*
00220  * Realise the following equation on vectors: y = alpha*x + y
00221  * Elements of x are stored linearly with increment incx
00222  * and elements of y are stored linearly with increment incx
00223  */
00224 void axpy_vector(int n, double alpha, const double *x, int incx, double *y,
00225                  int incy);
00226 void axpy_vector(int n, std::complex<double> alpha,
00227                  const std::complex<double> *x, int incx,
00228                  std::complex<double> *y, int incy);
00229 
00230 template<typename T> inline
00231 void axpy_vector(int n, T alpha, const T *x, int incx, T *y, int incy)
00232 {
00233   if (alpha != T(1)) {
00234     for (int i = 0; i < n; ++i) {
00235       y[i*incy] += alpha * x[i*incx];
00236     }
00237   }
00238   else {
00239     for (int i = 0; i < n; ++i) {
00240       y[i*incy] += x[i*incx];
00241     }
00242   }
00243 }
00244 
00245 
00246 } // namespace itpp
00247 
00249 
00250 #endif // #ifndef COPY_VECTOR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4