IT++ Logo
converters.cpp
Go to the documentation of this file.
1
30#include <itpp/base/itcompat.h>
31#include <itpp/base/matfunc.h>
33
35
36namespace itpp
37{
38
39// ----------------------------------------------------------------------
40// Vector converters
41// ----------------------------------------------------------------------
42
43ivec to_ivec(int s) { ivec out(1); out(0) = s; return out; }
44
45vec to_vec(double s) { vec out(1); out(0) = s; return out; }
46
47cvec to_cvec(double real, double imag)
48{
49 cvec out(1);
50 out(0) = std::complex<double>(real, imag);
51 return out;
52}
53
54// ----------------------------------------------------------------------
55// Miscellaneous converters
56// ----------------------------------------------------------------------
57
58bvec dec2bin(int length, int index)
59{
60 int i, bintemp = index;
61 bvec temp(length);
62
63 for (i = length - 1; i >= 0; i--) {
64 temp(i) = bin(bintemp & 1);
65 bintemp = (bintemp >> 1);
66 }
67 return temp;
68}
69
70bvec dec2bin(int index, bool msb_first)
71{
72 int length = int2bits(index);
73 int i, bintemp = index;
74 bvec temp(length);
75
76 for (i = length - 1; i >= 0; i--) {
77 temp(i) = bin(bintemp & 1);
78 bintemp = (bintemp >> 1);
79 }
80 if (msb_first) {
81 return temp;
82 }
83 else {
84 return reverse(temp);
85 }
86}
87
88void dec2bin(int index, bvec &v)
89{
90 int i, bintemp = index;
91 v.set_size(int2bits(index), false);
92
93 for (i = v.size() - 1; i >= 0; i--) {
94 v(i) = bin(bintemp & 1);
95 bintemp = (bintemp >> 1);
96 }
97}
98
99int bin2dec(const bvec &inbvec, bool msb_first)
100{
101 int i, temp = 0;
102 int sizebvec = inbvec.length();
103 if (msb_first) {
104 for (i = 0; i < sizebvec; i++) {
105 temp += pow2i(sizebvec - i - 1) * int(inbvec(i));
106 }
107 }
108 else {
109 for (i = 0; i < sizebvec; i++) {
110 temp += pow2i(i) * int(inbvec(i));
111 }
112 }
113 return temp;
114}
115
116bvec oct2bin(const ivec &octalindex, short keepzeros)
117{
118 int length = octalindex.length(), i;
119 bvec out(3*length);
120 for (i = 0; i < length; i++) {
121 out.replace_mid(3*i, dec2bin(3, octalindex(i)));
122 }
123 //remove zeros if keepzeros = 0
124 if (keepzeros == 0) {
125 for (i = 0; i < out.length(); i++) {
126 if ((short)out(i) != 0) {
127 return out.right(out.length() - i);
128 break;
129 }
130 }
131 return bvec("0");
132 }
133 else {
134 return out;
135 }
136}
137
138ivec bin2oct(const bvec &inbits)
139{
140 int start, Itterations = ceil_i(inbits.length() / 3.0);
141 ivec out(Itterations);
142 for (int i = Itterations - 1; i > 0; i--) {
143 start = 3 * i - (3 * Itterations - inbits.length());
144 out(i) = bin2dec(inbits.mid(start, 3));
145 }
146 out(0) = bin2dec(inbits.left(inbits.length() - ((Itterations - 1) * 3)));
147 return out;
148}
149
150ivec bin2pol(const bvec &inbvec)
151{
152 return 1 -2*to_ivec(inbvec);
153}
154
155bvec pol2bin(const ivec &inpol)
156{
157 return to_bvec((1 -inpol) / 2);
158}
159
160
161// Round to nearest integer, return result in double
162double round(double x) { return ::rint(x); }
163// Round to nearest integer
164vec round(const vec &x) { return apply_function<double>(::rint, x); }
165// Round to nearest integer
166mat round(const mat &x) { return apply_function<double>(::rint, x); }
167// Round to nearest integer
168int round_i(double x) { return static_cast<int>(::rint(x)); }
169
170// Round to nearest integer and return ivec
171ivec round_i(const vec &x) { return to_ivec(round(x)); }
172// Round to nearest integer and return imat
173imat round_i(const mat &x) { return to_imat(round(x)); }
174
175// Round to nearest upper integer
176ivec ceil_i(const vec &x) { return to_ivec(ceil(x)); }
177// Round to nearest upper integer
178imat ceil_i(const mat &x) { return to_imat(ceil(x)); }
179
180// Round to nearest lower integer
181ivec floor_i(const vec &x) { return to_ivec(floor(x)); }
182// Round to nearest lower integer
183imat floor_i(const mat &x) { return to_imat(floor(x)); }
184
185
186cvec round_to_zero(const cvec &x, double threshold)
187{
188 cvec temp(x.length());
189
190 for (int i = 0; i < x.length(); i++)
191 temp(i) = round_to_zero(x(i), threshold);
192
193 return temp;
194}
195
196cmat round_to_zero(const cmat &x, double threshold)
197{
198 cmat temp(x.rows(), x.cols());
199
200 for (int i = 0; i < x.rows(); i++) {
201 for (int j = 0; j < x.cols(); j++) {
202 temp(i, j) = round_to_zero(x(i, j), threshold);
203 }
204 }
205
206 return temp;
207}
208
209cvec round_to_infty(const cvec &in, const double threshold)
210{
211 cvec temp(in.length());
212
213 for (int i = 0; i < in.length(); i++)
214 temp(i) = round_to_infty(in(i), threshold);
215
216 return temp;
217}
218
219cmat round_to_infty(const cmat &in, const double threshold)
220{
221 cmat temp(in.rows(), in.cols());
222
223 for (int i = 0; i < in.rows(); i++) {
224 for (int j = 0; j < in.cols(); j++) {
225 temp(i, j) = round_to_infty(in(i, j), threshold);
226 }
227 }
228
229 return temp;
230}
231
232std::string to_str(const double &i, const int precision)
233{
234 std::ostringstream ss;
235 ss.precision(precision);
236 ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
237 ss << i;
238 return ss.str();
239}
240
241// ----------------------------------------------------------------------
242// Instantiations
243// ----------------------------------------------------------------------
244
245template ITPP_EXPORT bvec to_bvec(const svec &v);
246template ITPP_EXPORT bvec to_bvec(const ivec &v);
247
248template ITPP_EXPORT svec to_svec(const bvec &v);
249template ITPP_EXPORT svec to_svec(const ivec &v);
250template ITPP_EXPORT svec to_svec(const vec &v);
251
252template ITPP_EXPORT ivec to_ivec(const bvec &v);
253template ITPP_EXPORT ivec to_ivec(const svec &v);
254template ITPP_EXPORT ivec to_ivec(const vec &v);
255
256template ITPP_EXPORT vec to_vec(const bvec &v);
257template ITPP_EXPORT vec to_vec(const svec &v);
258template ITPP_EXPORT vec to_vec(const ivec &v);
259
260template ITPP_EXPORT cvec to_cvec(const bvec &v);
261template ITPP_EXPORT cvec to_cvec(const svec &v);
262template ITPP_EXPORT cvec to_cvec(const ivec &v);
263template ITPP_EXPORT cvec to_cvec(const vec &v);
264
265template ITPP_EXPORT cvec to_cvec(const bvec &real, const bvec &imag);
266template ITPP_EXPORT cvec to_cvec(const svec &real, const svec &imag);
267template ITPP_EXPORT cvec to_cvec(const ivec &real, const ivec &imag);
268template ITPP_EXPORT cvec to_cvec(const vec &real, const vec &imag);
269
270template ITPP_EXPORT bmat to_bmat(const smat &m);
271template ITPP_EXPORT bmat to_bmat(const imat &m);
272
273template ITPP_EXPORT smat to_smat(const bmat &m);
274template ITPP_EXPORT smat to_smat(const imat &m);
275template ITPP_EXPORT smat to_smat(const mat &m);
276
277template ITPP_EXPORT imat to_imat(const bmat &m);
278template ITPP_EXPORT imat to_imat(const smat &m);
279template ITPP_EXPORT imat to_imat(const mat &m);
280
281template ITPP_EXPORT mat to_mat(const bmat &m);
282template ITPP_EXPORT mat to_mat(const smat &m);
283template ITPP_EXPORT mat to_mat(const imat &m);
284
285template ITPP_EXPORT cmat to_cmat(const bmat &m);
286template ITPP_EXPORT cmat to_cmat(const smat &m);
287template ITPP_EXPORT cmat to_cmat(const imat &m);
288template ITPP_EXPORT cmat to_cmat(const mat &m);
289
290template ITPP_EXPORT cmat to_cmat(const bmat &real, const bmat &imag);
291template ITPP_EXPORT cmat to_cmat(const smat &real, const smat &imag);
292template ITPP_EXPORT cmat to_cmat(const imat &real, const imat &imag);
293template ITPP_EXPORT cmat to_cmat(const mat &real, const mat &imag);
294
295} // namespace itpp
296
Definitions of converters between different vector and matrix types.
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition: log_exp.h:53
int int2bits(int n)
Calculate the number of bits needed to represent an integer n.
Definition: log_exp.h:76
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
vec imag(const cvec &data)
Imaginary part of complex values.
Definition: elem_math.cpp:180
vec real(const cvec &data)
Real part of complex values.
Definition: elem_math.cpp:157
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
IT++ compatibility types and functions.
Logarithmic and exponenential functions - header file.
Mat< bin > bmat
bin matrix
Definition: mat.h:508
Various functions on vectors and matrices - 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
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
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.
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
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 Sun Jun 5 2022 21:26:42 for IT++ by Doxygen 1.9.3