//----------------------------------------------------------------------------- // vecop.h //----------------------------------------------------------------------------- #ifndef VECOP_H #define VECOP_H //----------------------------------------------------------------------------- #include // ostream istream #include // vector #include // plus minus multiplies divides #include // inner_product //----------------------------------------------------------------------------- // std::vector + std::vector //----------------------------------------------------------------------------- template std::vector operator+(const std::vector& v1, const std::vector& v2) { std::vector tmp = v1; std::transform(tmp.begin(), tmp.end(), v2.begin(), tmp.begin(), std::plus()); return tmp; } template std::vector operator-(const std::vector& v1, const std::vector& v2) { std::vector tmp = v1; std::transform(tmp.begin(), tmp.end(), v2.begin(), tmp.begin(), std::minus()); return tmp; } template T operator*(const std::vector& v1, const std::vector& v2) { return inner_product(v1.begin(), v1.end(), v2.begin(), static_cast(0)); } template T operator/(const std::vector& v1, const std::vector& v2) { return inner_product(v1.begin(), v1.end(), v2.begin(), static_cast(0), std::plus(), std::divides()); } //----------------------------------------------------------------------------- // std::vector += std::vector //----------------------------------------------------------------------------- template std::vector& operator+=(std::vector& v1, const std::vector& v2) { std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::plus()); return v1; } template std::vector& operator-=(std::vector& v1, const std::vector& v2) { std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::minus()); return v1; } //----------------------------------------------------------------------------- // std::vector + number //----------------------------------------------------------------------------- template std::vector operator+(const std::vector& a, const B& b) { std::vector tmp = a; std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::plus(), b)); return tmp; } template std::vector operator-(const std::vector& a, const B& b) { std::vector tmp = a; std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::minus(), b)); return tmp; } template std::vector operator*(const std::vector& a, const B& b) { std::vector tmp = a; std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::multiplies(), b)); return tmp; } template std::vector operator/(const std::vector& a, const B& b) { std::vector tmp = a; std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::divides(), b)); return tmp; } //----------------------------------------------------------------------------- // number + std::vector //----------------------------------------------------------------------------- template std::vector operator+(const B& b, const std::vector& a) { std::vector tmp = a; std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::plus(), b)); return tmp; } template std::vector operator-(const B& b, const std::vector& a) { std::vector tmp(a.size(), b); std::transform(tmp.begin(), tmp.end(), a.begin(), tmp.begin(), std::minus()); return tmp; } template std::vector operator*(const B& b, const std::vector& a) { std::vector tmp = a; std::transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::multiplies(), b)); return tmp; } template std::vector operator/(const B& b, const std::vector& a) { std::vector tmp(a.size(), b); std::transform(tmp.begin(), tmp.end(), a.begin(), tmp.begin(), std::divides()); return tmp; } //----------------------------------------------------------------------------- // std::vector += number //----------------------------------------------------------------------------- template std::vector& operator+=(std::vector& a, const B& b) { std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::plus(), b)); return a; } template std::vector& operator-=(std::vector& a, const B& b) { std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::minus(), b)); return a; } template std::vector& operator*=(std::vector& a, const B& b) { std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::multiplies(), b)); return a; } template std::vector& operator/=(std::vector& a, const B& b) { std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::divides(), b)); return a; } //----------------------------------------------------------------------------- // I/O //----------------------------------------------------------------------------- template std::ostream& operator<<(std::ostream& os, const std::vector& v) { os << '<'; if (v.size()) { std::copy(v.begin(), v.end() - 1, std::ostream_iterator(os, " ")); os << v.back(); } return os << '>'; } template std::istream& operator>>(std::istream& is, std::vector& v) { v.clear(); char c; is >> c; if (!is || c != '<') is.setstate(std::ios::failbit); else { T t; do { is >> c; if (is && c!= '>') { is.putback(c); is >> t; if (is) v.push_back(t); } } while (is && c != '>'); } return is; } //----------------------------------------------------------------------------- // euclidean_distance //----------------------------------------------------------------------------- template T euclidean_distance(const std::vector& v1, const std::vector& v2) { T sum = 0, tmp; for (unsigned i = 0; i < v1.size(); ++i) { tmp = v1[i] - v2[i]; sum += tmp * tmp; } return sqrt(sum); } //----------------------------------------------------------------------------- #endif