40 #ifndef _VECTORTOOLS_H_
41 #define _VECTORTOOLS_H_
47 #include "../Io/OutputStream.h"
48 #include "../App/ApplicationTools.h"
59 typedef std::vector<std::complex<double> >
Vcomplex;
63 typedef std::vector<std::complex<long double> >
Vlcomplex;
77 typedef std::vector<int>
Vint;
78 typedef std::vector<Vint>
VVint;
91 if (v1.size() != v2.size())
99 std::vector<T> result(size);
100 for (
size_t i = 0; i < size; i++)
102 result[i] = v1[i] + v2[i];
111 if (v1.size() != v2.size())
119 std::vector<T> result(size);
120 for (
size_t i = 0; i < size; i++)
122 result[i] = v1[i] - v2[i];
131 if (v1.size() != v2.size())
139 std::vector<T> result(size);
140 for (
size_t i = 0; i < size; i++)
142 result[i] = v1[i] * v2[i];
151 if (v1.size() != v2.size())
159 std::vector<T> result(size);
160 for (
size_t i = 0; i < size; i++)
162 result[i] = v1[i] / v2[i];
168 template<
class T,
class C>
169 std::vector<T>
operator+(
const std::vector<T>& v1,
const C& c)
171 std::vector<T> result(v1.size());
172 for (
size_t i = 0; i < result.size(); i++)
174 result[i] = v1[i] + c;
178 template<
class T,
class C>
179 std::vector<T>
operator+(
const C& c,
const std::vector<T>& v1)
181 std::vector<T> result(v1.size());
182 for (
size_t i = 0; i < result.size(); i++)
184 result[i] = c + v1[i];
189 template<
class T,
class C>
190 std::vector<T>
operator-(
const std::vector<T>& v1,
const C& c)
192 std::vector<T> result(v1.size());
193 for (
size_t i = 0; i < result.size(); i++)
195 result[i] = v1[i] - c;
199 template<
class T,
class C>
200 std::vector<T>
operator-(
const C& c,
const std::vector<T>& v1)
202 std::vector<T> result(v1.size());
203 for (
size_t i = 0; i < result.size(); i++)
205 result[i] = c - v1[i];
210 template<
class T,
class C>
211 std::vector<T>
operator*(
const std::vector<T>& v1,
const C& c)
213 std::vector<T> result(v1.size());
214 for (
size_t i = 0; i < result.size(); i++)
216 result[i] = v1[i] * c;
220 template<
class T,
class C>
221 std::vector<T>
operator*(
const C& c,
const std::vector<T>& v1)
223 std::vector<T> result(v1.size());
224 for (
size_t i = 0; i < result.size(); i++)
226 result[i] = c * v1[i];
231 template<
class T,
class C>
232 std::vector<T>
operator/(
const std::vector<T>& v1,
const C& c)
234 std::vector<T> result(v1.size());
235 for (
size_t i = 0; i < result.size(); i++)
237 result[i] = v1[i] / c;
241 template<
class T,
class C>
242 std::vector<T>
operator/(
const C& c,
const std::vector<T>& v1)
244 std::vector<T> result(v1.size());
245 for (
size_t i = 0; i < result.size(); i++)
247 result[i] = c / v1[i];
256 for (
size_t i = 0; i < v1.size(); i++)
265 for (
size_t i = 0; i < v1.size(); i++)
274 for (
size_t i = 0; i < v1.size(); i++)
283 for (
size_t i = 0; i < v1.size(); i++)
290 template<
class T,
class C>
293 for (
size_t i = 0; i < v1.size(); i++)
299 template<
class T,
class C>
302 for (
size_t i = 0; i < v1.size(); i++)
308 template<
class T,
class C>
311 for (
size_t i = 0; i < v1.size(); i++)
317 template<
class T,
class C>
320 for (
size_t i = 0; i < v1.size(); i++)
345 for (
size_t i = 0; i < n1; i++) { vv[i].resize(n2); }
352 for (
size_t i = 0; i < n1; i++)
355 for (
size_t j = 0; j < n2; j++)
357 vvv[i][j].resize(n3);
365 for (
size_t i = 0; i < n1; i++)
368 for (
size_t j = 0; j < n2; j++)
370 vvvv[i][j].resize(n3);
371 for (
size_t k = 0; k < n3; k++)
373 vvvv[i][j][k].resize(n4);
381 static void fill(std::vector<T>& v, T value)
383 for (
typename std::vector<T>::iterator it = v.begin(); it < v.end(); it++)
402 static std::vector<T>
seq(T from, T to, T by)
408 for (T i = from; i <= to + (by / 100); i += by)
415 for (T i = from; i >= to - (by / 100); i -= by)
436 for (
size_t i = 0; i < v.size(); i++)
438 if (v[i] ==
which)
return i;
456 std::vector<size_t> w;
457 for (
size_t i = 0; i < v.size(); i++)
459 if (v[i] ==
which) w.push_back(i);
478 static std::vector<T>
unique(
const std::vector<T>& v)
480 if (v.size() == 0)
return v;
481 std::vector<T> sortedV(v.begin(), v.end());
482 sort(sortedV.begin(), sortedV.end());
484 uniq.push_back(sortedV[0]);
485 for (
size_t i = 1; i < sortedV.size(); i++)
487 if (sortedV[i] != sortedV[i - 1]) uniq.push_back(sortedV[i]);
505 if (v.size() == 0)
return true;
506 std::vector<T> sortedV(v.begin(), v.end());
507 sort(sortedV.begin(), sortedV.end());
508 for (
size_t i = 1; i < sortedV.size(); i++)
510 if (sortedV[i] == sortedV[i - 1])
return false;
523 static std::vector<T>
extract(
const std::vector<T>& v1,
const std::vector<int>& v2)
525 std::vector<T> v(v2.size());
526 for (
size_t i = 0; i < v2.size(); i++)
542 std::map<T, size_t> c;
543 for (
size_t i = 0; i < v.size(); i++)
560 static std::vector<double>
breaks(
const std::vector<double>& v,
unsigned int n);
578 double h = 3.5 * VectorTools::sd<T, double>(v) * std::pow(n, -1. / 3);
579 return (
size_t) ceil(r / h);
587 static T
prod(
const std::vector<T>& v1)
590 for (
size_t i = 0; i < v1.size(); i++) { p *= v1[i]; }
600 static std::vector<T>
cumProd(
const std::vector<T>& v1)
602 std::vector<T> p(v1.size());
603 if (v1.size() == 0)
return p;
605 for (
size_t i = 1; i < v1.size(); i++) { p[i] = v1[i] * p[i - 1]; }
614 static T
sum(
const std::vector<T>& v1)
617 for (
size_t i = 0; i < v1.size(); i++) { s += v1[i]; }
627 static std::vector<T>
cumSum(
const std::vector<T>& v1)
629 std::vector<T> s(v1.size());
630 if (v1.size() == 0)
return s;
632 for (
size_t i = 1; i < v1.size(); i++) { s[i] = v1[i] + s[i - 1]; }
646 T x = std::exp(v[0] - M);
647 for (
size_t i = 1; i < v.size(); i++)
649 x += std::exp(v[i] - M);
651 v -= M + std::log(x);
663 T x = std::exp(v1[0] - M);
664 for (
size_t i = 1; i < v1.size(); i++)
666 x += std::exp(v1[i] - M);
668 return std::log(x) + M;
678 static T
logSumExp(
const std::vector<T>& v1,
const std::vector<T>& v2)
681 if (v1.size() != v2.size())
687 T x = v2[0] * std::exp(v1[0] - M);
688 for (
size_t i = 1; i < size; i++)
690 x += v2[i] * std::exp(v1[i] - M);
692 return std::log(x) + M;
704 T x = std::exp(v1[0] - M);
705 for (
size_t i = 1; i < v1.size(); i++)
707 x += std::exp(v1[i] - M);
709 return std::log(x) + M - std::log(v1.size());
719 static T
sumExp(
const std::vector<T>& v1)
722 T x = std::exp(v1[0] - M);
723 for (
size_t i = 1; i < v1.size(); i++)
725 x += std::exp(v1[i] - M);
727 return x * std::exp(M);
737 static T
sumExp(
const std::vector<T>& v1,
const std::vector<T>& v2)
740 if (v1.size() != v2.size())
746 T x = v2[0] * std::exp(v1[0] - M);
747 for (
size_t i = 1; i < size; i++)
749 x += v2[i] * std::exp(v1[i] - M);
751 return x * std::exp(M);
761 static std::vector<double>
log(
const std::vector<T>& v1)
763 std::vector<double> v2(v1.size());
764 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]); }
768 static std::vector<double>
log(
const std::vector<T>& v1,
double base)
770 std::vector<double> v2(v1.size());
771 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]) / std::log(base); }
776 static std::vector<double>
exp(
const std::vector<T>& v1)
778 std::vector<double> v2(v1.size());
779 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::exp(v1[i]); }
784 static std::vector<double>
cos(
const std::vector<T>& v1)
786 std::vector<double> v2(v1.size());
787 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::cos(v1[i]); }
792 static std::vector<double>
sin(
const std::vector<T>& v1)
794 std::vector<double> v2(v1.size());
795 for (
size_t i = 0; i < v2.size(); i++) { v2[i] = std::sin(v1[i]); }
800 static std::vector<double>
log10(
const std::vector<T>& v1)
802 std::vector<double> v2(v1.size());
803 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = std::log10(v1[i]); }
808 static std::vector<T>
fact(
const std::vector<T>& v1)
810 std::vector<T> v2(v1.size());
811 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::fact<T>(v1[i]); }
816 static std::vector<T>
sqr(
const std::vector<T>& v1)
818 std::vector<T> v2(v1.size());
819 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::sqr<T>(v1[i]); }
824 static std::vector<T>
pow(
const std::vector<T>& v1, T& b)
826 std::vector<T> v2(v1.size());
827 for (
size_t i = 0; i < v1.size(); i++) { v2[i] = std::pow(v1[i], b); }
839 static std::string
paste(
const std::vector<T>& v,
const std::string& delim =
" ")
841 std::ostringstream out;
842 for (
size_t i = 0; i < v.size(); i++)
845 if (i < v.size() - 1)
860 for (
size_t i = 0; i < v1.size(); i++)
863 if (i < v1.size() - 1)
876 static void printForR(
const std::vector<T>& v1, std::string variableName =
"x", std::ostream& out = std::cout)
879 out << variableName <<
"<-c(";
880 for (
size_t i = 0; i < v1.size(); i++)
883 if (i < v1.size() - 1)
886 out <<
")" << std::endl;
895 template<
class InputType,
class OutputType>
898 if (v1.size() != v2.size())
902 OutputType result = 0;
903 for (
size_t i = 0; i < v1.size(); i++)
905 result += v1[i] * v2[i];
925 template<
class InputType,
class OutputType>
926 static OutputType
scalar(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w)
throw (
DimensionException)
928 if (v1.size() != w.size())
932 if (v2.size() != w.size())
936 OutputType result = 0;
937 for (
size_t i = 0; i < v1.size(); i++)
939 result += v1[i] * v2[i] * w[i];
953 size_t n1 = v1.size();
954 size_t n2 = v2.size();
955 std::vector<T> v3(n1 * n2);
956 for (
size_t i = 0; i < n1; i++)
959 for (
size_t j = 0; j < n2; j++)
961 v3[i * n2 + j] = v1i * v2[j];
971 template<
class InputType,
class OutputType>
972 static OutputType
norm(
const std::vector<InputType>& v1)
974 OutputType result = 0;
975 for (
size_t i = 0; i < v1.size(); i++)
977 result += v1[i] * v1[i];
989 template<
class InputType,
class OutputType>
992 if (v1.size() != w.size())
996 OutputType result = 0;
997 for (
size_t i = 0; i < v1.size(); i++)
999 result += v1[i] * v1[i] * w[i];
1001 return sqrt(result);
1010 template<
class InputType,
class OutputType>
1013 return scalar<InputType, OutputType>(v1, v2)
1014 / (norm<InputType, OutputType>(v1) * norm<InputType, OutputType>(v2));
1024 template<
class InputType,
class OutputType>
1025 static OutputType
cos(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w)
throw (
DimensionException)
1027 return scalar<InputType, OutputType>(v1, v2, w)
1028 / (norm<InputType, OutputType>(v1, w) * norm<InputType, OutputType>(v2, w));
1051 for (
size_t i = 1; i < v.size(); i++)
1053 if (v[i] < mini) mini = v[i];
1072 for (
size_t i = 1; i < v.size(); i++)
1074 if (v[i] > maxi) maxi = v[i];
1095 for (
size_t i = 1; i < v.size(); i++)
1122 for (
size_t i = 1; i < v.size(); i++)
1148 std::vector<size_t> pos;
1149 for (
size_t i = 0; i < v.size(); i++)
1174 std::vector<size_t> pos;
1175 for (
size_t i = 0; i < v.size(); i++)
1200 std::vector<T> r(2);
1202 for (
size_t i = 1; i < v.size(); i++)
1204 if (v[i] < r[0]) r[0] = v[i];
1205 if (v[i] > r[1]) r[1] = v[i];
1236 std::vector<size_t> index(v.size());
1237 for (
size_t i = 0; i < index.size(); ++i)
1257 std::vector<T> vabs(v.size());
1258 for (
size_t i = 1; i < v.size(); i++)
1260 vabs[i] = std::abs(v[i]);
1269 template<
class InputType,
class OutputType>
1270 static OutputType
mean(
const std::vector<InputType>& v1)
1272 return (OutputType)sum<InputType>(v1) / (OutputType)v1.size();
1280 template<
class InputType,
class OutputType>
1281 static OutputType
mean(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool normalizeWeights =
true)
1283 if (normalizeWeights)
1285 std::vector<InputType> wn = w /
sum(w);
1286 return scalar<InputType, OutputType>(v1, wn);
1290 return scalar<InputType, OutputType>(v1, w);
1298 template<
class InputType>
1299 static InputType
median(std::vector<InputType>& v1)
1302 if (v1.size() == 0)
return med;
1303 if (v1.size() == 1)
return v1[0];
1304 sort(v1.begin(), v1.end());
1305 size_t i = v1.size() / 2;
1306 if (v1.size() % 2 == 0)
1309 med = double((v1[i - 1] + v1[i]) / 2);
1325 template<
class InputType,
class OutputType>
1326 static std::vector<OutputType>
center(
const std::vector<InputType>& v1)
1328 OutputType m = mean<InputType, OutputType>(v1);
1329 std::vector<OutputType> v(v1.size());
1330 for (
size_t i = 0; i < v1.size(); i++)
1332 v[i] = (OutputType)v1[i] - m;
1344 template<
class InputType,
class OutputType>
1345 static std::vector<OutputType>
center(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool normalizeWeights =
true)
1347 OutputType m = mean<InputType, OutputType>(v1, w, normalizeWeights);
1348 std::vector<OutputType> v(v1.size());
1349 for (
size_t i = 0; i < v1.size(); i++)
1351 v[i] = (OutputType)v1[i] - m;
1363 template<
class InputType,
class OutputType>
1364 static OutputType
cov(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
bool unbiased =
true) throw (
DimensionException)
1366 OutputType n = (OutputType)v1.size();
1367 OutputType x = scalar<InputType, OutputType>(
1368 center<InputType, OutputType>(v1),
1369 center<InputType, OutputType>(v2)
1371 if (unbiased) x = x * n / (n - 1);
1385 template<
class InputType,
class OutputType>
1386 static OutputType
cov(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w,
bool unbiased =
true,
bool normalizeWeights =
true) throw (
DimensionException)
1388 if (normalizeWeights)
1390 std::vector<InputType> wn = w /
sum(w);
1391 OutputType x = scalar<InputType, OutputType>(
1392 center<InputType, OutputType>(v1, wn,
false),
1393 center<InputType, OutputType>(v2, wn,
false),
1398 x = x / (1 -
sum(sqr<double>(wn)));
1404 OutputType x = scalar<InputType, OutputType>(
1405 center<InputType, OutputType>(v1, w,
false),
1406 center<InputType, OutputType>(v2, w,
false),
1411 x = x / (1 -
sum(
sqr(w)));
1421 template<
class InputType,
class OutputType>
1422 static OutputType
var(
const std::vector<InputType>& v1,
bool unbiased =
true)
1424 return cov<InputType, OutputType>(v1, v1, unbiased);
1434 template<
class InputType,
class OutputType>
1435 static OutputType
var(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool unbiased =
true,
bool normalizeWeights =
true) throw (
DimensionException)
1437 return cov<InputType, OutputType>(v1, v1, w, unbiased, normalizeWeights);
1445 template<
class InputType,
class OutputType>
1446 static OutputType
sd(
const std::vector<InputType>& v1,
bool unbiased =
true)
1448 return sqrt(var<InputType, OutputType>(v1, unbiased));
1459 template<
class InputType,
class OutputType>
1460 static OutputType
sd(
const std::vector<InputType>& v1,
const std::vector<InputType>& w,
bool unbiased =
true,
bool normalizeWeights =
true) throw (
DimensionException)
1462 return sqrt(var<InputType, OutputType>(v1, w, unbiased, normalizeWeights));
1471 template<
class InputType,
class OutputType>
1474 return cov<InputType, OutputType>(v1, v2)
1475 / ( sd<InputType, OutputType>(v1) * sd<InputType, OutputType>(v2) );
1486 template<
class InputType,
class OutputType>
1487 static OutputType
cor(
const std::vector<InputType>& v1,
const std::vector<InputType>& v2,
const std::vector<InputType>& w,
bool normalizeWeights =
true) throw (
DimensionException)
1489 if (normalizeWeights)
1491 std::vector<InputType> wn = w /
sum(w);
1492 return cov<InputType, OutputType>(v1, v2, wn,
false,
false)
1493 / ( sd<InputType, OutputType>(v1, wn,
false,
false) * sd<InputType, OutputType>(v2, wn,
false,
false) );
1497 return cov<InputType, OutputType>(v1, v2, w,
false,
false)
1498 / ( sd<InputType, OutputType>(v1, w,
false,
false) * sd<InputType, OutputType>(v2, w,
false,
false) );
1516 template<
class InputType,
class OutputType>
1517 static OutputType
shannon(
const std::vector<InputType>& v,
double base = 2.7182818)
1520 for (
size_t i = 0; i < v.size(); i++)
1522 if (v[i] > 0) s +=
static_cast<OutputType
>(v[i] * std::log(v[i]) / std::log(base));
1541 template<
class InputType,
class OutputType>
1542 static OutputType
shannonDiscrete(
const std::vector<InputType>& v,
double base = 2.7182818)
1544 std::map<InputType, double> counts;
1545 for (
size_t i = 0; i < v.size(); i++)
1550 double n =
static_cast<double>(v.size());
1551 for (
typename std::map<InputType, double>::iterator it = counts.begin(); it != counts.end(); it++)
1553 s +=
static_cast<OutputType
>((it->second / n) * std::log(it->second / n) / std::log(base));
1573 template<
class InputType,
class OutputType>
1576 if (v1.size() != v2.size())
1577 throw DimensionException(
"VectorTools::miDiscrete. The two samples must have the same length.", v2.size(), v1.size());
1578 std::map<InputType, double> counts1;
1579 std::map<InputType, double> counts2;
1580 std::map<InputType, std::map<InputType, double> > counts12;
1581 for (
size_t i = 0; i < v1.size(); i++)
1585 counts12[v1[i]][v2[i]]++;
1588 double n =
static_cast<double>(v1.size());
1589 for (
typename std::map<InputType, std::map<InputType, double> >::iterator it1 = counts12.begin(); it1 != counts12.end(); it1++)
1591 for (
typename std::map<InputType, double>::iterator it2 = it1->second.begin(); it2 != it1->second.end(); it2++)
1593 s +=
static_cast<OutputType
>((it2->second / n) * std::log(it2->second * n / (counts1[it1->first] * counts2[it2->first])) / std::log(base));
1614 template<
class InputType,
class OutputType>
1618 for (
size_t i = 0; i < v.size(); i++)
1624 std::vector<double> x(1);
1625 for (
size_t i = 0; i < v.size(); i++)
1627 x[0] =
static_cast<double>(v[i]);
1628 s +=
static_cast<OutputType
>(std::log(kd.
kDensity(x)) / std::log(base));
1630 return -s /
static_cast<double>(v.size());
1652 template<
class InputType,
class OutputType>
1655 if (v1.size() != v2.size())
1656 throw DimensionException(
"VectorTools::miContinuous. The two samples must have the same length.", v2.size(), v1.size());
1660 for (
size_t i = 0; i < v1.size(); i++)
1662 m1(0, i) = m12(0, i) = v1[i];
1663 m2(0, i) = m12(1, i) = v2[i];
1669 std::vector<double> x1(1);
1670 std::vector<double> x2(1);
1671 std::vector<double> x12(2);
1672 for (
size_t i = 0; i < v1.size(); i++)
1674 x1[0] = x12[0] =
static_cast<double>(v1[i]);
1675 x2[0] = x12[1] =
static_cast<double>(v2[i]);
1676 s +=
static_cast<OutputType
>(std::log(kd12.
kDensity(x12) / (kd1.
kDensity(x1) * kd2.
kDensity(x2))) / std::log(base));
1678 return s /
static_cast<double>(v1.size());
1689 std::vector<T> u1(v1);
1690 std::vector<T> u2(v2);
1691 if (u1.size() != u2.size())
return false;
1692 std::sort(u1.begin(), u1.end());
1693 std::sort(u2.begin(), u2.end());
1708 if (v1.size() != v2.size())
return false;
1709 std::sort(v1.begin(), v1.end());
1710 std::sort(v2.begin(), v2.end());
1722 for (
size_t i = 0; i < vec.size(); i++)
1724 if (vec[i] == el)
return true;
1740 std::sort(v1.begin(), v1.end());
1741 std::sort(v2.begin(), v2.end());
1743 for (
size_t i = 0; i < v2.size(); i++)
1745 if (i > 0 && v2[i] == v2[i - 1])
continue;
1746 while (j < v1.size() - 1 && v1[j] < v2[i]) j++;
1747 if (v1[j] != v2[i])
return false;
1759 static std::vector<T>
vectorUnion(
const std::vector<T>& vec1,
const std::vector<T>& vec2)
1761 std::vector<T> unionEl = vec1;
1762 for (
size_t j = 0; j < vec2.size(); j++)
1765 unionEl.push_back(vec2[j]);
1776 static std::vector<T>
vectorUnion(
const std::vector< std::vector<T> >& vecElementL)
1778 std::vector<T> unionEl;
1779 for (
size_t i = 0; i < vecElementL.size(); i++)
1781 for (
size_t j = 0; j < vecElementL[i].size(); j++)
1783 if (!
contains(unionEl, vecElementL[i][j]))
1784 unionEl.push_back(vecElementL[i][j]);
1798 std::vector<T> interEl;
1799 for (
size_t i = 0; i < vec1.size(); i++)
1801 if (
contains(vec2, vec1[i])) interEl.push_back(vec1[i]);
1813 if (vecElementL.size() == 1)
return vecElementL[0];
1814 std::vector<T> interEl;
1815 if (vecElementL.size() == 0)
return interEl;
1816 for (
size_t i = 0; i < vecElementL[0].size(); i++)
1819 for (
size_t j = 1; test && j < vecElementL.size(); j++)
1821 if (!
contains(vecElementL[j], vecElementL[0][i])) test =
false;
1823 if (test) interEl.push_back(vecElementL[0][i]);
1834 static void append(std::vector<T>& vec1,
const std::vector<T>& vec2)
1836 vec1.insert(vec1.end(), vec2.begin(), vec2.end());
1849 static void prepend(std::vector<T>& vec1,
const std::vector<T>& vec2)
1851 vec1.insert(vec1.begin(), vec2.begin(), vec2.end());
1860 static std::vector<T>
append(
const std::vector< std::vector<T> >& vecElementL)
1862 if (vecElementL.size() == 1)
return vecElementL[0];
1864 if (vecElementL.size() == 0)
return v;
1865 for (
size_t i = 0; i < vecElementL[0].size(); i++)
1867 v.push_back(vecElementL[0][i]);
1878 static void extend(std::vector<T>& vec1,
const std::vector<T>& vec2)
1880 for (
size_t i = 0; i < vec2.size(); i++)
1883 vec1.push_back(vec2[i]);
1893 static std::vector<T>
rep(
const std::vector<T>& vec,
size_t n)
1895 if (n == 1)
return vec;
1897 if (n == 0)
return v;
1898 v.resize(vec.size() * n);
1899 for (
size_t i = 0; i < v.size(); i++)
1901 v[i] = vec[i % vec.size()];
1916 static void diff(std::vector<T>& v1, std::vector<T>& v2, std::vector<T>& v3)
1918 if (v2.size() == 0)
append(v3, v1);
1919 std::sort(v1.begin(), v1.end());
1920 std::sort(v2.begin(), v2.end());
1922 for (
size_t i = 0; i < v1.size(); i++)
1924 if (i > 0 && v1[i] == v1[i - 1])
continue;
1925 while (j < v2.size() - 1 && v2[j] < v1[i]) j++;
1926 if (v2[j] != v1[i]) v3.push_back(v1[i]);
1938 #endif // _VECTORTOOLS_H_