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

Generated on Sat Apr 19 10:43:51 2008 for IT++ by Doxygen 1.5.5