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

Generated on Mon Jan 7 22:28:55 2008 for IT++ by Doxygen 1.5.4