1 | /*! |
---|
2 | * \file |
---|
3 | * \brief Definitions of converters between different vector and matrix types |
---|
4 | * \author Tony Ottosson, Tobias Ringstrom, Pal Frenger 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 CONVERTERS_H |
---|
31 | #define CONVERTERS_H |
---|
32 | |
---|
33 | #ifndef _MSC_VER |
---|
34 | # include <itpp/config.h> |
---|
35 | #else |
---|
36 | # include <itpp/config_msvc.h> |
---|
37 | #endif |
---|
38 | |
---|
39 | #include <itpp/base/help_functions.h> |
---|
40 | #include <itpp/base/math/misc.h> |
---|
41 | |
---|
42 | |
---|
43 | namespace itpp { |
---|
44 | |
---|
45 | //! \addtogroup convertfunc |
---|
46 | //@{ |
---|
47 | |
---|
48 | // ---------------------------------------------------------------------- |
---|
49 | // Converters for vectors |
---|
50 | // ---------------------------------------------------------------------- |
---|
51 | |
---|
52 | /*! |
---|
53 | \relatesalso Vec |
---|
54 | \brief Converts a Vec<T> to bvec |
---|
55 | */ |
---|
56 | template <class T> |
---|
57 | bvec to_bvec(const Vec<T> &v) |
---|
58 | { |
---|
59 | bvec temp(v.length()); |
---|
60 | for (int i = 0; i < v.length(); ++i) { |
---|
61 | temp(i) = static_cast<bin>(v(i)); |
---|
62 | } |
---|
63 | return temp; |
---|
64 | } |
---|
65 | |
---|
66 | /*! |
---|
67 | \relatesalso Vec |
---|
68 | \brief Converts a Vec<T> to svec |
---|
69 | */ |
---|
70 | template <class T> |
---|
71 | svec to_svec(const Vec<T> &v) |
---|
72 | { |
---|
73 | svec temp(v.length()); |
---|
74 | for (int i = 0; i < v.length(); ++i) { |
---|
75 | temp(i) = static_cast<short>(v(i)); |
---|
76 | } |
---|
77 | return temp; |
---|
78 | } |
---|
79 | |
---|
80 | /*! |
---|
81 | \relatesalso Vec |
---|
82 | \brief Converts a Vec<T> to ivec |
---|
83 | */ |
---|
84 | template <class T> |
---|
85 | ivec to_ivec(const Vec<T> &v) |
---|
86 | { |
---|
87 | ivec temp(v.length()); |
---|
88 | for (int i = 0; i < v.length(); ++i) { |
---|
89 | temp(i) = static_cast<int>(v(i)); |
---|
90 | } |
---|
91 | return temp; |
---|
92 | } |
---|
93 | |
---|
94 | /*! |
---|
95 | \relatesalso Vec |
---|
96 | \brief Converts a Vec<T> to vec |
---|
97 | */ |
---|
98 | template <class T> |
---|
99 | vec to_vec(const Vec<T> &v) |
---|
100 | { |
---|
101 | vec temp(v.length()); |
---|
102 | for (int i = 0; i < v.length(); ++i) { |
---|
103 | temp(i) = static_cast<double>(v(i)); |
---|
104 | } |
---|
105 | return temp; |
---|
106 | } |
---|
107 | |
---|
108 | /*! |
---|
109 | \relatesalso Vec |
---|
110 | \brief Converts a Vec<T> to cvec |
---|
111 | */ |
---|
112 | template <class T> |
---|
113 | cvec to_cvec(const Vec<T> &v) |
---|
114 | { |
---|
115 | cvec temp(v.length()); |
---|
116 | for (int i = 0; i < v.length(); ++i) { |
---|
117 | temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0); |
---|
118 | } |
---|
119 | return temp; |
---|
120 | } |
---|
121 | |
---|
122 | /*! |
---|
123 | \relatesalso Vec |
---|
124 | \brief Converts real and imaginary Vec<T> to cvec |
---|
125 | */ |
---|
126 | template <class T> |
---|
127 | cvec to_cvec(const Vec<T> &real, const Vec<T> &imag) |
---|
128 | { |
---|
129 | it_assert(real.length() == imag.length(), |
---|
130 | "to_cvec(): real and imaginary parts must have the same length"); |
---|
131 | cvec temp(real.length()); |
---|
132 | for(int i = 0; i < real.length(); ++i) { |
---|
133 | temp(i) = std::complex<double>(static_cast<double>(real(i)), |
---|
134 | static_cast<double>(imag(i))); |
---|
135 | } |
---|
136 | return temp; |
---|
137 | } |
---|
138 | |
---|
139 | /*! |
---|
140 | \relatesalso Vec |
---|
141 | \brief Converts an int to ivec |
---|
142 | */ |
---|
143 | ivec to_ivec(int s); |
---|
144 | |
---|
145 | /*! |
---|
146 | \relatesalso Vec |
---|
147 | \brief Converts an double to vec |
---|
148 | */ |
---|
149 | vec to_vec(double s); |
---|
150 | |
---|
151 | /*! |
---|
152 | \relatesalso Vec |
---|
153 | \brief Converts real and imaginary double to cvec |
---|
154 | */ |
---|
155 | cvec to_cvec(double real, double imag); |
---|
156 | |
---|
157 | // ---------------------------------------------------------------------- |
---|
158 | // Converters for matrices |
---|
159 | // ---------------------------------------------------------------------- |
---|
160 | |
---|
161 | /*! |
---|
162 | \relatesalso Mat |
---|
163 | \brief Converts a Mat<T> to bmat |
---|
164 | */ |
---|
165 | template <class T> |
---|
166 | bmat to_bmat(const Mat<T> &m) |
---|
167 | { |
---|
168 | bmat temp(m.rows(), m.cols()); |
---|
169 | for (int i = 0; i < temp.rows(); ++i) { |
---|
170 | for (int j = 0; j < temp.cols(); ++j) { |
---|
171 | temp(i, j) = static_cast<bin>(m(i, j)); |
---|
172 | } |
---|
173 | } |
---|
174 | return temp; |
---|
175 | } |
---|
176 | |
---|
177 | /*! |
---|
178 | \relatesalso Mat |
---|
179 | \brief Converts a Mat<T> to smat |
---|
180 | */ |
---|
181 | template <class T> |
---|
182 | smat to_smat(const Mat<T> &m) |
---|
183 | { |
---|
184 | smat temp(m.rows(), m.cols()); |
---|
185 | for (int i = 0; i < temp.rows(); ++i) { |
---|
186 | for (int j = 0; j < temp.cols(); ++j) { |
---|
187 | temp(i, j) = static_cast<short>(m(i, j)); |
---|
188 | } |
---|
189 | } |
---|
190 | return temp; |
---|
191 | } |
---|
192 | |
---|
193 | /*! |
---|
194 | \relatesalso Mat |
---|
195 | \brief Converts a Mat<T> to imat |
---|
196 | */ |
---|
197 | template <class T> |
---|
198 | imat to_imat(const Mat<T> &m) |
---|
199 | { |
---|
200 | imat temp(m.rows(), m.cols()); |
---|
201 | for (int i = 0; i < temp.rows(); ++i) { |
---|
202 | for (int j = 0; j < temp.cols(); ++j) { |
---|
203 | temp(i, j) = static_cast<int>(m(i, j)); |
---|
204 | } |
---|
205 | } |
---|
206 | return temp; |
---|
207 | } |
---|
208 | |
---|
209 | /*! |
---|
210 | \relatesalso Mat |
---|
211 | \brief Converts a Mat<T> to mat |
---|
212 | */ |
---|
213 | template <class T> |
---|
214 | mat to_mat(const Mat<T> &m) |
---|
215 | { |
---|
216 | mat temp(m.rows(), m.cols()); |
---|
217 | for (int i = 0; i < temp.rows(); ++i) { |
---|
218 | for (int j = 0; j < temp.cols(); ++j) { |
---|
219 | temp(i, j) = static_cast<double>(m(i, j)); |
---|
220 | } |
---|
221 | } |
---|
222 | return temp; |
---|
223 | } |
---|
224 | |
---|
225 | /*! |
---|
226 | \relatesalso Mat |
---|
227 | \brief Converts a Mat<T> to cmat |
---|
228 | */ |
---|
229 | template <class T> |
---|
230 | cmat to_cmat(const Mat<T> &m) |
---|
231 | { |
---|
232 | cmat temp(m.rows(), m.cols()); |
---|
233 | for (int i = 0; i < temp.rows(); ++i) { |
---|
234 | for (int j = 0; j < temp.cols(); ++j) { |
---|
235 | temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0); |
---|
236 | } |
---|
237 | } |
---|
238 | return temp; |
---|
239 | } |
---|
240 | |
---|
241 | /*! |
---|
242 | \relatesalso Mat |
---|
243 | \brief Converts real and imaginary Mat<T> to cmat |
---|
244 | */ |
---|
245 | template <class T> |
---|
246 | cmat to_cmat(const Mat<T> &real, const Mat<T> &imag) |
---|
247 | { |
---|
248 | it_assert_debug((real.rows() == imag.rows()) |
---|
249 | && (real.cols() == imag.cols()), |
---|
250 | "to_cmat(): real and imag part sizes does not match"); |
---|
251 | cmat temp(real.rows(), real.cols()); |
---|
252 | for (int i = 0; i < temp.rows(); ++i) { |
---|
253 | for (int j = 0; j < temp.cols(); ++j) { |
---|
254 | temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)), |
---|
255 | static_cast<double>(imag(i, j))); |
---|
256 | } |
---|
257 | } |
---|
258 | return temp; |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | /*! |
---|
263 | \brief Convert a decimal int \a index to bvec using \a length bits in the representation |
---|
264 | */ |
---|
265 | bvec dec2bin(int length, int index); |
---|
266 | |
---|
267 | /*! |
---|
268 | \brief Convert a decimal int \a index to bvec. Value returned in \a v. |
---|
269 | */ |
---|
270 | void dec2bin(int index, bvec &v); |
---|
271 | |
---|
272 | /*! |
---|
273 | \brief Convert a decimal int \a index to bvec with the first bit as MSB if \a msb_first == true |
---|
274 | */ |
---|
275 | bvec dec2bin(int index, bool msb_first = true); |
---|
276 | |
---|
277 | /*! |
---|
278 | \brief Convert a bvec to decimal int with the first bit as MSB if \a msb_first == true |
---|
279 | */ |
---|
280 | int bin2dec(const bvec &inbvec, bool msb_first = true); |
---|
281 | |
---|
282 | /*! |
---|
283 | \brief Convert ivec of octal form to bvec |
---|
284 | |
---|
285 | Converts from ivec containing {0,1,2,...,7} to bvec containing {0,1}. |
---|
286 | Removes zeros to the left if keepzeros = 0 (default). |
---|
287 | Example: oct2bin("3 5 5 1") returns {1 1 1 0 1 1 0 1 0 0 1}. |
---|
288 | */ |
---|
289 | bvec oct2bin(const ivec &octalindex, short keepzeros = 0); |
---|
290 | |
---|
291 | /*! |
---|
292 | \brief Convert bvec to octal ivec |
---|
293 | |
---|
294 | Converts from bvec containing {0,1} to ivec containing {0,1,2,...,7}. |
---|
295 | Adds zeros to the left if inbits.length() is not a factor of 3. |
---|
296 | Example: bin2oct("1 1 1 0 1 1 0 1 0 0 1") returns {3 5 5 1}. |
---|
297 | */ |
---|
298 | ivec bin2oct(const bvec &inbits); |
---|
299 | |
---|
300 | //! Convert bvec to polar binary representation as ivec |
---|
301 | ivec bin2pol(const bvec &inbvec); |
---|
302 | |
---|
303 | //! Convert binary polar ivec to bvec |
---|
304 | bvec pol2bin(const ivec &inpol); |
---|
305 | |
---|
306 | //! Convert radians to degrees |
---|
307 | inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); } |
---|
308 | //! Convert degrees to radians |
---|
309 | inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); } |
---|
310 | |
---|
311 | |
---|
312 | #ifndef HAVE_RINT |
---|
313 | //! Round to nearest integer, return result in double |
---|
314 | inline double rint(double x) { return floor(x + 0.5); } |
---|
315 | #endif |
---|
316 | //! Round to nearest integer, return result in double |
---|
317 | inline double round(double x) { return rint(x); } |
---|
318 | //! Round to nearest integer |
---|
319 | inline vec round(const vec &x) { return apply_function<double>(round, x); } |
---|
320 | //! Round to nearest integer |
---|
321 | inline mat round(const mat &x) { return apply_function<double>(round, x); } |
---|
322 | //! Round to nearest integer |
---|
323 | inline int round_i(double x) { return static_cast<int>(rint(x)); } |
---|
324 | //! Round to nearest integer and return ivec |
---|
325 | ivec round_i(const vec &x); |
---|
326 | //! Round to nearest integer and return imat |
---|
327 | imat round_i(const mat &x); |
---|
328 | |
---|
329 | //! Round to nearest upper integer |
---|
330 | inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); } |
---|
331 | //! Round to nearest upper integer |
---|
332 | inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); } |
---|
333 | //! The nearest larger integer |
---|
334 | inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); } |
---|
335 | //! Round to nearest upper integer |
---|
336 | ivec ceil_i(const vec &x); |
---|
337 | //! Round to nearest upper integer |
---|
338 | imat ceil_i(const mat &x); |
---|
339 | |
---|
340 | //! Round to nearest lower integer |
---|
341 | inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); } |
---|
342 | //! Round to nearest lower integer |
---|
343 | inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); } |
---|
344 | //! The nearest smaller integer |
---|
345 | inline int floor_i(double x) { return static_cast<int>(std::floor(x)); } |
---|
346 | //! Round to nearest lower integer |
---|
347 | ivec floor_i(const vec &x); |
---|
348 | //! Round to nearest lower integer |
---|
349 | imat floor_i(const mat &x); |
---|
350 | |
---|
351 | |
---|
352 | //! Round \a x to zero if \a abs(x) is smaller than \a threshold |
---|
353 | inline double round_to_zero(double x, double threshold = 1e-14) |
---|
354 | { |
---|
355 | return ((std::fabs(x) < threshold) ? 0.0 : x); |
---|
356 | } |
---|
357 | |
---|
358 | //! Round each part of \a x smaller than \a threshold to zero |
---|
359 | inline std::complex<double> round_to_zero(const std::complex<double>& x, |
---|
360 | double threshold = 1e-14) |
---|
361 | { |
---|
362 | return std::complex<double>(round_to_zero(x.real(), threshold), |
---|
363 | round_to_zero(x.imag(), threshold)); |
---|
364 | } |
---|
365 | |
---|
366 | //! Round each element to zero if element < threshold |
---|
367 | inline vec round_to_zero(const vec &x, double threshold = 1e-14) |
---|
368 | { |
---|
369 | return apply_function<double>(round_to_zero, x, threshold); |
---|
370 | } |
---|
371 | |
---|
372 | //! Round each element to zero if element < threshold |
---|
373 | inline mat round_to_zero(const mat &x, double threshold = 1e-14) |
---|
374 | { |
---|
375 | return apply_function<double>(round_to_zero, x, threshold); |
---|
376 | } |
---|
377 | |
---|
378 | //! Round each element to zero if element < threshold |
---|
379 | cvec round_to_zero(const cvec &x, double threshold = 1e-14); |
---|
380 | |
---|
381 | //! Round each element to zero if element < threshold |
---|
382 | cmat round_to_zero(const cmat &x, double threshold = 1e-14); |
---|
383 | |
---|
384 | |
---|
385 | //! Convert to Gray Code |
---|
386 | inline int gray_code(int x) { return x^(x >> 1); } |
---|
387 | |
---|
388 | |
---|
389 | /*! |
---|
390 | \brief Convert anything to string |
---|
391 | |
---|
392 | \param i (Input) The value to be converted to a string |
---|
393 | */ |
---|
394 | template <typename T> |
---|
395 | std::string to_str(const T &i); |
---|
396 | |
---|
397 | /*! |
---|
398 | \brief Convert double to string |
---|
399 | |
---|
400 | \param[in] i The value to be converted to a string |
---|
401 | \param[in] precision The number of digits used to represent the |
---|
402 | fractional part |
---|
403 | */ |
---|
404 | std::string to_str(const double &i, const int precision); |
---|
405 | |
---|
406 | //@} |
---|
407 | |
---|
408 | template <typename T> |
---|
409 | std::string to_str(const T &i) |
---|
410 | { |
---|
411 | std::ostringstream ss; |
---|
412 | ss.precision(8); |
---|
413 | ss.setf(std::ostringstream::scientific,std::ostringstream::floatfield); |
---|
414 | ss << i; |
---|
415 | return ss.str(); |
---|
416 | } |
---|
417 | |
---|
418 | //! \cond |
---|
419 | |
---|
420 | // --------------------------------------------------------------------- |
---|
421 | // Instantiations |
---|
422 | // --------------------------------------------------------------------- |
---|
423 | |
---|
424 | #ifdef HAVE_EXTERN_TEMPLATE |
---|
425 | |
---|
426 | extern template bvec to_bvec(const svec &v); |
---|
427 | extern template bvec to_bvec(const ivec &v); |
---|
428 | |
---|
429 | extern template svec to_svec(const bvec &v); |
---|
430 | extern template svec to_svec(const ivec &v); |
---|
431 | extern template svec to_svec(const vec &v); |
---|
432 | |
---|
433 | // Workaround for GCC 3.3.x error when using -finine-functions or -O3 flag |
---|
434 | #if (GCC_VERSION >= 30400) |
---|
435 | extern template ivec to_ivec(const bvec &v); |
---|
436 | #endif |
---|
437 | extern template ivec to_ivec(const svec &v); |
---|
438 | extern template ivec to_ivec(const vec &v); |
---|
439 | |
---|
440 | extern template vec to_vec(const bvec &v); |
---|
441 | extern template vec to_vec(const svec &v); |
---|
442 | extern template vec to_vec(const ivec &v); |
---|
443 | |
---|
444 | extern template cvec to_cvec(const bvec &v); |
---|
445 | extern template cvec to_cvec(const svec &v); |
---|
446 | extern template cvec to_cvec(const ivec &v); |
---|
447 | extern template cvec to_cvec(const vec &v); |
---|
448 | |
---|
449 | extern template cvec to_cvec(const bvec &real, const bvec &imag); |
---|
450 | extern template cvec to_cvec(const svec &real, const svec &imag); |
---|
451 | extern template cvec to_cvec(const ivec &real, const ivec &imag); |
---|
452 | extern template cvec to_cvec(const vec &real, const vec &imag); |
---|
453 | |
---|
454 | extern template bmat to_bmat(const smat &m); |
---|
455 | extern template bmat to_bmat(const imat &m); |
---|
456 | |
---|
457 | extern template smat to_smat(const bmat &m); |
---|
458 | extern template smat to_smat(const imat &m); |
---|
459 | extern template smat to_smat(const mat &m); |
---|
460 | |
---|
461 | extern template imat to_imat(const bmat &m); |
---|
462 | extern template imat to_imat(const smat &m); |
---|
463 | extern template imat to_imat(const mat &m); |
---|
464 | |
---|
465 | extern template mat to_mat(const bmat &m); |
---|
466 | #if (GCC_VERSION >= 30400) |
---|
467 | extern template mat to_mat(const smat &m); |
---|
468 | extern template mat to_mat(const imat &m); |
---|
469 | #endif |
---|
470 | |
---|
471 | extern template cmat to_cmat(const bmat &m); |
---|
472 | extern template cmat to_cmat(const smat &m); |
---|
473 | extern template cmat to_cmat(const imat &m); |
---|
474 | extern template cmat to_cmat(const mat &m); |
---|
475 | |
---|
476 | extern template cmat to_cmat(const bmat &real, const bmat &imag); |
---|
477 | extern template cmat to_cmat(const smat &real, const smat &imag); |
---|
478 | extern template cmat to_cmat(const imat &real, const imat &imag); |
---|
479 | extern template cmat to_cmat(const mat &real, const mat &imag); |
---|
480 | |
---|
481 | #endif // HAVE_EXTERN_TEMPLATE |
---|
482 | |
---|
483 | //! \endcond |
---|
484 | |
---|
485 | } // namespace itpp |
---|
486 | |
---|
487 | #endif // CONVERTERS_H |
---|