[35] | 1 | /*! |
---|
| 2 | * \file |
---|
| 3 | * \brief Vector copy functions for internal use |
---|
| 4 | * \author Tony Ottosson and Adam Piatyszek |
---|
| 5 | * |
---|
| 6 | * ------------------------------------------------------------------------- |
---|
| 7 | * |
---|
| 8 | * IT++ - C++ library of mathematical, signal processing, speech processing, |
---|
| 9 | * and communications classes and functions |
---|
| 10 | * |
---|
| 11 | * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
---|
| 12 | * |
---|
| 13 | * This program is free software; you can redistribute it and/or modify |
---|
| 14 | * it under the terms of the GNU General Public License as published by |
---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 16 | * (at your option) any later version. |
---|
| 17 | * |
---|
| 18 | * This program is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this program; if not, write to the Free Software |
---|
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 26 | * |
---|
| 27 | * ------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #ifndef COPY_VECTOR_H |
---|
| 31 | #define COPY_VECTOR_H |
---|
| 32 | |
---|
| 33 | #ifndef _MSC_VER |
---|
| 34 | # include <itpp/config.h> |
---|
| 35 | #else |
---|
| 36 | # include <itpp/config_msvc.h> |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | #if defined (HAVE_BLAS) |
---|
| 40 | # include <itpp/base/blas.h> |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | #include <itpp/base/binary.h> |
---|
| 44 | #include <cstring> |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | //! \cond |
---|
| 48 | |
---|
| 49 | namespace itpp { |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | /* |
---|
| 53 | Copy vector x to vector y. Both vectors are of size n |
---|
| 54 | */ |
---|
| 55 | inline void copy_vector(const int n, const int *x, int *y) { memcpy(y, x, (unsigned int)n*sizeof(int)); } |
---|
| 56 | inline void copy_vector(const int n, const short *x, short *y) { memcpy(y, x, (unsigned int)n*sizeof(short)); } |
---|
| 57 | inline void copy_vector(const int n, const bin *x, bin *y) { memcpy(y, x, (unsigned int)n*sizeof(bin)); } |
---|
| 58 | inline void copy_vector(const int n, const float *x, float *y) { memcpy(y, x, (unsigned int)n*sizeof(float)); } |
---|
| 59 | 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>)); } |
---|
| 60 | |
---|
| 61 | #if defined (HAVE_BLAS) |
---|
| 62 | inline void copy_vector(const int n, const double *x, double *y) |
---|
| 63 | { |
---|
| 64 | int incr = 1; |
---|
| 65 | blas::dcopy_(&n, x, &incr, y, &incr); |
---|
| 66 | } |
---|
| 67 | inline void copy_vector(const int n, const std::complex<double> *x, |
---|
| 68 | std::complex<double> *y) |
---|
| 69 | { |
---|
| 70 | int incr = 1; |
---|
| 71 | blas::zcopy_(&n, x, &incr, y, &incr); |
---|
| 72 | } |
---|
| 73 | #else |
---|
| 74 | inline void copy_vector(const int n, const double *x, double *y) { memcpy(y, x, (unsigned int)n*sizeof(double)); } |
---|
| 75 | 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>)); } |
---|
| 76 | #endif |
---|
| 77 | |
---|
| 78 | template<class T> inline |
---|
| 79 | void copy_vector(const int n, const T *x, T *y) |
---|
| 80 | { |
---|
| 81 | for (int i=0; i<n; i++) |
---|
| 82 | y[i] = x[i]; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | /* |
---|
| 89 | Copy vector x to vector y. Both vectors are of size n |
---|
| 90 | vector x elements are stored linearly with element increament incx |
---|
| 91 | vector y elements are stored linearly with element increament incx |
---|
| 92 | */ |
---|
| 93 | #if defined (HAVE_BLAS) |
---|
| 94 | inline void copy_vector(const int n, const double *x, const int incx, |
---|
| 95 | double *y, const int incy) |
---|
| 96 | { |
---|
| 97 | blas::dcopy_(&n, x, &incx, y, &incy); |
---|
| 98 | } |
---|
| 99 | inline void copy_vector(const int n, const std::complex<double> *x, |
---|
| 100 | const int incx, std::complex<double> *y, |
---|
| 101 | const int incy) |
---|
| 102 | { |
---|
| 103 | blas::zcopy_(&n, x, &incx, y, &incy); |
---|
| 104 | } |
---|
| 105 | #endif |
---|
| 106 | |
---|
| 107 | template<class T> inline |
---|
| 108 | void copy_vector(const int n, const T *x, const int incx, T *y, const int incy) |
---|
| 109 | { |
---|
| 110 | for (int i=0;i<n; i++) |
---|
| 111 | y[i*incy] = x[i*incx]; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | /* |
---|
| 116 | Swap vector x to vector y. Both vectors are of size n |
---|
| 117 | */ |
---|
| 118 | inline void swap_vector(const int n, int *x, int *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); } |
---|
| 119 | inline void swap_vector(const int n, short *x, short *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); } |
---|
| 120 | inline void swap_vector(const int n, bin *x, bin *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); } |
---|
| 121 | inline void swap_vector(const int n, float *x, float *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); } |
---|
| 122 | 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]); } |
---|
| 123 | |
---|
| 124 | #if defined (HAVE_BLAS) |
---|
| 125 | inline void swap_vector(const int n, double *x, double *y) |
---|
| 126 | { |
---|
| 127 | int incr = 1; |
---|
| 128 | blas::dswap_(&n, x, &incr, y, &incr); |
---|
| 129 | } |
---|
| 130 | inline void swap_vector(const int n, std::complex<double> *x, |
---|
| 131 | std::complex<double> *y) |
---|
| 132 | { |
---|
| 133 | int incr = 1; |
---|
| 134 | blas::zswap_(&n, x, &incr, y, &incr); |
---|
| 135 | } |
---|
| 136 | #else |
---|
| 137 | inline void swap_vector(const int n, double *x, double *y) { for (int i=0; i<n; i++) std::swap(x[i], y[i]); } |
---|
| 138 | 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]); } |
---|
| 139 | #endif |
---|
| 140 | |
---|
| 141 | template<class T> inline |
---|
| 142 | void swap_vector(const int n, T *x, T *y) |
---|
| 143 | { |
---|
| 144 | T tmp; |
---|
| 145 | for (int i=0; i<n; i++) { |
---|
| 146 | tmp = y[i]; |
---|
| 147 | y[i] = x[i]; |
---|
| 148 | x[i] = tmp; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | /* |
---|
| 154 | Swap vector x to vector y. Both vectors are of size n |
---|
| 155 | vector x elements are stored linearly with element increament incx |
---|
| 156 | vector y elements are stored linearly with element increament incx |
---|
| 157 | */ |
---|
| 158 | 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]); } |
---|
| 159 | 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]); } |
---|
| 160 | 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]); } |
---|
| 161 | 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]); } |
---|
| 162 | 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]); } |
---|
| 163 | |
---|
| 164 | #if defined (HAVE_BLAS) |
---|
| 165 | inline void swap_vector(const int n, double *x, const int incx, double *y, |
---|
| 166 | const int incy) |
---|
| 167 | { |
---|
| 168 | blas::dswap_(&n, x, &incx, y, &incy); |
---|
| 169 | } |
---|
| 170 | inline void swap_vector(const int n, std::complex<double> *x, const int incx, |
---|
| 171 | std::complex<double> *y, const int incy) |
---|
| 172 | { |
---|
| 173 | blas::zswap_(&n, x, &incx, y, &incy); |
---|
| 174 | } |
---|
| 175 | #else |
---|
| 176 | 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]); } |
---|
| 177 | 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]); } |
---|
| 178 | #endif |
---|
| 179 | |
---|
| 180 | template<class T> inline |
---|
| 181 | void swap_vector(const int n, T *x, const int incx, T *y, const int incy) |
---|
| 182 | { |
---|
| 183 | T tmp; |
---|
| 184 | for (int i=0; i<n; i++) { |
---|
| 185 | tmp = y[i*incy]; |
---|
| 186 | y[i*incy] = x[i*incx]; |
---|
| 187 | x[i*incx] = tmp; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | /* |
---|
| 193 | * Realise scaling operation: x = alpha*x |
---|
| 194 | */ |
---|
| 195 | #if defined(HAVE_BLAS) |
---|
| 196 | inline void scal_vector(int n, double alpha, double *x) |
---|
| 197 | { |
---|
| 198 | int incr = 1; |
---|
| 199 | blas::dscal_(&n, &alpha, x, &incr); |
---|
| 200 | } |
---|
| 201 | inline void scal_vector(int n, std::complex<double> alpha, |
---|
| 202 | std::complex<double> *x) |
---|
| 203 | { |
---|
| 204 | int incr = 1; |
---|
| 205 | blas::zscal_(&n, &alpha, x, &incr); |
---|
| 206 | } |
---|
| 207 | #endif |
---|
| 208 | |
---|
| 209 | template<typename T> inline |
---|
| 210 | void scal_vector(int n, T alpha, T *x) |
---|
| 211 | { |
---|
| 212 | if (alpha != T(1)) { |
---|
| 213 | for (int i = 0; i < n; ++i) { |
---|
| 214 | x[i] *= alpha; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | /* |
---|
| 221 | * Realise scaling operation: x = alpha*x |
---|
| 222 | * Elements of x are stored linearly with increament incx |
---|
| 223 | */ |
---|
| 224 | #if defined(HAVE_BLAS) |
---|
| 225 | inline void scal_vector(int n, double alpha, double *x, int incx) |
---|
| 226 | { |
---|
| 227 | blas::dscal_(&n, &alpha, x, &incx); |
---|
| 228 | } |
---|
| 229 | inline void scal_vector(int n, std::complex<double> alpha, |
---|
| 230 | std::complex<double> *x, int incx) |
---|
| 231 | { |
---|
| 232 | blas::zscal_(&n, &alpha, x, &incx); |
---|
| 233 | } |
---|
| 234 | #endif |
---|
| 235 | |
---|
| 236 | template<typename T> inline |
---|
| 237 | void scal_vector(int n, T alpha, T *x, int incx) |
---|
| 238 | { |
---|
| 239 | if (alpha != T(1)) { |
---|
| 240 | for (int i = 0; i < n; ++i) { |
---|
| 241 | x[i*incx] *= alpha; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | /* |
---|
| 248 | * Realise the following equation on vectors: y = alpha*x + y |
---|
| 249 | */ |
---|
| 250 | #if defined(HAVE_BLAS) |
---|
| 251 | inline void axpy_vector(int n, double alpha, const double *x, double *y) |
---|
| 252 | { |
---|
| 253 | int incr = 1; |
---|
| 254 | blas::daxpy_(&n, &alpha, x, &incr, y, &incr); |
---|
| 255 | } |
---|
| 256 | inline void axpy_vector(int n, std::complex<double> alpha, |
---|
| 257 | const std::complex<double> *x, |
---|
| 258 | std::complex<double> *y) |
---|
| 259 | { |
---|
| 260 | int incr = 1; |
---|
| 261 | blas::zaxpy_(&n, &alpha, x, &incr, y, &incr); |
---|
| 262 | } |
---|
| 263 | #endif |
---|
| 264 | |
---|
| 265 | template<typename T> inline |
---|
| 266 | void axpy_vector(int n, T alpha, const T *x, T *y) |
---|
| 267 | { |
---|
| 268 | if (alpha != T(1)) { |
---|
| 269 | for (int i = 0; i < n; ++i) { |
---|
| 270 | y[i] += alpha * x[i]; |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | else { |
---|
| 274 | for (int i = 0; i < n; ++i) { |
---|
| 275 | y[i] += x[i]; |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | /* |
---|
| 282 | * Realise the following equation on vectors: y = alpha*x + y |
---|
| 283 | * Elements of x are stored linearly with increament incx |
---|
| 284 | * and elements of y are stored linearly with increament incx |
---|
| 285 | */ |
---|
| 286 | #if defined(HAVE_BLAS) |
---|
| 287 | inline void axpy_vector(int n, double alpha, const double *x, int incx, |
---|
| 288 | double *y, int incy) |
---|
| 289 | { |
---|
| 290 | blas::daxpy_(&n, &alpha, x, &incx, y, &incy); |
---|
| 291 | } |
---|
| 292 | inline void axpy_vector(int n, std::complex<double> alpha, |
---|
| 293 | const std::complex<double> *x, int incx, |
---|
| 294 | std::complex<double> *y, int incy) |
---|
| 295 | { |
---|
| 296 | blas::zaxpy_(&n, &alpha, x, &incx, y, &incy); |
---|
| 297 | } |
---|
| 298 | #endif |
---|
| 299 | |
---|
| 300 | template<typename T> inline |
---|
| 301 | void axpy_vector(int n, T alpha, const T *x, int incx, T *y, int incy) |
---|
| 302 | { |
---|
| 303 | if (alpha != T(1)) { |
---|
| 304 | for (int i = 0; i < n; ++i) { |
---|
| 305 | y[i*incy] += alpha * x[i*incx]; |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | else { |
---|
| 309 | for (int i = 0; i < n; ++i) { |
---|
| 310 | y[i*incy] += x[i*incx]; |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | |
---|
| 316 | } // namespace itpp |
---|
| 317 | |
---|
| 318 | //! \endcond |
---|
| 319 | |
---|
| 320 | #endif // #ifndef COPY_VECTOR_H |
---|