13 #include "boost/container/flat_map.hpp" 30 vector<double>
linspace(
double xl,
double xh,
int n);
38 vector<double>
arange(
double x_min,
double x_max,
double dx);
45 template <
class T> vector<T>
operator+
46 (vector<T>
const& v1, vector<T>
const& v2)
48 assert(v1.size() == v2.size() &&
"Vectors must have the same length");
49 vector<T> res(v1.size());
50 for (
size_t i = 0; i < v1.size(); ++i)
51 res[i] = v1[i] + v2[i];
60 template<
class T> vector<T>
operator+
61 (
const vector<T>& v,
const T& t)
63 vector<T> res(v.size());
64 for (
size_t i = 0, endp = v.size(); i < endp; ++i)
74 template<
class T> vector<T>
operator+
75 (
const T& t,
const vector<T>& v)
85 template<
class T> vector<T>&
operator+=
86 (vector<T>& v,
const T& t)
88 for (
size_t i = 0, endp = v.size(); i < endp; ++i)
98 template <
class T> vector<T>
operator-
99 (vector<T>
const& v1, vector<T>
const& v2)
101 if (v1.size() != v2.size())
104 vector<T> res(v1.size());
105 for (
size_t i = 0; i < v1.size(); ++i)
106 res[i] = v1[i] - v2[i];
115 template <
class T> vector<T>
operator*
116 (
double d, vector<T>
const& v)
118 vector<T> res(v.size());
119 for (
size_t i = 0; i < v.size(); ++i)
133 template <
typename T>
134 T
BiLinearInterpolation(
const vector<T>& x,
const vector<T>& y,
const std::vector<std::vector<T> >& z, T xi, T yi);
136 template <
typename T>
139 typename vector<T>::const_iterator itx = upper_bound(x.begin(), x.end(), xi);
144 std::cout <<
"X too large in BiLinearInterpolation, x_i " << xi <<
" max X " << x.back() << std::endl;
150 if (itx == x.begin())
154 std::cout <<
"X too small in BiLinearInterpolation, x_i " << xi <<
" min X " << x.at(0) << std::endl;
158 typename vector<T>::const_iterator ity = upper_bound(y.begin(), y.end(), yi);
163 std::cout <<
"Y too large in LinearInterpolation, y_i " << yi <<
" max Y " << y.back() << std::endl;
169 if (ity == y.begin())
173 std::cout <<
"Y too small in LinearInterpolation, y_i " << yi <<
" min Y " << y.at(0) << std::endl;
178 T delta = 1.0 / ((*itx - *(itx - 1)) * (*ity - *(ity - 1)));
179 size_t idx =
static_cast<std::size_t
>(itx - x.begin());
180 size_t idy =
static_cast<std::size_t
>(ity - y.begin());
181 return (z[idx - 1][idy - 1] * (*itx - xi) * (*ity - yi) + z[idx][idy - 1] * (xi - *(itx - 1)) * (*ity - yi)
182 + z[idx - 1][idy] * (*itx - xi) * (yi - *(ity - 1)) + z[idx][idy] * (xi - *(itx - 1))
183 * (yi - *(ity - 1))) * delta;
193 template <
typename T>
196 template <
typename T>
199 typename vector<T>::const_iterator it = upper_bound(x.begin(), x.end(), xi);
202 std::cout <<
"X too large in LinearInterpolation, x_i " << xi <<
" max X " << x.back() << std::endl;
209 std::cout <<
"X too small in LinearInterpolation, x_i " << xi <<
" min X " << x.at(0) << std::endl;
214 return y[
static_cast<std::size_t
>(it - x.begin())];
216 return y[
static_cast<std::size_t
>(it - x.begin())] + (xi - *it)* (y[
static_cast<std::size_t
>(it - 1 - x.begin())]
217 - y[static_cast<std::size_t>(it - x.begin())]) / (*(it - 1) - *it);
228 template <
typename T>
229 T
LinearInterpolation(
typename vector<T>::const_iterator itx_begin,
typename vector<T>::const_iterator itx_end,
230 typename vector<T>::const_iterator ity_begin, T xi);
232 template <
typename T>
233 T
LinearInterpolation(
typename vector<T>::const_iterator itx_begin,
typename vector<T>::const_iterator itx_end,
234 typename vector<T>::const_iterator ity_begin, T xi)
236 typename vector<T>::const_iterator it = upper_bound(itx_begin, itx_end, xi);
239 std::cout <<
"X too large in LinearInterpolation, x_i " << xi <<
" max X " << *itx_end << std::endl;
244 if (*it < *itx_begin)
246 std::cout <<
"X too small in LinearInterpolation, x_i " << xi <<
" min X " << *itx_begin << std::endl;
250 const size_t toadd =
static_cast<std::size_t
>(it - itx_begin);
252 return *(ity_begin + toadd);
254 return *(ity_begin + toadd) + (xi - *it) * (*(ity_begin + toadd + 1) - *(ity_begin + toadd)) / (*(it - 1) - *it);
261 double min(vector<double>
const& v);
267 double max(vector<double>
const& v);
275 (vector<T> &v, vector<int> &indeces)
279 sort(indeces.begin(), indeces.end());
281 result.reserve(v.size() - indeces.size());
283 for (
size_t i = 0; i < static_cast<size_t>(indeces.back()); ++i)
285 if (
size_t(indeces[
size_t(counter)]) == i)
288 result.push_back(v[i]);
290 for (
size_t i = static_cast<size_t>(indeces.back()) + 1; i < v.size(); ++i)
291 result.push_back(v[i]);
301 (vector<T> &v, vector<size_t> &indeces)
305 sort(indeces.begin(), indeces.end());
307 result.reserve(v.size() - indeces.size());
309 for (
size_t i = 0; i < indeces.back(); ++i)
311 if (indeces[counter] == i)
314 result.push_back(v[i]);
316 for (
size_t i = indeces.back() + 1; i < v.size(); ++i)
317 result.push_back(v[i]);
326 template <
class T> vector<T>
VectorValues(vector<T>
const&v, vector<int>
const &index)
328 if (index.empty() || v.empty())
331 vector<T> result(index.size());
332 for (
size_t i = 0; i < index.size(); ++i)
333 result.at(i) = v.at(static_cast<size_t>(index.at(i)));
345 vector<size_t>
const &index)
347 if (index.empty() || v.empty())
350 vector<T> result(index.size());
351 for (
size_t i = 0; i < index.size(); ++i)
352 result.at(i) = v.at(index.at(i));
366 for (
size_t i = 1; i < v.size(); ++i)
376 template <
class T> vector<T>
unique(vector<T>
const& v)
384 for (
typename vector<T>::const_iterator it = v.begin() + 1; it != v.end(); ++it)
385 if (*it == *(it - 1))
400 return vector<int>();
404 for (
size_t i = 1; i < v.size(); ++i)
405 if (v[i] != v[i - 1])
406 res.push_back(static_cast<int>(i));
416 template <
class T> vector<T>
RemoveList(vector<T>
const&v, vector<T>
const&list)
419 for (
size_t i = 0; i < v.size(); ++i)
420 if (!binary_search(list.begin(), list.end(), v[i]))
431 template <
class T>
void RemoveVal(vector<T> &vec, T val)
433 for (
size_t i = 0; i < vec.size(); ++i)
437 vec.erase(vec.begin() +
static_cast<long>(i));
449 template <
class T>
bool InVector(vector<T>
const&vec, T val)
452 for (
int i = 0; i < n; ++i)
467 for (
int i = 0; i < n; ++i)
479 const vector<T> temp = v;
480 for (
size_t i = 0; i < v.size(); ++i)
481 v[i] = temp[static_cast<size_t>(indeces[i])];
485 template<
class T>
struct index_cmp
487 explicit index_cmp(
const T _arr) : arr(_arr) {}
488 bool operator()(
const size_t a,
const size_t b)
const 490 return arr[a] < arr[b];
500 template<
class T>
void sort_index(
const vector<T> & arr, vector<int>& res)
502 res.resize(arr.size());
503 for (
size_t i = 0; i < res.size(); ++i)
504 res[i] = static_cast<int>(i);
505 sort(res.begin(), res.end(), index_cmp<vector<T> >(arr));
512 template<
class T>
void sort_index(
const vector<T> & arr, vector<size_t>& res)
514 res.resize(arr.size());
515 for (
size_t i = 0; i < res.size(); ++i)
517 sort(res.begin(), res.end(), index_cmp<vector<T> >(arr));
525 template<
class T> vector<size_t>
sort_index(
const vector<T> & arr)
527 vector<size_t> res(arr.size());
528 for (
size_t i = 0; i < res.size(); ++i)
530 sort(res.begin(), res.end(), index_cmp<vector<T> >(arr));
536 template <
class RAIter,
class Compare>
541 explicit PairComp(Compare comp_) : comp(comp_) {}
542 bool operator() (
const std::pair<size_t, RAIter>& a,
543 const std::pair<size_t, RAIter>& b)
const {
544 return comp(*a.second, *b.second);
555 template <
class RAIter,
class Compare>
556 void sort_index(RAIter iterBegin, RAIter iterEnd, Compare comp,
557 std::vector<size_t>& indexes)
560 std::vector< std::pair<size_t, RAIter> > pv;
561 pv.reserve(iterEnd - iterBegin);
565 for (iter = iterBegin, k = 0; iter != iterEnd; iter++, k++) {
566 pv.push_back(std::pair<int, RAIter>(k, iter));
568 PairComp<RAIter, Compare> compy(comp);
569 std::sort(pv.begin(), pv.end(), compy);
571 indexes.resize(pv.size());
572 for (
size_t i = 0; i < pv.size(); ++i)
573 indexes[i] = pv[i].first;
581 template<
class T> vector<T>
join(vector<T>
const& v1,
584 if (v1.empty() && v2.empty())
586 vector<T> res(v1.size() + v2.size());
588 copy(v1.begin(), v1.end(), res.begin());
590 copy(v2.begin(), v2.end(), res.begin() +
static_cast<int>(v1.size()));
604 virtual T operator()(T
const& t1, T
const& t2)
const = 0;
619 assert(v1.size() == v2.size());
621 vector<T> res(v1.size());
622 for (
size_t i = 0; i < v1.size(); ++i)
623 res[i] = bin_op(v1[i], v2[i]);
636 virtual T operator()(T
const& t)
const = 0;
649 vector<T> res(v.size());
650 for (
size_t i = 0; i < v.size(); ++i)
651 res[i] = un_op(v[i]);
660 template<
class T> T
pair_member(
const std::pair<T, T>& p,
int index)
662 assert((0 == index) || (1 == index));
663 return 0 == index ? p.first : p.second;
673 assert((0 == index) || (1 == index));
686 vector<T> res(source.size());
687 for (
size_t i = 0; i < res.size(); ++i)
688 res[i] = static_cast<T>(source[i]);
698 if (!addendum.empty())
699 subject.insert(subject.end(), addendum.begin(), addendum.end());
708 template<
class Iter,
class T>
712 Iter i = std::lower_bound(begin, end, val);
714 if (i != end && !(val < *i))
726 template<
class S,
class T>
typename vector<T>::const_reference
safe_retrieve 727 (vector<T>
const& data, vector<S>
const& keys,
const S& key)
729 assert(data.size() == keys.size());
730 typename vector<S>::const_iterator it =
binary_find(keys.begin(), keys.end(), key);
731 assert(it != keys.end());
732 size_t index =
static_cast<size_t>(it - keys.begin());
733 return data.at(index);
742 template<
class S,
class T>
typename vector<T>::reference
safe_retrieve 743 (vector<T> &data, vector<S>
const& keys,
const S& key)
745 assert(data.size() == keys.size());
746 typename vector<S>::const_iterator it =
binary_find(keys.begin(), keys.end(), key);
747 assert(it != keys.end());
748 return data[
static_cast<size_t>(it - keys.begin())];
757 (
const boost::container::flat_map<S, T>& m,
760 typename boost::container::flat_map<S, T>::const_iterator it =
762 assert(it != m.end());
770 template<
class T> vector<vector<T> >
CombineVectors(vector<vector<vector<T> > >
const& data)
773 size_t out_size = data.size();
774 for (
size_t i = 0; i < out_size; ++i)
775 counter += data[i].size();
776 vector<vector<T> > res(counter);
778 for (
size_t i = 0; i < out_size; ++i)
780 for (
size_t j = 0; j < data[i].size(); ++j)
782 res[counter] = data[i][j];
797 for (
size_t i = 0; i < data.size(); ++i)
798 res.insert(res.end(), data[i].begin(), data[i].end());
808 (boost::container::flat_map<S, T>& m,
811 typename boost::container::flat_map<S, T>::iterator it =
813 assert(it != m.end());
vector< T > unique(vector< T > const &v)
Returns a vector containing only unique elements.
vector< T > VectorValues(vector< T > const &v, vector< int > const &index)
Returns only the values with indeces in index.
vector< T > list_static_cast(const vector< S > &source)
Performs type casting for an entire vector.
void sort_index(const vector< T > &arr, vector< int > &res)
Returns the indeces of a sort.
void RemoveVector(vector< T > &v, vector< int > &indeces)
Removes the elements in v given by indeces.
vector< vector< T > > CombineVectors(vector< vector< vector< T > > > const &data)
Reduces the dimension of the input vector.
Container for error reports.
int IndexInVector(vector< T > const &vec, T val)
Returns the index of val in the vector.
BinaryOperation Binary operator.
void set_pair_member(std::pair< T, T > &p, int index, const T &val)
Sets a member of std::pair.
vector< T > apply_to_each_term(vector< T > const &v, UnaryOperation< T > const &un_op)
Applies an unary operator to all terms in a std::vector.
double max(vector< double > const &v)
returns the maximal term in a vector
vector< T > binary_unite(vector< T > const &v1, vector< T > const &v2, BinaryOperation< T > const &bin_op)
Applies a binary operation on every pair of values from two vectors.
vector< T > join(vector< T > const &v1, vector< T > const &v2)
Concatenates two vectors.
T LinearInterpolation(const vector< T > &x, const vector< T > &y, T xi)
Linear Interpolation.
double fastsqrt(double x)
Fast approximate sqrt.
vector< double > linspace(double xl, double xh, int n)
Uniformly spaced array (like the matlab function with the same name)
T pair_member(const std::pair< T, T > &p, int index)
Selects a member of std::pair.
void RemoveVal(vector< T > &vec, T val)
Removes the first occurence of val inside a vector.
void ReArrangeVector(vector< T > &v, vector< int > const &indeces)
Rearranges the vector according to the indeces.
void insert_all_to_back(vector< T > &subject, const vector< T > &addendum)
Inserts all elements from one vector to the end of another.
vector< T >::const_reference safe_retrieve(vector< T > const &data, vector< S > const &keys, const S &key)
Checks for existence and retrieves entry from flat map.
double min(vector< double > const &v)
Returns the minimal term in a vector.
vector< double > arange(double x_min, double x_max, double dx)
Uniformly spaced array (line numpy function with the same name)
vector< int > unique_index(vector< T > const &v)
Returns a vector containing only indeces of unique elements.
bool InVector(vector< T > const &vec, T val)
checks if val is in the vector
bool is_nan(double x)
Checks whether a number is a nan.
Abstract class for an unary operation.
T BiLinearInterpolation(const vector< T > &x, const vector< T > &y, const std::vector< std::vector< T > > &z, T xi, T yi)
BiLinear Interpolation.
T VectorSum(vector< T > const &v)
Returns the sum of the vector.
Iter binary_find(Iter begin, Iter end, T val)
Binary search that return iterator of found object or end if not found.
vector< T > RemoveList(vector< T > const &v, vector< T > const &list)
Returns only elements from vector v which are not in vector list, assumes list is sorted...
A class for storing error and debug information.