File: converters.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (288 lines) | stat: -rw-r--r-- 7,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*!
 * \file
 * \brief Implementation of converters between different vector and matrix types
 * \author Tony Ottosson, Tobias Ringstrom, Pal Frenger and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/base/converters.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/math/log_exp.h>

//! \cond

#ifndef HAVE_RINT
double rint(double x)
{
  // zero or NaN case
  if ((x == 0.0) || (x != x))
    return x;

  // negative case
  bool neg = false;
  if (x < 0.0) {
    x = -x;
    neg = true;
  }

  double y = std::floor(x + 0.5);
  int i = static_cast<int>(y);
  if ((y - x >= 0.5) && (i & 1))
    --y;

  return neg ? -y : y;
}
#endif // HAVE_RINT


namespace itpp {

  // ----------------------------------------------------------------------
  // Vector converters
  // ----------------------------------------------------------------------

  ivec to_ivec(int s) { ivec out(1); out(0) = s; return out; }

  vec to_vec(double s) { vec out(1); out(0) = s; return out; }

  cvec to_cvec(double real, double imag)
  {
    cvec out(1);
    out(0) = std::complex<double>(real, imag);
    return out;
  }

  // ----------------------------------------------------------------------
  // Miscellaneous converters
  // ----------------------------------------------------------------------

  bvec dec2bin(int length, int index)
  {
    int i, bintemp = index;
    bvec temp(length);

    for (i=length-1; i>=0; i--) {
      temp(i) = bin(bintemp & 1);
      bintemp = (bintemp >> 1);
    }
    return temp;
  }

  bvec dec2bin(int index, bool msb_first)
  {
    int length = int2bits(index);
    int i, bintemp = index;
    bvec temp(length);

    for (i=length-1; i>=0; i--) {
      temp(i) = bin(bintemp & 1);
      bintemp = (bintemp >> 1);
    }
    if (msb_first){
      return temp;
    } else{
      return reverse(temp);
    }
  }

  void dec2bin(int index, bvec &v)
  {
    int i, bintemp = index;
    v.set_size(int2bits(index), false);

    for (i=v.size()-1; i>=0; i--) {
      v(i) = bin(bintemp & 1);
      bintemp = (bintemp >> 1);
    }
  }

  int bin2dec(const bvec &inbvec, bool msb_first)
  {
    int i, temp=0;
    int sizebvec=inbvec.length();
    if (msb_first) {
      for (i=0; i<sizebvec; i++) {
	temp+=pow2i(sizebvec-i-1)*int(inbvec(i));
      }
    } else {
      for (i=0; i<sizebvec; i++) {
	temp+=pow2i(i)*int(inbvec(i));
      }
    }
    return temp;
  }

  bvec oct2bin(const ivec &octalindex, short keepzeros)
  {
    int length = octalindex.length(), i;
    bvec out(3*length);
    for (i=0; i<length; i++) {
      out.replace_mid(3*i,dec2bin(3,octalindex(i)));
    }
    //remove zeros if keepzeros = 0
    if (keepzeros == 0) {
      for (i=0; i<out.length(); i++) {
	if ( (short)out(i) != 0) {
	  return out.right(out.length()-i);
	  break;
	}
      }
      return bvec("0");
    } else {
      return out;
    }
  }

  ivec bin2oct(const bvec &inbits)
  {
    int start, Itterations = ceil_i(inbits.length() / 3.0);
    ivec out(Itterations);
    for (int i=Itterations-1; i>0; i--) {
      start = 3*i - ( 3*Itterations - inbits.length() );
      out(i) = bin2dec(inbits.mid(start,3));
    }
    out(0) = bin2dec( inbits.left(inbits.length()-((Itterations-1)*3)) );
    return out;
  }

  ivec bin2pol(const bvec &inbvec)
  {
    return 1-2*to_ivec(inbvec);
  }

  bvec pol2bin(const ivec &inpol)
  {
    return to_bvec((1-inpol)/2);
  }


  // Round to nearest integer and return ivec
  ivec round_i(const vec &x) { return to_ivec(round(x)); }
  // Round to nearest integer and return imat
  imat round_i(const mat &x) { return to_imat(round(x)); }

  // Round to nearest upper integer
  ivec ceil_i(const vec &x) { return to_ivec(ceil(x)); }
  // Round to nearest upper integer
  imat ceil_i(const mat &x) { return to_imat(ceil(x)); }

  // Round to nearest lower integer
  ivec floor_i(const vec &x) { return to_ivec(floor(x)); }
  // Round to nearest lower integer
  imat floor_i(const mat &x) { return to_imat(floor(x)); }


  cvec round_to_zero(const cvec &x, double threshold) {
    cvec temp(x.length());

    for (int i = 0; i < x.length(); i++)
      temp(i) = round_to_zero(x(i), threshold);

    return temp;
  }

  cmat round_to_zero(const cmat &x, double threshold) {
    cmat temp(x.rows(), x.cols());

    for (int i = 0; i < x.rows(); i++) {
      for (int j = 0; j < x.cols(); j++) {
	temp(i, j) = round_to_zero(x(i, j), threshold);
      }
    }

    return temp;
  }


  std::string to_str(const double &i, const int precision)
  {
    std::ostringstream ss;
    ss.precision(precision);
    ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
    ss << i;
    return ss.str();
  }

  // ----------------------------------------------------------------------
  // Instantiations
  // ----------------------------------------------------------------------

  template bvec to_bvec(const svec &v);
  template bvec to_bvec(const ivec &v);

  template svec to_svec(const bvec &v);
  template svec to_svec(const ivec &v);
  template svec to_svec(const vec &v);

#if (GCC_VERSION >= 30400)
  template ivec to_ivec(const bvec &v);
#endif
  template ivec to_ivec(const svec &v);
  template ivec to_ivec(const vec &v);

  template vec to_vec(const bvec &v);
  template vec to_vec(const svec &v);
  template vec to_vec(const ivec &v);

  template cvec to_cvec(const bvec &v);
  template cvec to_cvec(const svec &v);
  template cvec to_cvec(const ivec &v);
  template cvec to_cvec(const vec &v);

  template cvec to_cvec(const bvec &real, const bvec &imag);
  template cvec to_cvec(const svec &real, const svec &imag);
  template cvec to_cvec(const ivec &real, const ivec &imag);
  template cvec to_cvec(const vec &real, const vec &imag);

  template bmat to_bmat(const smat &m);
  template bmat to_bmat(const imat &m);

  template smat to_smat(const bmat &m);
  template smat to_smat(const imat &m);
  template smat to_smat(const mat &m);

  template imat to_imat(const bmat &m);
  template imat to_imat(const smat &m);
  template imat to_imat(const mat &m);

  template mat to_mat(const bmat &m);
#if (GCC_VERSION >= 30400)
  template mat to_mat(const smat &m);
  template mat to_mat(const imat &m);
#endif

  template cmat to_cmat(const bmat &m);
  template cmat to_cmat(const smat &m);
  template cmat to_cmat(const imat &m);
  template cmat to_cmat(const mat &m);

  template cmat to_cmat(const bmat &real, const bmat &imag);
  template cmat to_cmat(const smat &real, const smat &imag);
  template cmat to_cmat(const imat &real, const imat &imag);
  template cmat to_cmat(const mat &real, const mat &imag);

} // namespace itpp

//! \endcond