Stylished documentation is available here
In current development.
This framework provides functions for statistical analysis, machine learning, parsing and data manipulation with its own implementation of matrices and dataframes. Other tools can be found at fulgurance_tools part.
The framework is developped with C++ 14 but should work properly with 11 and 17 and furthers.
The main branch provides algorithms developped on the top of stl vector, but a deque version is coming.
Dataframes implementation is a class. All functions that will transform 'voidly' (internaly) the relative data are built in the class. All functions that copy and transform the relative data are extern to classes.
Matrices are stl 2D vectors.
template <typename T> double mod(T ÷nd, T ÷r)
Returns the mod of 2 number (int, float or double)
Name | Definition |
---|---|
a | is an the dividend (int, float, double) |
b | is the divider (int, float, double) |
float a = 45.216;
float b = 3.2164;
mod(a, b)
0.186401
int int_lngth(const int &x)
Returns the length of an int.
Name | Definition |
---|---|
x | is an int |
int a = 896;
int_lngth(a);
3
template <typename T> T roundout(T x, int n)
Returns a rounded value with decimal precision.
Name | Definition |
---|---|
x | is an int, float, double |
n | is an int indicating the decimal precision |
float x = 34.476;
int n = 2;
float out = roundout(x, n);
34.48
n = 0;
out = roundout(x, n);
34
n = -1;
out = roundout(x, n)
30
template <typename T> void roundin(T &x, int n)
Transforms the input value to a rounded value with decimal precision.
Name | Definition |
---|---|
x | is an int, float, double |
n | is an int indicating the decimal precision |
float x = 34.476
int n = 2;
roundin(x, n);
34.48
n = 0;
x = 67.754;
roundin(x, n);
68
n = -1;
roundin(x, n);
70
auto randint(const int &min, const int max, int seed = -1)
Returns a pseudo-random number between min and max.
Name | Definition |
---|---|
min | is an int |
max | is a max |
seed | is an int that determines the pseudo-random output, defaults to -1, so the seed will be randomly picked up by default, if you want to determine the output, choose a seed between 0 and 9. |
int min = -300;
int max = 100;
randint(min, max);
-14
randint(min, max);
-231
// If you want to generate a float just do:
double x = randint(min, max);
min = 0;
max = 900;
x += randint(min, max) / 1000;
-13.257
template <typename T, typename T2> double logn(T &val, T2 &base)
Returns the logarithm of any positive number to any base. This generalizes the log(value) / log(base)
method.
Name | Definition |
---|---|
val | is the value of the logarith base n (must be positive) |
base | is the base of the logarithm |
double val = 2.63;
int base = 10;
logn(val, base);
0.419956
base = 2;
1.39506
unsigned int Facto(unsigned int x)
Returns the factorial of a positive integer.
Name | Definition |
---|---|
x | is a unsigned integer |
Facto(7);
5040
Facto(0);
1
double Comb(double r, double n)
Returns the result of the combination formula for given parameters.
Name | Definition |
---|---|
r | is the number of objects choosen among the set |
n | is the number of objects in the set |
Comb(2, 5);
10
Comb(5, 12);
792
int si(const std::string &x)
Returns a std::string that can be converted to an int, to an int.
Name | Definition |
---|---|
x | is a stl string that can be converted to an int |
std::string a = "341";
int out = si(a);
341
float sf(const std::string &x)
Returns a converted std::string that can be converted to a float, to a float. Produces the same results than stof
.
Name | Definition |
---|---|
x | is a stl string that can be converted to a float |
std::string a = "44.23";
float out = sf(a);
44.23
float sf2(const std::string &x)
Returns a converted std::string that can be converted to a float, to a float. Uses another algorithm than edm1_sf
.
Name | Definition |
---|---|
x | is a stl string that can be converted to a float |
std::string a = "44.23";
float out = sf2(a);
44.23
double stod(const std::string &x)
Returns a converted std::string, that can be converted to a double, to a double.
Name | Definition |
---|---|
x | is a stl string |
std::string a = "4566.132214";
double out = stod(a);
4566.132214
std::string itos(unsigned int x)
Returns the input integer as a std string.
Name | Definition |
---|---|
is an unsigned int |
itos(45897);
"45897"
std::map<std::vector<unsigned int>, std::map<bool, std::string>> regex_match(std::string &searched, std::string &x)
Performs a match with the regex flavor.
This library provides an entirely new RegEx flavor.
Read documentation in README_RegEx.md file for details about synthax.
Name | Definition |
---|---|
searched | is the regular expression |
x | is the text to search into |
std::string inpt_str = "Le radiateur fonctionne bien.";
std::string searched = "[A-Z a-z][A-Z{+1}a-z{+1} {+1}]{?bien}[A-Z{0}]{+1}";
std::map<std::vector<int>, std::map<bool, std::string>> out_mp = regex_match(searched, inpt_str);
std::map<std::vector<int>, std::map<bool, std::string>>::iterator out_it = out_mp.begin();
std::vector<int> idx_v = out_it->first;
std::map<bool, std::string>::iterator rslt_mp = out_it->second.begin();
std::string rtn_str = rslt_mp->second;
bool is_found = rslt_mp->first;
if (is_found) {
std::cout << idx_v[0] << "\n";
std::cout << idx_v[1] << "\n";
std::cout << is_found << "\n";
std::cout << rtn_str << "\n";
} else {
std::cout << "not found\n";
};
0
28
1
Le radiateur fonctionne bien.
std::map<std::vector<int>, std::vector<std::string>> regex_grep(std::string &searched, std::string &x)
Performs a grep with the regex flavor.
This library provides an entirely new RegEx flavor.
Read documentation in README_RegEx.md file for details about synthax.
Name | Definition |
---|---|
searched | is the input regular expression |
x | is the text to search into |
std::string inpt_str = "MMMLe radiateur fonctionne bien.";
std::string searched = "[A-Z a-z][A-Z{+1}a-z{+1} {+1}]{?bien}[A-Z{0}]{+1}";
std::map<std::vector<int>, std::vector<std::string>> out_mp2 = regex_search_all(searched, inpt_str);
std::map<std::vector<int>, std::vector<std::string>>::iterator out_it2 = out_mp2.begin();
std::vector<int> idx_v2 = out_it2->first;
std::vector<std::string> str_v2 = out_it2->second;
for (int i = 0; i < str_v2.size(); ++i) {
std::cout << "idx: " << idx_v2[i] << " str: " << str_v2[i] << "\n";
};
idx: 31 str: Le radiateur fonctionne bien.
idx: 31 str: radiateur fonctionne bien.
idx: 31 str: fonctionne bien.
idx: 31 str: e radiateur fonctionne bien.
idx: 31 str: r fonctionne bien.
idx: 31 str: e bien.
std::string regex_subout(std::string &searched, std::string &replacer, std::string x)
Substituates the first pattern matched by the regular expression, to a replacement pattern.
Name | Definition |
---|---|
searched | is the regular expression |
replacer | is the replacement pattern |
x | is the input string to replace searched by replacer into |
std::string inpt_str = "MMMLe radiateur fonctionne bien.... C'est un bon radiateur.";
std::string rplcd = " moteur ";
std::string searched = " a-z{9}[ .]";
std::string out_txt = regex_subout(searched, rplcd, inpt_str);
std::cout << out_txt << "\n";
MMMLe moteur fonctionne bien.... C'est un bon radiateur.
std::string regex_subout_all(std::string &searched, std::string &replacer, std::string x)
Substituates all patterns matched by the regular expression, to a replacement pattern.
Name | Definition |
---|---|
searched | is the regular expression |
replacer | is the replacement pattern |
x | is the input string to replace searched by replacer into |
std::string inpt_str = "MMMLe radiateur fonctionne bien.... C'est un bon radiateur.";
std::string rplcd = " moteur ";
std::string searched = " a-z{9}[ .]";
std::string out_txt = regex_subout_all(searched, rplcd, inpt_str);
std::cout << out_txt << "\n";
MMMLe moteur fonctionne bien.... C'est un bon moteur.
void regex_subin(std::string &searched, std::string &replacer, std::string &x)
Substituates the first pattern matched by the regular expression, to a replacement pattern.
Name | Definition |
---|---|
searched | is the regular expression |
replacer | is the replacement pattern |
x | is the input string to replace searched by replacer into |
std::string inpt_str = "MMMLe radiateur fonctionne bien.... C'est un bon radiateur.";
std::string rplcd = " moteur ";
std::string searched = " a-z{9}[ .]";
regex_subin(searched, rplcd, inpt_str);
std::cout << inpt_str << "\n";
MMMLe moteur fonctionne bien.... C'est un bon radiateur.
void regex_subin_all(std::string &searched, std::string &replacer, std::string &x)
Substituates all patterns matched by the regular expression, to a replacement pattern.
Name | Definition |
---|---|
searched | is the regular expression |
replacer | is the replacement pattern |
x | is the input string to replace searched by replacer into |
std::string inpt_str = "MMMLe radiateur fonctionne bien.... C'est un bon radiateur.";
std::string rplcd = " moteur ";
std::string searched = " a-z{9}[ .]";
regex_subin_all(searched, rplcd, inpt_str);
std::cout << inpt_str << "\n";
MMMLe moteur fonctionne bien.... C'est un bon moteur.
bool can_be_nb(std::string &x)
Returns a boolean, 1 if the input std::string
can be converted to a number (int, float...), 0 if not.
Name | Definition | ||
---|---|---|---|
x | is the input std | string |
std::string inpt_str = "15.69596";
bool rslt = can_be_nb(inpt_str);
std::cout << rslt << "\n";
1
inpt_str = "1569596";
rslt = can_be_nb(inpt_str);
std::cout << rslt << "\n";
1
inpt_str = "1569T596";
rslt = can_be_nb(inpt_str);
std::cout << rslt << "\n";
0
inpt_str = "15.69.596";
rslt = can_be_nb(inpt_str);
std::cout << rslt << "\n";
0
bool can_be_flt_dbl(std::string &x)
Returns a boolean, 1 if the input std::string
can be converted to a float or double, 0 if not.
Name | Definition | ||
---|---|---|---|
x | is the input std | string |
std::string inpt_str = "15.69596";
bool rslt = can_be_flt_dbl(inpt_str);
std::cout << rslt << "\n";
1
inpt_str = "1569596";
rslt = can_be_flt_dbl(inpt_str);
std::cout << rslt << "\n";
0
inpt_str = "1569T596";
rslt = can_be_flt_dbl(inpt_str);
std::cout << rslt << "\n";
0
inpt_str = "15.69.596";
rslt = can_be_flt_dbl(inpt_str);
std::cout << rslt << "\n";
0
template <typename T> T sum(const std::vector<T> &x)
Returns the sum of all elements in a vector (int, float, double, bool).
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<double> vec = {1.434, 22.3322, 32423.097};
double out = sum(vec);
32446.8632
template <typename T> T Mean(const std::vector<T> &x)
Returns the mean of all elements in a vector (int, float, double, bool).
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<int> vec = {1, 4, 2};
double out = Mean(vec);
2.333333
template <typename T, typename T2> double quantile(std::vector<T> &x, T2 &prob, double precision = 0.001)
Returns the quantile value for a given probability between 0 and 1 for an input stl vector (int, float, double, bool). If you just want to calculate median, the med()
function is more straight forward.
Name | Definition |
---|---|
x | stl vector (int, float, double, bool), must be ascendly sorted |
prob | is the probability(float, double) |
precision | is a double value representing the accuracy of the result. The lower the value is, higher the accuracy will be. |
std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> vec2 = {1, 2, 3, 4};
double prob = 0.89;
quantile(vec2, prob);
3.67188
prob = 0.65;
quantile(vec, prob);
6.84375
template <typename T> double med(std::vector<T> &x)
Returns the median of a stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | is an stl vector (int, float, double, bool), must be ascendly sorted |
std::vector<int> vec = {1, 2, 3, 4};
double out = med(vec);
2.5
template <typename T, typename T2> double cor(const std::vector<T> &x, const std::vector<T2> &x2)
Returns the correlation between two variables / two stl vectors (int, float, double, bool)
Name | Definition |
---|---|
x | is an stl vector (int, float, double, bool) |
x2 | is an stl vector (int, float, double, bool) |
std::vector<int> vec1 = {1, 2, 3, 4, 5, 6};
std::vector<int> vec2 = {-6, -5, -4, -3, -2, -1};
double out = cor(vec1, vec2);
1
template <typename T> double Sd(std::vector<T> &x)
Returns the standard deviation of a stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | is an stl vector (int, float, double, bool) |
std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 4, 5};
double out = Sd(vec);
1.224745
template <typename T> std::vector<double> dunif(std::vector<T> &x, double &min, double &max)
Returns the probability distribution of the uniform distribution.
Name | Definition |
---|---|
x | is a vector containing all the values you want the probability from |
min | is the minimum of the uniform distribution |
max | is the maximum of the uniform distribution |
double min = -2;
double max = 10;
std::vector<double> vec = {-7, -2, 3.5, 8, 12, 56};
std::vector<double> out = dunif(vec, min, max);
print_nvec(out);
:0: 0 0.0833333 0.0833333 0.0833333 0 0
template <typename T> std::vector<double> punif(std::vector<T> &x, double &min, double &max, double step = 0.01)
Returns the cumulative probablity distribution of the uniform distribution.
Name | Definition |
---|---|
x | is a vector containing the values you want the cumulative probability from, must be ascendly sorted |
min | is the minimum of the probability distribution |
max | is the maximum of the probability distribution |
step | the lower it is, the more accurate the result gets |
double min = -2;
double max = 10;
std::vector<double> vec = {-7, -2, 3.5, 8, 12, 56};
std::vector<double> out = punif(vec, min, max);
print_nvec(out);
:0: 0 0.000833333 0.459167 0.834167 1 1
std::vector<double> qunif(std::vector<double> &x, double &min, double &max)
Returns the quantile of the uniform distribution.
Name | Definition |
---|---|
x | is the probability vector |
min | is the minimum of the uniform distribution |
max | is the maximum of the uniform distribution |
double min = -2;
double max = 10;
std::vector<double> vec = {-7, -2, 3.5, 8, 12, 56};
std::vector<double> vec2 = {0.2, 0.4, 0.5, 0.6, 0.75};
std::vector<double> out = qunif(vec2, min, max);
print_nvec(out);
:0: 3.6 5.2 6 6.8 8
std::vector<double> unif(unsigned int &n, double &min, double &max, double noise = 0.1, int seed = -1)
Returns a stl double vector containing pseudo-random uniform distribution between a min and max.
Name | Definition |
---|---|
n | is the number of elements of the output stl vector |
min | is the minimum of the uniform distribution |
max | is the maximum of the uniform distribution |
noise | is the noise in the returnde uniform distribution |
seed | is an int, controlling the output values, defaults to -1, so by default the function returns pseudo-random uniform distribution. If you want to manually control the output, enter a positive int for this parameter. |
unsigned int n = 1500;
double min = 1;
double max = 55;
std::vector<double> out = unif(n, min, max);
print_nvec(out);
:0: 1 1.03701 1.07557 1.10951 1.14757 1.18151 1.21957 1.25351 1.29157 1.32551 1.36357 1.39751 1.43557 1.46951 1.50757 1.54151 1.57957 1.61351 1.65157 1.68551 1.72357 1.75751 1.79557 1.82951
...
:1475: 54.1015 54.1396 54.1735 54.2116 54.2455 54.2836 54.3175 54.3556 54.3895 54.4276 54.4615 54.4996 54.5335 54.5716 54.6055 54.6436 54.6775 54.7156 54.7495 54.7876 54.8215 54.8596 54.8935 54.9316
:1500: 55
std::vector<double> rnorm(unsigned int &n, double &mean, double &sd, double noise = 0.05, int seed = -1)
Returns a pseudo-random normal distribution as a double stl vector. Note, if you can it is preferable to choose the smallest standard deviation possible to increase speed. Example: N(14, 10) -> N(1.4, 1).
Name | Definition |
---|---|
n | is the number of elements in the output stl vector |
mean | is the mean of the normal distribution |
sd | is the standard deviation of the normal distribution |
noise | is the noise, defaults to 0.05 |
seed | is an int that dictates the result, defaults to -1, so by default the output is pseudo-random |
unsigned int n = 10000;
double sd = 250;
double mean = 155;
std::vector<double> out;
double result;
out = rnorm(n, mean, sd);
Sd(out);
250.6228
Mean(out);
154.9945
std::vector<double> rnorm2(unsigned int &n, double &mean, double &sd, double noise = 0.05, int seed = -1)
Same as norm()
, but faster and less accurate.
Name | Definition |
---|---|
n | is the number of elements in the output stl vector |
mean | is the mean of the normal distribution |
sd | is the standard deviation of the normal distribution |
noise | is the noise, defaults to 0.05 |
seed | is an int that dictates the result, defaults to -1, so by default the output is pseudo-random |
unsigned int n = 10000;
double sd = 50;
double mean = 155;
std::vector<double> out;
double result;
out = rnorm2(n, mean, sd);
Sd(out);
42.06729
Mean(out);
155.0009
template <typename T, typename T2> double qnorm1(T &mean, T2 &sd, double &val, double offset_prob = 0.05)
Returns the quantile value for a given theoretical normal distribution. There is an offset probability input that tells the most offset probability the function has to takein count in order to return the quantile value.
Name | Definition |
---|---|
mean | is the mean of the normal distribution |
sd | is the standard deviation of the normal distribution |
val | is the quantile percentage (between 0 and 1) |
offset_prob | is the probability from which is no longer not taken in count by the function in order to return a coherent value |
double mean = 12;
double sd = 2;
std::vector<double> vec = {0.33, 0.40, 0.45, 0.5, 0.55};
std::vector<double> out = qnorm1(vec, mean, sd);
print_nvec(out);
:0: 10.8688 11.3346 11.6673 12 12.3327
template <typename T, typename T2> double qnorm1(T &mean, T2 &sd, double &val, double offset_prob = 0.05)
Returns the quantile value for a given theoretical normal distribution. This algorithm may be more precise than qnorm1 but takes slightly longer times to compute.
Name | Definition |
---|---|
mean | is the mean of the normal distribution |
sd | is the standard deviation of the normal distribution |
x | are the quantile percentage (between 0 and 1), must be ascendly sorted |
step | is the accuracy, the lower it is, the more precise it gets |
double mean = 12;
double sd = 2;
std::vector<double> vec = {0.33, 0.40, 0.45, 0.5, 0.55};
std::vector<double> out = qnorm2(vec, mean, sd);
print_nvec(out);
:0: 9.92 10.85 11.48 12.11 12.74
template <typename T> std::vector<double> dnorm(std::vector<T> &x, double &mean, double &sd, double step = 1)
Returns the density function of a given normal distribution as an stl double vector.
Name | Definition |
---|---|
x | is an stl vector of values you want the probability from |
mean | is the mean of the normal distribution |
sd | is the standard deviation of the normal distribution |
step | the step of each element you want the probability from, see examples. Defaults to 1, so it ouputs the same result as the dnorm() function in R by default. |
double mean = 12;
double sd = 2;
std::vector<double> vec = {1, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14};
std::vector<double> vec2 = {9, 10, 11, 12, 13, 14};
std::vector<double> out = dnorm(vec, mean, sd, 0.5);
print_nvec(out);
:0: 0.0323794 0.0456623 0.0604927 0.0752844 0.0880163 0.096667 0.0997356 0.096667 0.0880163 0.0752844 0.0604927
out = dnorm(vec2, mean, sd, 1);
:0: 0.0647588 0.120985 0.176033 0.199471 0.176033 0.120985
template <typename T> std::vector<double> pnorm(std::vector<T> &x, double &mean, double &sd, double step = 0.01)
Returns the cumulative distribution function of a given normal distribution.
Name | Definition |
---|---|
x | is an stl vector containing all the elements you want the function distribution to be calculated with, must be ascendly sorted |
mean | is the mean of the normal distribution |
sd | is the standard deviation of the normal distribution |
step | the lower it is the higher the accuracy is |
double mean = 15;
double sd = 2;
std::vector<double> vec = {13, 13.5, 14, 14.5, 15, 15.5, 16, 18};
std::vector<double> out = pnorm(vec, mean, sd);
print_nvec(out);
:0: 0.00120985 0.0693298 0.151367 0.24421 0.342947 0.441622 0.534291 0.774818
std::vector<double> dbinom(std::vector<unsigned int> &k, unsigned int &n, double &p)
Returns the probability function of a binomial distribution as an stl double vector.
Name | Definition |
---|---|
x | is an stl unsigned int vector containing all the x's |
n | is the size of the set |
p | is the probability of success |
std::vector<unsigned int> vec = {39, 40, 41, 42, 43, 44, 45, 46, 47, 48};
unsigned int n = 100;
double p = 0.45;
std::vector<double> out = dbinom(vec, n, p);
print_nvec(out);
:0: 0.03875 0.0483929 0.0580423 0.066859 0.0739653 0.0785867 0.0801904 0.0785867 0.0739653 0.066859 0.0580423 0.0483929
std::vector<double> pbinom(std::vector<unsigned int> &k, unsigned int &n, double &p)
Returns the cumulative distribution function of range P(X = {x1,x2...}) as an stl double vector.
Name | Definition |
---|---|
k | is an stl unsigned int vector containing all the k's, must be ascendly sorted |
n | is the size of the set |
p | is the probability of success |
unsigned int n = 10;
double p = 0.5;
std::vector<unsigned int> vec = {3, 5, 7};
std::vector<double> out = pbinom(vec, n, p);
print_nvec(out);
:0: 0.117188 0.568359 0.890625
std::vector<unsigned int> qbinom(std::vector<double> &pvec, unsigned int &n, double &p)
Returns the quantiles of a binomial distribution.
Name | Definition |
---|---|
pvec | is an stl vector of probabilities, must be ascendly sorted |
n | is size of the set, as an unsigned int |
p | is the probability of success, as a double |
std::vector<double> vec3 = {0.3, 0.4, 0.5, 0.6, 0.7};
unsigned int n = 100;
double prob = 0.55;
std::vector<unsigned int> out = qbinom(vec3, n, prob);
print_nvec(out);
:0: 47 48 50 51 52 54
std::vector<unsigned int> rbinom(unsigned int &n, unsigned int size, double p)
Returns pseudo-random values of binomial distribution.
Name | Definition |
---|---|
n | is the number of observations |
size | is the size of the individuals |
p | is the probability of success |
unsigned int size = 100;
double p = 0.5;
unsigned int n = 60;
std::vector<unsigned int> out = rbinom(n, size, p);
print_nvec(out);
:25: 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50
:50: 50 50 50 50 50 50 50 50 50 50
Mean(out);
49
Sd(out);
5.90141
std::sqrt(size * p * (1 - p));
5
std::vector<double> dpois(std::vector<int> &k, int &lambda)
Returns the poisson probability distribution.
Name | Definition |
---|---|
k | is the vector containing the k values |
lambda | is the mean |
int lambda = 500;
std::vector<int> vec2 = {492, 500, 520};
std::vector<double> out = dpois(vec2, lambda);
print_nvec(out);
:0: 0.0167352 0.0178412 0.0119593
std::vector<double> ppois(std::vector<int> &k, int &lambda)
Returns the poisson cumulative probability distribution.
Name | Definition |
---|---|
k | is the vector containing the k values |
lambda | is the mean |
int lambda = 500;
std::vector<int> vec2 = {492, 500, 520};
std::vector<double> out = ppois(vec2, lambda);
print_nvec(out);
:0: 0.0167352 0.157008 0.468481
std::vector<unsigned int> qpois(std::vector<double> &p, int &lambda)
Returns the quantile of the poisson distribution
Name | Definition |
---|---|
p | is the vector of probabilities |
lambda | is the mean |
std::vector<double> vec = {0.22, 0.5, 0.7};
int lambda = 500;
std::vector<unsigned int> out = qpois(vec, lambda);
:0: 483 500 512
std::vector<unsigned int> rpois(unsigned int &n, unsigned int lambda)
Name | Definition |
---|---|
n | is the number of observations |
lambda | is the mean |
unsigned int lambda = 100;
unsigned int n = 60;
std::vector<unsigned int> out = rpois(n, lambda);
print_nvec(out);
:0: 114 86 86 86 115 115 85
85 85 116 116 84 84 119 119
83 83 120 120 79 79 133 133 67
:25: 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100
:50: 101 101 101 101 101 101 101 101
101 101
Mean(out);
99
Sd(out);
13.0799
std::sqrt(lambda);
10
std::vector<double> dexp(std::vector<double> &x, double &rate)
Returns the probability distribution of the exponential distribution
Name | Definition |
---|---|
x | is the vector containing the values you want the probability from |
rate | is the rate value for the exponential distribution, given by 1 / mean |
double rate = 0.2;
std::vector<double> vec = {1, 2, 3, 4, 5, 6};
std::vector<double> out = dexp(vec, rate);
print_nvec(out);
:0: 0.163746 0.134064 0.109762 0.0898658 0.0735759 0.0602388
std::vector<double> pexp(std::vector<double> &x, double &rate, double step = 0.01)
Returns the cumulative probability distribution for the exponential distribution.
Name | Definition |
---|---|
x | is the vector of the values you want the cumulative probability distribution from, must be ascendly sorted |
rate | is the rate for the exponential distribution |
step | the lower it is the more accurate the result will be |
:0: 0.00163746 0.148559 0.271287 0.37067 0.452038 0.518657
std::vector<double> qexp(std::vector<double> &p, double &rate)
Returns the quantile of the exponential probability distribution
Name | Definition |
---|---|
p | is the vector of probabilities |
rate | is the rate of the exponential distribution |
double rate = 0.2;
std::vector<double> vec2 = {0.1, 0.2, 0.3, 0.4, 0.5, 0.75};
std::vector<double> out = qexp(vec2, rate);
print_nvec(out);
:0: 0.526803 1.11572 1.78337 2.55413 3.46574 6.93147
std::vector<double> rexp(unsigned int &n, double rate)
Returns a pseudo-random distribution of the exponential distribution
Name | Definition |
---|---|
n | is the number of observations |
rate | is the rate of the exponential distribution |
double rate = 0.2;
unsigned int n = 100;
std::vector<double> out = rexp(n, rate);
print_nvec(out);
:0: 13.2 13.2 2.79385 2.79385 2.79385
13.6804 13.6804 4.10504 4.10504 15.9378
15.9378 5.60406 5.60406 21.5331 21.5331
10.4864 10.4864 5.20657 5.20657 5.20657
5.20657 5.20657 5.20657 5.20657
:25: 5.20657 5.20657 5.20657 5.20657 5.20657
4.80913 4.80913 4.80913 4.80913 4.80913 4.80913
4.80913 4.80913 4.80913 4.80913 4.80913 4.80913
4.80913 4.80913 5.3978 5.3978 5.3978 5.3978 5.3978
:50: 5.3978 5.3978 5.3978 5.3978 5.3978 4.57492
4.57492 4.57492 4.57492 4.57492 4.57492 4.57492
4.57492 4.57492 4.57492 4.57492 4.57492 5.58841
5.63733 5.63733 5.63733 5.63733 5.63733 5.63733
:75: 5.63733 5.63733 4.35393 4.35393 4.35393 4.35393
4.35393 4.35393 4.35393 4.35393 4.35393 4.35393
4.35393 5.82063 5.82063 5.82063 5.82063 5.82063
5.82063 5.82063 5.82063 4.19025 4.19025 4.19025
:100: 4.19025
Mean(out);
5.94307
Sd(out);
4.29426
std::vector<double> dcauchy(std::vector<double> &x, double location = 0, double scale = 1)
Returns the probability distribution of the cauchy distribution.
Name | Definition |
---|---|
x | is the vector of values you want the probability from |
location | is the x coordinate |
scale | is the t coordinate |
double location = 0;
double scale = 1;
std::vector<double> vec = {-2, -1, 0, 1, 2, 4};
std::vector<double> out = dcauchy(vec, location, scale);
print_nvec(out);
:0: 0.063662 0.159155 0.31831 0.159155 0.063662 0.0187241
std::vector<double> pcauchy(std::vector<double> &x, double location = 0, double scale = 1)
Returns the cumulative probability distribution of the cauchy distribution (starts from first value).
Name | Definition |
---|---|
x | is the vector of values you want the cumulative probability from |
location | is the x coordinate |
scale | is the t coordinate |
step | the lowest it is the more accurate the result gets |
double location = 0;
double scale = 1;
std::vector<double> vec = {-2, -1, 0, 1, 2, 4};
std::vector<double> out = pcauchy(vec, location, scale);
print_nvec(out);
:0: 0.000634083 0.10305 0.35305 0.60305 0.705467 0.775071
std::vector<double> qcauchy(std::vector<double> &p, double location = 0, double scale = 1)
Returns the quantile of the cauchy probability distribution
Name | Definition |
---|---|
p | is the vector containing the probabilities |
location | is the x coordiante |
scale | is the y coordinate |
double location = 0;
double scale = 1;
std::vector<double> vec = {0.1, 0.25, 0.4, 0.5, 0.63, 0.78};
std::vector<double> out = qcauchy(vec, location, scale);
print_nvec(out);
:0: -3.07768 -1 -0.32492 0 0.432739 1.20879
std::vector<double> rcauchy(unsigned int n, double x = 0, double y = 1)
Returns a pseudo-random generation of numbers following a cauchy distribution.
Name | Definition |
---|---|
n | is the number of numbers to generate |
x | is the x coordinate |
y | is the y coordinate |
int ref_min = -2;
double location = -4;
double scale = 8;
unsigned int n = 100;
std::vector<double> out = rcauchy(n, location, scale);
std::vector<bool> out2 = supcmp(out, ref_min);
sum(out2);
42
std::vector<double> dgamma(std::vector<double> &x, double &shape, double &rate)
Returns the gamma density probability distribution. Uses a normal law of mean = 1/rate * shape and sd = 1/rate * sqrt(shape) to approximate for shape value greater than 171
Name | Definition |
---|---|
x | is the input vector composed of the x values |
shape | is the alpha value |
rate | is the rate value (lambda or 1/theta) |
std::vector<double> vec = {6444, 6666, 6888};
double shape = 3333;
double rate = 0.5;
std::vector<double> out = dgamma(vec, shape, rate);
print_nvec(out);
:0: 0.000544178 0.00345511 0.000544178
std::vector<double> pgamma(std::vector<double> &x, double &shape, double &rate, double step)
Returns the gamma cmulative probability distribution between an interval (first x value to last x value)
Name | Definition |
---|---|
x | is the input x values, must be ascendly sorted |
shape | is the alpha value |
rate | is the lambda value, 1/theta |
step | the lower it is, the more accurate the result will be at a computational cost |
std::vector<double> vec = {6444, 6555, 6666, 6888};
double shape = 3333;
double rate = 0.5;
double step = 0.1;
std::vector<double> out = pgamma(vec, shape, rate, step);
print_nvec(out);
:0: 5.44178e-05 0.141121 0.473211 0.946151
std::vector<double> qgamma(std::vector<double> &x, double &shape, double &rate, double step)
Returns the quantile value of the gamma probability distribution
Name | Definition |
---|---|
x | is the input vector of probabilities, must be ascendly sorted |
shape | is the alpha value |
rate | is the lambda value (1 / theta) |
step | the lower it is, the more accurate the result will be at a cmputational cost |
std::vector<double> vec = {0.26, 0.45, 0.5, 0.6, 0.88};
double shape = 3333;
double rate = 0.5;
double step = 0.1;
std::vector<double> out = qgamma(vec, shape, rate, step);
print_nvec(out);
:0: 6591.86 6651.56 6666.06 6695.36 6801.76
std::vector<double> rgamma(unsigned int &n, double &shape, double &rate, double step)
Generates pseudo-random variables that follow a gamma probability distribution
Name | Definition |
---|---|
n | is the number of observations, more than 1 |
shape | is the alpha value |
rate | is the lambda (1 / theta) |
step | the lower it is, the more the result will be accurate, at a computational cost |
unsigned int n = 100;
double shape = 3333;
double rate = 0.25;
double step = 0.01;
std::vector out = rgamma(n, shape, rate, step);
print_nvec(out);
:0: 12794.8 12857.7 12897.7 12927.7 12952.2 12973 12991.2
13007.5 13022.4 13036.1 13048.8 13060.7 13071.9 13082.5 13092.7
13102.4 13111.7 13120.6 13129.3 13137.7 13145.8 13153.7 13161.4
13168.9
:25: 13183.4 13190.5 13197.4 13204.2 13210.9 13217.5 13224 13230.4
13236.8 13243 13249.2 13255.4 13261.5 13267.5 13273.5 13279.5
13285.4 13291.3 13297.1 13303 13308.8 13314.6 13320.4 13326.2
:50: 13337.8 13343.6 13349.4 13355.2 13361 13366.9 13372.7 13378.6
13384.6 13390.5 13396.5 13402.6 13408.6 13414.8 13421 13427.3
13433.6 13440 13446.5 13453.1 13459.8 13466.6 13473.5 13480.6
:75: 13495.1 13502.6 13510.3 13518.2 13526.4 13534.7 13543.4
13552.4 13561.7 13571.3 13581.5 13592.1 13603.3 13615.3 13628
13641.6 13656.5 13672.8 13691 13711.9 13736.3 13766.3 13806.3
13869.2
:100: 13276.2
std::vector<double> dbeta(std::vector<double> &x, double &a, double &b, double normalisation_step = 1)
Returns the beta density probability distribution
Name | Definition |
---|---|
x | is the vector of the probabilities |
a | is alpha, number of successes |
b | is beta, the number of failures |
normalisation_step | is the probability unit of the x vector |
double a = 40;
double b = 60;
std::vector<double> vec = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1};
double step = 0.005;
std::vector<double> out = dbeta(vec, a, b, step);
print_nvec(out);
:0: 0 1.24746e-15 1.1697e-06 0.00428754 0.0410157
0.00547615 1.23346e-05 1.87358e-10 1.06384e-18 0
std::vector<double> pbeta(std::vector<double> &x, double &a, double &b, double step = 0.01)
Returns the beta cumulative probability distribution, of an interval of the first input value to the last input value (from the input vector of probabilities)
Name | Definition |
---|---|
x | is the vector of the probabilities, must be ascendly sorted |
a | is alpha, the number of successes |
b | is beta, the number of failures |
step | the lower this value is, the more accurate the result will be at a computational cost |
double a = 40;
double b = 60;
std::vector<double> vec = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1};
double step = 0.005;
std::vector<double> out = pbeta(vec, a, b, step);
print_nvec(out);
:0: 0 2.65054e-16 1.23119e-06 0.0129867 0.468685 0.974149
0.999966 1 1 1
std::vector<double> qbeta(std::vector<double> &x, double &a, double &b, double step = 0.01)
Returns the quantile of given probabilities according to the beta probability distribution
Name | Definition |
---|---|
x | is the vector of probabilities you want the probabilities from |
a | is alpha, the number of successes |
b | is beta, the number of failures |
step | the lower this value is, the more accurate the result will be at a computational cost |
double a = 40;
double b = 60;
std::vector<double> vec = {0.3, 0.55, 0.9, 0.99};
double step = 0.005;
std::vector<double> out = qbeta(vec2, a, b, step);
print_nvec(out);
std::vector<double> rbeta(unsigned int &n, double &a, double &b, double step = 0.01)
Returns pseudo-ranomly value that follow a beta probability distribution
Name | Definition |
---|---|
n | is the number of observations, values to generate |
a | is alpha, the number of successes |
b | is beta, the number of failures |
step | the lower this value is, the more accurate the result will be. Have to be lowered if the output starts having clone values, it can happen when n is very high |
double a = 40;
double b = 60;
std::vector<double> vec = {0.3, 0.55, 0.9, 0.99};
double step = 0.005;
unsigned int n = 100;
std::vector<double> out = rbeta(n, a, b, step);
print_nvec(out);
:0: 0.29716 0.31027 0.31901 0.32338 0.32775 0.33212
0.33649 0.34086 0.34086 0.34523 0.34523 0.3496 0.35397
0.35397 0.35397 0.35834 0.35834 0.36271 0.36271 0.36708
0.36708 0.36708 0.37145 0.37145
:25: 0.37582 0.37582 0.37582 0.38019 0.38019 0.38019
0.38456 0.38456 0.38456 0.38893 0.38893 0.38893 0.38893
0.3933 0.3933 0.3933 0.39767 0.39767 0.39767 0.39767
0.40204 0.40204 0.40204 0.40641
:50: 0.40641 0.40641 0.41078 0.41078 0.41078 0.41515
0.41515 0.41515 0.41515 0.41952 0.41952 0.41952 0.42389
0.42389 0.42389 0.42389 0.42826 0.42826 0.42826 0.43263
0.43263 0.43263 0.437 0.437
:75: 0.44137 0.44137 0.44574 0.44574 0.44574 0.45011 0.45011
0.45448 0.45448 0.45448 0.45885 0.46322 0.46322 0.46759
0.46759 0.47196 0.47633 0.47633 0.4807 0.48507 0.49381 0.49818
0.50692 0.52003
:100: 0.3933
std::vector<double> dchisq(std::vector<double> &x, double °f)
Returns the chi square density probability function
Name | Definition |
---|---|
x | is the input vector of quantiles |
degf | is the degree of freedom |
std::vector<double> vec = {180, 200, 210, 250, 290, 310};
double degf = 240;
std::vector<double> out = dchisq(vec, degf);
print_nvec(out);
:0: 0.000263702 0.00333664 0.00747074 0.0157848
0.00152353 0.000193457
std::vector<double> pchisq(std::vector<double> &x, double °f, double step = 0.05)
Returns the chi square cumulative probability function
Name | Definition |
---|---|
x | is the input vector of quantiles, must be ascendly sorted |
degf | is the degree of freedom |
step | the lower this value is the more accurate the result will be at a computational cost |
std::vector<double> vec = {180, 200, 210, 250, 290, 310};
double degf = 240;
std::vector<double> out = pchisq(vec, degf);
print_nvec(out);
:0: 1.31851e-05 0.0266942 0.0790938 0.682744
0.983524 0.996995
std::vector<double> qchisq(std::vector<double> &x, double °f, double step = 0.05)
Returns the probability of the input quantile values
Name | Definition |
---|---|
x | is the input vector of probabilities, must be ascendly sorted |
degf | is the degree of freedom |
step | th lower this value is the more accurate the result will be at a computational cost |
std::vector<double> vec2 = {0.2, 0.45, 0.56, 0.69, 0.88};
double degf = 240;
std::vector<double> out = qchisq(vec2, degf);
print_nvec(out);
:0: 221.45 236.65 242.7 250.4 266
std::vector<double> rchisq(unsigned int &n, double °f, double step = 0.05)
Returns pseudo-random values that follow a chi square probability distribution
Name | Definition |
---|---|
n | is the number of observations |
degf | is the degree of freedom |
step | the lower it is the more accurate the result is. Have to be lowered if the output begins to have clone values. It can happen if n is very high |
unsigned int n = 100;
double degf = 240;
std::vector out = rchisq(n, degf);
print_nvec(out);
:0: 192.049 197.248 200.571 203.09 205.181 206.95
208.558 209.951 211.238 212.417 213.542 214.614 215.579
216.544 217.402 218.313 219.117 219.921 220.671 221.422
222.172 222.869 223.566 224.262
:25: 225.602 226.246 226.889 227.478 228.122 228.711
229.301 229.89 230.48 231.07 231.606 232.195 232.785
233.321 233.91 234.446 234.982 235.572 236.108 236.644
237.18 237.77 238.306 238.842
:50: 239.914 240.503 241.039 241.575 242.165 242.701
243.29 243.826 244.416 244.952 245.542 246.131 246.721
247.31 247.9 248.543 249.133 249.776 250.419 251.062 251.706
252.349 253.046 253.742
:75: 255.19 255.94 256.69 257.441 258.245 259.102 259.96 260.871
261.782 262.801 263.819 264.891 266.017 267.25 268.536 269.93
271.43 273.146 275.022 277.166 279.738 282.901 287.189 293.942
:100: 223.914
bool test_chisq_fit(std::vector<double> theoretical, std::vector<double> observed, double a_value = 0.05, double step = 0.05)
Performs a chi square goodness of fit test. Returns 1 if the observed values fit the observed values at a given p_value, 0 else
Name | Definition |
---|---|
theoretical | is the vector containing all the theoretical data |
observed | is the vector containing all the observed data |
a_value | is the significance level |
step | the lower this value is the more accurate the result wil be at a computational cost |
std::vector theoretical = {20, 20, 30, 40, 60, 30};
std::vector observed = {30, 14, 34, 45, 57, 20};
double a_value = 0.05;
bool out = test_chisq_fit(theoretical, observed, a_value);
0 // the observed data does not fit the theoretical data
bool test_chisq_independance(std::vector<std::vector<double>> &matr, double a_value = 0.05, double step = 0.05)
Performs a chi square independance test. Returns 0 if the variables are independant, 1 else
Name | Definition |
---|---|
matr is the input matrice (observed values) | |
a_value | is the significance level (the greater it is the more likely the 2 variables will be percieved as independant) |
step | the lower this value is the more accurate the result will be at a computational cost |
std::vector<std::vector<double>> matr = {{8, 16, 11, 10},
{9, 27, 22, 16},
{7, 13, 8, 12},
{9, 13, 12, 7}};
print_matr(matr);
double step = 0.05;
double a_value = 0.05;
bool out = test_chisq_independance(matr, a_value, step);
0 // the variables are independant
std::vector<double> dgeom(std::vector<unsigned int> &x, double &p)
Returns the geometric density probability distribution
Name | Definition |
---|---|
x | is the input vector of quantiles, representing the number of failures before success |
p | is the probability of success |
std::vector<unsigned int> vec = {2, 3, 4, 5};
double p = (double)1 / 6;
std::vector<double> out = dgeom(vec, p);
print_nvec(out);
:0: 0.115741 0.0964506 0.0803755 0.0669796
std::vector<double> pgeom(std::vector<unsigned int> &x, double &p)
Returns the geometric cumulative probability distribution (interval between firts value in vector and last, see example)
Name | Definition |
---|---|
x | is the input vector of quantiles, representing the number of failures before success, must be ascendly sorted |
p | is the probability of success |
std::vector<unsigned int> vec = {2, 3, 4, 5};
double p = (double)1 / 6;
std::vector<double> out = pgeom(vec, p);
print_nvec(out);
:0: 0.115741 0.212191 0.292567 0.359546
std::vector<unsigned int> qgeom(std::vector<double> &x, double &p)
Returns the quantiles of the input probabilities according to a geometric probability distribution
Name | Definition |
---|---|
x | is the input vector of probabilities, must be ascendly sorted |
p | is the probability of success |
std::vector<double> vec2 = {0.2, 0.3, 0.5, 0.52, 0.6, 0.85};
double p = (double)1 / 6;
std::vector<unsigned int> out = qgeom(vec2, p);
print_nvec(out);
:0: 1 1 3 4 5 10
std::vector<unsigned int> rgeom(unsigned int &n, double &p)
Returns pseudo-randomly generated values that follow a geometric distribution
Name | Definition |
---|---|
n | is the number of observations |
p | is the probability of success |
double p = (double)2 / 9;
unsigned int n = 100;
std::vector<unsigned int> out = rgeom(n, p);
print_nvec(out);
:0: 8 13 3 0 4 2 0 4 2 11 4 1 12 3 9
5 2 10 15 7 0 5 7 2
:25: 8 4 3 9 17 2 10 5 2 1 4 8 12 6 1
5 6 9 4 2 0 5 2 1
:50: 8 2 6 1 13 6 0 4 7 1 4 7 1 5 1
4 3 9 4 2 10 5 2 10
:75: 1 12 3 8 17 2 9 4 2 11 6 8 3 6 0
5 7 10 5 2 2 3 1 13
:100: 6
std::vector<double> dhyper(std::vector<int> &x, unsigned int &n_ones, unsigned int &n_others, int &n_trials)
Returns the hypergeometric probability distribution
Name | Definition |
---|---|
x | is the vector of quantiles |
n_ones | is the number of desired elements in the set |
n_others | is the number of undesired elements in the set |
n_trials | is the number of drawns |
unsigned int n_others = 1300;
unsigned int n_ones = 415;
std::vector<int> x = {150, 190, 400};
int n_trials = 555;
std::vector<double> out = dhyper(x, n_ones, n_others, n_trials);
print_nvec(out);
:0: 0.00807227 1.69452e-11 4.42674e-236
std::vector<double> phyper(std::vector<int> &x, unsigned int &n_ones, unsigned int &n_others, int &n_trials)
Returns the cumulative hypergeometric distribution (interval between the first value of the input quantiles and last value)
Name | Definition |
---|---|
x | is the vector of qualtiles |
n_ones | is the number of desired elements in the set |
n_others | is the number of undesired elements in the set |
n_trials | is the number of drawns |
unsigned int n_others = 1300;
unsigned int n_ones = 415;
std::vector<int> x = {0, 150};
int n_trials = 555;
std::vector<double> out_v = phyper(x, n_ones, n_others, n_trials);
print_nvec(out_v);
:0: 2.59543e-84 0.973988
std::vector<unsigned int> qhyper(std::vector<double> &x, unsigned int &n_ones, unsigned int &n_others, int &n_trials)
Returns the quantiles of the input probabilities according to the hypergeometric probability distribution.
Name | Definition |
---|---|
x | is the vector of the input probabilities, must be ascendly sorted |
n_ones | is the number of desired elements in the set |
n_others | is the number of undesired elements in the set |
n_trials | is the number of drawns |
unsigned int n_others = 1300;
unsigned int n_ones = 415;
std::vector<double> x = {0.05, 0.12, 0.35, 0.36, 0.36, 0.56, 0.78};
int n_trials = 555;
std::vector<unsigned int> out_v = qhyper(x, n_ones, n_others, n_trials);
print_nvec(out_v);
:0: 121 124 131 131 131 135 139
std::vector<unsigned int> rhyper(unsigned int &n_obs, unsigned int &n_ones, unsigned int n_others, int &n_trials)
Returns pseudo-randomly generated values that follow a hypergeometric distribution
Name | Definition |
---|---|
n_obs | is the number of observations |
n_ones | is the number of desired elements in the set |
n_others | is the number of undesired elements in the set |
n_trials | is the number of drawns |
unsigned int n_others = 1300;
unsigned int n_ones = 415;
int n_trials = 555;
unsigned int n_obs = 12;
std::vector<unsigned int> out_v = rhyper(n_obs, n_ones, n_others, n_trials);
print_nvec(out_v);
:0: 129 133 136 139 143 149 124 129 133 136 139 143
template <typename T, typename T2> std::vector<T> add_vout(std::vector<T> x, T2 &to_add)
Adds a value to all elements of the input vector, returns the vector.
Name | Definition |
---|---|
x | is the input vector |
to_add | is the value to add |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
std::vector<double> outv = add_vout(inv, val);
print_nvec(outv);
:0: 6.3 9.3 5.3 8.3
template <typename T, typename T2> std::vector<T> subs_vout(std::vector<T> x, T2 &to_subs)
Substracts a value to all elements of the input vector, returns the vector.
Name | Definition |
---|---|
x | is the input vector |
to_substract | is the value to substract |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
std::vector<double> outv = subs_vout(inv, val);
print_nvec(outv);
:0: -2.3 0.7 -3.3 -0.3
template <typename T, typename T2> std::vector<T> mult_vout(std::vector<T> x, T2 &to_mult)
Multiplicates a value to all elements of the input vector, returns the vector.
Name | Definition |
---|---|
x | is the input vector |
to_multiplicate | is the value to multiplicate |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
std::vector<double> outv = mult_vout(inv, val);
print_nvec(outv);
:0: 8.6 21.5 4.3 17.2
template <typename T, typename T2> std::vector<T> divide_vout(std::vector<T> x, T2 &to_divide)
Divides a value to all elements of the input vector, returns the vector.
Name | Definition |
---|---|
x | is the input vector |
to_divide | is the value to divide |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
std::vector<double> outv = divide_vout(inv, val);
print_nvec(outv);
:0: 0.465116 1.16279 0.232558 0.930233
template <typename T, typename T2> void add_vin(std::vector<T> &x, T2 &to_add)
Adds a value to all elements of the input vector.
Name | Definition |
---|---|
x | is the input vector |
to_add | is the value to add |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
add_vin(inv, val);
print_nvec(inv);
:0: 6.3 9.3 5.3 8.3
template <typename T, typename T2> void subs_vin(std::vector<T> &x, T2 &to_subs)
Substracts a value to all elements of the input vector.
Name | Definition |
---|---|
x | is the input vector |
to_subs | is the value to substract |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
subs_vin(inv, val);
print_nvec(inv);
:0: -2.3 0.7 -3.3 -0.3
template <typename T, typename T2> void mult_vin(std::vector<T> &x, T2 &to_mult)
Multiplicates a value to all elements of the input vector.
Name | Definition |
---|---|
x | is the input vector |
to_mult | is the value to multiplicate |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
mult_vin(inv, val);
print_nvec(inv);
:0: 8.6 21.5 4.3 17.2
template <typename T, typename T2> void divide_vin(std::vector<T> &x, T2 &to_divide)
Divides a value to all elements of the input vector.
Name | Definition |
---|---|
x | is the input vector |
to_divide | is the value to divide |
std::vector<double> inv = {2, 5, 1, 4};
double val = 4.3;
divide_vin(inv, val);
print_nvec(inv);
:0: 0.465116 1.16279 0.232558 0.930233
template <typename T, typename T2> std::vector<T>
add_v2out(std::vector<T> x,
std::vector<T2> &x2)
Add corresponding elements of 2 vectors, returns vector.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
std::vector<double> outv = add_v2out(inv, inv2);
print_nvec(outv);
:0: 4 10 3 9
template <typename T, typename T2> std::vector<T>
subs_v2out(std::vector<T> x,
std::vector<T2> &x2)
Substracts corresponding elements of 2 vectors, returns vector.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
std::vector<double> outv = subs_v2out(inv, inv2);
print_nvec(outv);
:0: 0 0 -1 -1
template <typename T, typename T2> std::vector<T>
mult_v2out(std::vector<T> x,
std::vector<T2> &x2)
Multiplies corresponding elements of 2 vectors, returns vector.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
std::vector<double> outv = mult_v2out(inv, inv2);
print_nvec(outv);
:0: 4 25 2 20
template <typename T, typename T2> std::vector<T>
divide_v2out(std::vector<T> x,
std::vector<T2> &x2)
Divides corresponding elements of 2 vectors, returns vector.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
std::vector<double> outv = divide_v2out(inv, inv2);
print_nvec(outv);
:0: 1 1 0.5 0.8
template <typename T, typename T2> void add_v2in(std::vector<T> &x,
std::vector<T2> &x2)
Adds corresponding elements of 2 vectors.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
add_v2in(inv, inv2);
print_nvec(inv);
:0: 4 10 3 9
template <typename T, typename T2> void subs_v2in(std::vector<T> &x,
std::vector<T2> &x2)
Substract corresponding elements of 2 vectors.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
subs_v2in(inv, inv2);
print_nvec(inv);
:0: 0 0 -1 -1
template <typename T, typename T2> void mult_v2in(std::vector<T> &x,
std::vector<T2> &x2)
Multiplies corresponding elements of 2 vectors.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
mult_v2in(inv, inv2);
print_nvec(inv);
:0: 4 25 2 20
template <typename T, typename T2> void divide_v2in(std::vector<T> &x,
std::vector<T2> &x2)
Adds corresponding elements of 2 vectors.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> inv = {2, 5, 1, 4};
std::vector<double> inv2 = {2, 5};
double val = 4.3;
divide_v2in(inv, inv2);
print_nvec(inv);
:0: 1 1 0.5 0.8
template <typename T> T min(const std::vector<T> &x)
Returns the min element from a vector (int, float, double, bool)
For finding the index of the min element refer here
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<int> vec = {4, 1, -7};
int out = min(vec);
-7
template <typename T> T max(const std::vector<T> &x)
Returns the max element from a vector (int, float, double, bool)
For finding the index of the min element refer here
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<int> vec = {4, 1, -7};
int out = max(vec);
4
template <typename T> void mixout(std::vector<T> &x)
Returns a stl vector with its elements at different indexes pseudo-randomly chosen.
Name | Definition |
---|---|
x | is an stl vector of any type |
std::vector<int> vec;
for (int i = 0; i < 100; ++i) {
vec.push_back(i);
};
std::vector<int> out = mixout(vec);
print_nvec(out);
:0: 66 10 51 47 46 57 13 6 85 40 28 55 42 91 61 34 63 12 23 19 79 62 35 84
:25: 43 5 54 17 93 90 0 73 9 18 49 71 20 89 70 41 4 56 22 45 2 29 88 31
:50: 65 3 75 25 94 77 52 78 39 87 83 27 1 15 24 44 76 99 58 95 92 68 97 26
:75: 64 74 11 80 81 69 59 38 8 7 50 14 98 36 82 60 72 32 96 33 37 30 53 67
:100: 48
template <typename T> void mixin(std::vector<T> &x)
Transforms a stl vector with its elements at different indexes pseudo-randomly chosen.
Name | Definition |
---|---|
x | is an stl vector of any type |
std::vector<int> vec;
for (int i = 0; i < 100; ++i) {
vec.push_back(i);
};
mixin(vec);
print_nvec(vec);
:0: 66 10 51 47 46 57 13 6 85 40 28 55 42 91 61 34 63 12 23 19 79 62 35 84
:25: 43 5 54 17 93 90 0 73 9 18 49 71 20 89 70 41 4 56 22 45 2 29 88 31
:50: 65 3 75 25 94 77 52 78 39 87 83 27 1 15 24 44 76 99 58 95 92 68 97 26
:75: 64 74 11 80 81 69 59 38 8 7 50 14 98 36 82 60 72 32 96 33 37 30 53 67
:100: 48
template <typename T> std::vector<T> mixoutd(std::vector<T> x)
Returns a stl vector with its elements at different indexes. The function is determinitic based on the size of the input stl vector.
Name | Definition |
---|---|
x | is an stl vector of any type |
std::vector<int> vec;
for (int i = 0; i < 100; ++i) {
vec.push_back(i);
};
std::vector<int> out = mixoutd(vec);
print_nvec(out);
:0: 93 65 90 45 1 41 51 79 28 13 18 84 68 37 77 22 15 29 44 9 6 47 36 57
:25: 14 24 26 46 56 96 33 61 54 59 5 73 34 76 75 71 11 78 92 31 48 50 55 49
:50: 52 17 89 21 0 81 64 82 39 67 53 40 63 66 83 97 99 42 2 86 85 80 60 72
:75: 20 87 27 4 35 19 10 88 43 98 38 8 30 69 58 23 16 95 32 94 91 70 74 62
:100: 25
template <typename T> std::vector<T> mixind(std::vector<T> &x)
Transforms a stl vector with its elements at different indexes. The function is determinitic based on the size of the input stl vector.
Name | Definition |
---|---|
x | is an stl vector of any type |
std::vector<int> vec;
for (int i = 0; i < 100; ++i) {
vec.push_back(i);
};
mixind(vec);
print_nvec(vec);
:0: 93 65 90 45 1 41 51 79 28 13 18 84 68 37 77 22 15 29 44 9 6 47 36 57
:25: 14 24 26 46 56 96 33 61 54 59 5 73 34 76 75 71 11 78 92 31 48 50 55 49
:50: 52 17 89 21 0 81 64 82 39 67 53 40 63 66 83 97 99 42 2 86 85 80 60 72
:75: 20 87 27 4 35 19 10 88 43 98 38 8 30 69 58 23 16 95 32 94 91 70 74 62
:100: 25
template <typename T> void print_nvec(const std::vector<T> &x, int untl = -1)
Print a stl vector (int, float, double, bool)
Name | Definition |
---|---|
x | stl vector (int, float, double, bool) |
untl | is an int idicating how many elements must be printed, defaults to -1, so by default all elements will be printed |
std::vector<int> vec = {12, 2, 4534, 7, -78, 12122};
for (int i = 0; i < 50; ++i) { vec.push_back(i); }
print_nvec(vec);
:0: 12 2 4534 7 -78 12122 0 1 2 3
:10: 4 5 6 7 8 9 10 11 12 13
:20: 14 15 16 17 18 19 20 21 22 23
:30: 24 25 26 27 28 29 30 31 32 33
:40: 34 35 36 37 38 39 40 41 42 43
:50: 44 45 46 47 48 49
template <typename T> void print_nvec(const std::vector<T> &x, int untl = -1)
Print a stl vector (int, float, double, bool)
Name | Definition |
---|---|
x | stl vector (int, float, double, bool) |
untl | is an int idicating how many elements must be printed, defaults to -1, so by default all elements will be printed |
std::vector<std::string> vec2 = {"peugeot", "wolkswagen", "honda", "renault", "stellantis"};
for (int i = 0; i < 20; ++i) { vec2.push_back("yesss"); }
print_svec(vec2);
:0: peugeot wolkswagen honda renault stellantis yesss yesss yesss
:8: yesss yesss yesss yesss yesss yesss yesss yesss
:16: yesss yesss yesss yesss yesss yesss yesss yesss
:24: yesss
template <typename T> std::vector<T> getpart(std::vector<T> &x, std::string prt)
Retursn part of stl vector with R like synthax.
Name | Definition | ||
---|---|---|---|
x | is an stl vector | ||
prt | is a std | string that describe the parts of the stl vector to get (R like synthax) |
std::vector<int> vec;
std::vector<int> vec2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vec = getpart(vec2, "1,3,5:9,0:9");
print_nvec(vec);
:0: 2 4 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
template <typename T> std::vector<T> partout(std::vector<T> &inpt, std::vector<T> &x, std::string prt)
Returns the input stl vector with elements added from another stl vector of the same type. The elements added are described with a std::string following the R synthax.
Name | Definition | ||
---|---|---|---|
inpt | is the stl vector to which elements from the second stl vector will be added | ||
x | is the stle vecotr from which elements will be added | ||
prt | is a std | string that describes the elements from the second stl vector to be aded to the first stl vector |
std::vector<int> out;
std::vector<int> vec = {52, 88};
std::vector<int> vec2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
out = partout(vec, vec2, "1,3,5:9,0:9");
print_nvec(out);
0: 52 88 2 4 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
template <typename T> std::vector<T> partin(std::vector<T> &inpt, std::vector<T> &x, std::string prt)
Transforms the input stl vector adding elements from another stl vector of the same type. The elements added are described with a std::string following the R synthax.
Name | Definition | ||
---|---|---|---|
inpt | is the stl vector to which elements from the second stl vector will be added | ||
x | is the stle vecotr from which elements will be added | ||
prt | is a std | string that describes the elements from the second stl vector to be aded to the first stl vector |
std::vector<int> vec = {52, 88};
std::vector<int> vec2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
partin(vec, vec2, "1,3,5:9,0:9");
print_nvec(vec);
0: 52 88 2 4 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
template <typename T> void abs_vin(std::vector<T> &x)
Converts the elements of a vector to absolute values.
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<int> vec = {1, -5, -7, 3};
abs_vin(vec);
{1, 5, 7, 3}
template <typename T> void abs_vout(std::vector<T> &x)
Returns the input vector with all of its elements converted to absolute values.
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<int> vec = {1, -5, -7, 3};
std::vector<unsigned int> out = abs_vout(vec);
{1, 5, 7, 3}
template <typename T> std::vector<T> abs_voutb(const std::vector<T> &x)
Returns the input vector with all of its elements converted to absolute values.
uses another algorithm than abs_vout, with indices instead of iterators.
Name | Definition |
---|---|
x | is a stl vector (int, float, double) |
std::vector<int> vec = {-45, 23, 21, -6, 45};
std::vector<unsigned int> out = abs_voutb(vec);
{45, 23, 21, 6, 45}
template <typename T, typename T2> bool matchl(const std::vector<T> &source, const T2 &ptrn)
Returns 1 if the pattern is found in the stl vector, 0 if not. (returns bool)
Name | Definition |
---|---|
source | is an stl vector |
ptrn | is the pattern |
std::vector<std::string> vec = {"yess", "no", "maybe", "euuh", "maybe"};
std::string ptrn = "maybe";
bool out = matchl(vec, ptrn);
1
template <typename T, typename T2> unsigned int match(const std::vector<T> &source, const T2 &ptrn)
Returns the index of the pattern in the vector, -1 if not found.
Name | Definition |
---|---|
source | is an stl vector |
ptrn | is the pattern |
std::vector<std::string> vec = {"yess", "no", "maybe", "euuh", "maybe"};
std::string ptrn = "maybe";
unsigned int out = matchl(vec, ptrn);
2
template <typename T> unsigned int match_max(const std::vector<T> &x)
Returns the index of the maximum value in a stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | is an stl vector (int, float, double) |
std::vector<int> vec = {3, 1, -7, 23, 21};
unsigned int out = match_max(vec);
3
template <typename T> unsigned int match_min(const std::vector<T> &x)
Returns the index of the minimum value in a stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | is an stl vector (int, float, double) |
std::vector<int> vec = {3, 1, -7, 23, 21};
unsigned int out = match_min(vec);
2
template <typename T, typename T2> std::vector<unsigned int> grep(const std::vector<T> &source, const T2 &ptrn)
Returns the indices of a pattern inside a stl vector.
Name | Definition |
---|---|
source | is an stl vector |
ptrn | is the pattern |
std::vector<std::string> vec = {"yess", "no", "maybe", "euuh", "maybe"};
std::string ptrn = "maybe";
std::vector<unsigned int> out = grep(vec, ptrn);
{3 4}
template <typename T, typename T2> std::vector<bool> grepl(const std::vector<T> &source, const T2 &ptrn)
Returns a boolean vector where O corresponds to a non match with the pattern andthe corresponding elements of the stl vector. 1 represents a match.
Name | Definition |
---|---|
source | is an stl vector |
ptrn | is the pattern |
std::vector<std::string> vec = {"yess", "no", "maybe", "euuh", "maybe"};
std::string ptrn = "maybe";
std::vector<bool> out = grep(vec, ptrn);
{0 0 1 0 1}
template <typename T> std::vector<T> unique(const std::vector<T> &x)
Returns a stl vector with unique elements from an input stl vector.
Name | Definition |
---|---|
x | is an stl vector |
std::vector<int> vec = {1, 2, 3, 4, 4, 5, 6, 6};
std::vector<int> out = unique(vec);
{1, 2, 3, 4, 5, 6}
template <typename T> std::vector<T> reverse_out(const std::vector<T> &x)
Returns a reverse stl vector from an input stl vector.
Name | Definition |
---|---|
x | is an stl vector |
std::vector<std::string> vec = {"he", "ll", "o"};
std::vector<std::string> out = reverse_out(vec);
{"ll", "o", "he"}
template <typename T> std::vector<T> reverse_in(const std::vector<T> &x)
Reverses a stl vector..
Name | Definition |
---|---|
x | is an stl vector |
std::vector<std::string> vec = {"he", "ll", "o"};
reverse_out(vec);
{"ll", "o", "he"}
template <typename T> std::vector<T> reverse_out(const std::vector<T> &x)
Returns a reverse stl vector from an input stl vector. Uses another algorithm than reverse_out.
Name | Definition |
---|---|
x | is an stl vector |
std::vector<std::string> vec = {"he", "ll", "o"};
std::vector<std::string> out = reverse_out(vec);
{"ll", "o", "he"}
template <typename T> std::vector<T> rep(const std::vector<T> &x, const std::vector<int> &hmn)
Returns a stl vector of elements repeted multiple times relatively to an int stl vector.
Name | Definition |
---|---|
x | is a stl vector containing all the elements that will be repeated |
hmn | is a stl int vector containing all the times each element will be repeated |
std::vector<std::string> vec;
std::vector<int> hmn = {4, 2, 7};
std::vector<std::string> elmnts = {"ok", "ko", "ok2"};
vec = rep(elmnts, hmn);
print_svec(vec);
:0: ok ok ok ok ko ko ok2 ok2 ok2 ok2 ok2 ok2 ok2
template <typename T, typename T2, typename T3> std::vector<T> seq(T from, T2 const &to, T3 const &by)
Returns a stl range vector(int, float, double).
Name | Definition |
---|---|
from | is the starting value |
to | is the end value |
by | is the step value |
float from = 1.25;
float to = 3;
float by = 0.25;
std::vector<float> = seq(from, to, by);
{1.25, 1.5, 1.75, 2, 1.25, 2.5, 2.75, 3}
template <typename T, typename T2> std::vector<bool> comp2(const std::vector<T> &x, const std::vector<T2> &x2)
Returns a boolean vector of 2 stl vectors that will be compared elements by elements. The vectors should not necessarily be the same size. The output boolean vector will be the same size as the first stl vector argument. This is the prefered way when there are only 2 vectors to compare, compared to using Compv class.
Name | Definition |
---|---|
x | is an stl vector |
x2 is an stl vector |
std::vector<unsigned int> vec = {1, 5, 2};
std::vector<unsigned int> vecb = {1, 5, 22};
std::vector<bool> out = comp2(vec, vecb);
1 1 0
vec = {1, 5, 2, 1, 5, 2};
out = comp2(vec, vecb);
1 1 0 1 1 0
Compv comp1(std::vector<Type> vec1);
comp1.to_comp(std::vector<Type> vec2, std::vector<Type> vec3);
comp1.result();
comp1.reinitiate(std::vector<OtherType> vec4);
...
Returns a boolean vector of multiple stl vectors that will be compared elements by elements. The vectors do not have necessarily to be the same size. The output boolean vector will be the size of the initiation vector.
Name | Definition |
---|---|
... | undefinite number of stl vectors |
std::vector<unsigned int> vec = {1, 5, 2};
std::vector<unsigned int> vecb = {1, 5, 2};
std::vector<unsigned int> vecc = {1, 5, 22};
std::vector<unsigned int> vec2 = {1, 5};
std::vector<unsigned int> vec2b = {1, 5};
std::vector<unsigned int> vec2c = {1, 5};
Compv obj1(vec);
obj1.to_comp(vecb, vecc);
std::vector<bool> out = obj1.result();
1 1 0
obj1.reinitiate(vec2);
out = obj1.result();
1 1
vec = {1, 5, 22, 1, 5, 2};
obj1.reinitiate(vec);
obj1.to_comp(vec, vecb, vecc);
out = obj1.result();
1 1 0 1 1 0
std::vector<unsigned int> bool_to_idx(std::vector<bool> &x)
Converts a boolean vector to an indices vector.
Name | Definition |
---|---|
x | is the input boolean vector |
std::vector<bool> xbool = {0, 0, 1, 1, 0, 1, 0};
std::vector xidx = bool_to_idx(xbool);
print_nvec(xidx);
2 3 5
std::vector<bool> idx_to_bool(std::vector<unsigned int> &x, int target_size = -1)
Converts a indice vector to a boolean vector
Name | Definition |
---|---|
x | is the input indices vector |
target_size | is the size of the wanted output boolean vector |
std::vector<unsigned int> xidx = {2, 3, 5};
std::vector<bool> xbool = idx_to_bool(xidx, 7);
print_nvec(xbool);
:0: 0 0 1 1 0 1 0
template <typename T, typename T2> std::vector<bool> lowercomp2(std::vector<T> &x, std::vector<T2> &x2)
Returns a boolean vector, 1 if the corresponding values from the first vector is lower than the corresponding values from the second vecotr. The output vector is the size of the first vector.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> vec = {1, 2.5, -6, 7.8, 2};
std::vector<double> vec = {2, 2};
lowercomp2(vec, vec2);
1 0 1 0 0
template <typename T, typename T2> std::vector<bool> greatercomp2(std::vector<T> x, T2 x2)
Returns a boolean vector, 1 if the corresponding values from the first vector is greater than the corresponding values from the second vecotr. The output vector is the size of the first vector.
Name | Definition |
---|---|
x | is the first vector |
x2 | is the second vector |
std::vector<double> vec = {1, 2.5, -6, 7.8, 2};
std::vector<double> vec = {2, 2, 2, 2, 2};
greatercomp2(vec, vec2);
0 1 0 1 0
bool any(const std::vector<bool> &x)
Returns 1 if a boolean vector has at least 1 as value of one of its elements, 0 if not.
Name | Definition |
---|---|
x | is a stl boolean vector |
std::vector<bool> vec = {0, 0, 1, 0};
bool out = any(vec);
1
std::vector<bool> vec = {0, 0, 0, 0};
out = any(vec);
0
all any(const std::vector<bool> &x)
Returns 1 if all the elements of a stl boolean vector equals to 1, 0 if not.
Name | Definition |
---|---|
x | is a stl boolean vector |
std::vector<bool> vec = {0, 0, 1, 0};
bool out = all(vec);
0
std::vector<bool> vec = {1, 1, 1, 1};
out = all(vec);
1
template <typename T> void sort_descin(std::vector<T> &x)
Transforms a stl vector (int, float, double, bool) to a sorted decreasing stl vector.
Name | Definition |
---|---|
x | stl vector (int, float, double, bool) |
std::vector<int> vec = {1, 5, 2, 1, 5, 22};
sort_descin(vec);
{22, 5, 5, 2, 1, 1}
template <typename T> void sort_ascin(std::vector<T> &x)
Transforms a stl vector (int, float, double, bool) to a sorted increasing stl vector.
Name | Definition |
---|---|
x | stl vector (int, float, double, bool) |
std::vector<int> vec = {1, 5, 2, 1, 5, 22};
sort_ascin(vec);
{1, 1, 2, 5, 5, 22}
template <typename T> void sort_descout(std::vector<T> &x)
Returns a stl sorted decreasingly vector from a stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | stl vector (int, float, double, bool) |
std::vector<int> vec = {1, 5, 2, 1, 5, 22};
std::<int> out = sort_descout(vec);
{22, 5, 5, 2, 1, 1}
template <typename T> void sort_ascout(std::vector<T> &x)
Returns a stl sorted increasingly vector from a stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | stl vector (int, float, double, bool) |
std::vector<int> vec = {1, 5, 2, 1, 5, 22};
std::<int> out = sort_ascout(vec);
{1, 1, 2, 5, 5, 22}
std::vector<std::string> str_sort_descend(std::vector<std::string> &x,
std::vector order_v = {
' ', '!', '"', '#', '$', '%', '&',
'\'', '(', ')', '*', '+', ',', '-',
'.', '/', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', ':', ';', '<', '=',
'>', '?', '@', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', '[', '\\',
']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}',
'~'
})
Returns a descendly sorted vector of std::string (according to ascii table order by default)
Name | Definition |
---|---|
x | is the input vector |
order_v | is the order of each character (ascii table by default) |
std::vector<std::string> inv = {"b", "c", "a", "b", "a", "aa", "ab"};
std::vector<std::string> outv = str_sort_descend(inv);
print_svec(outv);
:0: c b b ab aa a a
std::vector<std::string> str_sort_ascend(std::vector<std::string> &x, std::vector order_v = {
' ', '!', '"', '#', '$', '%', '&',
'\'', '(', ')', '*', '+', ',', '-',
'.', '/', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', ':', ';', '<', '=',
'>', '?', '@', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', '[', '\\',
']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}',
'~'
})
Returns a ascendly sorted vector of std::string. (according to ascii table order by default)
Name | Definition |
---|---|
x | is the input vector |
order_v | is the order of each character (ascii table by default) |
std::vector<std::string> inv = {"b", "c", "a", "b", "a", "aa", "ab"};
std::vector<std::string> outv = str_sort_ascend(inv);
print_svec(outv);
:0: a a aa ab b b c
template <typename T> void rm_ordered(std::vector<T> &x, std::vector<int> ids)
Remove elements from a stl vector. Keeps the vector sorted at a certain computational cost compared to rm_unordered. The stl int vector provided for the indices of the element to be removed must be decreasingly sorted. The capacity of the vector is kept unchanged, so if you want to shrink it, consider doing shrink_to_fit()
method.
Name | Definition |
---|---|
x | is an stl vector |
ids | is an stl int vector decreasingly sorted |
std::vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> ids = {8, 5, 3, 2};
rm_ordered(vec, ids);
print_nvec(vec);
:0: 0 1 4 6 7 9
template <typename T> void rm_unordered(std::vector<T> &x, std::vector<int> ids)
Remove elements from a stl vector. Does not keep the vector sorted for computational speed compared to rm_ordered. The stl int vector provided for the indices of the element to be removed must be decreasingly sorted. The capacity of the vector is kept unchanged, so if you want to shrink it, consider doing shrink_to_fit()
method.
Name | Definition |
---|---|
x | is an stl vector |
ids | is an stl int vector decreasingly sorted |
std::vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> ids = {8, 5, 3, 2};
rm_unordered(vec, ids);
print_nvec(vec);
:0: 0 1 6 7 4 9
template <typename T> std::vector<T> union2(std::vector<T> &x, std::vector<T> &x2)
Returns the union of two stl vectors. Does not returns a stl vector with unique elements, so if you want it to return unique elements, make sure to enter unique stl vectors as input.
Name | Definition |
---|---|
x | is an stl vector |
x2 | is an stl vector |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11};
std::vector<int> out = union2(vec1, vec2);
print_nvec(out);
:0: 3 4 4 5 7 8 2 4 0 1 2 3 9 11
Unionv union1(std::vector<Type> vec1);
union1.to_union(std::vector<Type> vec2, std::vector<Type> vec3);
union1.result();
union1.reinitiate(std::vector<Type2> vec4);
...
Name | Definition |
---|---|
... | undefinite number of stl vector of the same type |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4, 11};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11, 4};
std::vector<int> vec3 = {0, 1, 2, 3, 11, 9};
Unionv union1(vec1);
union1.to_union(vec2, vec3, vec3);
std::vector<int> out = union1.result();
print_nvec(out);
:0: 3 4 4 5 7 8 2 4 11 0 1 2 3 9 11 4 0 1 2 3 11 9 0 1
:25: 2 3 11 9
union1.reinitiate(vec2);
union1.to_union(vec1);
out = union1.result();
print_nvec(out);
:0: 0 1 2 3 9 11 4 3 4 4 5 7 8 2 4 11
template <typename T> std::vector<T> union2(std::vector<T> &x, std::vector<T> &x2)
Returns the commun elements of two stl vectors of the same type. Does not returns a stl vector with unique elements, so if you want it to return unique elements, make sure to enter unique stl vectors as input.
Name | Definition |
---|---|
x | is an stl vector |
x2 | is an stl vector |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11};
std::vector<int> out = intersect2(vec1, vec2);
print_nvec(out);
:0: 2 3
Intersectv intersect1(std::vector<Type> vec1);
intersect1.to_intersect(std::vector<Type> vec2, std::vector<Type> vec3);
intersect1.reinitiate(std::vector<Type2>);
...
Returns the commun elements of undefinite number of stl vectors of the same type. The returned vector can have extra capacity non initiated, to get rid of that consider applying it the shrink_to_fit()
method. The returned vector does not return unique elements, so if you want unique elements, consider enter unique stl vectors as input.
Name | Definition |
---|---|
... | undefinite number of stl vectors |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4, 11};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11, 4};
std::vector<int> vec3 = {0, 1, 2, 3, 11, 9};
Intersectv intersect1(vec1);
intersect1.to_intersect(vec2, vec3);
std::vector<int> out = intersect1.result();
print_nvec(out);
:0: 3 2 11
intersect1.reinitiate(vec1);
intersect1.to_intersect(vec2);
out = intersect1.result();
print_nvec(out);
:0: 3 4 4 2 4 11
template <typename T> std::vector<T> diff2(std::vector<T> &x, std::vector<T> &x2)
Returns the elements that are in one of the stl vector but not in the intersection of all stl vectors. Does not returns a stl vector with unique elements, so if you want it to return unique elements, make sure to enter unique stl vectors as input.
Name | Definition |
---|---|
x | is an stl vector |
x2 | is an stl vector |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11};
std::vector<int> out = diff2(vec1, vec2);
print_nvec(out);
:0: 9 4 4 5 7 8 11 4 0 1
Diffv diff1(std::vector<Type> vec1)
diff1.to_diff(std::vector<Type> vec2, std::vector<Type> vec2);
diff1.result();
diff1.reinitiate(std::vector<Type2> vec4);
...
Returs a stl vector of all the elements that are in one of the undefinite number of input stl vectors (of the same type) or more, but not in the intersection of all these stl vectors. Does not return a stl vector with unique elements, if you want it to return a stl vector with unique elements make sure that your input vectors contain unique elements. The returned vector can have more capacity than its size, to get rid of this unusable memory, you can apply the shrink_to_fit()
method on it.
Name | Definition |
---|---|
... | undefinite number of stl vectors of the same type |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4, 11};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11, 4};
std::vector<int> vec3 = {0, 1, 2, 3, 11, 9};
Diffv diff1(vec1);
diff1.to_diff(vec2, vec3);
std::vector<int> out = diff1.result();
print_nvec(out);
:0: 9 4 4 5 7 8 9 4 4 0 1 0 1
diff1.reinitiate(vec3);
diff1.to_diff(vec2, vec1);
out = diff1.result();
print_nvec(out);
:0: 0 1 4 4 4 9 0 1 5 7 9 8 4 //same elements, different orders because the initializer vector is not the same
template <typename T> void rm_shared_in(std::vector<T> &x, std::vector<T> &x2)
Transforms the first stl vector input removing its commun elements with the second stl vector of the same type. The returned vector has its capacity unchanged, so consider applying the shrink_to_fit()
method to it if you want to free some memory.
Name | Definition |
---|---|
x | is an stl vector |
x2 | is an stl vector |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4, 11};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11, 4};
rm_shared_in(vec1, vec2);
print_nvec(vec1);
:0: 5 7 8
template <typename T> std::vector<T> rm_shared_out(std::vector<T> &x, std::vector<T> &x2)
Returns the first stl vector input minus the its commun elements with the second stl vector of the same type. The returned vector has its capacity unchanged, so consider applying the shrink_to_fit()
method to it if you want to free some memory.
Name | Definition |
---|---|
x | is an stl vector |
x2 | is an stl vector |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4, 11};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11, 4};
std::vector<int> out = rm_shared_out(vec1, vec2);
print_nvec(out);
:0: 5 7 8
Rm_sharedv rm1(std::vector<Type> vec1);
rm1.to_comp(std::vector<Type> vec2, std::vector<Type> vec3);
rm1.result();
rm1.reinitiate(std::vector<OtherType> vec4);
...
Returns the initializer vector with the shared elements between this vector and an undefinite number of stl vectors, removed. This method is faster than finding commun elements between undefinite number of stl vectors and the initializer vector, and then removing the commun elements.
Name | Definition |
---|---|
... | undefinite number of stl vectors |
std::vector<int> vec1 = {3, 4, 4, 5, 7, 8, 2, 4};
std::vector<int> vec2 = {0, 1, 2, 3, 9, 11};
std::vector<int> vec3 = {0, 1, 2, 3, 9, 11, 8};
Rm_sharedv obj1(vec1);
obj1.to_rm(vec2, vec3);
std::vector<int> out = obj1.result();
print_nvec(out);
:0: 4 4 4 5 7
obj1.reinitiate(vec1);
out = obj1.result();
print_nvec(out);
:0: 3 4 4 5 7 8 2 4
template <typename T> unsigned int closest_idx(std::vector<T> &x, T &val)
Returns the closest elements from a stl vector (int, float, double, bool) and a given value as the index of the closet element of the stl vector. This is equivalent to finding the index of the minimum difference between the value and the elements in the stl vector, but more efficiently since the function assumes the stl vector is already sorted ascendly or descendly as you see in examples.
Name | Definition |
---|---|
x | is an stl vector (int, float, double, bool), must be ascendly or descendly sorted |
val | is an int, float, double, bool |
std::vector<double> vec = {0.1, 0.89, 1.2, 1.66, 1.78, 2.25, 4.56};
double val = 1.97;
closest_idx(vec, val);
4
val = 0.99;
closest_idx(vec, val);
1
std::vector<double> vec2 = sort_descout(vec);
closest_idx(vec, val);
5
val = 5.33;
closest_val(vec2, val);
0
template <typename T, typename T2> std::string ncollapse(const std::vector<T> &x, const T2 &sep)
Collapses all elements from an stl vector (int, float, double, bool) to a string with a given separator.
Name | Definition |
---|---|
x | is an stl vector (int, float, double, bool) |
sep | is a char or string that will be the separator between the elements of x |
std::vector<int> vec = {5, 7, 22, 879};
char sep = "-";
std::string out = ncollapse(vec, sep);
"5-7-22-879"
std::string sep2 = "--";
out = ncollapse(vec, sep2);
"5--7--22--879"
template <typename T> std::string scollapse(const std::vector<std::string> &x, const T &sep)
Collapses all elements from an stl string vector to a string with a given separator.
Name | Definition |
---|---|
x | is an stl vector (stl string) |
sep | is a char or string that will be the separator between the elements of x |
std::vector<std::string> vec = {"yess", "no", "maybe"};
char sep = "-";
std::string out = ncollapse(vec, sep);
"yess-no-maybe"
std::string sep2 = "--";
out = ncollapse(vec, sep2);
"yess--no--maybe"
std::vector<std::string> split(const std::string &x, const char &sep)
Returns a stl vector of stl strings that are part of the input stl string. The input string must have a separator to differenciate elements for the output stl vector.
Name | Definition |
---|---|
x | is a stl string |
sep | is a char |
std::string test = "y-e-ss";
char sep = '-';
std::vector<std::string> out = split(test, sep);
{"y", "e", "ss"}
std::vector<std::string> merge_strv(std::vector<std::string> &x1, std::vector<std::string> &x2, std::string sep = "-")
Returns a vector of merged strings of the input vectors
Name | Definition |
---|---|
x1 | is the first vector |
x2 | is the second vector |
std::vector<std::string> vec = {"lm", "lm", "mm", "lm", "po", "rr", "rr", "po"};
std::vector<std::string> vec2 = {"lm", "lm", "O"};
std::vector<std::string> outv = merge_strv(vec, vec2);
print_svec(outv);
:0: lm-lm lm-lm mm-O lm-lm po-lm rr-O rr-lm po-lm
template <typename T> std::map<std::vector<T>, std::vector<unsigned int>> occu(std::vector<T> &x)
Returns a map containing: the input vector of elements with unique elements, and their associated occurence in another vector (second in map)
Name | Definition |
---|---|
x | is the input vector |
std::vector<std::string> vec = {"lm", "lm", "mm", "lm", "po", "rr", "rr", "po"};
std::map<std::vector<std::string>, std::vector<unsigned int>> rtn_mp = occu(vec);
std::map<std::vector<std::string>, std::vector<unsigned int>>::iterator rtn_it = rtn_mp.begin();
int i;
std::vector<std::string> uvec = rtn_it->first;
std::vector<unsigned int> freqv = rtn_it->second;
for (i = 0; i < freqv.size(); ++i) {
std::cout << uvec[i] << " " << freqv[i] << "\n";
};
lm 3
mm 1
po 2
rr 2
template <typename T> std::map<std::vector<T>, std::vector<unsigned int>> desc_occu(std::vector<T> vec, std::vector<unsigned int> freqv)
Returns a descendly sorted occu()
output.
Name | Definition |
---|---|
vec | is the vector of unique elements |
freqv | is the associated occurence of the elements |
std::vector<std::string> vec = {"lm", "lm", "mm", "lm", "po", "rr", "rr", "po"};
std::map<std::vector<std::string>, std::vector<unsigned int>> rtn_mp = occu(vec);
std::map<std::vector<std::string>, std::vector<unsigned int>>::iterator rtn_it = rtn_mp.begin();
int i;
std::vector<std::string> uvec = rtn_it->first;
std::vector<unsigned int> freqv = rtn_it->second;
std::cout << "######\n";
rtn_mp = desc_occu(uvec, freqv);
rtn_it = rtn_mp.begin();
uvec = rtn_it->first;
freqv = rtn_it->second;
for (i = 0; i < freqv.size(); ++i) {
std::cout << uvec[i] << " " << freqv[i] << "\n";
};
lm 3
po 2
rr 2
mm 1
template <typename T> std::map<std::vector<T>, std::vector<unsigned int>> asc_occu(std::vector<T> vec, std::vector<unsigned int> freqv)
Returns a ascendly sorted out put of the occu()
output.
Name | Definition |
---|---|
vec | is the vector of unique elements |
freqv | is the associated occurence of the elements |
std::vector<std::string> vec = {"lm", "lm", "mm", "lm", "po", "rr", "rr", "po"};
std::map<std::vector<std::string>, std::vector<unsigned int>> rtn_mp = occu(vec);
std::map<std::vector<std::string>, std::vector<unsigned int>>::iterator rtn_it = rtn_mp.begin();
int i;
std::vector<std::string> uvec = rtn_it->first;
std::vector<unsigned int> freqv = rtn_it->second;
for (i = 0; i < freqv.size(); ++i) {
std::cout << uvec[i] << " " << freqv[i] << "\n";
};
rtn_mp = asc_occu(uvec, freqv);
rtn_it = rtn_mp.begin();
uvec = rtn_it->first;
freqv = rtn_it->second;
for (i = 0; i < freqv.size(); ++i) {
std::cout << uvec[i] << " " << freqv[i] << "\n";
};
mm 1
po 2
rr 2
lm 3
unsigned int percentage_to_idx(double &val, unsigned int &sizen)
Returns an idx of a stl vector from a percentage between 0 and 1.
Name | Definition |
---|---|
val | is the percentage value, between 0 and 1 |
sizen | is the size of the vector you want to have the index. |
std::<int> vec = {1, 2, 3, 4};
unsigned int sizen = vec.size();
double pct = 0.6;
unsigned int out = pct_to_idx(pct, sizen);
2
template <typename T> double diff_mean(std::vector<T> &x)
Returns the mean of all the differences in value between the contiguous elementsof a stl vector.
Name | Definition |
---|---|
x | is a stl vector (int, float, double, bool) |
std::vector<double> vec = {10, 10.5, 11, 11.5};
diff_mean(vec);
0.5
Dataframe my_dataframe
Dataframe objects supporting reading csv, with custom separator, storing columns in differents type vectors, creating a new Dataframe object on top of an already existing one specifying the rows and columns to copy, the same goes for a matrix (as std::vector<std::vector<T>>
) and std::vector<T>
. See examples.
Name | Definition |
---|---|
See_below | See below |
See below
void readf(std::string &file_name, char delim = ',', bool header_name = 1, char str_context_begin = '\'', char str_context_end = '\'')
Import a csv as a Dataframe object.
Name | Definition |
---|---|
file_name | is the file_name of the csv to read |
delim | is the column delimiter |
header_name | is if the first row is in fact the column names |
str_context_begin | is the first symbol of a quote, (to not take in count a comma as a new column if it is in a quote for example) |
str_context_end | is the end symbol for a quote context |
Dataframe obj1;
std::string file_name = "teste_dataframe.csv";
obj1.readf(file_name);
void writef(std::string &file_name, char delim = ',', bool header_name = 1, char str_context_bgn = '\'', char str_context_end = '\'')
Write a dataframe object into a csv file.
Name | Definition |
---|---|
file_name | is the file name to write data into |
delim | is the column delimiter |
header_name | 1 to write the column names, 0 else |
str_context_begin | is the first symbol of a quote, (to not take in count a comma as a new column if it is in a quote for example) |
str_context_end | is the end symbol for a quote context |
// after reading teste_dataframe.csv as obj1
std::string out_file = "out.csv";
obj1.writef(out_file);
void display();
Print the current dataframe.
Name | Definition |
---|---|
no | no |
// after reading teste_dataframe.csv as obj1
obj1.display();
<uint> <uint> <uint> <str> <int> <char>
col1 col2 col3 col4 col5 col6
:0: 1 2 3 aa 5 z
:1: 6 7 8 bb 10 e
:2: 1 2 3 cc 5 h
:3: 6 7 8 uu 10 a
:4: 1 2 3 s4 -5 q
:5: 6 7 8 s9 10 p
:6: 1 2 3 a4 5 j
:7: 6 7 8 m9 10 i
:8: 6 7 8 s9 10 p
:9: 1 2 3 a4 5 j
:10: 6 7 8 m9 10 i
:11: 6 7 8 m9 10 i
:12: 6 7 8 s9 10 p
:13: 1 2 3 a4 5 j
:14: 6 7 8 m9 10 i
void idx_dataframe(std::vector<int> &rows, std::vector<int> &cols, Dataframe &cur_obj)
Allow to copy a dataframe choosing rows and columns (by index) of the copied dataframe.
Name | Definition |
---|---|
rows | is the vector containing all the rows to copy ({-1} ) for all |
cols | is the vector of the index of the columns to copy |
cur_obj | is the dataframe that will contain all the rows and columns of the copied dataframe |
// after reading teste_dataframe.csv as obj1
Dataframe obj2;
std::vector<int> idx_rows = {-1};
std::vector<int> idx_cols2 = {1, 2, 3};
obj2.idx_dataframe(idx_rows, idx_cols2, obj1);
obj2.display();
col1 col2 col3
:0: 2 3 aa
:1: 7 8 bb
:2: 2 3 cc
:3: 7 8 uu
:4: 2 3 s4
:5: 7 8 s9
:6: 2 3 a4
:7: 7 8 m9
:8: 7 8 s9
:9: 2 3 a4
:10: 7 8 m9
:11: 7 8 m9
:12: 7 8 s9
:13: 2 3 a4
:14: 7 8 m9
void name_dataframe(std::vector<int> &rows, std::vector<int> &name_cols, Dataframe &cur_obj)
Allow to copy a dataframe choosing rows and columns (by name) of the copied dataframe.
Name | Definition |
---|---|
rows | is the vector containing all the rows to copy ({-1} ) for all |
cols | is the vector of the name of the columns to copy |
cur_obj | is the dataframe that will contain all the rows and columns of the copied dataframe |
// after reading teste_dataframe.csv as obj1
Dataframe obj2;
std::vector<int> idx_rows = {-1};
std::vector<std::string> idx_cols2 = {"col2", "col3", "col3"};
obj2.name_dataframe(idx_rows, idx_cols2, obj1);
obj2.display();
col1 col2 col3
:0: 2 3 aa
:1: 7 8 bb
:2: 2 3 cc
:3: 7 8 uu
:4: 2 3 s4
:5: 7 8 s9
:6: 2 3 a4
:7: 7 8 m9
:8: 7 8 s9
:9: 2 3 a4
:10: 7 8 m9
:11: 7 8 m9
:12: 7 8 s9
:13: 2 3 a4
:14: 7 8 m9
template <typename T> void idx_colint(std::vector<int> rows, unsigned int x, std::vector<T> &rtn_v)
Allow to copy a int, unsigned int , bool or double column as a vector<T>, by column index.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x | is the index of the column to copy |
rtn_v | is the vector that will contain the column to copy |
// after reading teste_dataframe.csv as obj1
std::vector<int> currows = {1, 0, 1};
std::vector<unsigned int> outv = {};
obj1.idx_colint(currows, 2, outv);
print_nvec(outv);
:0: 8 3 8
template <typename T> void name_colint(std::vector<int> rows, std::string colname, std::vector<T> &rtn_v)
Allow to copy a int, unsigned int , bool or double column as a vector<T>, by column name.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x | is the name of the column to copy |
rtn_v | is the vector that will contain the column to copy |
// after reading teste_dataframe.csv as obj1
std::vector<int> currows = {1, 0, 1};
std::vector<unsigned int> outv = {};
obj1.idx_colint(currows, "col2", outv);
print_nvec(outv);
:0: 8 3 8
void idx_colstr(std::vector<int> rows, unsigned int x, std::vector<std::string> &rtn_v)
Allow to copy a std::string column as a vector<std::string>, by column index.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x | is the index of the column to copy |
rtn_v | is the vector that will contain the column to copy |
// after reading teste_dataframe.csv as obj1
std::vector<std::string> outv2 = {};
obj1.idx_colstr(currows, 3, outv2);
print_svec(outv2);
:0: bb aa bb
template <typename T> void name_colint(std::vector<int> rows, std::string colname, std::vector<T> &rtn_v)
Allow to copy a int, unsigned int , bool or double column as a vector<std::string>, by column name.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x | is the name of the column to copy |
rtn_v | is the vector that will contain the column to copy |
// after reading teste_dataframe.csv as obj1
std::vector<std::string> outv2 = {};
obj1.name_colstr(currows, "col4", outv2);
print_svec(outv2);
:0: bb aa bb
void idx_colchr(std::vector<int> rows, unsigned int x, std::vector<char> &rtn_v)
Allow to copy a char column as a vector<char>, by column index.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x | is the index of the column to copy |
rtn_v | is the vector that will contain the column to copy |
// after reading teste_dataframe.csv as obj1
std::vector<char> outv3 = {};
obj1.idx_colchr(currows, 5, outv3);
for (char i : outv3) {
std::cout << i << " ";
};
std::cout << "\n";
e z e
void name_colchr(std::vector<int> rows, std::string colname, std::vector<char> &rtn_v)
Allow to copy a char column as a vector<char>, by column name.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x | is the name of the column to copy |
rtn_v | is the vector that will contain the column to copy |
// after reading teste_dataframe.csv as obj1
std::vector<char> outv3 = {};
obj1.name_colchr(currows, "col6", outv3);
for (char i : outv3) {
std::cout << i << " ";
};
std::cout << "\n";
e z e
template <typename T> void idx_matrint(std::vector<int> rows, std::vector<unsigned int> x_v, std::vector<std::vector<T>> &rtn_matr)
Allow to copy a set of columns that are same type (int, unsigned int, bool or double) as a std::vector<std::vector<T>>
, by column index.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ( {-1} ) for all |
x_v | is the vector containing the indices of the column to copy |
rtn_matr | is the matrix that will contain all the columns copyed |
// after reading teste_dataframe.csv as obj1
std::vector<std::vector<unsigned int>> out_matr = {};
std::vector<unsigned int> idx_cols = {1, 0, 2, 2};
obj1.idx_matrint(currows, idx_cols, out_matr);
print_nmatr(out_matr);
[0] [1] [2] [3]
:0: 7 6 8 8
:1: 2 1 3 3
:2: 7 6 8 8
template <typename T> void name_matrint(std::vector<int> rows, std::vector<std::string> x_v, std::vector<std::vector<T>> &rtn_matr)
Allow to copy a set of columns that are same type (int, unsigned int, bool or double) as a std::vector<std::vector<T>>
, by column name.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x_v | is the vector containing the names of the column to copy |
rtn_matr | is the matrix that will contain all the columns copyed |
// after reading teste_dataframe.csv as obj1
std::vector<std::vector<unsigned int>> out_matr = {};
std::vector<std::string> idx_vec2 = {"col1", "col2", "col3"};
obj1.name_matrint(currows, idx_vec2, out_matr);
print_nmatr(out_matr);
[0] [1] [2]
:0: 6 7 8
:1: 1 2 3
:2: 6 7 8
void idx_matrstr(std::vector<int> rows, std::vector<unsigned int> x_v, std::vector<std::vector<std::string>> &rtn_matr)
Allow to copy a set of columns that are std::string
type as a std::vector<std::vector<std::string>>
, by column index.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x_v | is the vector containing the indices of the column to copy |
rtn_matr | is the matrix that will contain all the columns copyed |
// after reading teste_dataframe.csv as obj1
std::vector<std::vector<std::string>> out_matr2 = {};
std::vector<unsigned int> idx_cols = {3};
obj1.idx_matrstr(currows, idx_cols, out_matr2);
print_smatr(out_matr2);
[0]
:0: bb
:1: aa
:2: bb
void name_matrstr(std::vector<int> rows, std::vector<std::string> x_v, std::vector<std::vector<std::string>> &rtn_matr)
Allow to copy a set of columns that are std::string
type as a std::vector<std::vector<std::string>>
, by column name.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x_v | is the vector containing the names of the column to copy |
rtn_matr | is the matrix that will contain all the columns copyed |
// after reading teste_dataframe.csv as obj1
std::vector<std::vector<std::string>> out_matr2 = {};
std::vector<std::string> idx_vec2 = {"col4"};
obj1.name_matrstr(currows, idx_vec2, out_matr2);
print_smatr(out_matr2);
[0]
:0: bb
:1: aa
:2: bb
void idx_matrchr(std::vector<int> rows, std::vector<unsigned int> x_v, std::vector<std::vector<char>> &rtn_matr)
Allow to copy a set of columns that are char
type as a std::vector<std::vector<char>>
, by column index.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x_v | is the vector containing the indices of the column to copy |
rtn_matr | is the matrix that will contain all the columns copyed |
// after reading teste_dataframe.csv as obj1
std::vector<std::vector<char>> out_matr3 = {};
std::vector<unsigned int> idx_cols = {5};
obj1.idx_matrchr(currows, idx_cols, out_matr3);
print_smatr(out_matr3);
[0]
:0: e
:1: z
:2: e
void name_matrchr(std::vector<int> rows, std::vector<std::string> x_v, std::vector<std::vector<char>> &rtn_matr)
Allow to copy a set of columns that are char
type as a std::vector<std::vector<char>>
, by column name.
Name | Definition |
---|---|
rows | is a vector containing the row indices to copy ({-1} ) for all |
x_v | is the vector containing the names of the column to copy |
rtn_matr | is the matrix that will contain all the columns copyed |
// after reading teste_dataframe.csv as obj1
std::vector<char> out_matr3 = {};
std::vector<std::string> idx_vec2 = {"col6"};
obj1.name_matrchr(currows, idx_vec2, out_matr3);
print_smatr(out_matr3);
[0]
:0: e
:1: z
:2: e
unsigned int get_nrow();
Returns the number of rows for the associated dataframe.
// after reading teste_dataframe.csv as obj1
unsigned int nrow = obj1.get_nrow();
15
unsigned int get_ncol();
Returns the number of columns for the associated dataframe.
// after reading teste_dataframe.csv as obj1
unsigned int ncol = obj1.get_ncol();
6
std::vector<std::string> get_rowname();
Returns the rowname of the associated dataframe.
// after reading teste_dataframe.csv as obj1
std::vector<std::string> row_names = obj1.get_rowname();
nothing becuase obj1 has no rownames
std::vector<std::string> get_colname();
Returns the colname of the associated dataframe.
// after reading teste_dataframe.csv as obj1
std::vector<std::string> col_names = obj1.get_colname();
col1 col2 col3 col4 col5 col6
void set_rowname(std::vector<std::string> &x);
Set rowname to the associated dataframe.
// after reading teste_dataframe.csv as obj1
std::vector<std::string> row_names = {"n1", "n2", "n3"..."n15"};
obj1.set_rowname(row_names);
void set_colname(std::vector<std::string> &x);
Set colname to the associated dataframe.
// after reading teste_dataframe.csv as obj1
std::vector<std::string> col_names = {"col1", "col2", "col3", "col4",
"col5", "col6"};
obj1.set_colname();
template <typename T> void replace_colint(std::vector<T> &x, unsigned int &colnb)
Replace a int, unsigned int, bool or double column of the associated dataframe.
Name | Definition |
---|---|
x | is the column (as vector) that will replace the dataframe column |
colnb | is the index of the column to replace |
// after reading teste_dataframe.csv as obj1
std::vector<unsigned int> rpl_col = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14};
unsigned int col = 0;
obj1.replace_colint(rpl_col, col);
obj1.display();
<uint> <uint> <uint> <str> <int> <char>
col1 col2 col3 col4 col5 col6
:0: 0 2 3 aa 5 z
:1: 1 7 8 bb 10 e
:2: 2 2 3 cc 5 h
:3: 3 7 8 uu 10 a
:4: 4 2 3 s4 -5 q
:5: 5 7 8 s9 10 p
:6: 6 2 3 a4 5 j
:7: 7 7 8 m9 10 i
:8: 8 7 8 s9 10 p
:9: 9 2 3 a4 5 j
:10: 10 7 8 m9 10 i
:11: 11 7 8 m9 10 i
:12: 12 7 8 s9 10 p
:13: 13 2 3 NA 5 j
:14: 14 7 8 m9 10 i
void replace_colstr(std::vector<std::string> &x, unsigned int &colnb)
Replace a string column of the associated dataframe.
Name | Definition |
---|---|
x | is the column (as vector) that will replace the dataframe column |
colnb | is the index of the column to replace |
// after reading teste_dataframe.csv as obj1
std::vector<std::string> rpl_col = {"0", "1", "2", "3", "4", "5", "6",
"7", "8", "9",
"10", "11", "12", "13", "14"};
unsigned int col = 3;
obj1.replace_colstr(rpl_col, col);
obj1.display();
<uint> <uint> <uint> <str> <int> <char>
col1 col2 col3 col4 col5 col6
:0: 1 2 3 0 5 z
:1: 6 7 8 1 10 e
:2: 1 2 3 2 5 h
:3: 6 7 8 3 10 a
:4: 1 2 3 4 -5 q
:5: 6 7 8 5 10 p
:6: 1 2 3 6 5 j
:7: 6 7 8 7 10 i
:8: 6 7 8 8 10 p
:9: 1 2 3 9 5 j
:10: 6 7 8 10 10 i
:11: 6 7 8 11 10 i
:12: 6 7 8 12 10 p
:13: 1 2 3 13 5 j
:14: 6 7 8 14 10 i
void replace_colchr(std::vector<char> &x, unsigned int &colnb)
Replace a string column of the associated dataframe.
Name | Definition |
---|---|
x | is the column (as vector) that will replace the dataframe column |
colnb | is the index of the column to replace |
// after reading teste_dataframe.csv as obj1
std::vector<char> rpl_col = {'c', 'c', 'c', 'c', 'c', 'c', 'c',
'c', 'c', 'c',
'b', 'b', 'v', 'v', 'v'};
unsigned int col = 5;
obj1.replace_colchr(rpl_col, col);
obj1.display();
<uint> <uint> <uint> <str> <int> <char>
col1 col2 col3 col4 col5 col6
:0: 1 2 3 aa 5 c
:1: 6 7 8 bb 10 c
:2: 1 2 3 cc 5 c
:3: 6 7 8 uu 10 c
:4: 1 2 3 s4 -5 c
:5: 6 7 8 s9 10 c
:6: 1 2 3 a4 5 c
:7: 6 7 8 m9 10 c
:8: 6 7 8 s9 10 c
:9: 1 2 3 a4 5 c
:10: 6 7 8 m9 10 b
:11: 6 7 8 m9 10 b
:12: 6 7 8 s9 10 v
:13: 1 2 3 NA 5 v
:14: 6 7 8 m9 10 v
template <typename T> void add_colint(std::vector<T> &x, std::string name = "NA")
Add a column int, unsigned int, bool or double type to the associated dataframe
Name | Definition |
---|---|
x | is the column to add |
name | is the column to add name |
// after reading teste_dataframe.csv as obj1
std::vector<unsigned int> rpl_col = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14};
obj1.add_colint(rpl_col);
obj1.display();
<uint> <uint> <uint> <str> <int> <char> <uint>
col1 col2 col3 col4 col5 col6 NA
:0: 1 2 3 aa 5 z 0
:1: 6 7 8 bb 10 e 1
:2: 1 2 3 cc 5 h 2
:3: 6 7 8 uu 10 a 3
:4: 1 2 3 s4 -5 q 4
:5: 6 7 8 s9 10 p 5
:6: 1 2 3 a4 5 j 6
:7: 6 7 8 m9 10 i 7
:8: 6 7 8 s9 10 p 8
:9: 1 2 3 a4 5 j 9
:10: 6 7 8 m9 10 i 10
:11: 6 7 8 m9 10 i 11
:12: 6 7 8 s9 10 p 12
:13: 1 2 3 NA 5 j 13
:14: 6 7 8 m9 10 i 14
void add_colstr(std::vector<std::string> &x, std::string name = "NA")
Add a column string type to the associated dataframe.
Name | Definition |
---|---|
x | is the column to add |
name | is the column to add name |
// after reading teste_dataframe.csv as obj1
std::vector<std::string> rpl_col = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14"};
obj1.add_colstr(rpl_col);
obj1.display();
<uint> <uint> <uint> <str> <int> <char> <str>
col1 col2 col3 col4 col5 col6 NA
:0: 1 2 3 aa 5 z 0
:1: 6 7 8 bb 10 e 1
:2: 1 2 3 cc 5 h 2
:3: 6 7 8 uu 10 a 3
:4: 1 2 3 s4 -5 q 4
:5: 6 7 8 s9 10 p 5
:6: 1 2 3 a4 5 j 6
:7: 6 7 8 m9 10 i 7
:8: 6 7 8 s9 10 p 8
:9: 1 2 3 a4 5 j 9
:10: 6 7 8 m9 10 i 10
:11: 6 7 8 m9 10 i 11
:12: 6 7 8 s9 10 p 12
:13: 1 2 3 NA 5 j 13
:14: 6 7 8 m9 10 i 14
add_colchr(std::vector<char> &x, std::string name = "NA")
Add a column char type to the associated dataframe.
Name | Definition |
---|---|
x | is the column to add |
name | is the column to add name |
// after reading teste_dataframe.csv as obj1
std::vector<char> rpl_col = {'c', 'c', 'c', 'c', 'c', 'c', 'c',
'c', 'c', 'c',
'b', 'b', 'v', 'v', 'v'};
obj1.add_colchr(rpl_col);
obj1.display();
<uint> <uint> <uint> <str> <int> <char> <char>
col1 col2 col3 col4 col5 col6 NA
:0: 1 2 3 aa 5 z c
:1: 6 7 8 bb 10 e c
:2: 1 2 3 cc 5 h c
:3: 6 7 8 uu 10 a c
:4: 1 2 3 s4 -5 q c
:5: 6 7 8 s9 10 p c
:6: 1 2 3 a4 5 j c
:7: 6 7 8 m9 10 i c
:8: 6 7 8 s9 10 p c
:9: 1 2 3 a4 5 j c
:10: 6 7 8 m9 10 i b
:11: 6 7 8 m9 10 i b
:12: 6 7 8 s9 10 p v
:13: 1 2 3 NA 5 j v
:14: 6 7 8 m9 10 i v
void rm_col(std::vector<unsigned int> &nbcolv)
Removes columns from associated dataframe.
Name | Definition |
---|---|
nbcolv | is a vector containing all the indices of the columns to erase from the associated dataframe. The indices must be sorted descendly. |
// after reading teste_dataframe.csv as obj1
std::vector<unsigned int> colv = {4, 1};
obj1.rm_col(colv);
obj1.display();
<uint> <uint> <str> <char>
col1 col3 col4 col6
:0: 1 3 aa z
:1: 6 8 bb e
:2: 1 3 cc h
:3: 6 8 uu a
:4: 1 3 s4 q
:5: 6 8 s9 p
:6: 1 3 a4 j
:7: 6 8 m9 i
:8: 6 8 s9 p
:9: 1 3 a4 j
:10: 6 8 m9 i
:11: 6 8 m9 i
:12: 6 8 s9 p
:13: 1 3 NA j
:14: 6 8 m9 i
void rm_col(std::vector<unsigned int> &nbcolv)
Removes rows from associated dataframe.
Name | Definition |
---|---|
nbcolv | is a vector containing all the indices of the rows to erase from the associated dataframe. The indices must be sorted descendly. |
// after reading teste_dataframe.csv as obj1
std::vector<unsigned int> rowv = {4, 1};
obj1.rm_row(rowv);
obj1.display();
<uint> <uint> <uint> <str> <int> <char>
col1 col2 col3 col4 col5 col6
:0: 1 2 3 aa 5 z
:1: 1 2 3 cc 5 h
:2: 6 7 8 uu 10 a
:3: 6 7 8 s9 10 p
:4: 1 2 3 a4 5 j
:5: 6 7 8 m9 10 i
:6: 6 7 8 s9 10 p
:7: 1 2 3 a4 5 j
:8: 6 7 8 m9 10 i
:9: 6 7 8 m9 10 i
:10: 6 7 8 s9 10 p
:11: 1 2 3 NA 5 j
:12: 6 7 8 m9 10 i
void transform_inner(Dataframe &cur_obj, unsigned int &in_col, unsigned int &ext_col)
Applies a inner join on the associated dataframe.
Name | Definition |
---|---|
cur_obj | is the other dataframe used for inner join |
in_col | is the index of the column representing the key (primary) of the associated dataframe |
ext_col | is the index of the column representing the key (foreign) of the other dataframe used for the inner join |
Dataframe obj1, obj2;
std::string filename = "outb.csv";
obj1.readf(filename);
std::vector<unsigned int> colv = {4, 3, 2};
obj1.rm_col(colv);
std::string f2 = "outb2.csv";
obj2.readf(f2);
unsigned int col1 = 0;
unsigned int col2 = 0;
obj1.transform_inner(obj2, col1, col2);
obj1.display();
<str> <uint>
col1 col2
:0: id1 1
:1: id2 6
:2: id4 6
:3: id5 1
:4: id6 6
:5: id7 1
:6: id8 6
:7: id9 6
:8: id11 6
:9: id12 6
:10: id14 1
:11: id15 6
void transform_excluding(Dataframe &cur_obj, unsigned int &in_col, unsigned int &ext_col)
Applies a excluding join on the associated dataframe.
Name | Definition |
---|---|
cur_obj | is the other dataframe used for the excluding join |
in_col | is the index of the column representing the key (primary) of the associated dataframe |
ext_col | is the index of the column representing the key (foreign) of the other dataframe used for the excluding join |
Dataframe obj1, obj2;
std::string filename = "outb.csv";
obj1.readf(filename);
std::vector<unsigned int> colv = {4, 3, 2};
obj1.rm_col(colv);
std::string f2 = "outb2.csv";
obj2.readf(f2);
unsigned int col1 = 0;
unsigned int col2 = 0;
obj1.transform_excluding(obj2, col1, col2);
obj1.display();
<str> <uint>
col1 col2
:0: id3 1
:1: id10 1
:2: id13 6
void merge_inner(Dataframe &obj1, Dataframe &obj2, bool colname, unsigned int &key1, unsigned int &key2)
Performs a inner join on a newly created dataframe.
Name | Definition |
---|---|
obj1 | is the first dataframe |
obj2 | is the second dataframe |
colname | 1 to give the column names to the newly created dataframe |
key1 | is the index of the first dataframe column as primary key |
key1 | is the index of the first dataframe column as foreign key |
Dataframe obj1, obj2, obj3;
std::string filename = "outb.csv";
obj1.readf(filename);
std::vector<unsigned int> colv = {4, 3, 2};
obj1.rm_col(colv);
std::string f2 = "outb2.csv";
obj2.readf(f2);
unsigned int col1 = 0;
unsigned int col2 = 0;
obj3.merge_inner(obj1, obj2, 1, col1, col2);
obj3.display();
<str> <uint> <str> <uint> <uint>
:0: id1 1 id1 2 3
:1: id2 6 id2 7 8
:2: id4 6 id4 7 8
:3: id5 1 id5 2 3
:4: id6 6 id6 7 8
:5: id7 1 id7 2 3
:6: id8 6 id8 2 3
:7: id9 6 id9 7 8
:8: id11 6 id11 7 8
:9: id12 6 id12 7 8
:10: id14 1 id14 7 8
:11: id15 6 id15 2 3
void merge_excluding(Dataframe &obj1, Dataframe &obj2, bool colname, unsigned int &key1, unsigned int &key2)
Performs a left excluding join to the associated dataframe (newly created). The first dataframe as argument is considered as the left one.
Name | Definition |
---|---|
obj1 | is the left dataframe |
obj2 | is the right dataframe |
colname | 1 to give the column names to the newly created dataframe |
key1 | is the index of the column of the left dataframe |
key2 | is the index of the column of the right dataframe |
Dataframe obj1, obj2, obj3;
std::string filename = "outb.csv";
obj1.readf(filename);
std::vector<unsigned int> colv = {4, 3, 2};
obj1.rm_col(colv);
std::string f2 = "outb2.csv";
obj2.readf(f2);
unsigned int col1 = 0;
unsigned int col2 = 0;
obj3.merge_excluding(obj1, obj2, 1, col1, col2);
obj3.display();
<str> <uint> <str> <uint> <uint>
:0: id3 1 NA NA NA
:1: id10 1 NA NA NA
:2: id13 6 NA NA NA
void merge_excluding_both(Dataframe &obj1, Dataframe &obj2, bool colname, unsigned int &key1, unsigned int &key2)
Performs a full excluding join to the associated dataframe (newly created). The first dataframe as argument is considered as the left one.
Name | Definition |
---|---|
obj1 | is the left dataframe |
obj2 | is the right dataframe |
colname | 1 to give the column names to the newly created dataframe |
key1 | is the index of the column of the left dataframe |
key2 | is the index of the column of the right dataframe |
Dataframe obj1, obj2, obj3;
std::string filename = "outb.csv";
obj1.readf(filename);
std::vector<unsigned int> colv = {4, 3, 2};
obj1.rm_col(colv);
std::string f2 = "outb2.csv";
obj2.readf(f2);
unsigned int col1 = 0;
unsigned int col2 = 0;
obj3.merge_excluding_both(obj1, obj2, 1, col1, col2);
obj3.display();
<str> <uint> <str> <uint> <uint>
:0: id3 1 NA NA NA
:1: id10 1 NA NA NA
:2: id13 6 NA NA NA
:3: NA NA id119 7 8
void merge_all(Dataframe &obj1, Dataframe &obj2, bool colname, unsigned int &key1, unsigned int &key2)
Performs a full join to the associated dataframe (newly created). The first dataframe as argument is considered as the left one.
Name | Definition |
---|---|
obj1 | is the left dataframe |
obj2 | is the right dataframe |
colname | 1 to give the column names to the newly created dataframe |
key1 | is the index of the column of the left dataframe |
key2 | is the index of the column of the right dataframe |
Dataframe obj1, obj2, obj3;
std::string filename = "outb.csv";
obj1.readf(filename);
std::vector<unsigned int> colv = {4, 3, 2};
obj1.rm_col(colv);
std::string f2 = "outb2.csv";
obj2.readf(f2);
unsigned int col1 = 0;
unsigned int col2 = 0;
obj3.merge_all(obj1, obj2, 1, col1, col2);
obj3.display();
<str> <uint> <str> <uint> <uint>
:0: id3 1 NA NA NA
:1: id10 1 NA NA NA
:2: id13 6 NA NA NA
:3: NA NA id119 7 8
:4: id1 1 id1 2 3
:5: id2 6 id2 7 8
:6: id4 6 id4 7 8
:7: id5 1 id5 2 3
:8: id6 6 id6 7 8
:9: id7 1 id7 2 3
:10: id8 6 id8 2 3
:11: id9 6 id9 7 8
:12: id11 6 id11 7 8
:13: id12 6 id12 7 8
:14: id14 1 id14 7 8
:15: id15 6 id15 2 3
template <typename T> void read_matr(std::string &file_name, std::vector<std::vector<T>> &out_matr, char delim = ',')
Returns a matrix stored in a file.
Name | Definition |
---|---|
file_name | is the name of the file |
out_matr | is a declared matrix |
delim | is the column delimiter |
std::string teste_file = "teste.csv";
std::vector<std::vector<double>> out_matr;
read_matr(teste_file, out_matr);
print_matr(out_matr);
[0] [1] [2] [3] [4]
:0: 1 2 3 4 5
:1: 6 7 8 9 10
:2: 1 2 3 4 5
:3: 6 7 8 9 10
:4: 1 2 3 4 5
:5: 6 7 8 9 10
:6: 1 2 3 4 5
:7: 6 7 8 9 10
template <typename T> void write_matr(std::string &file_name, std::vector<std::vector<T>> &in_matr, char delim = ',')
Writes a matrix into a file.
Name | Definition |
---|---|
file_name | is the name of the file |
in_matr | is the input matrix |
delim | is the chosen column delimiter |
std::string teste_file = "teste2.csv";
std::vector<std::vector<int>> in_matr = {
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4}
};
write_matr(teste_file, in_matr);
template <typename T> std::vector<std::vector<T>> sum_nxp(std::vector<std::vector<T>> &matr)
Returns as the first vector the sum of elements in each row, and in the second vector the sum of each elements in each column
Name | Definition |
---|---|
matr | is the input matrice |
std::vector<std::vector<double>> matr = {{1, 2}, {3, 4}, {5, 6}};
print_matr(matr);
std::vector<std::vector<double>> outmatr = sum_xnx(matr);
for (double i : outmatr[0]) {
std::cout << i << " ";
};
std::cout << "\n";
for (double i : outmatr[1]) {
std::cout << i << " ";
};
std::cout << "\n";
9 12
3 7 11
template <typename T> std::vector<std::vector<T>> t(const std::vector<std::vector<T>> &x)
Retursn the transpose of a matrix as 2D stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | is a 2D stl vector (int, float, double, bool) |
std::vector<std::vector<int>> matr = {{1, 2, 3}, {4, 5, 6}};
print_matr(matr); // another function from this library to print matrix as 2D stl vector
[0] [1]
:0: 1 4
:1: 2 5
:2: 3 6
std::vector<std::vector<int>> out = t(matr);
print_matr(out);
[0] [1] [2]
:0: 1 2 3
:1: 4 5 6
template <typename T> void t_in_square(std::vector<std::vector<T>> &x)
Transforms the input square matrix as 2D stl vector (int, float, double, bool) to its transpose.
Name | Definition |
---|---|
x | is a 2D stl vector (int, float, double, bool) |
std::vector<std::vector<int>> matr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print_matr(matr); // another function from this library to print matrix as 2D stl vector
[0] [1] [2]
:0: 1 4 7
:1: 2 5 8
:2: 3 6 9
t_in_square(matr);
print_matr(matr);
[0] [1] [2]
:0: 1 2 3
:1: 4 5 6
:2: 7 8 9
template <typename T> void t_in(std::vector<std::vector<T>> &x)
Transforms a matrix as 2D stl vector to its transpose
Name | Definition |
---|---|
x | is a matrix as a 2D stl vector |
std::vector<std::vector<int>> matr = {{1, 2, 3, 88, 90}, {4, -5, 6, 78, -7}, {-7, 8, -9, 12, 478}};
print_matr(matr);
[0] [1] [2]
:0: 1 4 -7
:1: 2 -5 8
:2: 3 6 -9
:3: 88 78 12
:4: 90 -7 478
t_in(matr);
print_matr(matr);
[0] [1] [2] [3] [4]
:0: 1 2 3 88 90
:1: 4 -5 6 78 -7
:2: -7 8 -9 12 478
t_in(matr);
print_matr(matr);
[0] [1] [2]
:0: 1 4 -7
:1: 2 -5 8
:2: 3 6 -9
:3: 88 78 12
:4: 90 -7 478
template <typename T> void print_nmatr(const std::vector<std::vector<T>> &x)
Print a matrix (float, int, double...) as 2D stl vector.
Name | Definition |
---|---|
x | is a 2D stl vector (float, int, double...) |
std::vector<std::vector<int>> out_matr = {
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 333, 4},
{1, 2, 3, 4},
{1, 2, 3, 4}
};
print_nmatr(out_matr);
[0] [1] [2] [3] [4]
:0: 1 1 1 1 1
:1: 2 2 2 2 2
:2: 3 3 333 3 3
:3: 4 4 4 4 4
std::vector<std::vector<double>> out_matr2 = {
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 333.23, 4},
{1, 2, 3, 4},
{1, 2, 3, 4}
};
print_nmatr(out_matr2);
[0] [1] [2] [3] [4]
:0: 1 1 1 1 1
:1: 2 2 2 2 2
:2: 3 3 333.23 3 3
:3: 4 4 4 4 4
template <typename T> void print_smatr(const std::vector<std::vector<T>> &x)
Print a matrix (std::string, char) as 2D stl vector.
Name | Definition | ||
---|---|---|---|
x | is a 2D stl vector (std | string, char) |
std::vector<std::vector<std::string>> out_matr3 = {
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "333", "44224441111111"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"}
};
print_smatr(out_matr3);
[0] [1] [2] [3] [4]
:0: 1 1 1 1 1
:1: 2 2 2 2 2
:2: 3 3 333 3 3
:3: 4 4 44224441111111 4 4
std::vector<std::vector<char>> out_matr4 = {
{'1', '2', '3', '4'},
{'1', '2', '3', '4'},
{'1', '2', '3', '4'},
{'1', '2', '3', '4'},
{'1', '2', '3', '4'}
};
print_smatr(out_matr4);
[0] [1] [2] [3] [4]
:0: 1 1 1 1 1
:1: 2 2 2 2 2
:2: 3 3 3 3 3
:3: 4 4 4 4 4
template <typename T> void abs_matrin(std::vector<std::vector<T>> &x)
Transforms all the values of the cells from a matrix as 2D stl vector (int, float, double, bool) to absolute values.
Name | Definition |
---|---|
x | is a matrix as 2D stl vector (int, float, double, bool) |
std::vector<std::vector<int>> matr = {{1, 2, 3}, {4, -5, 6}, {-7, 8, -9}};
print_matr(matr); // another function from this library to print matrix as 2D stl vector
[0] [1] [2]
:0: 1 4 -7
:1: 2 -5 8
:2: 3 6 -9
abs_matrin(matr);
print_matr(matr);
[0] [1] [2]
:0: 1 4 7
:1: 2 5 8
:2: 3 6 9
template <typename T> void abs_matrout(std::vector<std::vector<T>> &x)
Returns a marix as 2D stl vector (int, float, double, bool) with only absolute values from a matrix as 2D stl vector (int, float, double, bool).
Name | Definition |
---|---|
x | is a matrix as 2D stl vector (int, float, double, bool) |
std::vector<std::vector<int>> matr = {{1, 2, 3}, {4, -5, 6}, {-7, 8, -9}};
print_matr(matr); // another function from this library to print matrix as 2D stl vector
[0] [1] [2]
:0: 1 4 -7
:1: 2 -5 8
:2: 3 6 -9
std::vector<std::vector<int>> out = abs_matrout(matr);
print_matr(out);
[0] [1] [2]
:0: 1 4 7
:1: 2 5 8
:2: 3 6 9
template <typename T> double det_small(std::vector<std::vector<T>> &inpt_matr)
Calculates matrix determinant up to 5x5.
Name | Definition |
---|---|
impt_matr | is the input matrix |
std::vector<std::vector<int>> inpt_matr = {
{81, 55, 1, 81, 40},
{42, 48, 41, 37, 5},
{84, 1, 61, 612, 6},
{52, 59, 23, 50, 22},
{39, 6, 95, 69, 23},
};
int out_val = det_small(inpt_matr);
std::cout << out_val << "\n";
1473982232
Fapply
Allows to perform any function on a set of same type vectors, simplified
Name | Definition |
---|---|
... | all the vectors |
// declare a custom function
int add(std::vector<int> x) {
int rtn_v = x[0];
for (int i = 1; i < x.size(); ++i) {
rtn_v += x[i];
};
return rtn_v;
};
std::vector<int> inpt_v1 = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> inpt_v2 = {7, 6, 5, 4, 3, 2, 1};
Fapply obj1(inpt_v1); //initialise object with a vector argument
obj1.set_args(inpt_v1, inpt_v2, inpt_v2); //ingest args
//performs the chosen function
std::vector<int> outv = obj1.fapply(merge_str);
print_nvec(outv);
:0: 15 14 13 12 11 10 9
Fapply(std::vector<TB> &x)
Apllies any function onto a variadic numbers of vectors of any type
Name | Definition |
---|---|
x | is an empty vector of the type choosen for the operations between vectors |
std::vector<int> xint = {0, 1, 2};
std::vector<int> xint2 = {0, 1, 2};
std::vector<int> iinv = {};
Fapply2d obj1(iinv);
template <typename T, typename ...T2> void set_args(std::vector<T> &x, std::vector<T2>&... x2)
Is a variadic function allowing to put in the class an indefinite number of vectors of the same type that will be used by your custom function later, see Fapply2d.fapply2d()
Name | Definition |
---|---|
... | is some same types vectors |
std::vector<int> xint = {0, 1, 2};
std::vector<int> xint2 = {0, 1, 2};
obj1.set_args(xint, xint, xint2);
template <typename T> std::vector<T> fapply2d(std::vector<T> (&f)(std::vector<std::vector<T>>))
Performs your custom function on the std::vector
created by Fapply2d.set_args()
.
Name | Definition |
---|---|
f | is the reference to the custom function |
std::vector<int> addv(std::vector<std::vector<int>> x) {
std::vector<int> rtn_v = {};
int cur_val;
unsigned int i2;
const unsigned int n = x[0].size();
const unsigned int n2 = x.size();
for (int i = 0; i < n; ++i) {
cur_val = 0;
for (i2 = 0; i2 < n2; ++i2) {
cur_val += x[i2][i];
};
rtn_v.push_back(cur_val);
};
return rtn_v;
};
std::vector<int> xint = {0, 1, 2};
std::vector<int> xint2 = {0, 1, 2};
std::vector<int> iinv = {};
Fapply2d obj1(iinv);
obj1.set_args(xint, xint, xint2);
iinv = obj1.fapply2d(addv);
print_nvec(outv);
:0: 0 3 6
void reinitiate();
Empties the 2d T internal vector. Necessary to reset args (vectors input of the internal 2d vector) of same type before applying the function (or another) to new vectors.
Name | Definition |
---|---|
no | no |
Fapply2d.reinitiate();
double geo_min(double &lat1, double &longit1, double &lat2, double &longit2, const double sphere_ray = 6378)
Returns the shortest distance between 2 geographical points.
Name | Definition |
---|---|
lat1 | is the lattitude of the first point |
longit1 | is the longitude of the second point |
lat2 | is the lattitude of the second point |
longit2 | is the longitude of the second point |
sphere_ray | is the rayon of the sphere in km (defaults to Earth) |
double lat1 = 23;
double longit1 = -45;
double lat2 = -23;
double longit2 = 45;
double delta = geo_min(lat1, longit1, lat2, longit2);
std::cout << delta << "\n";
lat1 = 60;
longit1 = 30;
lat2 = 60;
longit2 = -150;
delta = geo_min(lat1, longit1, lat2, longit2);
std::cout << delta << "\n";
6679.1
std::string fr_to_eng_datefmt(std::string &x, char sep = '-')
Converts a french date format to an english one.
Name | Definition |
---|---|
x | is the input date |
sep | is the date separator |
std::string fr_date = "02-07-2022";
std::string eng_date = fr_to_eng_datefmt(fr_date);
std::cout << eng_date<< "\n";
07-02-2022
std::string eng_to_fr_datefmt(std::string &x, char sep = '-')
Converts a english date format to an french one.
Name | Definition |
---|---|
x | is the input date |
sep | is the date separator |
std::string eng_date = "07-02-2022";
std::string fr_date = eng_to_fr_datefmt(eng_date);
std::cout << fr_date << "\n";
02-07-2022
std::string fmt_converter_date(std::string &x, std::string &in_fmt, std::string &out_fmt, char delim = '-')
Cpnverts a date to another date format.
Name | Definition |
---|---|
x | is the input date |
in_fmt | is the input date format |
out_fmt | is the output date format |
std::string in_fmt = "dmy";
std::string out_fmt = "mdy";
std::string inpt_date = "02-07-2003";
std::string outfmtdate = fmt_converter_date(inpt_date, in_fmt, out_fmt);
std::cout << outfmtdate << "\n";
07-02-2003
std::vector<std::string> ascend_sort_date(std::vector<std::string> &x,
std::string &fmt, char delim = '-')
Returns a vector of date ascendly sorted. The format meaning is 'y' for year, 'm' for month, 'd' for day, 'h' for hour, 'n' for minute and 's' for second.
Name | Definition |
---|---|
x | is the input vector of dates |
fmt | is the date format |
delim | is the date delimiter |
std::vector<std::string> vec = {
"02-04-2026",
"02-11-2023",
"07-02-2026",
"22-04-2016",
"02-12-2006",
"17-9-2015",
"03-04-2017",
"02-05-2017",
"15-01-2026"
};
std::string fmt = "dmy";
std::vector<std::string> outv = ascend_sort_date(vec, fmt);
print_svec(outv);
:0: 02-12-2006 17-9-2015 22-04-2016 03-04-2017 02-05-2017 02-11-2023 15-01-2026 07-02-2026
:8: 02-04-2026
std::vector<std::string> descend_sort_date(std::vector<std::string> &x,
std::string &fmt, char delim = '-')
Returns a vector of date descendly sorted. The format meaning is 'y' for year, 'm' for month, 'd' for day, 'h' for hour, 'n' for minute and 's' for second.
Name | Definition |
---|---|
x | is the input vector of dates |
fmt | is the date format |
delim | is the date delimiter |
std::vector<std::string> vec = {
"02-04-2026",
"02-11-2023",
"07-02-2026",
"22-04-2016",
"02-12-2006",
"17-9-2015",
"03-04-2017",
"02-05-2017",
"15-01-2026"
};
std::string fmt = "dmy";
std::vector<std::string> outv = descend_sort_date(vec, fmt);
print_svec(outv);
:0: 02-04-2026 07-02-2026 15-01-2026 02-11-2023 02-05-2017 03-04-2017 22-04-2016 17-9-2015
:8: 02-12-2006
template <typename T> bool is_leap(unsigned T &x)
Returns if the input year is leap.
Name | Definition |
---|---|
x | is the input year |
int year = 2024;
bool bsx = is_leap(year);
std::cout << bsx << "\n";
1
double delta_second(std::string &bgn_date, std::string &end_date, int ref_year = 0, char delim = '-')
Returns the number of second elapsed between 2 dates.
Name | Definition |
---|---|
bgn_date | is the first date, must be lower than the second date |
end_date | is the second date |
ref_year | is a reference year from which the time elapsed wil be calculated. This value must be lower than the year of the first date |
delim | is the date delimiter |
std::string bgn_date = "2003-07-02-2-23-46";
std::string end_date = "2025-04-01-12-13-26";
double delta = delta_second(bgn_date, end_date);
std::cout << delta << "\n";
6.86311e+08
bool is_greater_date(std::string &x, std::string &x2, char delim = '-')
Returns if the first date is greater than the second one. The dates must have the same format, which is the highest time unit at the beginning and so on.
Name | Definition |
---|---|
x | is the first date |
x2 | is the second date |
delim | is the date delimiter |
std::string bgn_date = "2003-07-02-2-23-46";
std::string end_date = "2025-04-01-12-13-26";
bool outbool = is_greater_date(end_date, bgn_date);
std::cout << outbool << "\n";
1
std::vector<std::vector<unsigned int>> Parser_tokenizer_full(std::string &x, char frst_chr = '(', char scd_chr = ')')
Returns a 2d stl vectors. First vector is the pair of each parenthesis. Second stl vector is the index of each parenthesis. Takes a stl string as input.
Name | Definition |
---|---|
x | is a stl string |
std::string teste = "(o((ldjf)de)po(m()()m)po)()()";
std::vector<std::vector<unsigned int>> out = Parser_tokenizer_full(teste);
{5 1 0 0 1 4 2 2 3 3 4 5 6 6 7 7}
{0 2 3 8 11 14 16 17 18 19 21 24 25 26 27 28}
bool is_intricated (unsigned int &idx, std::vector<unsigned int> &tkn_v)
Returns a boolean, 1 if the parenthesis refered is intricated in others parenthesis, 0 if not. See examples.
Name | Definition |
---|---|
tkn_v | is a std vector containing all the parenthesis pairs. The one outputed by Parser_tokennizer_full function. |
idx | is an unsigned int that indicates the parenthesis refered in tkn_v vector |
std::string inpt_str = "(ldkhf(f)ek(()))dkjf(gf())()()";
std::vector<std::vector<unsigned int>> out_matr = Parser_tokenizer_full(inpt_str);
std::vector<unsigned int> tkn_v = out_matr[0];
std::vector<unsigned int> idx_v = out_matr[1];
unsigned int i;
for (i = 0; i < tkn_v.size(); ++i) {
std::cout << tkn_v[i] << " ";
};
std::cout << "\n";
for (i = 0; i < tkn_v.size(); ++i) {
std::cout << idx_v[i] << " ";
};
3 0 0 2 1 1 2 3 5 4 4 5 6 6 7 7
0 6 8 11 12 13 14 15 20 23 24 25 26 27 28 29
std::cout << "\n";
unsigned int cur_idx = 15;
bool is_it = is_intricated(cur_idx, tkn_v);
std::cout << is_it << "\n";
0
cur_idx = 3;
is_it = is_intricated(cur_idx, tkn_v);
std::cout << is_it << "\n";
1
cur_idx = 7;
is_it = is_intricated(cur_idx, tkn_v);
std::cout << is_it << "\n";
0
cur_idx = 6;
is_it = is_intricated(cur_idx, tkn_v);
std::cout << is_it << "\n";
1
template <typename T> bool is_symetric(std::vector<T> &x)
Returns 1 if the vector is symetric, 0 if not. See examples.
Name | Definition |
---|---|
x | is the input vector of any type |
std::vector<int> vec = {0, 1, 2, 43, 2, 1, 0};
bool out = is_symetric(vec);
1
vec = {0, 1, 2, 2, 1, 0};
out = is_symetric(vec);
1
vec = {0, 1, 2, 2, 22, 0};
out = is_symetric(vec);
0
std::vector<std::vector<bool>> all_comb(unsigned int &k, unsigned int &n)
Returns all the combinations of k elements in a set of n elements.
Name | Definition |
---|---|
k | is the k value |
n | is the total number of the set |
unsigned int k = 5;
unsigned int n = 9;
std::vector<std::vector<bool>> outmatr = all_comb(k, n);
int i2;
for (int i = 0; i < outmatr.size(); ++i) {
for (i2 = 0; i2 < outmatr[0].size(); ++i2) {
std::cout << outmatr[i][i2] <<< " ";
};
std::cout << "\n";
};
1 1 1 1 1 0 0 0 0
1 1 1 1 0 1 0 0 0
1 1 1 1 0 0 1 0 0
1 1 1 1 0 0 0 1 0
1 1 1 1 0 0 0 0 1
1 1 1 0 1 1 0 0 0
1 1 1 0 1 0 1 0 0
1 1 1 0 1 0 0 1 0
1 1 1 0 1 0 0 0 1
1 1 1 0 0 1 1 0 0
1 1 1 0 0 1 0 1 0
1 1 1 0 0 1 0 0 1
1 1 1 0 0 0 1 1 0
1 1 1 0 0 0 1 0 1
1 1 1 0 0 0 0 1 1
1 1 0 1 1 1 0 0 0
1 1 0 1 1 0 1 0 0
1 1 0 1 1 0 0 1 0
1 1 0 1 1 0 0 0 1
1 1 0 1 0 1 1 0 0
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 0 1
1 1 0 1 0 0 1 1 0
1 1 0 1 0 0 1 0 1
1 1 0 1 0 0 0 1 1
1 1 0 0 1 1 1 0 0
1 1 0 0 1 1 0 1 0
1 1 0 0 1 1 0 0 1
1 1 0 0 1 0 1 1 0
1 1 0 0 1 0 1 0 1
1 1 0 0 1 0 0 1 1
1 1 0 0 0 1 1 1 0
1 1 0 0 0 1 1 0 1
1 1 0 0 0 1 0 1 1
1 1 0 0 0 0 1 1 1
1 0 1 1 1 1 0 0 0
1 0 1 1 1 0 1 0 0
1 0 1 1 1 0 0 1 0
1 0 1 1 1 0 0 0 1
1 0 1 1 0 1 1 0 0
1 0 1 1 0 1 0 1 0
1 0 1 1 0 1 0 0 1
1 0 1 1 0 0 1 1 0
1 0 1 1 0 0 1 0 1
1 0 1 1 0 0 0 1 1
1 0 1 0 1 1 1 0 0
1 0 1 0 1 1 0 1 0
1 0 1 0 1 1 0 0 1
1 0 1 0 1 0 1 1 0
1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 0 1 1
1 0 1 0 0 1 1 1 0
1 0 1 0 0 1 1 0 1
1 0 1 0 0 1 0 1 1
1 0 1 0 0 0 1 1 1
1 0 0 1 1 1 1 0 0
1 0 0 1 1 1 0 1 0
1 0 0 1 1 1 0 0 1
1 0 0 1 1 0 1 1 0
1 0 0 1 1 0 1 0 1
1 0 0 1 1 0 0 1 1
1 0 0 1 0 1 1 1 0
1 0 0 1 0 1 1 0 1
1 0 0 1 0 1 0 1 1
1 0 0 1 0 0 1 1 1
1 0 0 0 1 1 1 1 0
1 0 0 0 1 1 1 0 1
1 0 0 0 1 1 0 1 1
1 0 0 0 1 0 1 1 1
1 0 0 0 0 1 1 1 1
0 1 1 1 1 1 0 0 0
0 1 1 1 1 0 1 0 0
0 1 1 1 1 0 0 1 0
0 1 1 1 1 0 0 0 1
0 1 1 1 0 1 1 0 0
0 1 1 1 0 1 0 1 0
0 1 1 1 0 1 0 0 1
0 1 1 1 0 0 1 1 0
0 1 1 1 0 0 1 0 1
0 1 1 1 0 0 0 1 1
0 1 1 0 1 1 1 0 0
0 1 1 0 1 1 0 1 0
0 1 1 0 1 1 0 0 1
0 1 1 0 1 0 1 1 0
0 1 1 0 1 0 1 0 1
0 1 1 0 1 0 0 1 1
0 1 1 0 0 1 1 1 0
0 1 1 0 0 1 1 0 1
0 1 1 0 0 1 0 1 1
0 1 1 0 0 0 1 1 1
0 1 0 1 1 1 1 0 0
0 1 0 1 1 1 0 1 0
0 1 0 1 1 1 0 0 1
0 1 0 1 1 0 1 1 0
0 1 0 1 1 0 1 0 1
0 1 0 1 1 0 0 1 1
0 1 0 1 0 1 1 1 0
0 1 0 1 0 1 1 0 1
0 1 0 1 0 1 0 1 1
0 1 0 1 0 0 1 1 1
0 1 0 0 1 1 1 1 0
0 1 0 0 1 1 1 0 1
0 1 0 0 1 1 0 1 1
0 1 0 0 1 0 1 1 1
0 1 0 0 0 1 1 1 1
0 0 1 1 1 1 1 0 0
0 0 1 1 1 1 0 1 0
0 0 1 1 1 1 0 0 1
0 0 1 1 1 0 1 1 0
0 0 1 1 1 0 1 0 1
0 0 1 1 1 0 0 1 1
0 0 1 1 0 1 1 1 0
0 0 1 1 0 1 1 0 1
0 0 1 1 0 1 0 1 1
0 0 1 1 0 0 1 1 1
0 0 1 0 1 1 1 1 0
0 0 1 0 1 1 1 0 1
0 0 1 0 1 1 0 1 1
0 0 1 0 1 0 1 1 1
0 0 1 0 0 1 1 1 1
0 0 0 1 1 1 1 1 0
0 0 0 1 1 1 1 0 1
0 0 0 1 1 1 0 1 1
0 0 0 1 1 0 1 1 1
0 0 0 1 0 1 1 1 1
0 0 0 0 1 1 1 1 1
std::cout << outmatr.size() << "\n";
126
unsigned int all_comb_iter(std::deque<bool> &x)
Returns the number of iterations to find the input boolean vector according to all_comb algorithm
Name | Definition |
---|---|
x | is the input boolean vector |
std::vector<bool> teste_dq = {1, 0, 1, 1, 1, 1, 0};
unsigned int out = all_comb_iter(teste_dq);
11
unsigned int all_comb_iterdq(std::deque<bool> &x)
Returns the number of iterations to find the input boolean deque according to all_comb algorithm
Name | Definition |
---|---|
x | is the input boolean deque |
std::deque<bool> teste_dq = {1, 0, 1, 1, 1, 1, 0};
unsigned int out = all_comb_iterdq(teste_dq);
11
std::vector<bool> bool_gen(unsigned int &k, unsigned int &n, double seed = 0)
Returns a boolean vector of size n, with k elements equal to 1
Name | Definition |
---|---|
k | is the number of elements that should equal to 1 |
n | is the size of the vector |
seed | 0, if the vector should be randomly generated, strictly positive values either |
unsigned int k = 5;
unsigned int n = 17;
std::vector<bool> out = bool_gen(k, n, 0);
print_nvec(out);
:0: 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1
std::deque<bool> int_to_binarydq(unsigned int x)
Converts an unsigned int to a binary format as a boolean deque
Name | Definition |
---|---|
x | is the input unsigned int |
std::deque<bool> rtn_dq = int_to_binarydq(1286);
1 0 1 0 0 0 0 0 1 1 0
unsigned int binarydq_to_int(std::deque<bool> &x)
Converts a binary format as a boolean deque to an unsigned int
Name | Definition |
---|---|
x | is the input boolean std deque |
std::deque<bool> dq_input = {1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0};
unsigned int out = binarydq_to_int(rtn_dq);
1286
unsigned int letter_to_nb(std::string &x)
Returns an int corresponding to the input letter.
This algorithm can be used to get which unique id number it is, if we know that it has been generated with nb_to_letter function
Name | Definition |
---|---|
x | is the input string |
std::string inpt_str = "ecfy";
unsigned int rtn_val = letter_to_nb(inpt_str);
std::cout << rtn_val << "\n";
90089
inpt_str = "ajf";
rtn_val = letter_to_nb(inpt_str);
std::cout << rtn_val << "\n";
942
std::string nb_to_letter(unsigned int &x)
Returns a unique combination of letters based on an input number, see examples.
Name | Definition |
---|---|
x | is the input number |
unsigned int inpt_val = 702;
std::string out_val = nb_to_letter(inpt_val);
std::cout << out_val << "\n";
"zz"
unsigned int inpt_val = 104601;
std::string out_val = nb_to_letter(inpt_val);
std::cout << out_val << "\n";
"exsc"