IT++ Logo Newcom Logo

converters.cpp

Go to the documentation of this file.
00001 
00033 #include <itpp/base/converters.h>
00034 #include <itpp/base/matfunc.h>
00035 
00036 
00037 namespace itpp {
00038 
00039   //--------------- Converters for vectors ------------------
00040 
00041   template <class T>
00042   bvec to_bvec(const Vec<T> &v)
00043   {
00044     bvec temp(v.length());
00045     for (int i=0;i<v.length();i++){
00046       //temp(i)=static_cast<bin>(v(i));
00047       temp(i)=bin(v(i));
00048     }
00049     return temp;
00050   }
00051 
00052   template <class T>
00053   svec to_svec(const Vec<T> &v)
00054   {
00055     svec temp(v.length());
00056     for (int i=0;i<v.length();i++){
00057       //temp(i)=static_cast<short>(v(i));
00058       temp(i)=short(v(i));
00059     }
00060     return temp;
00061   }
00062 
00063   template <class T>
00064   ivec to_ivec(const Vec<T> &v)
00065   {
00066     ivec temp(v.length());
00067     for (int i=0;i<v.length();i++){
00068       //temp(i)=static_cast<int>(v(i));
00069       temp(i)=int(v(i));
00070     }
00071     return temp;
00072   }
00073 
00074   template <class T>
00075   vec to_vec(const Vec<T> &v)
00076   {
00077     vec temp(v.length());
00078     for (int i=0;i<v.length();i++){
00079       //temp(i)=static_cast<double>(v(i));
00080       temp(i)=double(v(i));
00081     }
00082     return temp;
00083   }
00084 
00085   template <class T>
00086   cvec to_cvec(const Vec<T> &v)
00087   {
00088     cvec temp(v.length());
00089     for (int i=0;i<v.length();i++){
00090       //temp(i)=static_cast<complex<double> >(v(i));
00091       temp(i)=std::complex<double>(v(i));
00092     }
00093     return temp;
00094   }
00095 
00096   cvec to_cvec(const bvec &v)
00097   {
00098     cvec temp(v.length());
00099     for (int i=0;i<v.length();i++){
00100       if (v(i)==bin(0)) { 
00101         temp(i) = std::complex<double>(0.0,0.0);
00102       } else {
00103         temp(i) = std::complex<double>(1.0,0.0);
00104       }
00105     }
00106     return temp;
00107   }
00108 
00109   ivec to_ivec(int s) {ivec out(1); out(0) = s; return out;}
00110 
00111   vec to_vec(double s) {vec out(1); out(0) = s; return out;}
00112 
00113   cvec to_cvec(double real, double imag) {cvec out(1); out(0) = std::complex<double>(real,imag); return out;}
00114 
00115   //------------- end of converters for vectors -------------
00116 
00117   //--------------- Converters for matricies ------------------
00118 
00119   template <class T>
00120   bmat to_bmat(const Mat<T> &m)
00121   {
00122     bmat temp(m.rows(),m.cols());
00123 
00124     for (int i=0;i<temp.rows();i++) {
00125       for (int j=0;j<temp.cols();j++) {
00126         //temp(i,j)=static_cast<bin>(m(i,j));
00127         temp(i,j)=bin(m(i,j));
00128       }
00129     }
00130 
00131     return temp;
00132   }
00133 
00134   template <class T>
00135   smat to_smat(const Mat<T> &m)
00136   {
00137     smat temp(m.rows(),m.cols());
00138 
00139     for (int i=0;i<temp.rows();i++) {
00140       for (int j=0;j<temp.cols();j++) {
00141         //temp(i,j)=static_cast<short>(m(i,j));
00142         temp(i,j)=short(m(i,j));
00143       }
00144     }
00145 
00146     return temp;
00147   }
00148 
00149   template <class T>
00150   imat to_imat(const Mat<T> &m)
00151   {
00152     imat temp(m.rows(),m.cols());
00153 
00154     for (int i=0;i<temp.rows();i++) {
00155       for (int j=0;j<temp.cols();j++) {
00156         //temp(i,j)=static_cast<int>(m(i,j));
00157         temp(i,j)=int(m(i,j));
00158       }
00159     }
00160 
00161     return temp;
00162   }
00163 
00164   template <class T>
00165   mat to_mat(const Mat<T> &m)
00166   {
00167     mat temp(m.rows(),m.cols());
00168 
00169     for (int i=0;i<temp.rows();i++) {
00170       for (int j=0;j<temp.cols();j++) {
00171         //temp(i,j)=static_cast<double>(m(i,j));
00172         temp(i,j)=double(m(i,j));
00173       }
00174     }
00175 
00176     return temp;
00177   }
00178 
00179   template <class T>
00180   cmat to_cmat(const Mat<T> &m)
00181   {
00182     cmat temp(m.rows(),m.cols());
00183 
00184     for (int i=0;i<temp.rows();i++) {
00185       for (int j=0;j<temp.cols();j++) {
00186         //temp(i,j)=static_cast<std::complex<double> >(m(i,j));
00187         temp(i,j)=std::complex<double>(m(i,j));
00188       }
00189     }
00190 
00191     return temp;
00192   }
00193 
00194   cmat to_cmat(const bmat &m)
00195   {
00196     cmat temp(m.rows(),m.cols());
00197 
00198     for (int i=0;i<temp.rows();i++) {
00199       for (int j=0;j<temp.cols();j++) {
00200         if (m(i,j)==bin(0)) { 
00201           temp(i,j) = std::complex<double>(0.0,0.0);
00202         } else {
00203           temp(i,j) = std::complex<double>(1.0,0.0);
00204         }
00205       }
00206     }
00207 
00208     return temp;
00209   }
00210 
00211   //------------- end of converters for matricies -------------
00212 
00213   template <class T>
00214   cvec to_cvec(const Vec<T> &real, const Vec<T> &imag)
00215   {
00216     it_assert(real.length()==imag.length(),"to_cvec: real and imaginary parts must have the same length");
00217     cvec temp(real.length());
00218     for(int i=0;i<real.length();i++){
00219       temp(i)=std::complex<double>(real(i),imag(i));
00220     }
00221                 
00222     return temp;
00223   }
00224 
00225   template <class T>
00226   cmat to_cmat(const Mat<T> &real, const Mat<T> &imag)
00227   {
00228     it_assert1(real.rows()==imag.rows() && real.cols()==imag.cols(), "to_cmat::real and imag part sizes does not match");
00229     cmat temp(real.rows(),real.cols());
00230 
00231     for (int i=0;i<temp.rows();i++) {
00232       for (int j=0;j<temp.cols();j++) {
00233         temp(i,j)=std::complex<double>(real(i,j), imag(i,j));
00234       }
00235     }
00236 
00237     return temp;
00238   }
00239 
00240   bvec dec2bin(int length, int index)
00241   {
00242     int i, bintemp = index;
00243     bvec temp(length);
00244 
00245     for (i=length-1; i>=0; i--) {
00246       temp(i) = bin(bintemp & 1);
00247       bintemp = (bintemp >> 1);
00248     }
00249     return temp;
00250   }
00251 
00252   bvec dec2bin(int index, bool msb_first)
00253   {
00254     int length = int2bits(index);
00255     int i, bintemp = index;
00256     bvec temp(length);
00257 
00258     for (i=length-1; i>=0; i--) {
00259       temp(i) = bin(bintemp & 1);
00260       bintemp = (bintemp >> 1);
00261     }
00262     if (msb_first){
00263       return temp;
00264     } else{
00265       return reverse(temp);
00266     }
00267   }
00268 
00269   void dec2bin(int index, bvec &v)
00270   {
00271     int i, bintemp = index;
00272     v.set_size(int2bits(index), false);
00273 
00274     for (i=v.size()-1; i>=0; i--) {
00275       v(i) = bin(bintemp & 1);
00276       bintemp = (bintemp >> 1);
00277     }
00278   }
00279 
00280   int bin2dec(const bvec &inbvec, bool msb_first)
00281   {
00282     int i, temp=0;
00283     int sizebvec=inbvec.length();
00284     if (msb_first) {
00285       for (i=0; i<sizebvec; i++) {
00286         temp+=pow2i(sizebvec-i-1)*int(inbvec(i));
00287       }
00288     } else {
00289       for (i=0; i<sizebvec; i++) {
00290         temp+=pow2i(i)*int(inbvec(i));
00291       }
00292     }
00293     return temp;
00294   }
00295 
00296   bvec oct2bin(const ivec &octalindex, short keepzeros)
00297   {
00298     int length = octalindex.length(), i;
00299     bvec out(3*length);
00300     for (i=0; i<length; i++) {
00301       out.replace_mid(3*i,dec2bin(3,octalindex(i)));
00302     }
00303     //remove zeros if keepzeros = 0
00304     if (keepzeros == 0) {
00305       for (i=0; i<out.length(); i++) {
00306         if ( (short)out(i) != 0) { 
00307           return out.right(out.length()-i);
00308           break;
00309         }
00310       }
00311       return bvec("0");
00312     } else {
00313       return out;
00314     }
00315   }
00316 
00317   ivec bin2oct(const bvec &inbits)
00318   {
00319     int start, Itterations = (int)ceil(float(inbits.length())/3.0);
00320     ivec out(Itterations);
00321     for (int i=Itterations-1; i>0; i--) {
00322       start = 3*i - ( 3*Itterations - inbits.length() );
00323       out(i) = bin2dec(inbits.mid(start,3));
00324     }
00325     out(0) = bin2dec( inbits.left(inbits.length()-((Itterations-1)*3)) );
00326     return out;
00327   }
00328 
00329   ivec bin2pol(const bvec &inbvec)
00330   {
00331     return 1-2*to_ivec(inbvec);
00332   }
00333 
00334   bvec pol2bin(const ivec &inpol)
00335   {
00336     return to_bvec((1-inpol)/2);
00337   }
00338 
00339   /*
00340   template <>
00341   string to_str(const double &i)
00342   {
00343     std::ostringstream ss;
00344     ss.precision(8);
00345     ss.setf(ostringstream::scientific,ostringstream::floatfield);
00346     ss << i;
00347     return ss.str();
00348   }
00349 */  
00350   std::string to_str(const double &i, const int precision)
00351   {
00352     std::ostringstream ss;
00353     ss.precision(precision);
00354     ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
00355     ss << i;
00356     return ss.str();
00357   }
00358   
00359   // ---------------------- Instantiations -----------------------------------------
00360 
00361   template bvec to_bvec(const svec &v);
00362   template bvec to_bvec(const Vec<int> &v);
00363   template svec to_svec(const bvec &v);
00364   template svec to_svec(const ivec &v);
00365   template svec to_svec(const svec &v);
00366   template svec to_svec(const vec &v);
00367 
00368   template ivec to_ivec(const bvec &v);
00369   template ivec to_ivec(const svec &v);
00370   template ivec to_ivec(const ivec &v);
00371   template ivec to_ivec(const vec &v);
00372 
00373   template vec to_vec(const bvec &v);
00374   template vec to_vec(const svec &v);
00375   template vec to_vec(const ivec &v);
00376   template vec to_vec(const vec &v);
00377 
00378   // Template instantiation of to_cvec
00379   //template cvec to_cvec(const bvec &v); //Specialization created above
00380 
00381   template cvec to_cvec(const svec &v);
00382   template cvec to_cvec(const ivec &v);
00383   template cvec to_cvec(const vec &v);
00384   template cvec to_cvec(const cvec &v);
00385   template cvec to_cvec(const bvec &real, const bvec &imag);
00386   template cvec to_cvec(const svec &real, const svec &imag);
00387   template cvec to_cvec(const ivec &real, const ivec &imag);
00388   template cvec to_cvec(const vec &real, const vec &imag);
00389 
00390   template bmat to_bmat(const smat &m);
00391   template bmat to_bmat(const imat &m);
00392   template smat to_smat(const bmat &m);
00393   template smat to_smat(const imat &m);
00394 
00395   template imat to_imat(const bmat &m);
00396   template imat to_imat(const smat &m);
00397   template imat to_imat(const imat &m);
00398   template imat to_imat(const mat &m);
00399 
00400   template mat to_mat(const bmat &m);
00401   template mat to_mat(const smat &m);
00402   template mat to_mat(const imat &m);
00403   template mat to_mat(const mat &m);
00404 
00405   // Template instantiation of to_cmat
00406   // template cmat to_cmat(const bmat &m); //Specialization created above
00407 
00408   template cmat to_cmat(const smat &m);
00409   template cmat to_cmat(const imat &m);
00410   template cmat to_cmat(const mat &m);
00411   template cmat to_cmat(const cmat &m);
00412   template cmat to_cmat(const bmat &real, const bmat &imag);
00413   template cmat to_cmat(const smat &real, const smat &imag);
00414   template cmat to_cmat(const imat &real, const imat &imag);
00415   template cmat to_cmat(const mat &real, const mat &imag);
00416 
00417 } // namespace itpp
SourceForge Logo

Generated on Thu Aug 30 02:47:17 2007 for IT++ by Doxygen 1.5.3