Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <iterator>
- #include <random>
- #include <cmath>
- using namespace std;
- class generatorLiczbNorm {
- // static – ponieważ ma być jeden generator dla całego programu:
- static default_random_engine rng;
- // produkuje liczby o rozkł. normalnym korzystając z generatora rng:
- normal_distribution<double> dist;
- public:
- generatorLiczbNorm(double mi = 0, double sigma = 1) : dist(mi, sigma) {};
- double operator()() {
- return dist(rng);
- }
- };
- default_random_engine generatorLiczbNorm::rng;
- class wypisanie
- {
- int help;
- public:
- wypisanie() : help(0) {};
- void operator()(const double x)
- {
- cout << ++help << " = " << x << endl;
- return;
- }
- };
- class sumowanie
- {
- public:
- vector<double> operator()(vector<double> a, vector<double> b)
- {
- vector<double> zwrot;
- for (vector<double>::iterator aa = a.begin(), bb = b.begin(); aa != a.end() && bb !=b.end(); ++aa, ++bb)
- {
- zwrot.push_back((*aa)+(*bb));
- }
- return zwrot;
- }
- };
- ostream& operator<<(ostream& os, const vector<double>& vec)
- {
- copy(vec.begin(), vec.end(), ostream_iterator<double>(cout, " "));
- return os;
- }
- double diffPow2(double x, double y) { return pow((x - y), 2.0); }
- int main()
- {
- vector<double> A(3), B(3);
- vector<vector<double>> AB, BA;
- generatorLiczbNorm generatorek(0, 10), generatorek2(10, 10);
- #pragma region Zapełnienie i wypisanie wektorów
- for (int i = 0; i < 10; ++i)
- {
- for_each(A.begin(), A.end(), [&generatorek](double &a) {a = generatorek(); });
- AB.push_back(A);
- for_each(B.begin(), B.end(), [&generatorek2](double &a) {a = generatorek2(); });
- BA.push_back(B);
- }
- /*cout << "Pierwsza chmura: " << endl;
- for_each(AB.begin(), AB.end(), [](vector<double> xx) {for_each(xx.begin(), xx.end(), wypisanie());});
- cout << "Druga chmura: " << endl;
- for_each(BA.begin(), BA.end(), [](vector<double> xx) {for_each(xx.begin(), xx.end(), wypisanie());});*/
- #pragma endregion
- #pragma region Sumowanie i dzielenie vectorów - powstanie i wypisanie centroidów
- vector<double> centroidA(3), centroidB(3);
- centroidA = accumulate(AB.begin(), AB.end(), centroidA, sumowanie());
- centroidB = accumulate(BA.begin(), BA.end(), centroidB, sumowanie());
- for_each(centroidA.begin(), centroidA.end(), [](double &x) {x /= 100; });
- for_each(centroidB.begin(), centroidB.end(), [](double &x) {x /= 100; });
- /*cout << "Centroid A: " << endl;
- for_each(centroidA.begin(), centroidA.end(), wypisanie());
- cout << "Centroid B: " << endl;
- for_each(centroidB.begin(), centroidB.end(), wypisanie());*/
- #pragma endregion
- //Wypisanie centroidów przez copy
- cout << "CentroidA: " << endl;
- copy(centroidA.begin(), centroidA.end(), ostream_iterator<double>(cout, "\n"));
- cout << endl << "CentroidB: " << endl;
- copy(centroidB.begin(), centroidB.end(), ostream_iterator<double>(cout, "\n"));
- //Odległość euklidesowa
- cout << endl << "Odleglosc euklidesowa wynosi: " << sqrt(inner_product(centroidA.begin(), centroidA.end(), centroidB.begin(), 0.0, std::plus<double>(), diffPow2)) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement