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
39namespace itpp
40{
41
42
43/*
44 Copy vector x to vector y. Both vectors are of size n
45*/
46inline void copy_vector(int n, const int *x, int *y)
47{
48 memcpy(y, x, n * sizeof(int));
49}
50inline void copy_vector(int n, const short *x, short *y)
51{
52 memcpy(y, x, n * sizeof(short));
53}
54inline void copy_vector(int n, const bin *x, bin *y)
55{
56 memcpy(y, x, n * sizeof(bin));
57}
58
59ITPP_EXPORT void copy_vector(int n, const double *x, double *y);
60ITPP_EXPORT void copy_vector(int n, const std::complex<double> *x,
61 std::complex<double> *y);
62
63template<class T> inline
64void 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*/
76ITPP_EXPORT void copy_vector(int n, const double *x, int incx, double *y, int incy);
77ITPP_EXPORT void copy_vector(int n, const std::complex<double> *x, int incx,
78 std::complex<double> *y, int incy);
79
80template<class T> inline
81void 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*/
91inline 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}
96inline 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}
101inline 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
107ITPP_EXPORT void swap_vector(int n, double *x, double *y);
108ITPP_EXPORT void swap_vector(int n, std::complex<double> *x, std::complex<double> *y);
109
110template<class T> inline
111void 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*/
127inline 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}
132inline 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}
137inline 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
143ITPP_EXPORT void swap_vector(int n, double *x, int incx, double *y, int incy);
144ITPP_EXPORT void swap_vector(int n, std::complex<double> *x, int incx,
145 std::complex<double> *y, int incy);
146
147template<class T> inline
148void 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 */
162ITPP_EXPORT void scal_vector(int n, double alpha, double *x);
163ITPP_EXPORT void scal_vector(int n, std::complex<double> alpha, std::complex<double> *x);
164
165template<typename T> inline
166void 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 */
180ITPP_EXPORT void scal_vector(int n, double alpha, double *x, int incx);
181ITPP_EXPORT void scal_vector(int n, std::complex<double> alpha, std::complex<double> *x,
182 int incx);
183
184template<typename T> inline
185void 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 */
198ITPP_EXPORT void axpy_vector(int n, double alpha, const double *x, double *y);
199
200ITPP_EXPORT void axpy_vector(int n, std::complex<double> alpha,
201 const std::complex<double> *x, std::complex<double> *y);
202
203template<typename T> inline
204void 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 */
224ITPP_EXPORT void axpy_vector(int n, double alpha, const double *x, int incx, double *y,
225 int incy);
226ITPP_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
230template<typename T> inline
231void 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
Binary class definition.
itpp namespace
Definition: itmex.h:37

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.4