-
INTRODUCTION
- Commun functions
- On elements
- Standard operations mod
- String to int, float, double si
- Int, double, to string
- On std::vector<Type>
- Statistical functions sum
- Uniform distribution dunif
- Normal distribution rnorm
- Binomial dbinom
- Poisson dpois
- Exponential distribution dexp
- Cauchy dcauchy
- Gamma distribution dgamma
- Beta distribution dbeta
- Chi Square distribution dchisq
- Geometric distributions dgeom
- Min - Max min
- Mixing
- Heuristic (slightly slower) mixout
- Deterministic (slightly faster) mixoutd
- Parts getpart
- Add parts to existing stl vector partout
- Absolute values abs_vin
- Match matchl
- Grep grep
- RegEx regex_findr
- Unique unique
- Reverse reverse_out
- Repetition of elements rep
- Sequence/Range of elements seq
- Comparisons to booleans comp2
- Variadic / Indefinite number of arguments - Compv Class Compv.to_comp()
- Lower lowercomp2
- Greater greatercomp2
- Any - All any
- Sorting algorithms sort_descin
- Remove range of elements rm_ordered
- Sets (Union - Diff - Removing shared elements) union2
- Variadic / Indefinite number of arguments - Unionv Class Unionv.to_union()
- Variadic / Indefinite number of arguments - Intersectv Class Intersectv.to_intersect()
- Variadic / Indefinite number of arguments - Diffv Class Diffv.to_diff()
- Variadic / Indefinite number of arguments - Rm_sharedv Class Rm_sharedv.to_rm()
- Finding closest elements in stl vector closest_idx
- String and vectors conversions
- Collapse (vector to string) ncollapse
- Split (string to vector) split
- Others pct_to_idx
- Operations on matrices like 2d vectors std::vector<std::vector<Type>>
- Sum elements for each rows and columns sum_nxp
- Transposition t
- Absolute values abs_matrin
- Fulgurance Tools Parser_tokenizer_full
- Binary conversions int_to_binarydq
int_lngth
roundout
roundin
randint
logn
Facto
Comb
sf
sf2
stod
Mean
quantile
med
cor
Sd
punif
qunif
runif
rnorm2
qnorm1
qnorm2
dnorm
pnorm
pbinom
qbinom
rbinom
ppois
qpois
rpois
pexp
qexp
rexp
pcauchy
qcauchy
rcauchy
pgamma
qgamma
rgamma
pbeta
qbeta
rbeta
pchisq
qchisq
rchisq
test_chisq_fit
test_chisq_independance
pgeom
qgeom
rgeom
dhyper
max
mixin
mixind
print_svec
partin
abs_vout
abs_voutb
match
match_max
match_min
grepl
reverse_in
reverse_out_standard
all
sort_ascin
sort_descout
sort_ascout
rm_unordered
intersect2
diff2
rm_shared_in
rm_shared_out
scollapse
diff_mean
t_in_square
t_in
abs_matrout
is_symetric
all_comb
all_comb_iter
all_comb_iterdq
bool_gen
binarydq_to_int
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.
Matrices and dataframes implementation are classes. All functions that will transform 'voidly' (internaly) the relative data are built in the classes. All functions that copy and transform the relative data are extern to classes.
Also, all the functions relative to matrices classes exist for more standard type of matrices that is 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"
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 x = {150, 190, 400};
int n_trials = 555;
std::vector out = dhyper(x, n_ones, n_others, n_trials);
print_nvec(out);
:0: 0.00807227 1.69452e-11 4.42674e-236
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 < 20; ++i) { vec.push_back(7); }
print_nvec(vec);
:0: 12 2 4534 7 -78 12122 23323 12 6 2 8 45 7 7 7 7 7 7 7 7 7 7 7 7
:25: 7 7 7 7 7 7 7
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> vec = {"peugeot", "wolkswagen", "honda", "renault", "stellantis"};
for (int i = 0; i < 20; ++i) { vec.push_back("yesss"); }
print_svec(vec);
:0: peugeot wolkswagen honda renault stellantis yesss yesss yesss yesss yesss yesss yesss yesss yesss yesss yesss yesss
:18: yesss yesss yesss yesss yesss yesss 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}
std::map<std::vector<unsigned int>, std::map<bool, std::string>> regex_findr(std::string &searched, std::string &x)
A minimalist RegEx flavor.
- or context which is the set of elements that are inside []
, evaluates the expression from left to right
- range elements matches every elements that are between x-y
acording to the ASCII table
- repetition is the number of times a set of elements have to be matched, this is declared inside {n}
after the set of elements
- greedyness allows to match a given number of times a set of elements or more, this is declared by {+n}
after the set of elements
- \\
is used to escape special characters, apart when it is in a range context, so \\-x
or x-\\
are valid
Name | Definition | ||
---|---|---|---|
searched | is the RegEx expression | ||
x | is the input std | string |
std::string inpt_str = "uouuupeieeeppppiimi";
std::string searched = "[u{1}p{2}]{2}ii[a-em]";
std::map<std::vector<unsigned int>, std::map<bool, std::string>> outmp = regex_findr(searched, inpt_str);
std::map<std::vector<unsigned int>, std::map<bool, std::string>>::iterator it = outmp.begin();
std::vector<unsigned int> vec1 = it->first;
std::map<bool, std::string>::iterator it2b = it->second.begin();
std::cout << vec1[0] << "\n";
std::cout << vec1[1] << "\n";
std::cout << it2b->first << "\n";
std::cout << it2b->second << "\n";
11
17
1
ppppiim
std::string inpt_str = "uouuupeieeeppppiimi";
std::string searched = "[u{1}p{2}]{+1}ii[a-em]";
std::map<std::vector<unsigned int>, std::map<bool, std::string>> outmp = regex_findr(searched, inpt_str);
std::map<std::vector<unsigned int>, std::map<bool, std::string>>::iterator it = outmp.begin();
std::vector<unsigned int> vec1 = it->first;
std::map<bool, std::string>::iterator it2b = it->second.begin();
std::cout << vec1[0] << "\n";
std::cout << vec1[1] << "\n";
std::cout << it2b->first << "\n";
std::cout << it2b->second << "\n";
11
17
1
ppppiim
std::string inpt_str = "uouuupeieeeppppiimi";
std::string searched = "e{+1}p{2}";
std::map<std::vector<unsigned int>, std::map<bool, std::string>> outmp = regex_findr(searched, inpt_str);
std::map<std::vector<unsigned int>, std::map<bool, std::string>>::iterator it = outmp.begin();
std::vector<unsigned int> vec1 = it->first;
std::map<bool, std::string>::iterator it2b = it->second.begin();
std::cout << vec1[0] << "\n";
std::cout << vec1[1] << "\n";
std::cout << it2b->first << "\n";
std::cout << it2b->second << "\n";
8
12
1
eeepp
std::string inpt_str = "uouuupeieeeppppiimi";
std::string searched = "[a-ia-z]{+1}";
std::map<std::vector<unsigned int>, std::map<bool, std::string>> outmp = regex_findr(searched, inpt_str);
std::map<std::vector<unsigned int>, std::map<bool, std::string>>::iterator it = outmp.begin();
std::vector<unsigned int> vec1 = it->first;
std::map<bool, std::string>::iterator it2b = it->second.begin();
std::cout << vec1[0] << "\n";
std::cout << vec1[1] << "\n";
std::cout << it2b->first << "\n";
std::cout << it2b->second << "\n";
6
10
1
eieee
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
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}
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
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"}
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
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_matr(const std::vector<std::vector<T>> &x)
Print a matrix as 2D stl vector.
Name | Definition |
---|---|
x | is a 2D stl vector |
std::vector<std::vector<int>> matr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print_matr(matr);
[0] [1] [2]
:0: 1 4 7
:1: 2 5 8
:2: 3 6 9
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
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}
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 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