converters.h
Go to the documentation of this file.
#ifndef CONVERTERS_H
#define CONVERTERS_H
#include <itpp/base/help_functions.h>
#include <itpp/base/math/misc.h>
#include <itpp/itexports.h>
namespace itpp
{
// ----------------------------------------------------------------------
// Converters for vectors
// ----------------------------------------------------------------------
template <class T>
bvec to_bvec(const Vec<T> &v)
{
bvec temp(v.length());
for (int i = 0; i < v.length(); ++i) {
temp(i) = static_cast<bin>(v(i));
}
return temp;
}
template <class T>
svec to_svec(const Vec<T> &v)
{
svec temp(v.length());
for (int i = 0; i < v.length(); ++i) {
temp(i) = static_cast<short>(v(i));
}
return temp;
}
template <class T>
ivec to_ivec(const Vec<T> &v)
{
ivec temp(v.length());
for (int i = 0; i < v.length(); ++i) {
temp(i) = static_cast<int>(v(i));
}
return temp;
}
template <class T>
vec to_vec(const Vec<T> &v)
{
vec temp(v.length());
for (int i = 0; i < v.length(); ++i) {
temp(i) = static_cast<double>(v(i));
}
return temp;
}
template <class T>
cvec to_cvec(const Vec<T> &v)
{
cvec temp(v.length());
for (int i = 0; i < v.length(); ++i) {
temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0);
}
return temp;
}
template<> inline
cvec to_cvec(const cvec& v)
{
return v;
}
template <class T>
cvec to_cvec(const Vec<T> &real, const Vec<T> &imag)
{
it_assert(real.length() == imag.length(),
"to_cvec(): real and imaginary parts must have the same length");
cvec temp(real.length());
for (int i = 0; i < real.length(); ++i) {
temp(i) = std::complex<double>(static_cast<double>(real(i)),
static_cast<double>(imag(i)));
}
return temp;
}
ivec to_ivec(int s);
vec to_vec(double s);
cvec to_cvec(double real, double imag);
// ----------------------------------------------------------------------
// Converters for matrices
// ----------------------------------------------------------------------
template <class T>
bmat to_bmat(const Mat<T> &m)
{
bmat temp(m.rows(), m.cols());
for (int i = 0; i < temp.rows(); ++i) {
for (int j = 0; j < temp.cols(); ++j) {
temp(i, j) = static_cast<bin>(m(i, j));
}
}
return temp;
}
template <class T>
smat to_smat(const Mat<T> &m)
{
smat temp(m.rows(), m.cols());
for (int i = 0; i < temp.rows(); ++i) {
for (int j = 0; j < temp.cols(); ++j) {
temp(i, j) = static_cast<short>(m(i, j));
}
}
return temp;
}
template <class T>
imat to_imat(const Mat<T> &m)
{
imat temp(m.rows(), m.cols());
for (int i = 0; i < temp.rows(); ++i) {
for (int j = 0; j < temp.cols(); ++j) {
temp(i, j) = static_cast<int>(m(i, j));
}
}
return temp;
}
template <class T>
mat to_mat(const Mat<T> &m)
{
mat temp(m.rows(), m.cols());
for (int i = 0; i < temp.rows(); ++i) {
for (int j = 0; j < temp.cols(); ++j) {
temp(i, j) = static_cast<double>(m(i, j));
}
}
return temp;
}
template <class T>
cmat to_cmat(const Mat<T> &m)
{
cmat temp(m.rows(), m.cols());
for (int i = 0; i < temp.rows(); ++i) {
for (int j = 0; j < temp.cols(); ++j) {
temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0);
}
}
return temp;
}
template<> inline
cmat to_cmat(const cmat& m)
{
return m;
}
template <class T>
cmat to_cmat(const Mat<T> &real, const Mat<T> &imag)
{
it_assert_debug((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 < temp.rows(); ++i) {
for (int j = 0; j < temp.cols(); ++j) {
temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)),
static_cast<double>(imag(i, j)));
}
}
return temp;
}
ITPP_EXPORT bvec dec2bin(int length, int index);
ITPP_EXPORT void dec2bin(int index, bvec &v);
ITPP_EXPORT bvec dec2bin(int index, bool msb_first = true);
ITPP_EXPORT int bin2dec(const bvec &inbvec, bool msb_first = true);
ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros = 0);
ITPP_EXPORT ivec bin2oct(const bvec &inbits);
ITPP_EXPORT ivec bin2pol(const bvec &inbvec);
ITPP_EXPORT bvec pol2bin(const ivec &inpol);
inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); }
inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); }
ITPP_EXPORT double round(double x);
ITPP_EXPORT vec round(const vec &x);
ITPP_EXPORT mat round(const mat &x);
ITPP_EXPORT int round_i(double x);
ITPP_EXPORT ivec round_i(const vec &x);
ITPP_EXPORT imat round_i(const mat &x);
inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); }
inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); }
inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); }
ITPP_EXPORT ivec ceil_i(const vec &x);
ITPP_EXPORT imat ceil_i(const mat &x);
inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); }
inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); }
inline int floor_i(double x) { return static_cast<int>(std::floor(x)); }
ITPP_EXPORT ivec floor_i(const vec &x);
ITPP_EXPORT imat floor_i(const mat &x);
inline double round_to_zero(double x, double threshold = 1e-14)
{
return ((std::fabs(x) < threshold) ? 0.0 : x);
}
inline std::complex<double> round_to_zero(const std::complex<double>& x,
double threshold = 1e-14)
{
return std::complex<double>(round_to_zero(x.real(), threshold),
round_to_zero(x.imag(), threshold));
}
inline vec round_to_zero(const vec &x, double threshold = 1e-14)
{
return apply_function<double>(round_to_zero, x, threshold);
}
inline mat round_to_zero(const mat &x, double threshold = 1e-14)
{
return apply_function<double>(round_to_zero, x, threshold);
}
ITPP_EXPORT cvec round_to_zero(const cvec &x, double threshold = 1e-14);
ITPP_EXPORT cmat round_to_zero(const cmat &x, double threshold = 1e-14);
inline double round_to_infty(const double in, const double threshold = 1e9)
{
return (std::fabs(in)>threshold)?itpp::round(in):in;
}
inline std::complex<double> round_to_infty(const std::complex<double> &in, const double threshold = 1e9)
{
return std::complex<double>(round_to_infty(in.real(), threshold),
round_to_infty(in.imag(), threshold));
}
inline vec round_to_infty(const vec &in, const double threshold = 1e9)
{
return apply_function<double>(round_to_infty, in, threshold);
}
inline mat round_to_infty(const mat &in, const double threshold = 1e9)
{
return apply_function<double>(round_to_infty, in, threshold);
}
ITPP_EXPORT cvec round_to_infty(const cvec &in, const double threshold = 1e9);
ITPP_EXPORT cmat round_to_infty(const cmat &in, const double threshold = 1e9);
inline int gray_code(int x) { return x ^(x >> 1); }
template <typename T>
std::string to_str(const T &i);
ITPP_EXPORT std::string to_str(const double &i, const int precision);
template <typename T>
std::string to_str(const T &i)
{
std::ostringstream ss;
ss.precision(8);
ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
ss << i;
return ss.str();
}
// ---------------------------------------------------------------------
// Instantiations
// ---------------------------------------------------------------------
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec to_bvec(const svec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec to_bvec(const ivec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const bvec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const ivec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const vec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const bvec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const svec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const vec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const bvec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const svec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const ivec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const bvec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const svec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const ivec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const vec &v);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const bvec &real, const bvec &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const svec &real, const svec &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const ivec &real, const ivec &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const vec &real, const vec &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat to_bmat(const smat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat to_bmat(const imat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const bmat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const imat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const mat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const bmat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const smat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const mat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const bmat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const smat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const imat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const bmat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const smat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const imat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const mat &m);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const bmat &real, const bmat &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const smat &real, const smat &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const imat &real, const imat &imag);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const mat &real, const mat &imag);
} // namespace itpp
#endif // CONVERTERS_H
itpp::Mat< T >
itpp::Mat::to_cmat
cmat to_cmat(const Mat< T > &real, const Mat< T > &imag)
Converts real and imaginary Mat<T> to cmat.
Definition: converters.h:256
itpp::Mat::to_cmat
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition: converters.h:232
itpp::Mat::to_bmat
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition: converters.h:168
itpp::Mat::rows
int rows() const
The number of rows.
Definition: mat.h:237
itpp::Mat::to_mat
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition: converters.h:216
itpp::Mat::cols
int cols() const
The number of columns.
Definition: mat.h:235
itpp::Mat::to_imat
imat to_imat(const Mat< T > &m)
Converts a Mat<T> to imat.
Definition: converters.h:200
itpp::Mat::to_smat
smat to_smat(const Mat< T > &m)
Converts a Mat<T> to smat.
Definition: converters.h:184
itpp::Vec
Vector Class (Templated)
Definition: vec.h:245
itpp::Vec::to_cvec
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition: converters.h:107
itpp::Vec::to_svec
svec to_svec(const Vec< T > &v)
Converts a Vec<T> to svec.
Definition: converters.h:65
itpp::Vec::to_cvec
cvec to_cvec(double real, double imag)
Converts real and imaginary double to cvec.
itpp::Vec::to_vec
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
Definition: converters.h:93
itpp::Vec::to_cvec
cvec to_cvec(const Vec< T > &real, const Vec< T > &imag)
Converts real and imaginary Vec<T> to cvec.
Definition: converters.h:129
itpp::Vec::to_ivec
ivec to_ivec(int s)
Converts an int to ivec.
itpp::Vec::to_vec
vec to_vec(double s)
Converts an double to vec.
itpp::Vec::to_ivec
ivec to_ivec(const Vec< T > &v)
Converts a Vec<T> to ivec.
Definition: converters.h:79
itpp::Vec::length
int length() const
The size of the vector.
Definition: vec.h:269
itpp::Vec::to_bvec
bvec to_bvec(const Vec< T > &v)
Converts a Vec<T> to bvec.
Definition: converters.h:51
itpp::bin
Binary arithmetic (boolean) class.
Definition: binary.h:57
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
itpp::length
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
itpp::imag
vec imag(const cvec &data)
Imaginary part of complex values.
Definition: elem_math.cpp:180
itpp::real
vec real(const cvec &data)
Real part of complex values.
Definition: elem_math.cpp:157
help_functions.h
Help functions to make functions with vec and mat as arguments.
bmat
Mat< bin > bmat
bin matrix
Definition: mat.h:508
misc.h
Miscellaneous functions - header file.
itpp
itpp namespace
Definition: itmex.h:37
itpp::round_to_infty
double round_to_infty(const double in, const double threshold=1e9)
Remove trailing digits, found after the decimal point, for numbers greater than threshold.
Definition: converters.h:390
itpp::to_cvec
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition: converters.h:107
itpp::ceil
mat ceil(const mat &x)
Round to nearest upper integer.
Definition: converters.h:337
itpp::floor
mat floor(const mat &x)
Round to nearest lower integer.
Definition: converters.h:348
itpp::round_to_zero
double round_to_zero(double x, double threshold=1e-14)
Round x to zero if abs(x) is smaller than threshold.
Definition: converters.h:358
itpp::bin2oct
ITPP_EXPORT ivec bin2oct(const bvec &inbits)
Convert bvec to octal ivec.
itpp::to_svec
svec to_svec(const Vec< T > &v)
Converts a Vec<T> to svec.
Definition: converters.h:65
itpp::round_i
ITPP_EXPORT int round_i(double x)
Round to nearest integer.
itpp::floor
vec floor(const vec &x)
Round to nearest lower integer.
Definition: converters.h:346
itpp::ceil
vec ceil(const vec &x)
Round to nearest upper integer.
Definition: converters.h:335
itpp::rad_to_deg
double rad_to_deg(double x)
Convert radians to degrees.
Definition: converters.h:317
itpp::pol2bin
ITPP_EXPORT bvec pol2bin(const ivec &inpol)
Convert binary polar ivec to bvec.
itpp::oct2bin
ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros=0)
Convert ivec of octal form to bvec.
itpp::to_cmat
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition: converters.h:232
itpp::bin2pol
ITPP_EXPORT ivec bin2pol(const bvec &inbvec)
Convert bvec to polar binary representation as ivec.
itpp::bin2dec
ITPP_EXPORT int bin2dec(const bvec &inbvec, bool msb_first=true)
Convert a bvec to decimal int with the first bit as MSB if msb_first == true.
itpp::to_bmat
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition: converters.h:168
itpp::gray_code
int gray_code(int x)
Convert to Gray Code.
Definition: converters.h:421
itpp::to_vec
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
Definition: converters.h:93
itpp::to_str
std::string to_str(const T &i)
Convert anything to string.
Definition: converters.h:444
itpp::dec2bin
ITPP_EXPORT bvec dec2bin(int length, int index)
Convert a decimal int index to bvec using length bits in the representation.
itpp::pi
const double pi
Constant Pi.
Definition: misc.h:103
itpp::to_mat
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
Definition: converters.h:216
itpp::round
ITPP_EXPORT double round(double x)
Round to nearest integer, return result in double.
itpp::ceil_i
int ceil_i(double x)
The nearest larger integer.
Definition: converters.h:339
itpp::to_imat
imat to_imat(const Mat< T > &m)
Converts a Mat<T> to imat.
Definition: converters.h:200
itpp::deg_to_rad
double deg_to_rad(double x)
Convert degrees to radians.
Definition: converters.h:319
itpp::to_smat
smat to_smat(const Mat< T > &m)
Converts a Mat<T> to smat.
Definition: converters.h:184
itpp::floor_i
int floor_i(double x)
The nearest smaller integer.
Definition: converters.h:350
itpp::to_ivec
ivec to_ivec(const Vec< T > &v)
Converts a Vec<T> to ivec.
Definition: converters.h:79
itpp::to_bvec
bvec to_bvec(const Vec< T > &v)
Converts a Vec<T> to bvec.
Definition: converters.h:51

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.1