IT++ Logo

converters.cpp

Go to the documentation of this file.
00001 
00030 #include <itpp/base/converters.h>
00031 #include <itpp/base/matfunc.h>
00032 #include <itpp/base/math/log_exp.h>
00033 
00035 
00036 #ifndef HAVE_RINT
00037 double rint(double x)
00038 {
00039   // zero or NaN case
00040   if ((x == 0.0) || (x != x))
00041     return x;
00042 
00043   // negative case
00044   bool neg = false;
00045   if (x < 0.0) {
00046     x = -x;
00047     neg = true;
00048   }
00049 
00050   double y = std::floor(x + 0.5);
00051   int i = static_cast<int>(y);
00052   if ((y - x >= 0.5) && (i & 1))
00053     --y;
00054 
00055   return neg ? -y : y;
00056 }
00057 #endif // HAVE_RINT
00058 
00059 
00060 namespace itpp
00061 {
00062 
00063 // ----------------------------------------------------------------------
00064 // Vector converters
00065 // ----------------------------------------------------------------------
00066 
00067 ivec to_ivec(int s) { ivec out(1); out(0) = s; return out; }
00068 
00069 vec to_vec(double s) { vec out(1); out(0) = s; return out; }
00070 
00071 cvec to_cvec(double real, double imag)
00072 {
00073   cvec out(1);
00074   out(0) = std::complex<double>(real, imag);
00075   return out;
00076 }
00077 
00078 // ----------------------------------------------------------------------
00079 // Miscellaneous converters
00080 // ----------------------------------------------------------------------
00081 
00082 bvec dec2bin(int length, int index)
00083 {
00084   int i, bintemp = index;
00085   bvec temp(length);
00086 
00087   for (i = length - 1; i >= 0; i--) {
00088     temp(i) = bin(bintemp & 1);
00089     bintemp = (bintemp >> 1);
00090   }
00091   return temp;
00092 }
00093 
00094 bvec dec2bin(int index, bool msb_first)
00095 {
00096   int length = int2bits(index);
00097   int i, bintemp = index;
00098   bvec temp(length);
00099 
00100   for (i = length - 1; i >= 0; i--) {
00101     temp(i) = bin(bintemp & 1);
00102     bintemp = (bintemp >> 1);
00103   }
00104   if (msb_first) {
00105     return temp;
00106   }
00107   else {
00108     return reverse(temp);
00109   }
00110 }
00111 
00112 void dec2bin(int index, bvec &v)
00113 {
00114   int i, bintemp = index;
00115   v.set_size(int2bits(index), false);
00116 
00117   for (i = v.size() - 1; i >= 0; i--) {
00118     v(i) = bin(bintemp & 1);
00119     bintemp = (bintemp >> 1);
00120   }
00121 }
00122 
00123 int bin2dec(const bvec &inbvec, bool msb_first)
00124 {
00125   int i, temp = 0;
00126   int sizebvec = inbvec.length();
00127   if (msb_first) {
00128     for (i = 0; i < sizebvec; i++) {
00129       temp += pow2i(sizebvec - i - 1) * int(inbvec(i));
00130     }
00131   }
00132   else {
00133     for (i = 0; i < sizebvec; i++) {
00134       temp += pow2i(i) * int(inbvec(i));
00135     }
00136   }
00137   return temp;
00138 }
00139 
00140 bvec oct2bin(const ivec &octalindex, short keepzeros)
00141 {
00142   int length = octalindex.length(), i;
00143   bvec out(3*length);
00144   for (i = 0; i < length; i++) {
00145     out.replace_mid(3*i, dec2bin(3, octalindex(i)));
00146   }
00147   //remove zeros if keepzeros = 0
00148   if (keepzeros == 0) {
00149     for (i = 0; i < out.length(); i++) {
00150       if ((short)out(i) != 0) {
00151         return out.right(out.length() - i);
00152         break;
00153       }
00154     }
00155     return bvec("0");
00156   }
00157   else {
00158     return out;
00159   }
00160 }
00161 
00162 ivec bin2oct(const bvec &inbits)
00163 {
00164   int start, Itterations = ceil_i(inbits.length() / 3.0);
00165   ivec out(Itterations);
00166   for (int i = Itterations - 1; i > 0; i--) {
00167     start = 3 * i - (3 * Itterations - inbits.length());
00168     out(i) = bin2dec(inbits.mid(start, 3));
00169   }
00170   out(0) = bin2dec(inbits.left(inbits.length() - ((Itterations - 1) * 3)));
00171   return out;
00172 }
00173 
00174 ivec bin2pol(const bvec &inbvec)
00175 {
00176   return 1 -2*to_ivec(inbvec);
00177 }
00178 
00179 bvec pol2bin(const ivec &inpol)
00180 {
00181   return to_bvec((1 -inpol) / 2);
00182 }
00183 
00184 
00185 // Round to nearest integer and return ivec
00186 ivec round_i(const vec &x) { return to_ivec(round(x)); }
00187 // Round to nearest integer and return imat
00188 imat round_i(const mat &x) { return to_imat(round(x)); }
00189 
00190 // Round to nearest upper integer
00191 ivec ceil_i(const vec &x) { return to_ivec(ceil(x)); }
00192 // Round to nearest upper integer
00193 imat ceil_i(const mat &x) { return to_imat(ceil(x)); }
00194 
00195 // Round to nearest lower integer
00196 ivec floor_i(const vec &x) { return to_ivec(floor(x)); }
00197 // Round to nearest lower integer
00198 imat floor_i(const mat &x) { return to_imat(floor(x)); }
00199 
00200 
00201 cvec round_to_zero(const cvec &x, double threshold)
00202 {
00203   cvec temp(x.length());
00204 
00205   for (int i = 0; i < x.length(); i++)
00206     temp(i) = round_to_zero(x(i), threshold);
00207 
00208   return temp;
00209 }
00210 
00211 cmat round_to_zero(const cmat &x, double threshold)
00212 {
00213   cmat temp(x.rows(), x.cols());
00214 
00215   for (int i = 0; i < x.rows(); i++) {
00216     for (int j = 0; j < x.cols(); j++) {
00217       temp(i, j) = round_to_zero(x(i, j), threshold);
00218     }
00219   }
00220 
00221   return temp;
00222 }
00223 
00224 
00225 std::string to_str(const double &i, const int precision)
00226 {
00227   std::ostringstream ss;
00228   ss.precision(precision);
00229   ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
00230   ss << i;
00231   return ss.str();
00232 }
00233 
00234 // ----------------------------------------------------------------------
00235 // Instantiations
00236 // ----------------------------------------------------------------------
00237 
00238 template bvec to_bvec(const svec &v);
00239 template bvec to_bvec(const ivec &v);
00240 
00241 template svec to_svec(const bvec &v);
00242 template svec to_svec(const ivec &v);
00243 template svec to_svec(const vec &v);
00244 
00245 #if (GCC_VERSION >= 30400)
00246 template ivec to_ivec(const bvec &v);
00247 #endif
00248 template ivec to_ivec(const svec &v);
00249 template ivec to_ivec(const vec &v);
00250 
00251 template vec to_vec(const bvec &v);
00252 template vec to_vec(const svec &v);
00253 template vec to_vec(const ivec &v);
00254 
00255 template cvec to_cvec(const bvec &v);
00256 template cvec to_cvec(const svec &v);
00257 template cvec to_cvec(const ivec &v);
00258 template cvec to_cvec(const vec &v);
00259 
00260 template cvec to_cvec(const bvec &real, const bvec &imag);
00261 template cvec to_cvec(const svec &real, const svec &imag);
00262 template cvec to_cvec(const ivec &real, const ivec &imag);
00263 template cvec to_cvec(const vec &real, const vec &imag);
00264 
00265 template bmat to_bmat(const smat &m);
00266 template bmat to_bmat(const imat &m);
00267 
00268 template smat to_smat(const bmat &m);
00269 template smat to_smat(const imat &m);
00270 template smat to_smat(const mat &m);
00271 
00272 template imat to_imat(const bmat &m);
00273 template imat to_imat(const smat &m);
00274 template imat to_imat(const mat &m);
00275 
00276 template mat to_mat(const bmat &m);
00277 #if (GCC_VERSION >= 30400)
00278 template mat to_mat(const smat &m);
00279 template mat to_mat(const imat &m);
00280 #endif
00281 
00282 template cmat to_cmat(const bmat &m);
00283 template cmat to_cmat(const smat &m);
00284 template cmat to_cmat(const imat &m);
00285 template cmat to_cmat(const mat &m);
00286 
00287 template cmat to_cmat(const bmat &real, const bmat &imag);
00288 template cmat to_cmat(const smat &real, const smat &imag);
00289 template cmat to_cmat(const imat &real, const imat &imag);
00290 template cmat to_cmat(const mat &real, const mat &imag);
00291 
00292 } // namespace itpp
00293 
SourceForge Logo

Generated on Fri May 1 11:09:15 2009 for IT++ by Doxygen 1.5.8