IT++ Logo
copy_vector.h
Go to the documentation of this file.
1 
29 #ifndef COPY_VECTOR_H
30 #define COPY_VECTOR_H
31 
32 #include <itpp/base/binary.h>
33 #include <complex>
34 #include <cstring>
35 #include <itpp/itexports.h>
36 
38 
39 namespace itpp
40 {
41 
42 
43 /*
44  Copy vector x to vector y. Both vectors are of size n
45 */
46 inline void copy_vector(int n, const int *x, int *y)
47 {
48  memcpy(y, x, n * sizeof(int));
49 }
50 inline void copy_vector(int n, const short *x, short *y)
51 {
52  memcpy(y, x, n * sizeof(short));
53 }
54 inline void copy_vector(int n, const bin *x, bin *y)
55 {
56  memcpy(y, x, n * sizeof(bin));
57 }
58 
59 ITPP_EXPORT void copy_vector(int n, const double *x, double *y);
60 ITPP_EXPORT void copy_vector(int n, const std::complex<double> *x,
61  std::complex<double> *y);
62 
63 template<class T> inline
64 void copy_vector(int n, const T *x, T *y)
65 {
66  for (int i = 0; i < n; i++)
67  y[i] = x[i];
68 }
69 
70 
71 /*
72  Copy vector x to vector y. Both vectors are of size n
73  vector x elements are stored linearly with element increament incx
74  vector y elements are stored linearly with element increament incx
75 */
76 ITPP_EXPORT void copy_vector(int n, const double *x, int incx, double *y, int incy);
77 ITPP_EXPORT void copy_vector(int n, const std::complex<double> *x, int incx,
78  std::complex<double> *y, int incy);
79 
80 template<class T> inline
81 void copy_vector(int n, const T *x, int incx, T *y, int incy)
82 {
83  for (int i = 0; i < n; i++)
84  y[i*incy] = x[i*incx];
85 }
86 
87 
88 /*
89  Swap vector x and vector y. Both vectors are of size n
90 */
91 inline void swap_vector(int n, int *x, int *y)
92 {
93  for (int i = 0; i < n; i++)
94  std::swap(x[i], y[i]);
95 }
96 inline void swap_vector(int n, short *x, short *y)
97 {
98  for (int i = 0; i < n; i++)
99  std::swap(x[i], y[i]);
100 }
101 inline void swap_vector(int n, bin *x, bin *y)
102 {
103  for (int i = 0; i < n; i++)
104  std::swap(x[i], y[i]);
105 }
106 
107 ITPP_EXPORT void swap_vector(int n, double *x, double *y);
108 ITPP_EXPORT void swap_vector(int n, std::complex<double> *x, std::complex<double> *y);
109 
110 template<class T> inline
111 void swap_vector(int n, T *x, T *y)
112 {
113  T tmp;
114  for (int i = 0; i < n; i++) {
115  tmp = y[i];
116  y[i] = x[i];
117  x[i] = tmp;
118  }
119 }
120 
121 
122 /*
123  Swap vector x and vector y. Both vectors are of size n
124  vector x elements are stored linearly with element increament incx
125  vector y elements are stored linearly with element increament incx
126 */
127 inline void swap_vector(int n, int *x, int incx, int *y, int incy)
128 {
129  for (int i = 0; i < n; i++)
130  std::swap(x[i*incx], y[i*incy]);
131 }
132 inline void swap_vector(int n, short *x, int incx, short *y, int incy)
133 {
134  for (int i = 0; i < n; i++)
135  std::swap(x[i*incx], y[i*incy]);
136 }
137 inline void swap_vector(int n, bin *x, int incx, bin *y, int incy)
138 {
139  for (int i = 0; i < n; i++)
140  std::swap(x[i*incx], y[i*incy]);
141 }
142 
143 ITPP_EXPORT void swap_vector(int n, double *x, int incx, double *y, int incy);
144 ITPP_EXPORT void swap_vector(int n, std::complex<double> *x, int incx,
145  std::complex<double> *y, int incy);
146 
147 template<class T> inline
148 void swap_vector(int n, T *x, int incx, T *y, int incy)
149 {
150  T tmp;
151  for (int i = 0; i < n; i++) {
152  tmp = y[i*incy];
153  y[i*incy] = x[i*incx];
154  x[i*incx] = tmp;
155  }
156 }
157 
158 
159 /*
160  * Realise scaling operation: x = alpha*x
161  */
162 ITPP_EXPORT void scal_vector(int n, double alpha, double *x);
163 ITPP_EXPORT void scal_vector(int n, std::complex<double> alpha, std::complex<double> *x);
164 
165 template<typename T> inline
166 void scal_vector(int n, T alpha, T *x)
167 {
168  if (alpha != T(1)) {
169  for (int i = 0; i < n; ++i) {
170  x[i] *= alpha;
171  }
172  }
173 }
174 
175 
176 /*
177  * Realise scaling operation: x = alpha*x
178  * Elements of x are stored linearly with increament incx
179  */
180 ITPP_EXPORT void scal_vector(int n, double alpha, double *x, int incx);
181 ITPP_EXPORT void scal_vector(int n, std::complex<double> alpha, std::complex<double> *x,
182  int incx);
183 
184 template<typename T> inline
185 void scal_vector(int n, T alpha, T *x, int incx)
186 {
187  if (alpha != T(1)) {
188  for (int i = 0; i < n; ++i) {
189  x[i*incx] *= alpha;
190  }
191  }
192 }
193 
194 
195 /*
196  * Realise the following equation on vectors: y = alpha*x + y
197  */
198 ITPP_EXPORT void axpy_vector(int n, double alpha, const double *x, double *y);
199 
200 ITPP_EXPORT void axpy_vector(int n, std::complex<double> alpha,
201  const std::complex<double> *x, std::complex<double> *y);
202 
203 template<typename T> inline
204 void axpy_vector(int n, T alpha, const T *x, T *y)
205 {
206  if (alpha != T(1)) {
207  for (int i = 0; i < n; ++i) {
208  y[i] += alpha * x[i];
209  }
210  }
211  else {
212  for (int i = 0; i < n; ++i) {
213  y[i] += x[i];
214  }
215  }
216 }
217 
218 
219 /*
220  * Realise the following equation on vectors: y = alpha*x + y
221  * Elements of x are stored linearly with increment incx
222  * and elements of y are stored linearly with increment incx
223  */
224 ITPP_EXPORT void axpy_vector(int n, double alpha, const double *x, int incx, double *y,
225  int incy);
226 ITPP_EXPORT void axpy_vector(int n, std::complex<double> alpha,
227  const std::complex<double> *x, int incx,
228  std::complex<double> *y, int incy);
229 
230 template<typename T> inline
231 void axpy_vector(int n, T alpha, const T *x, int incx, T *y, int incy)
232 {
233  if (alpha != T(1)) {
234  for (int i = 0; i < n; ++i) {
235  y[i*incy] += alpha * x[i*incx];
236  }
237  }
238  else {
239  for (int i = 0; i < n; ++i) {
240  y[i*incy] += x[i*incx];
241  }
242  }
243 }
244 
245 
246 } // namespace itpp
247 
249 
250 #endif // #ifndef COPY_VECTOR_H
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:20 for IT++ by Doxygen 1.8.2