43 ivec
to_ivec(
int s) { ivec out(1); out(0) = s;
return out; }
45 vec
to_vec(
double s) { vec out(1); out(0) = s;
return out; }
50 out(0) = std::complex<double>(
real,
imag);
60 int i, bintemp = index;
63 for (i =
length - 1; i >= 0; i--) {
64 temp(i) = bin(bintemp & 1);
65 bintemp = (bintemp >> 1);
70 bvec
dec2bin(
int index,
bool msb_first)
73 int i, bintemp = index;
76 for (i =
length - 1; i >= 0; i--) {
77 temp(i) = bin(bintemp & 1);
78 bintemp = (bintemp >> 1);
88 void dec2bin(
int index, bvec &v)
90 int i, bintemp = index;
93 for (i = v.size() - 1; i >= 0; i--) {
94 v(i) = bin(bintemp & 1);
95 bintemp = (bintemp >> 1);
99 int bin2dec(
const bvec &inbvec,
bool msb_first)
102 int sizebvec = inbvec.length();
104 for (i = 0; i < sizebvec; i++) {
105 temp +=
pow2i(sizebvec - i - 1) * int(inbvec(i));
109 for (i = 0; i < sizebvec; i++) {
110 temp +=
pow2i(i) * int(inbvec(i));
116 bvec
oct2bin(
const ivec &octalindex,
short keepzeros)
118 int length = octalindex.length(), i;
120 for (i = 0; i <
length; i++) {
121 out.replace_mid(3*i,
dec2bin(3, octalindex(i)));
124 if (keepzeros == 0) {
125 for (i = 0; i < out.length(); i++) {
126 if ((
short)out(i) != 0) {
127 return out.right(out.length() - i);
138 ivec
bin2oct(
const bvec &inbits)
140 int start, Itterations =
ceil_i(inbits.length() / 3.0);
141 ivec out(Itterations);
142 for (
int i = Itterations - 1; i > 0; i--) {
143 start = 3 * i - (3 * Itterations - inbits.length());
144 out(i) =
bin2dec(inbits.mid(start, 3));
146 out(0) =
bin2dec(inbits.left(inbits.length() - ((Itterations - 1) * 3)));
150 ivec
bin2pol(
const bvec &inbvec)
155 bvec
pol2bin(
const ivec &inpol)
157 return to_bvec((1 -inpol) / 2);
162 double round(
double x) { return ::rint(x); }
164 vec
round(
const vec &x) {
return apply_function<double>(::rint, x); }
166 mat
round(
const mat &x) {
return apply_function<double>(::rint, x); }
168 int round_i(
double x) {
return static_cast<int>(::rint(x)); }
188 cvec temp(x.length());
190 for (
int i = 0; i < x.length(); i++)
198 cmat temp(x.rows(), x.cols());
200 for (
int i = 0; i < x.rows(); i++) {
201 for (
int j = 0; j < x.cols(); j++) {
211 cvec temp(in.length());
213 for (
int i = 0; i < in.length(); i++)
221 cmat temp(in.rows(), in.cols());
223 for (
int i = 0; i < in.rows(); i++) {
224 for (
int j = 0; j < in.cols(); j++) {
232 std::string
to_str(
const double &i,
const int precision)
234 std::ostringstream ss;
235 ss.precision(precision);
236 ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
245 template ITPP_EXPORT bvec
to_bvec(
const svec &v);
246 template ITPP_EXPORT bvec
to_bvec(
const ivec &v);
248 template ITPP_EXPORT svec
to_svec(
const bvec &v);
249 template ITPP_EXPORT svec
to_svec(
const ivec &v);
250 template ITPP_EXPORT svec
to_svec(
const vec &v);
252 template ITPP_EXPORT ivec
to_ivec(
const bvec &v);
253 template ITPP_EXPORT ivec
to_ivec(
const svec &v);
254 template ITPP_EXPORT ivec
to_ivec(
const vec &v);
256 template ITPP_EXPORT vec
to_vec(
const bvec &v);
257 template ITPP_EXPORT vec
to_vec(
const svec &v);
258 template ITPP_EXPORT vec
to_vec(
const ivec &v);
260 template ITPP_EXPORT cvec
to_cvec(
const bvec &v);
261 template ITPP_EXPORT cvec
to_cvec(
const svec &v);
262 template ITPP_EXPORT cvec
to_cvec(
const ivec &v);
263 template ITPP_EXPORT cvec
to_cvec(
const vec &v);
274 template ITPP_EXPORT smat
to_smat(
const imat &m);
275 template ITPP_EXPORT smat
to_smat(
const mat &m);
278 template ITPP_EXPORT imat
to_imat(
const smat &m);
279 template ITPP_EXPORT imat
to_imat(
const mat &m);
281 template ITPP_EXPORT mat
to_mat(
const bmat &m);
282 template ITPP_EXPORT mat
to_mat(
const smat &m);
283 template ITPP_EXPORT mat
to_mat(
const imat &m);
286 template ITPP_EXPORT cmat
to_cmat(
const smat &m);
287 template ITPP_EXPORT cmat
to_cmat(
const imat &m);
288 template ITPP_EXPORT cmat
to_cmat(
const mat &m);
Various functions on vectors and matrices - header file.
ITPP_EXPORT int round_i(double x)
Round to nearest integer.
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
vec imag(const cvec &data)
Imaginary part of complex values.
int ceil_i(double x)
The nearest larger integer.
double round_to_infty(const double in, const double threshold=1e9)
Remove trailing digits, found after the decimal point, for numbers greater than threshold.
vec floor(const vec &x)
Round to nearest lower integer.
imat to_imat(const Mat< T > &m)
Converts a Mat<T> to imat.
std::string to_str(const T &i)
Convert anything to string.
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Logarithmic and exponenential functions - header file.
int length(const Vec< T > &v)
Length of vector.
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
ITPP_EXPORT ivec bin2oct(const bvec &inbits)
Convert bvec to octal ivec.
ITPP_EXPORT double round(double x)
Round to nearest integer, return result in double.
Definitions of converters between different vector and matrix types.
bvec to_bvec(const Vec< T > &v)
Converts a Vec<T> to bvec.
svec to_svec(const Vec< T > &v)
Converts a Vec<T> to svec.
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
smat to_smat(const Mat< T > &m)
Converts a Mat<T> to smat.
int floor_i(double x)
The nearest smaller integer.
ITPP_EXPORT bvec dec2bin(int length, int index)
Convert a decimal int index to bvec using length bits in the representation.
ivec to_ivec(const Vec< T > &v)
Converts a Vec<T> to ivec.
IT++ compatibility types and functions.
ITPP_EXPORT bvec pol2bin(const ivec &inpol)
Convert binary polar ivec to bvec.
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
double round_to_zero(double x, double threshold=1e-14)
Round x to zero if abs(x) is smaller than threshold.
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros=0)
Convert ivec of octal form to bvec.
int int2bits(int n)
Calculate the number of bits needed to represent an integer n.
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.
vec real(const cvec &data)
Real part of complex values.
ITPP_EXPORT ivec bin2pol(const bvec &inbvec)
Convert bvec to polar binary representation as ivec.
vec ceil(const vec &x)
Round to nearest upper integer.
Mat< bin > bmat
bin matrix