/*---------------------------------------------------------------------------* * 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 Definitions of converters between different vector and matrix types \author Tony Ottosson, Tobias Ringström and Pål Frenger 1.15 2004/08/19 08:25:18 */ #ifndef __converters_h #define __converters_h #include "base/vec.h" #include "base/mat.h" #include using std::ostringstream; namespace itpp { /*! \defgroup convertfunc Conversion Functions */ //!@{ /*! \relates Vec \brief Converts a Vec to bvec */ template bvec to_bvec(const Vec &v); /*! \relates Vec \brief Converts a Vec to svec */ template svec to_svec(const Vec &v); /*! \relates Vec \brief Converts a Vec to ivec */ template ivec to_ivec(const Vec &v); /*! \relates Vec \brief Converts a Vec to vec */ template vec to_vec(const Vec &v); /*! \relates Vec \brief Converts a Vec to cvec */ template cvec to_cvec(const Vec &v); /*! \relates Vec \brief Converts real and imaginary Vec to cvec */ template cvec to_cvec(const Vec &real, const Vec &imag); /*! \relates Vec \brief Converts an int to ivec */ ivec to_ivec(int s); /*! \relates Vec \brief Converts an double to vec */ vec to_vec(double s); /*! \relates Vec \brief Converts real and imaginary double to cvec */ cvec to_cvec(double real, double imag); /*! \relates Mat \brief Converts a Mat to bmat */ template bmat to_bmat(const Mat &m); /*! \relates Mat \brief Converts a Mat to smat */ template smat to_smat(const Mat &m); /*! \relates Mat \brief Converts a Mat to imat */ template imat to_imat(const Mat &m); /*! \relates Mat \brief Converts a Mat to mat */ template mat to_mat(const Mat &m); /*! \relates Mat \brief Converts a Mat to cmat */ template cmat to_cmat(const Mat &m); /*! \relates Mat \brief Converts real and imaginary Mat to cmat */ template cmat to_cmat(const Mat &real, const Mat &imag); /*! \brief Convert a decimal int \a index to bvec using \a length bits in the representation */ bvec dec2bin(int length, int index); /*! \brief Convert a decimal int \a index to bvec. Value returned in \a v. */ void dec2bin(int index, bvec &v); /*! \brief Convert a decimal int \a index to bvec with the first bit as MSB if \a msb_first == true */ bvec dec2bin(int index, bool msb_first = true); /*! \brief Convert a bvec to decimal int with the first bit as MSB if \a msb_first == true */ int bin2dec(const bvec &inbvec, bool msb_first = true); /*! \brief Convert ivec of octal form to bvec Converts from ivec containing {0,1,2,...,7} to bvec containing {0,1}. Removes zeros to the left if keepzeros = 0 (default). Example: oct2bin("3 5 5 1") returns {1 1 1 0 1 1 0 1 0 0 1}. */ bvec oct2bin(const ivec &octalindex, short keepzeros = 0); /*! \brief Convert bvec to octal ivec Converts from bvec containing {0,1} to ivec containing {0,1,2,...,7}. Adds zeros to the left if inbits.length() is not a factor of 3. Example: bin2oct("1 1 1 0 1 1 0 1 0 0 1") returns {3 5 5 1}. */ ivec bin2oct(const bvec &inbits); //! Convert bvec to polar binary representation as ivec ivec bin2pol(const bvec &inbvec); //! Convert binary polar ivec to bvec bvec pol2bin(const ivec &inpol); /*! \brief Convert anything to string \param i (Input) The value to be converted to a string */ template string to_str(const T &i); /*! \brief Convert double to string \param i (Input) The value to be converted to a string \param precision (Input) The number of digits used to represent the fractional part */ string to_str(const double &i, const int precision); //!@} template string to_str(const T &i) { std::ostringstream ss; ss.precision(8); ss.setf(ostringstream::scientific,ostringstream::floatfield); ss << i; return ss.str(); } } //namespace itpp #endif // __converters_h