5 #ifndef BPP_NUMERIC_VECTORTOOLS_H 6 #define BPP_NUMERIC_VECTORTOOLS_H 9 #include "../App/ApplicationTools.h" 10 #include "../Io/OutputStream.h" 26 typedef std::vector<std::complex<double>>
Vcomplex;
30 typedef std::vector<std::complex<long double>>
Vlcomplex;
44 typedef std::vector<int>
Vint;
45 typedef std::vector<Vint>
VVint;
49 typedef std::vector<unsigned int>
Vuint;
59 std::vector<T>
operator+(
const std::vector<T>& v1,
const std::vector<T>& v2)
62 if (v1.size() != v2.size())
70 std::vector<T> result(size);
71 for (
size_t i = 0; i < size; i++)
73 result[i] = v1[i] + v2[i];
79 std::vector<T>
operator-(
const std::vector<T>& v1,
const std::vector<T>& v2)
82 if (v1.size() != v2.size())
90 std::vector<T> result(size);
91 for (
size_t i = 0; i < size; i++)
93 result[i] = v1[i] - v2[i];
99 std::vector<T>
operator*(
const std::vector<T>& v1,
const std::vector<T>& v2)
102 if (v1.size() != v2.size())
110 std::vector<T> result(size);
111 for (
size_t i = 0; i < size; i++)
113 result[i] = v1[i] * v2[i];
119 std::vector<T>
operator/(
const std::vector<T>& v1,
const std::vector<T>& v2)
122 if (v1.size() != v2.size())
130 std::vector<T> result(size);
131 for (
size_t i = 0; i < size; i++)
133 result[i] = v1[i] / v2[i];
139 template<
class T,
class C>
140 std::vector<T>
operator+(
const std::vector<T>& v1,
const C& c)
142 std::vector<T> result(v1.size());
143 for (
size_t i = 0; i < result.size(); i++)
145 result[i] = v1[i] + T(c);
149 template<
class T,
class C>
150 std::vector<T>
operator+(
const C& c,
const std::vector<T>& v1)
152 std::vector<T> result(v1.size());
153 for (
size_t i = 0; i < result.size(); i++)
155 result[i] = T(c) + v1[i];
160 template<
class T,
class C>
161 std::vector<T>
operator-(
const std::vector<T>& v1,
const C& c)
163 std::vector<T> result(v1.size());
164 for (
size_t i = 0; i < result.size(); i++)
166 result[i] = v1[i] - T(c);
170 template<
class T,
class C>
171 std::vector<T>
operator-(
const C& c,
const std::vector<T>& v1)
173 std::vector<T> result(v1.size());
174 for (
size_t i = 0; i < result.size(); i++)
176 result[i] = T(c) - v1[i];
181 template<
class T,
class C>
182 std::vector<T>
operator*(
const std::vector<T>& v1,
const C& c)
184 std::vector<T> result(v1.size());
185 for (
size_t i = 0; i < result.size(); i++)
187 result[i] = v1[i] * c;
191 template<
class T,
class C>
192 std::vector<T>
operator*(
const C& c,
const std::vector<T>& v1)
194 std::vector<T> result(v1.size());
195 for (
size_t i = 0; i < result.size(); i++)
197 result[i] = c * v1[i];
202 template<
class T,
class C>
203 std::vector<T>
operator/(
const std::vector<T>& v1,
const C& c)
205 std::vector<T> result(v1.size());
206 for (
size_t i = 0; i < result.size(); i++)
208 result[i] = v1[i] / c;
212 template<
class T,
class C>
213 std::vector<T>
operator/(
const C& c,
const std::vector<T>& v1)
215 std::vector<T> result(v1.size());
216 for (
size_t i = 0; i < result.size(); i++)
218 result[i] = c / v1[i];
224 void operator+=(std::vector<T>& v1,
const std::vector<T>& v2)
226 for (
size_t i = 0; i < v1.size(); i++)
233 void operator-=(std::vector<T>& v1,
const std::vector<T>& v2)
235 for (
size_t i = 0; i < v1.size(); i++)
242 void operator*=(std::vector<T>& v1,
const std::vector<T>& v2)
244 for (
size_t i = 0; i < v1.size(); i++)
251 void operator/=(std::vector<T>& v1,
const std::vector<T>& v2)
253 for (
size_t i = 0; i < v1.size(); i++)
259 template<
class T,
class C>
268 template<
class T,
class C>
277 template<
class T,
class C>
286 template<
class T,
class C>
295 template<
class T,
class C>
320 static void resize2(VVdouble& vv,
size_t n1,
size_t n2)
323 for (
auto& x : vv) { x.resize(n2); }
326 static void resize3(VVVdouble& vvv,
size_t n1,
size_t n2,
size_t n3)
339 static void resize4(VVVVdouble& vvvv,
size_t n1,
size_t n2,
size_t n3,
size_t n4)
342 for (
auto& vvv : vvvv)
359 static void fill(std::vector<T>& v, T value)
361 std::fill(v.begin(), v.end(), value);
377 static std::vector<T>
seq(T from, T
to, T by)
379 std::vector<T> v((
size_t)((std::abs(from - to) + by / 100) / by) + 1);
380 T step = from < to ? by : -by;
381 T val(from < to ? from : to);
404 for (
size_t i = 0; i < v.size(); i++)
406 if (v[i] == which)
return i;
422 static std::vector<size_t>
whichAll(
const std::vector<T>& v,
const T&
which)
424 std::vector<size_t> w;
425 for (
size_t i = 0; i < v.size(); i++)
427 if (v[i] == which) w.push_back(i);
446 static std::vector<T>
unique(
const std::vector<T>& v)
448 if (v.size() == 0)
return v;
449 std::vector<T> sortedV(v.begin(), v.end());
450 sort(sortedV.begin(), sortedV.end());
452 uniq.push_back(sortedV[0]);
453 for (
size_t i = 1; i < sortedV.size(); i++)
455 if (sortedV[i] != sortedV[i - 1]) uniq.push_back(sortedV[i]);
473 if (v.size() == 0)
return true;
474 std::vector<T> sortedV(v.begin(), v.end());
475 sort(sortedV.begin(), sortedV.end());
476 for (
size_t i = 1; i < sortedV.size(); i++)
478 if (sortedV[i] == sortedV[i - 1])
return false;
493 static std::vector<T>
extract(
const std::vector<T>& v1,
const std::vector<size_t>& v2)
495 std::vector<T> v(v2.size());
496 for (
size_t i = 0; i < v2.size(); ++i)
512 std::map<T, size_t> c;
530 static std::vector<double>
breaks(
const std::vector<double>& v,
unsigned int n);
548 double h = 3.5 * VectorTools::sd<T, double>(v) * std::pow(n, -1. / 3);
549 return (
size_t) ceil(r / h);
557 static T
prod(
const std::vector<T>& v1)
573 static std::vector<T>
cumProd(
const std::vector<T>& v1)
575 std::vector<T> p(v1.size());
576 if (v1.size() == 0)
return p;
578 for (
size_t i = 1; i < v1.size(); i++) { p[i] = v1[i] * p[i - 1]; }
587 static T
sum(
const std::vector<T>& v1)
590 for (
const auto& x : v1) { s += x; }
601 static T
sumProd(
const std::vector<T>& v1,
const std::vector<T>& v2)
604 if (v1.size() != v2.size())
610 for (
size_t i = 1; i < size; i++)
624 static std::vector<T>
cumSum(
const std::vector<T>& v1)
627 std::partial_sum(s.begin(), s.end(), s.begin());
658 T x = std::accumulate(std::next(v1.begin()), v1.end(), std::exp(v1[0] - M),
660 return y + std::exp(z - M);
663 return std::log(x) + M;
673 static T
logSumExp(
const std::vector<T>& v1,
const std::vector<T>& v2)
675 if (v1.size() != v2.size())
678 size_t size = v1.size();
684 T x = v2[0] * std::exp(v1[0] - M);
685 for (
size_t i = 1; i < size; i++)
687 x += v2[i] * std::exp(v1[i] - M);
689 return std::log(x) + M;
710 static T
sumExp(
const std::vector<T>& v1)
713 return std::exp(v1[0]);
717 return M < 0 ? 0 : M;
719 T x = std::accumulate(std::next(v1.begin()), v1.end(), std::exp(v1[0] - M),
721 return y + std::exp(z - M);
723 return x * std::exp(M);
733 static T
sumExp(
const std::vector<T>& v1,
const std::vector<T>& v2)
735 if (v1.size() != v2.size())
738 size_t size = v1.size();
741 return v2[0] * std::exp(v1[0]);
747 T x = v2[0] * std::exp(v1[0] - M);
748 for (
size_t i = 1; i < size; i++)
750 x += v2[i] * std::exp(v1[i] - M);
752 return x * std::exp(M);
762 static std::vector<T>
log(
const std::vector<T>& v1)
764 std::vector<T> v2(v1.size());
765 for (
size_t i = 0; i < v2.size(); i++)
773 static double log(
const T& x)
779 static std::vector<double>
log(
const std::vector<T>& v1,
double base)
781 std::vector<double> v2(v1.size());
782 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]) / std::log(base); }
787 static double exp(
const T& x)
793 static std::vector<T>
exp(
const std::vector<T>& v1)
795 std::vector<T> v2(v1.size());
801 static std::vector<double>
cos(
const std::vector<T>& v1)
803 std::vector<double> v2(v1.size());
804 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::cos(v1[i]); }
809 static std::vector<double>
sin(
const std::vector<T>& v1)
811 std::vector<double> v2(v1.size());
812 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::sin(v1[i]); }
817 static std::vector<double>
log10(
const std::vector<T>& v1)
819 std::vector<double> v2(v1.size());
820 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = std::log10(v1[i]); }
825 static std::vector<T>
fact(
const std::vector<T>& v1)
827 std::vector<T> v2(v1.size());
828 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::fact<T>(v1[i]); }
833 static std::vector<T>
sqr(
const std::vector<T>& v1)
835 std::vector<T> v2(v1.size());
836 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::sqr<T>(v1[i]); }
841 static std::vector<T>
pow(
const std::vector<T>& v1, T& b)
843 std::vector<T> v2(v1.size());
844 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = std::pow(v1[i], b); }
856 static std::string
paste(
const std::vector<T>& v,
const std::string& delim =
" ")
858 std::ostringstream out;
860 for (
size_t i = 0; i < v.size(); i++)
863 if (i < v.size() - 1)
878 for (
size_t i = 0; i < v1.size(); i++)
881 if (i < v1.size() - 1)
890 size_t vs = v1.size();
892 for (
size_t i = 0; i < vs; i++)
898 if (v1[j] == v1[j - 1] + 1)
904 out << rangeDelim << v1[j - 1];
919 static void printForR(
const std::vector<T>& v1, std::string variableName =
"x", std::ostream& out = std::cout)
922 out << variableName <<
"<-c(";
923 for (
size_t i = 0; i < v1.size(); i++)
926 if (i < v1.size() - 1)
929 out <<
")" << std::endl;
938 template<
class InputType,
class OutputType>
939 static OutputType
scalar(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2)
941 if (v1.size() != v2.size())
945 OutputType result = 0;
946 for (
size_t i = 0; i < v1.size(); i++)
948 result += v1[i] * v2[i];
968 template<
class InputType,
class OutputType>
969 static OutputType
scalar(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w)
971 if (v1.size() != w.size())
975 if (v2.size() != w.size())
979 OutputType result = 0;
980 for (
size_t i = 0; i < v1.size(); i++)
982 result += v1[i] * v2[i] * w[i];
994 static std::vector<T>
kroneckerMult(
const std::vector<T>& v1,
const std::vector<T>& v2)
996 size_t n1 = v1.size();
997 size_t n2 = v2.size();
998 std::vector<T> v3(n1 * n2);
999 for (
size_t i = 0; i < n1; i++)
1002 for (
size_t j = 0; j < n2; j++)
1004 v3[i * n2 + j] = v1i * v2[j];
1014 template<
class InputType,
class OutputType>
1015 static OutputType
norm(
const std::vector<InputType>& v1)
1017 OutputType result = 0;
1018 for (
size_t i = 0; i < v1.size(); i++)
1020 result += v1[i] * v1[i];
1022 return sqrt(result);
1032 template<
class InputType,
class OutputType>
1033 static OutputType
norm(
const std::vector<InputType>& v1,
const std::vector<InputType>& w)
1035 if (v1.size() != w.size())
1039 OutputType result = 0;
1040 for (
size_t i = 0; i < v1.size(); i++)
1042 result += v1[i] * v1[i] * w[i];
1044 return sqrt(result);
1053 template<
class InputType,
class OutputType>
1054 static OutputType
cos(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2)
1056 return scalar<InputType, OutputType>(v1, v2)
1057 / (norm<InputType, OutputType>(v1) * norm<InputType, OutputType>(v2));
1067 template<
class InputType,
class OutputType>
1068 static OutputType
cos(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w)
1070 return scalar<InputType, OutputType>(v1, v2, w)
1071 / (norm<InputType, OutputType>(v1, w) * norm<InputType, OutputType>(v2, w));
1090 static T
min(
const std::vector<T>& v)
1094 for (
size_t i = 1; i < v.size(); i++)
1096 if (v[i] < mini) mini = v[i];
1111 static T
max(
const std::vector<T>& v)
1115 for (
size_t i = 1; i < v.size(); i++)
1117 if (v[i] > maxi) maxi = v[i];
1138 for (
size_t i = 1; i < v.size(); i++)
1165 for (
size_t i = 1; i < v.size(); i++)
1191 std::vector<size_t> pos;
1192 for (
size_t i = 0; i < v.size(); i++)
1217 std::vector<size_t> pos;
1218 for (
size_t i = 0; i < v.size(); i++)
1239 static std::vector<T>
range(
const std::vector<T>& v)
1243 std::vector<T> r(2);
1245 for (
size_t i = 1; i < v.size(); i++)
1247 if (v[i] < r[0]) r[0] = v[i];
1248 if (v[i] > r[1]) r[1] = v[i];
1260 bool operator()(
size_t a,
size_t b) {
return values_[a] < values_[b]; }
1274 static std::vector<size_t>
order(
const std::vector<T>& v)
1279 std::vector<size_t> index(v.size());
1280 for (
size_t i = 0; i < index.size(); ++i)
1298 static std::vector<T>
abs(
const std::vector<T>& v)
1300 std::vector<T> vabs(v.size());
1301 for (
size_t i = 0; i < v.size(); i++)
1303 vabs[i] = std::abs(v[i]);
1312 template<
class InputType,
class OutputType>
1313 static OutputType
mean(
const std::vector<InputType>& v1)
1315 return (OutputType)sum<InputType>(v1) / (OutputType)v1.size();
1323 template<
class InputType,
class OutputType>
1324 static OutputType
mean(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool normalizeWeights =
true)
1326 if (normalizeWeights)
1328 std::vector<InputType> wn = w /
sum(w);
1329 return scalar<InputType, OutputType>(v1, wn);
1333 return scalar<InputType, OutputType>(v1, w);
1341 template<
class InputType>
1342 static InputType
median(std::vector<InputType>& v1)
1345 if (v1.size() == 0)
return med;
1346 if (v1.size() == 1)
return v1[0];
1347 sort(v1.begin(), v1.end());
1348 size_t i = v1.size() / 2;
1349 if (v1.size() % 2 == 0)
1352 med = double((v1[i - 1] + v1[i]) / 2);
1368 template<
class InputType,
class OutputType>
1369 static std::vector<OutputType>
center(
const std::vector<InputType>& v1)
1371 OutputType m = mean<InputType, OutputType>(v1);
1372 std::vector<OutputType> v(v1.size());
1373 for (
size_t i = 0; i < v1.size(); i++)
1375 v[i] = (OutputType)v1[i] - m;
1387 template<
class InputType,
class OutputType>
1388 static std::vector<OutputType>
center(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool normalizeWeights =
true)
1390 OutputType m = mean<InputType, OutputType>(v1, w, normalizeWeights);
1391 std::vector<OutputType> v(v1.size());
1392 for (
size_t i = 0; i < v1.size(); i++)
1394 v[i] = (OutputType)v1[i] - m;
1406 template<
class InputType,
class OutputType>
1407 static OutputType
cov(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
bool unbiased =
true)
1409 OutputType n = (OutputType)v1.size();
1410 OutputType x = scalar<InputType, OutputType>(
1411 center<InputType, OutputType>(v1),
1412 center<InputType, OutputType>(v2)
1414 if (unbiased) x = x * n / (n - 1);
1428 template<
class InputType,
class OutputType>
1429 static OutputType
cov(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w,
bool unbiased =
true,
bool normalizeWeights =
true)
1431 if (normalizeWeights)
1433 std::vector<InputType> wn = w /
sum(w);
1434 OutputType x = scalar<InputType, OutputType>(
1435 center<InputType, OutputType>(v1, wn,
false),
1436 center<InputType, OutputType>(v2, wn,
false),
1441 x = x / (1 -
sum(sqr<double>(wn)));
1447 OutputType x = scalar<InputType, OutputType>(
1448 center<InputType, OutputType>(v1, w,
false),
1449 center<InputType, OutputType>(v2, w,
false),
1454 x = x / (1 -
sum(
sqr(w)));
1464 template<
class InputType,
class OutputType>
1465 static OutputType
var(
const std::vector<InputType>& v1,
bool unbiased =
true)
1467 return cov<InputType, OutputType>(v1, v1, unbiased);
1477 template<
class InputType,
class OutputType>
1478 static OutputType
var(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool unbiased =
true,
bool normalizeWeights =
true)
1480 return cov<InputType, OutputType>(v1, v1, w, unbiased, normalizeWeights);
1488 template<
class InputType,
class OutputType>
1489 static OutputType
sd(
const std::vector<InputType>& v1,
bool unbiased =
true)
1491 return sqrt(var<InputType, OutputType>(v1, unbiased));
1502 template<
class InputType,
class OutputType>
1503 static OutputType
sd(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool unbiased =
true,
bool normalizeWeights =
true)
1505 return sqrt(var<InputType, OutputType>(v1, w, unbiased, normalizeWeights));
1514 template<
class InputType,
class OutputType>
1515 static OutputType
cor(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2)
1517 return cov<InputType, OutputType>(v1, v2)
1518 / ( sd<InputType, OutputType>(v1) * sd<InputType, OutputType>(v2) );
1529 template<
class InputType,
class OutputType>
1530 static OutputType
cor(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w,
bool normalizeWeights =
true)
1532 if (normalizeWeights)
1534 std::vector<InputType> wn = w /
sum(w);
1535 return cov<InputType, OutputType>(v1, v2, wn,
false,
false)
1536 / ( sd<InputType, OutputType>(v1, wn,
false,
false) * sd<InputType, OutputType>(v2, wn,
false,
false) );
1540 return cov<InputType, OutputType>(v1, v2, w,
false,
false)
1541 / ( sd<InputType, OutputType>(v1, w,
false,
false) * sd<InputType, OutputType>(v2, w,
false,
false) );
1559 template<
class InputType,
class OutputType>
1560 static OutputType
shannon(
const std::vector<InputType>& v,
double base = 2.7182818)
1566 s +=
static_cast<OutputType
>(x * std::log(x) / std::log(base));
1585 template<
class InputType,
class OutputType>
1586 static OutputType
shannonDiscrete(
const std::vector<InputType>& v,
double base = 2.7182818)
1588 std::map<InputType, double> counts;
1594 double n =
static_cast<double>(v.size());
1595 for (
auto& it : counts)
1597 s +=
static_cast<OutputType
>((it.second / n) * std::log(it.second / n) / std::log(base));
1617 template<
class InputType,
class OutputType>
1618 static OutputType
miDiscrete(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
double base = 2.7182818)
1620 if (v1.size() != v2.size())
1621 throw DimensionException(
"VectorTools::miDiscrete. The two samples must have the same length.", v2.size(), v1.size());
1622 std::map<InputType, double> counts1;
1623 std::map<InputType, double> counts2;
1624 std::map<InputType, std::map<InputType, double>> counts12;
1625 for (
size_t i = 0; i < v1.size(); i++)
1629 counts12[v1[i]][v2[i]]++;
1632 double n =
static_cast<double>(v1.size());
1633 for (
auto& it1 : counts12)
1635 for (
auto& it2 : it1.second)
1637 s +=
static_cast<OutputType
>((it2.second / n) * std::log(it2.second * n / (counts1[it1.first] * counts2[it2.first])) / std::log(base));
1658 template<
class InputType,
class OutputType>
1662 for (
size_t i = 0; i < v.size(); i++)
1668 std::vector<double> x(1);
1671 x[0] =
static_cast<double>(it);
1672 s +=
static_cast<OutputType
>(std::log(kd.
kDensity(x)) / std::log(base));
1674 return -s /
static_cast<double>(v.size());
1696 template<
class InputType,
class OutputType>
1697 static OutputType
miContinuous(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
double base = 2.7182818)
1699 if (v1.size() != v2.size())
1700 throw DimensionException(
"VectorTools::miContinuous. The two samples must have the same length.", v2.size(), v1.size());
1704 for (
size_t i = 0; i < v1.size(); i++)
1706 m1(0, i) = m12(0, i) = v1[i];
1707 m2(0, i) = m12(1, i) = v2[i];
1713 std::vector<double> x1(1);
1714 std::vector<double> x2(1);
1715 std::vector<double> x12(2);
1716 for (
size_t i = 0; i < v1.size(); i++)
1718 x1[0] = x12[0] =
static_cast<double>(v1[i]);
1719 x2[0] = x12[1] =
static_cast<double>(v2[i]);
1720 s +=
static_cast<OutputType
>(std::log(kd12.
kDensity(x12) / (kd1.
kDensity(x1) * kd2.
kDensity(x2))) / std::log(base));
1722 return s /
static_cast<double>(v1.size());
1733 std::vector<T> u1(v1);
1734 std::vector<T> u2(v2);
1735 if (u1.size() != u2.size())
return false;
1736 std::sort(u1.begin(), u1.end());
1737 std::sort(u2.begin(), u2.end());
1752 if (v1.size() != v2.size())
return false;
1753 std::sort(v1.begin(), v1.end());
1754 std::sort(v2.begin(), v2.end());
1768 if (it == el)
return true;
1773 template<
class T,
class U>
1778 if (it == (T)el)
return true;
1794 std::sort(v1.begin(), v1.end());
1795 std::sort(v2.begin(), v2.end());
1797 for (
size_t i = 0; i < v2.size(); i++)
1799 if (i > 0 && v2[i] == v2[i - 1])
continue;
1800 while (j < v1.size() - 1 && v1[j] < v2[i]) j++;
1801 if (v1[j] != v2[i])
return false;
1813 static std::vector<T>
vectorUnion(
const std::vector<T>& vec1,
const std::vector<T>& vec2)
1815 std::vector<T> unionEl = vec1;
1816 for (
auto it : vec2)
1819 unionEl.push_back(it);
1830 static std::vector<T>
vectorUnion(
const std::vector< std::vector<T>>& vecElementL)
1832 std::vector<T> unionEl;
1833 for (
auto it : vecElementL)
1838 unionEl.push_back(it2);
1852 std::vector<T> interEl;
1853 for (
auto it : vec1)
1855 if (
contains(vec2, it)) interEl.push_back(it);
1860 template<
class T,
class U>
1863 std::vector<T> interEl;
1864 for (
auto it : vec1)
1866 if (
contains(vec2, it)) interEl.push_back(it);
1878 if (vecElementL.size() == 1)
return vecElementL[0];
1879 std::vector<T> interEl;
1880 if (vecElementL.size() == 0)
return interEl;
1881 for (
auto it : vecElementL[0])
1884 for (
size_t j = 1; test && j < vecElementL.size(); j++)
1886 if (!
contains(vecElementL[j], it)) test =
false;
1888 if (test) interEl.push_back(it);
1899 static void append(std::vector<T>& vec1,
const std::vector<T>& vec2)
1901 vec1.insert(vec1.end(), vec2.begin(), vec2.end());
1910 static void prepend(std::vector<T>& vec1,
const std::vector<T>& vec2)
1912 vec1.insert(vec1.begin(), vec2.begin(), vec2.end());
1921 static std::vector<T>
append(
const std::vector< std::vector<T>>& vecElementL)
1923 if (vecElementL.size() == 1)
return vecElementL[0];
1925 if (vecElementL.size() == 0)
return v;
1926 for (
auto it : vecElementL[0])
1939 static void extend(std::vector<T>& vec1,
const std::vector<T>& vec2)
1941 for (
auto it : vec2)
1954 static std::vector<T>
rep(
const std::vector<T>& vec,
size_t n)
1956 if (n == 1)
return vec;
1958 if (n == 0)
return v;
1959 v.resize(vec.size() * n);
1960 for (
size_t i = 0; i < v.size(); i++)
1962 v[i] = vec[i % vec.size()];
1977 static void diff(std::vector<T>& v1, std::vector<T>& v2, std::vector<T>& v3)
1979 if (v2.size() == 0)
append(v3, v1);
1980 std::sort(v1.begin(), v1.end());
1981 std::sort(v2.begin(), v2.end());
1983 for (
size_t i = 0; i < v1.size(); i++)
1985 if (i > 0 && v1[i] == v1[i - 1])
continue;
1986 while (j < v2.size() - 1 && v2[j] < v1[i]) j++;
1987 if (v2[j] != v1[i]) v3.push_back(v1[i]);
1998 #endif // BPP_NUMERIC_VECTORTOOLS_H void operator/=(std::vector< T > &v1, const std::vector< T > &v2)
Number exception: doubles.
T to(const std::string &s)
Template to string conversion.
std::vector< Vuint > VVuint
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< long double > Vldouble
Exception thrown when a given element was not found in the vector.
std::vector< VVlcomplex > VVVlcomplex
Matrix storage in one vector.
std::vector< VVldouble > VVVldouble
std::vector< VVcomplex > VVVcomplex
void operator+=(std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< VVint > VVVint
std::vector< std::complex< double > > Vcomplex
std::vector< VVVint > VVVVint
std::vector< Vcomplex > VVcomplex
std::vector< unsigned int > Vuint
void operator &=(std::vector< T > &v1, const C &c)
Exception thrown when an empty vector was found.
std::vector< Vint > VVint
std::vector< double > Vdouble
std::vector< T > operator/(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > operator*(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< std::complex< long double > > Vlcomplex
std::vector< VVVdouble > VVVVdouble
double kDensity(const std::vector< double > &x)
std::vector< VVVuint > VVVVuint
Density estimation using the adaptive kernel method.
std::vector< Vldouble > VVldouble
std::vector< VVuint > VVVuint
void operator-=(std::vector< T > &v1, const std::vector< T > &v2)
Exception thrown when a dimension problem occured.
std::vector< VVVldouble > VVVVldouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble
void operator*=(std::vector< T > &v1, const std::vector< T > &v2)
std::vector< Vlcomplex > VVlcomplex