/*---------------------------------------------------------------------------* * IT++ * *---------------------------------------------------------------------------* * Copyright (c) 1995-2004 by Tony Ottosson, Thomas Eriksson, Pål Frenger, * * Tobias Ringström, and Jonas Samuelsson. * * * * Permission to use, copy, modify, and distribute this software and its * * documentation under the terms of the GNU General Public License is hereby * * granted. No representations are made about the suitability of this * * software for any purpose. It is provided "as is" without expressed or * * implied warranty. See the GNU General Public License for more details. * *---------------------------------------------------------------------------*/ /*! \file \brief Implementation of converters between different vector and matrix types. \author Tony Ottosson, Tobias Ringström and Pål Frenger 1.19 2004/08/19 08:25:19 */ #include "base/converters.h" #include "base/scalfunc.h" #include "base/matfunc.h" #include "base/binary.h" namespace itpp { //--------------- Converters for vectors ------------------ template bvec to_bvec(const Vec &v) { bvec temp(v.length()); for (int i=0;i(v(i)); temp(i)=bin(v(i)); } return temp; } template svec to_svec(const Vec &v) { svec temp(v.length()); for (int i=0;i(v(i)); temp(i)=short(v(i)); } return temp; } template ivec to_ivec(const Vec &v) { ivec temp(v.length()); for (int i=0;i(v(i)); temp(i)=int(v(i)); } return temp; } template vec to_vec(const Vec &v) { vec temp(v.length()); for (int i=0;i(v(i)); temp(i)=double(v(i)); } return temp; } template cvec to_cvec(const Vec &v) { cvec temp(v.length()); for (int i=0;i >(v(i)); temp(i)=complex(v(i)); } return temp; } //! Specialized implementation of to_cvec for bvec. cvec to_cvec(const bvec &v) { cvec temp(v.length()); for (int i=0;i(0.0,0.0); } else { temp(i) = complex(1.0,0.0); } } return temp; } //! Specialized implementation of to_ivec for scalar. ivec to_ivec(int s) {ivec out(1); out(0) = s; return out;} //! Specialized implementation of to_vec for scalar. vec to_vec(double s) {vec out(1); out(0) = s; return out;} //! Specialized implementation of to_cvec for scalar. cvec to_cvec(double real, double imag) {cvec out(1); out(0) = complex(real,imag); return out;} //------------- end of converters for vectors ------------- //--------------- Converters for matricies ------------------ template bmat to_bmat(const Mat &m) { bmat temp(m.rows(),m.cols()); for (int i=0;i(m(i,j)); temp(i,j)=bin(m(i,j)); } } return temp; } template smat to_smat(const Mat &m) { smat temp(m.rows(),m.cols()); for (int i=0;i(m(i,j)); temp(i,j)=short(m(i,j)); } } return temp; } template imat to_imat(const Mat &m) { imat temp(m.rows(),m.cols()); for (int i=0;i(m(i,j)); temp(i,j)=int(m(i,j)); } } return temp; } template mat to_mat(const Mat &m) { mat temp(m.rows(),m.cols()); for (int i=0;i(m(i,j)); temp(i,j)=double(m(i,j)); } } return temp; } template cmat to_cmat(const Mat &m) { cmat temp(m.rows(),m.cols()); for (int i=0;i >(m(i,j)); temp(i,j)=complex(m(i,j)); } } return temp; } //! Specialization of to_cmat for bmat cmat to_cmat(const bmat &m) { cmat temp(m.rows(),m.cols()); for (int i=0;i(0.0,0.0); } else { temp(i,j) = complex(1.0,0.0); } } } return temp; } //------------- end of converters for matricies ------------- template cvec to_cvec(const Vec &real, const Vec &imag) { assert(real.length()==imag.length()); cvec temp(real.length()); for(int i=0;i(real(i),imag(i)); } return temp; } template cmat to_cmat(const Mat &real, const Mat &imag) { it_assert1(real.rows()==imag.rows() && real.cols()==imag.cols(), "to_cmat::real and imag part sizes does not match"); cmat temp(real.rows(),real.cols()); for (int i=0;i(real(i,j), imag(i,j)); } } return temp; } bvec dec2bin(int length, int index) { int i, bintemp = index; bvec temp(length); for (i=length-1; i>=0; i--) { temp(i) = bin(bintemp & 1); bintemp = (bintemp >> 1); } return temp; } bvec dec2bin(int index, bool msb_first) { int length = needed_bits(index)+1; int i, bintemp = index; bvec temp(length); for (i=length-1; i>=0; i--) { temp(i) = bin(bintemp & 1); bintemp = (bintemp >> 1); } if (msb_first){ return temp; } else{ return reverse(temp); } } void dec2bin(int index, bvec &v) { int i, bintemp = index; v.set_size(needed_bits(index)+1, false); for (i=v.size()-1; i>=0; i--) { v(i) = bin(bintemp & 1); bintemp = (bintemp >> 1); } } int bin2dec(const bvec &inbvec, bool msb_first) { int i, temp=0; int sizebvec=inbvec.length(); if (msb_first) { for (i=0; i0; i--) { start = 3*i - ( 3*Itterations - inbits.length() ); out(i) = bin2dec(inbits.mid(start,3)); } out(0) = bin2dec( inbits.left(inbits.length()-((Itterations-1)*3)) ); return out; } ivec bin2pol(const bvec &inbvec) { return 1-2*to_ivec(inbvec); } bvec pol2bin(const ivec &inpol) { return to_bvec((1-inpol)/2); } /* template <> string to_str(const double &i) { std::ostringstream ss; ss.precision(8); ss.setf(ostringstream::scientific,ostringstream::floatfield); ss << i; return ss.str(); } */ string to_str(const double &i, const int precision) { std::ostringstream ss; ss.precision(precision); ss.setf(ostringstream::scientific,ostringstream::floatfield); ss << i; return ss.str(); } // ---------------------- Instantiations ----------------------------------------- //! Template instantiation of to_bvec template bvec to_bvec(const svec &v); //! Template instantiation of to_bvec //#ifndef _MSC_VER template bvec to_bvec(const Vec &v); //#endif //! Template instantiation of to_svec template svec to_svec(const bvec &v); //! Template instantiation of to_svec template svec to_svec(const ivec &v); //! Template instantiation of to_svec template svec to_svec(const svec &v); //#ifndef _MSC_VER //! Template instantiation of to_ivec template ivec to_ivec(const bvec &v); //#endif //! Template instantiation of to_ivec template ivec to_ivec(const svec &v); //! Template instantiation of to_ivec template ivec to_ivec(const ivec &v); //! Template instantiation of to_ivec template ivec to_ivec(const vec &v); //! Template instantiation of to_vec template vec to_vec(const bvec &v); //! Template instantiation of to_vec template vec to_vec(const svec &v); //! Template instantiation of to_vec template vec to_vec(const ivec &v); //! Template instantiation of to_vec template vec to_vec(const vec &v); // Template instantiation of to_cvec //template cvec to_cvec(const bvec &v); //Specialization created above //! Template instantiation of to_cvec template cvec to_cvec(const svec &v); //! Template instantiation of to_cvec template cvec to_cvec(const ivec &v); //! Template instantiation of to_cvec template cvec to_cvec(const vec &v); //! Template instantiation of to_cvec template cvec to_cvec(const cvec &v); //! Template instantiation of to_cvec template cvec to_cvec(const bvec &real, const bvec &imag); //! Template instantiation of to_cvec template cvec to_cvec(const svec &real, const svec &imag); //! Template instantiation of to_cvec template cvec to_cvec(const ivec &real, const ivec &imag); //! Template instantiation of to_cvec template cvec to_cvec(const vec &real, const vec &imag); //! Template instantiation of to_bmat template bmat to_bmat(const smat &m); //! Template instantiation of to_bmat template bmat to_bmat(const imat &m); //! Template instantiation of to_bmat template smat to_smat(const bmat &m); //! Template instantiation of to_bmat template smat to_smat(const imat &m); //! Template instantiation of to_imat template imat to_imat(const bmat &m); //! Template instantiation of to_imat template imat to_imat(const smat &m); //! Template instantiation of to_imat template imat to_imat(const imat &m); // Template instantiation of to_imat template imat to_imat(const mat &m); //! Template instantiation of to_mat template mat to_mat(const bmat &m); //! Template instantiation of to_mat template mat to_mat(const smat &m); //! Template instantiation of to_mat template mat to_mat(const imat &m); //! Template instantiation of to_mat template mat to_mat(const mat &m); // Template instantiation of to_cmat //template cmat to_cmat(const bmat &m); //Specialization created above //! Template instantiation of to_cmat template cmat to_cmat(const smat &m); //! Template instantiation of to_cmat template cmat to_cmat(const imat &m); //! Template instantiation of to_cmat template cmat to_cmat(const mat &m); //! Template instantiation of to_cmat template cmat to_cmat(const cmat &m); //! Template instantiation of to_cmat template cmat to_cmat(const bmat &real, const bmat &imag); //! Template instantiation of to_cmat template cmat to_cmat(const smat &real, const smat &imag); //! Template instantiation of to_cmat template cmat to_cmat(const imat &real, const imat &imag); //! Template instantiation of to_cmat template cmat to_cmat(const mat &real, const mat &imag); } //namespace itpp