converters.cpp
Go to the documentation of this file.
#include <itpp/base/converters.h>
#include <itpp/base/itcompat.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/math/log_exp.h>
namespace itpp
{
// ----------------------------------------------------------------------
// Vector converters
// ----------------------------------------------------------------------
ivec to_ivec(int s) { ivec out(1); out(0) = s; return out; }
vec to_vec(double s) { vec out(1); out(0) = s; return out; }
cvec to_cvec(double real, double imag)
{
cvec out(1);
out(0) = std::complex<double>(real, imag);
return out;
}
// ----------------------------------------------------------------------
// Miscellaneous converters
// ----------------------------------------------------------------------
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 = int2bits(index);
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(int2bits(index), 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; i < sizebvec; i++) {
temp += pow2i(sizebvec - i - 1) * int(inbvec(i));
}
}
else {
for (i = 0; i < sizebvec; i++) {
temp += pow2i(i) * int(inbvec(i));
}
}
return temp;
}
bvec oct2bin(const ivec &octalindex, short keepzeros)
{
int length = octalindex.length(), i;
bvec out(3*length);
for (i = 0; i < length; i++) {
out.replace_mid(3*i, dec2bin(3, octalindex(i)));
}
//remove zeros if keepzeros = 0
if (keepzeros == 0) {
for (i = 0; i < out.length(); i++) {
if ((short)out(i) != 0) {
return out.right(out.length() - i);
break;
}
}
return bvec("0");
}
else {
return out;
}
}
ivec bin2oct(const bvec &inbits)
{
int start, Itterations = ceil_i(inbits.length() / 3.0);
ivec out(Itterations);
for (int i = Itterations - 1; i > 0; 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);
}
// Round to nearest integer, return result in double
double round(double x) { return ::rint(x); }
// Round to nearest integer
vec round(const vec &x) { return apply_function<double>(::rint, x); }
// Round to nearest integer
mat round(const mat &x) { return apply_function<double>(::rint, x); }
// Round to nearest integer
int round_i(double x) { return static_cast<int>(::rint(x)); }
// Round to nearest integer and return ivec
ivec round_i(const vec &x) { return to_ivec(round(x)); }
// Round to nearest integer and return imat
imat round_i(const mat &x) { return to_imat(round(x)); }
// Round to nearest upper integer
ivec ceil_i(const vec &x) { return to_ivec(ceil(x)); }
// Round to nearest upper integer
imat ceil_i(const mat &x) { return to_imat(ceil(x)); }
// Round to nearest lower integer
ivec floor_i(const vec &x) { return to_ivec(floor(x)); }
// Round to nearest lower integer
imat floor_i(const mat &x) { return to_imat(floor(x)); }
cvec round_to_zero(const cvec &x, double threshold)
{
cvec temp(x.length());
for (int i = 0; i < x.length(); i++)
temp(i) = round_to_zero(x(i), threshold);
return temp;
}
cmat round_to_zero(const cmat &x, double threshold)
{
cmat temp(x.rows(), x.cols());
for (int i = 0; i < x.rows(); i++) {
for (int j = 0; j < x.cols(); j++) {
temp(i, j) = round_to_zero(x(i, j), threshold);
}
}
return temp;
}
cvec round_to_infty(const cvec &in, const double threshold)
{
cvec temp(in.length());
for (int i = 0; i < in.length(); i++)
temp(i) = round_to_infty(in(i), threshold);
return temp;
}
cmat round_to_infty(const cmat &in, const double threshold)
{
cmat temp(in.rows(), in.cols());
for (int i = 0; i < in.rows(); i++) {
for (int j = 0; j < in.cols(); j++) {
temp(i, j) = round_to_infty(in(i, j), threshold);
}
}
return temp;
}
std::string to_str(const double &i, const int precision)
{
std::ostringstream ss;
ss.precision(precision);
ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
ss << i;
return ss.str();
}
// ----------------------------------------------------------------------
// Instantiations
// ----------------------------------------------------------------------
template ITPP_EXPORT bvec to_bvec(const svec &v);
template ITPP_EXPORT bvec to_bvec(const ivec &v);
template ITPP_EXPORT svec to_svec(const bvec &v);
template ITPP_EXPORT svec to_svec(const ivec &v);
template ITPP_EXPORT svec to_svec(const vec &v);
template ITPP_EXPORT ivec to_ivec(const bvec &v);
template ITPP_EXPORT ivec to_ivec(const svec &v);
template ITPP_EXPORT ivec to_ivec(const vec &v);
template ITPP_EXPORT vec to_vec(const bvec &v);
template ITPP_EXPORT vec to_vec(const svec &v);
template ITPP_EXPORT vec to_vec(const ivec &v);
template ITPP_EXPORT cvec to_cvec(const bvec &v);
template ITPP_EXPORT cvec to_cvec(const svec &v);
template ITPP_EXPORT cvec to_cvec(const ivec &v);
template ITPP_EXPORT cvec to_cvec(const vec &v);
template ITPP_EXPORT cvec to_cvec(const bvec &real, const bvec &imag);
template ITPP_EXPORT cvec to_cvec(const svec &real, const svec &imag);
template ITPP_EXPORT cvec to_cvec(const ivec &real, const ivec &imag);
template ITPP_EXPORT cvec to_cvec(const vec &real, const vec &imag);
template ITPP_EXPORT bmat to_bmat(const smat &m);
template ITPP_EXPORT bmat to_bmat(const imat &m);
template ITPP_EXPORT smat to_smat(const bmat &m);
template ITPP_EXPORT smat to_smat(const imat &m);
template ITPP_EXPORT smat to_smat(const mat &m);
template ITPP_EXPORT imat to_imat(const bmat &m);
template ITPP_EXPORT imat to_imat(const smat &m);
template ITPP_EXPORT imat to_imat(const mat &m);
template ITPP_EXPORT mat to_mat(const bmat &m);
template ITPP_EXPORT mat to_mat(const smat &m);
template ITPP_EXPORT mat to_mat(const imat &m);
template ITPP_EXPORT cmat to_cmat(const bmat &m);
template ITPP_EXPORT cmat to_cmat(const smat &m);
template ITPP_EXPORT cmat to_cmat(const imat &m);
template ITPP_EXPORT cmat to_cmat(const mat &m);
template ITPP_EXPORT cmat to_cmat(const bmat &real, const bmat &imag);
template ITPP_EXPORT cmat to_cmat(const smat &real, const smat &imag);
template ITPP_EXPORT cmat to_cmat(const imat &real, const imat &imag);
template ITPP_EXPORT cmat to_cmat(const mat &real, const mat &imag);
} // namespace itpp
itpp::Array::set_size
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Definition array.h:257
itpp::Array::length
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
converters.h
Definitions of converters between different vector and matrix types.
itpp::pow2i
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition log_exp.h:53
itpp::int2bits
int int2bits(int n)
Calculate the number of bits needed to represent an integer n.
Definition log_exp.h:76
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
itpp::reverse
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition matfunc.h:777
itcompat.h
IT++ compatibility types and functions.
log_exp.h
Logarithmic and exponenential functions - header file.
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
matfunc.h
Various functions on vectors and matrices - 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::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::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::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::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::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 Mon Jun 10 2024 11:49:17 for IT++ by Doxygen 1.9.8