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

Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4