Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <numeric>
  7. #include <ctime>
  8. #include <math.h>
  9. #include <ostream>
  10. #include <random>
  11. #include <iterator>
  12. using namespace std;
  13. double dodawanie(double a, double b);
  14. double potega(double a, double b);
  15. template<class Type>
  16. class dzielenie
  17. {
  18. private:
  19. Type dzielnik;
  20. public:
  21. dzielenie(const Type& wartosc) : dzielnik(wartosc) {};
  22. void operator()(Type& elem) const
  23. {
  24. elem /= dzielnik;
  25. };
  26. };
  27.  
  28. class generatorLiczbNorm {
  29. // static – ponieważ ma być jeden generator dla całego programu:
  30. static default_random_engine rng;
  31.  
  32. // produkuje liczby o rozkł. normalnym korzystajšc z generatora rng:
  33. normal_distribution<double> dist;
  34.  
  35. public:
  36. generatorLiczbNorm(double mi = 0, double sigma = 1) : dist(mi, sigma) {};
  37.  
  38. double operator()() {
  39. return dist(rng);
  40. }
  41. };
  42. class generator_pkt{
  43. int n;
  44. double mi;
  45. double sigma;
  46. public:
  47. vector<double> operator()()
  48. {
  49. vector<double> punkt(n);
  50. generate_n(punkt.begin(), n, generatorLiczbNorm(mi, sigma));
  51. return punkt;
  52. }
  53. generator_pkt(int i, double x, double y) : n(i), mi(x), sigma(y) {};
  54.  
  55. };
  56. default_random_engine generatorLiczbNorm::rng;
  57. class dodaj
  58. {
  59. public:
  60. vector<double> operator()(vector<double> const a, vector<double> const b)
  61. {
  62. vector<double> suma(a.size());
  63. transform(a.begin(), a.end(), b.begin(), suma.begin(), dodawanie);
  64. return suma;
  65. }
  66. };
  67. class Vector : public vector<double>
  68. {
  69. public:
  70. char pochodzenie;
  71. Vector(char x) : pochodzenie(x) {};
  72. };
  73.  
  74. int main()
  75. {
  76. srand(time(NULL));
  77. int const rozmiar = 100;
  78. int n;
  79. cout << "Podaj liczbe wymiarow: ";
  80. cin >> n;
  81. vector<vector<double>> A(rozmiar);
  82. generate(A.begin(), A.end(), generator_pkt(n, 0, 10));
  83. vector<vector<double>>::iterator itA= A.begin();
  84. //while (itA != A.end())
  85. //{
  86. // vector<double>::iterator iti = (*itA).begin();
  87. // while (iti != (*itA).end())
  88. // {
  89. // cout << *iti << " ";
  90. // iti++;
  91. // }
  92. // cout << endl;
  93. // itA++;
  94. //}
  95. vector<vector<double>> B(rozmiar);
  96. generate(B.begin(), B.end(), generator_pkt(n, 10, 10));
  97. vector<double> centroidA(n);
  98. centroidA=accumulate(A.begin(), A.end(), centroidA, dodaj());
  99. for_each(centroidA.begin(), centroidA.end(), dzielenie<double>(100));
  100. vector<double> centroidB(n);
  101. centroidB=accumulate(B.begin(), B.end(), centroidB, dodaj());
  102. vector<double>::iterator it = centroidB.begin();
  103. for_each(centroidB.begin(), centroidB.end(), dzielenie<double>(100));
  104. //while (it != centroidB.end())
  105. //{
  106. // cout << *it << " ";
  107. // it++;
  108. //}
  109. double odleglosc=0;
  110. odleglosc=inner_product(centroidA.begin(), centroidA.end(), centroidB.begin(), 0.0, dodawanie,potega );
  111. odleglosc = sqrt(odleglosc);
  112. cout << endl<< "Odleglosc wynosi: " << odleglosc<< endl;
  113. ostream_iterator<double> out_it(cout, ", ");
  114. cout << "Centroid A:" << endl<<endl;
  115. copy(centroidA.begin(), centroidA.end(), out_it);
  116. cout <<endl<<endl<< "Centroid B:" << endl;
  117. copy(centroidB.begin(), centroidB.end(), out_it);
  118. cout << endl;
  119. }
  120.  
  121. double dodawanie(double a, double b)
  122. {
  123. return (double) (a + b);
  124. }
  125.  
  126. double potega(double a, double b)
  127. {
  128. return (double)(pow((a-b),2.0));
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement