IT++ Logo
converters.h
Go to the documentation of this file.
1
29#ifndef CONVERTERS_H
30#define CONVERTERS_H
31
33#include <itpp/base/math/misc.h>
34#include <itpp/itexports.h>
35
36namespace itpp
37{
38
40
41
42// ----------------------------------------------------------------------
43// Converters for vectors
44// ----------------------------------------------------------------------
45
50template <class T>
52{
53 bvec temp(v.length());
54 for (int i = 0; i < v.length(); ++i) {
55 temp(i) = static_cast<bin>(v(i));
56 }
57 return temp;
58}
59
64template <class T>
66{
67 svec temp(v.length());
68 for (int i = 0; i < v.length(); ++i) {
69 temp(i) = static_cast<short>(v(i));
70 }
71 return temp;
72}
73
78template <class T>
80{
81 ivec temp(v.length());
82 for (int i = 0; i < v.length(); ++i) {
83 temp(i) = static_cast<int>(v(i));
84 }
85 return temp;
86}
87
92template <class T>
94{
95 vec temp(v.length());
96 for (int i = 0; i < v.length(); ++i) {
97 temp(i) = static_cast<double>(v(i));
98 }
99 return temp;
100}
101
106template <class T>
108{
109 cvec temp(v.length());
110 for (int i = 0; i < v.length(); ++i) {
111 temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0);
112 }
113 return temp;
114}
115
117template<> inline
118cvec to_cvec(const cvec& v)
119{
120 return v;
121}
123
128template <class T>
130{
131 it_assert(real.length() == imag.length(),
132 "to_cvec(): real and imaginary parts must have the same length");
133 cvec temp(real.length());
134 for (int i = 0; i < real.length(); ++i) {
135 temp(i) = std::complex<double>(static_cast<double>(real(i)),
136 static_cast<double>(imag(i)));
137 }
138 return temp;
139}
140
146
151vec to_vec(double s);
152
157cvec to_cvec(double real, double imag);
158
159// ----------------------------------------------------------------------
160// Converters for matrices
161// ----------------------------------------------------------------------
162
167template <class T>
169{
170 bmat temp(m.rows(), m.cols());
171 for (int i = 0; i < temp.rows(); ++i) {
172 for (int j = 0; j < temp.cols(); ++j) {
173 temp(i, j) = static_cast<bin>(m(i, j));
174 }
175 }
176 return temp;
177}
178
183template <class T>
185{
186 smat temp(m.rows(), m.cols());
187 for (int i = 0; i < temp.rows(); ++i) {
188 for (int j = 0; j < temp.cols(); ++j) {
189 temp(i, j) = static_cast<short>(m(i, j));
190 }
191 }
192 return temp;
193}
194
199template <class T>
201{
202 imat temp(m.rows(), m.cols());
203 for (int i = 0; i < temp.rows(); ++i) {
204 for (int j = 0; j < temp.cols(); ++j) {
205 temp(i, j) = static_cast<int>(m(i, j));
206 }
207 }
208 return temp;
209}
210
215template <class T>
217{
218 mat temp(m.rows(), m.cols());
219 for (int i = 0; i < temp.rows(); ++i) {
220 for (int j = 0; j < temp.cols(); ++j) {
221 temp(i, j) = static_cast<double>(m(i, j));
222 }
223 }
224 return temp;
225}
226
231template <class T>
233{
234 cmat temp(m.rows(), m.cols());
235 for (int i = 0; i < temp.rows(); ++i) {
236 for (int j = 0; j < temp.cols(); ++j) {
237 temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0);
238 }
239 }
240 return temp;
241}
242
244template<> inline
245cmat to_cmat(const cmat& m)
246{
247 return m;
248}
250
255template <class T>
257{
258 it_assert_debug((real.rows() == imag.rows())
259 && (real.cols() == imag.cols()),
260 "to_cmat(): real and imag part sizes does not match");
261 cmat temp(real.rows(), real.cols());
262 for (int i = 0; i < temp.rows(); ++i) {
263 for (int j = 0; j < temp.cols(); ++j) {
264 temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)),
265 static_cast<double>(imag(i, j)));
266 }
267 }
268 return temp;
269}
270
271
275ITPP_EXPORT bvec dec2bin(int length, int index);
276
280ITPP_EXPORT void dec2bin(int index, bvec &v);
281
285ITPP_EXPORT bvec dec2bin(int index, bool msb_first = true);
286
290ITPP_EXPORT int bin2dec(const bvec &inbvec, bool msb_first = true);
291
299ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros = 0);
300
308ITPP_EXPORT ivec bin2oct(const bvec &inbits);
309
311ITPP_EXPORT ivec bin2pol(const bvec &inbvec);
312
314ITPP_EXPORT bvec pol2bin(const ivec &inpol);
315
317inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); }
319inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); }
320
322ITPP_EXPORT double round(double x);
324ITPP_EXPORT vec round(const vec &x);
326ITPP_EXPORT mat round(const mat &x);
328ITPP_EXPORT int round_i(double x);
330ITPP_EXPORT ivec round_i(const vec &x);
332ITPP_EXPORT imat round_i(const mat &x);
333
335inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); }
337inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); }
339inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); }
341ITPP_EXPORT ivec ceil_i(const vec &x);
343ITPP_EXPORT imat ceil_i(const mat &x);
344
346inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); }
348inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); }
350inline int floor_i(double x) { return static_cast<int>(std::floor(x)); }
352ITPP_EXPORT ivec floor_i(const vec &x);
354ITPP_EXPORT imat floor_i(const mat &x);
355
356
358inline double round_to_zero(double x, double threshold = 1e-14)
359{
360 return ((std::fabs(x) < threshold) ? 0.0 : x);
361}
362
364inline std::complex<double> round_to_zero(const std::complex<double>& x,
365 double threshold = 1e-14)
366{
367 return std::complex<double>(round_to_zero(x.real(), threshold),
368 round_to_zero(x.imag(), threshold));
369}
370
372inline vec round_to_zero(const vec &x, double threshold = 1e-14)
373{
374 return apply_function<double>(round_to_zero, x, threshold);
375}
376
378inline mat round_to_zero(const mat &x, double threshold = 1e-14)
379{
380 return apply_function<double>(round_to_zero, x, threshold);
381}
382
384ITPP_EXPORT cvec round_to_zero(const cvec &x, double threshold = 1e-14);
385
387ITPP_EXPORT cmat round_to_zero(const cmat &x, double threshold = 1e-14);
388
390inline double round_to_infty(const double in, const double threshold = 1e9)
391{
392 return (std::fabs(in)>threshold)?itpp::round(in):in;
393}
394
396inline std::complex<double> round_to_infty(const std::complex<double> &in, const double threshold = 1e9)
397{
398 return std::complex<double>(round_to_infty(in.real(), threshold),
399 round_to_infty(in.imag(), threshold));
400}
401
403inline vec round_to_infty(const vec &in, const double threshold = 1e9)
404{
405 return apply_function<double>(round_to_infty, in, threshold);
406}
407
409inline mat round_to_infty(const mat &in, const double threshold = 1e9)
410{
411 return apply_function<double>(round_to_infty, in, threshold);
412}
413
415ITPP_EXPORT cvec round_to_infty(const cvec &in, const double threshold = 1e9);
416
418ITPP_EXPORT cmat round_to_infty(const cmat &in, const double threshold = 1e9);
419
421inline int gray_code(int x) { return x ^(x >> 1); }
422
423
429template <typename T>
430std::string to_str(const T &i);
431
439ITPP_EXPORT std::string to_str(const double &i, const int precision);
440
442
443template <typename T>
444std::string to_str(const T &i)
445{
446 std::ostringstream ss;
447 ss.precision(8);
448 ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
449 ss << i;
450 return ss.str();
451}
452
454
455// ---------------------------------------------------------------------
456// Instantiations
457// ---------------------------------------------------------------------
458
459ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec to_bvec(const svec &v);
460ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec to_bvec(const ivec &v);
461
462ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const bvec &v);
463ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const ivec &v);
464ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const vec &v);
465
466ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const bvec &v);
467ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const svec &v);
468ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const vec &v);
469
470ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const bvec &v);
471ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const svec &v);
472ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const ivec &v);
473
474ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const bvec &v);
475ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const svec &v);
476ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const ivec &v);
477ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const vec &v);
478
479ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const bvec &real, const bvec &imag);
480ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const svec &real, const svec &imag);
481ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const ivec &real, const ivec &imag);
482ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const vec &real, const vec &imag);
483
484ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat to_bmat(const smat &m);
485ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat to_bmat(const imat &m);
486
487ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const bmat &m);
488ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const imat &m);
489ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const mat &m);
490
491ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const bmat &m);
492ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const smat &m);
493ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const mat &m);
494
495ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const bmat &m);
496ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const smat &m);
497ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const imat &m);
498
499ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const bmat &m);
500ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const smat &m);
501ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const imat &m);
502ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const mat &m);
503
504ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const bmat &real, const bmat &imag);
505ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const smat &real, const smat &imag);
506ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const imat &real, const imat &imag);
507ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const mat &real, const mat &imag);
508
510
511} // namespace itpp
512
513#endif // CONVERTERS_H
General array class.
Definition array.h:105
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
Matrix Class (Templated)
Definition mat.h:202
cmat to_cmat(const Mat< T > &real, const Mat< T > &imag)
Converts real and imaginary Mat<T> to cmat.
Definition converters.h:256
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition converters.h:232
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition converters.h:168
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition converters.h:216
imat to_imat(const Mat< T > &m)
Converts a Mat<T> to imat.
Definition converters.h:200
smat to_smat(const Mat< T > &m)
Converts a Mat<T> to smat.
Definition converters.h:184
Vector Class (Templated)
Definition vec.h:245
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition converters.h:107
svec to_svec(const Vec< T > &v)
Converts a Vec<T> to svec.
Definition converters.h:65
cvec to_cvec(double real, double imag)
Converts real and imaginary double to cvec.
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
Definition converters.h:93
cvec to_cvec(const Vec< T > &real, const Vec< T > &imag)
Converts real and imaginary Vec<T> to cvec.
Definition converters.h:129
ivec to_ivec(int s)
Converts an int to ivec.
vec to_vec(double s)
Converts an double to vec.
ivec to_ivec(const Vec< T > &v)
Converts a Vec<T> to ivec.
Definition converters.h:79
bvec to_bvec(const Vec< T > &v)
Converts a Vec<T> to bvec.
Definition converters.h:51
Binary arithmetic (boolean) class.
Definition binary.h:57
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
#define it_assert(t, s)
Abort if t is not true.
Definition itassert.h:94
int length(const Vec< T > &v)
Length of vector.
Definition matfunc.h:51
vec imag(const cvec &data)
Imaginary part of complex values.
vec real(const cvec &data)
Real part of complex values.
Help functions to make functions with vec and mat as arguments.
Mat< bin > bmat
bin matrix
Definition mat.h:508
Miscellaneous functions - header file.
itpp namespace
Definition itmex.h:37
double round_to_infty(const double in, const double threshold=1e9)
Remove trailing digits, found after the decimal point, for numbers greater than threshold.
Definition converters.h:390
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition converters.h:107
double round_to_zero(double x, double threshold=1e-14)
Round x to zero if abs(x) is smaller than threshold.
Definition converters.h:358
ITPP_EXPORT ivec bin2oct(const bvec &inbits)
Convert bvec to octal ivec.
svec to_svec(const Vec< T > &v)
Converts a Vec<T> to svec.
Definition converters.h:65
ITPP_EXPORT int round_i(double x)
Round to nearest integer.
vec floor(const vec &x)
Round to nearest lower integer.
Definition converters.h:346
vec ceil(const vec &x)
Round to nearest upper integer.
Definition converters.h:335
double rad_to_deg(double x)
Convert radians to degrees.
Definition converters.h:317
ITPP_EXPORT bvec pol2bin(const ivec &inpol)
Convert binary polar ivec to bvec.
ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros=0)
Convert ivec of octal form to bvec.
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition converters.h:232
ITPP_EXPORT ivec bin2pol(const bvec &inbvec)
Convert bvec to polar binary representation as ivec.
ITPP_EXPORT int bin2dec(const bvec &inbvec, bool msb_first=true)
Convert a bvec to decimal int with the first bit as MSB if msb_first == true.
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition converters.h:168
int gray_code(int x)
Convert to Gray Code.
Definition converters.h:421
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
Definition converters.h:93
std::string to_str(const T &i)
Convert anything to string.
Definition converters.h:444
ITPP_EXPORT bvec dec2bin(int length, int index)
Convert a decimal int index to bvec using length bits in the representation.
const double pi
Constant Pi.
Definition misc.h:103
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition converters.h:216
ITPP_EXPORT double round(double x)
Round to nearest integer, return result in double.
int ceil_i(double x)
The nearest larger integer.
Definition converters.h:339
imat to_imat(const Mat< T > &m)
Converts a Mat<T> to imat.
Definition converters.h:200
double deg_to_rad(double x)
Convert degrees to radians.
Definition converters.h:319
smat to_smat(const Mat< T > &m)
Converts a Mat<T> to smat.
Definition converters.h:184
int floor_i(double x)
The nearest smaller integer.
Definition converters.h:350
ivec to_ivec(const Vec< T > &v)
Converts a Vec<T> to ivec.
Definition converters.h:79
bvec to_bvec(const Vec< T > &v)
Converts a Vec<T> to bvec.
Definition converters.h:51
SourceForge Logo

Generated on Mon Jun 10 2024 11:49:17 for IT++ by Doxygen 1.9.8