43ivec
to_ivec(
int s) { ivec out(1); out(0) = s;
return out; }
45vec
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);
70bvec
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);
90 int i, bintemp = index;
93 for (i = v.size() - 1; i >= 0; i--) {
94 v(i) = bin(bintemp & 1);
95 bintemp = (bintemp >> 1);
99int 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));
116bvec
oct2bin(
const ivec &octalindex,
short keepzeros)
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);
138ivec
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)));
150ivec
bin2pol(
const bvec &inbvec)
157 return to_bvec((1 -inpol) / 2);
162double round(
double x) { return ::rint(x); }
164vec
round(
const vec &x) {
return apply_function<double>(::rint, x); }
166mat
round(
const mat &x) {
return apply_function<double>(::rint, x); }
168int 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++) {
232std::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);
245template ITPP_EXPORT bvec
to_bvec(
const svec &v);
246template ITPP_EXPORT bvec
to_bvec(
const ivec &v);
248template ITPP_EXPORT svec
to_svec(
const bvec &v);
249template ITPP_EXPORT svec
to_svec(
const ivec &v);
250template ITPP_EXPORT svec
to_svec(
const vec &v);
252template ITPP_EXPORT ivec
to_ivec(
const bvec &v);
253template ITPP_EXPORT ivec
to_ivec(
const svec &v);
254template ITPP_EXPORT ivec
to_ivec(
const vec &v);
256template ITPP_EXPORT vec
to_vec(
const bvec &v);
257template ITPP_EXPORT vec
to_vec(
const svec &v);
258template ITPP_EXPORT vec
to_vec(
const ivec &v);
260template ITPP_EXPORT cvec
to_cvec(
const bvec &v);
261template ITPP_EXPORT cvec
to_cvec(
const svec &v);
262template ITPP_EXPORT cvec
to_cvec(
const ivec &v);
263template ITPP_EXPORT cvec
to_cvec(
const vec &v);
274template ITPP_EXPORT smat
to_smat(
const imat &m);
275template ITPP_EXPORT smat
to_smat(
const mat &m);
278template ITPP_EXPORT imat
to_imat(
const smat &m);
279template ITPP_EXPORT imat
to_imat(
const mat &m);
281template ITPP_EXPORT mat
to_mat(
const bmat &m);
282template ITPP_EXPORT mat
to_mat(
const smat &m);
283template ITPP_EXPORT mat
to_mat(
const imat &m);
286template ITPP_EXPORT cmat
to_cmat(
const smat &m);
287template ITPP_EXPORT cmat
to_cmat(
const imat &m);
288template ITPP_EXPORT cmat
to_cmat(
const mat &m);
void set_size(int n, bool copy=false)
Resizing an Array<T>.
int length() const
Returns the number of data elements in the array object.
Definitions of converters between different vector and matrix types.
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
int int2bits(int n)
Calculate the number of bits needed to represent an integer n.
int length(const Vec< T > &v)
Length of vector.
vec imag(const cvec &data)
Imaginary part of complex values.
vec real(const cvec &data)
Real part of complex values.
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
IT++ compatibility types and functions.
Logarithmic and exponenential functions - header file.
Mat< bin > bmat
bin matrix
Various functions on vectors and matrices - header file.
double round_to_infty(const double in, const double threshold=1e9)
Remove trailing digits, found after the decimal point, for numbers greater than threshold.
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.
ITPP_EXPORT ivec bin2oct(const bvec &inbits)
Convert bvec to octal ivec.
svec to_svec(const Vec< T > &v)
Converts a Vec<T> to svec.
ITPP_EXPORT int round_i(double x)
Round to nearest integer.
vec floor(const vec &x)
Round to nearest lower integer.
vec ceil(const vec &x)
Round to nearest upper integer.
ITPP_EXPORT bvec pol2bin(const ivec &inpol)
Convert binary polar ivec to bvec.
ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros=0)
Convert ivec of octal form to bvec.
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
ITPP_EXPORT ivec bin2pol(const bvec &inbvec)
Convert bvec to polar binary representation as ivec.
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.
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
vec to_vec(const Vec< T > &v)
Converts a Vec<T> to vec.
std::string to_str(const T &i)
Convert anything to string.
ITPP_EXPORT bvec dec2bin(int length, int index)
Convert a decimal int index to bvec using length bits in the representation.
mat to_mat(const Mat< T > &m)
Converts a Mat<T> to mat.
ITPP_EXPORT double round(double x)
Round to nearest integer, return result in double.
int ceil_i(double x)
The nearest larger integer.
imat to_imat(const Mat< T > &m)
Converts a Mat<T> to imat.
smat to_smat(const Mat< T > &m)
Converts a Mat<T> to smat.
int floor_i(double x)
The nearest smaller integer.
ivec to_ivec(const Vec< T > &v)
Converts a Vec<T> to ivec.
bvec to_bvec(const Vec< T > &v)
Converts a Vec<T> to bvec.