Advertisement
Guest User

task7

a guest
Nov 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1.  
  2.  
  3. #include "stdafx.h"
  4. #pragma once
  5. #include <ppl.h>
  6. #define _USE_MATH_DEFINES
  7. #include <iostream>
  8. #include <cmath>
  9. #include <time.h>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. using namespace concurrency;
  15.  
  16. typedef double(*Double_Func_Double)(double);
  17. double Simps(double a, double b, int N, Double_Func_Double);
  18. double Concurrent_Simps(double a, double b, int N, Double_Func_Double);
  19.  
  20. namespace MethodCall {
  21. static void *ObjAddr = nullptr;
  22.  
  23. template <class Ty>
  24. double Sub_Int_Func(double x) {
  25. return (*((Ty*)ObjAddr))(x);
  26. }
  27. template <class Ty>
  28. double Simpson(double a, double b, int N, Ty const &Obj) {
  29. ObjAddr = (void *)&Obj;
  30. return Simps(a, b, N, Sub_Int_Func<Ty>);
  31. }
  32. template <class Ty>
  33. double Concurrent_Simpson(double a, double b, int N, Ty const &Obj) {
  34. ObjAddr = (void*)&Obj;
  35. return Concurrent_Simps(a, b, N, Sub_Int_Func<Ty>);
  36. }
  37. };
  38.  
  39.  
  40. double Simps(double a, double b, int N, Double_Func_Double Func) {
  41. double h = (b - a) / (2 * N);
  42. double S1 = 0, S2 = 0;
  43. for (int k = 1; k < N; k++) {
  44. double Tmp = a + (2 * k - 1) * h;
  45. S1 += Func(Tmp);
  46. S2 += Func(Tmp + h);
  47. }
  48. S1 += Func(b - h);
  49. return h * (Func(a) + Func(b) + 4.0 * S1 + 2.0 * S2) / 3.0;
  50. }
  51.  
  52. double Concurrent_Simps(double a, double b, int N, Double_Func_Double Func) {
  53. double h = (b - a) / (2 * N);
  54. combinable<double> CS1([]() {return 0.0; }), CS2([]() {return 0.0; });
  55. parallel_for(1, N, [a, h, Func, &CS1, &CS2](int k) {
  56. double Tmp = a + (2 * k - 1) * h;
  57. CS1.local() += Func(Tmp);
  58. CS2.local() += Func(Tmp + h); });
  59. double S1 = CS1.combine([](double x, double y) {return x + y; });
  60. double S2 = CS2.combine([](double x, double y) {return x + y; });
  61. S1 += Func(b - h);
  62. return h * (Func(a) + Func(b) + 4.0 * S1 + 2.0 * S2) / 3.0;
  63. }
  64.  
  65.  
  66. class My_Class {
  67. private:
  68. double y;
  69. const int NNN = 200000;
  70. const double _Inf = 5000.0;
  71. public:
  72. My_Class(double _y = 0.0) { y = _y; }
  73.  
  74. double Sub_Int_Func(double x) {
  75. double Tmp = 15 * log(10.0) + log(abs(x)) - sqrt(x);
  76. int N = Tmp > 0 ? ceil(Tmp * Tmp + 1) : 1;
  77. double P = 1 / (exp(-sqrt(abs(x))) + exp(sqrt(abs(x))));
  78. double Tmp2 = y * y + x * x;
  79. for (int k = 0; k <= N; k++)
  80. P *= cos(x / (Tmp2 + exp(sqrt((double)k))));
  81. return P;
  82. }
  83.  
  84. double Quad() {
  85. return MethodCall::Simpson(0.0, _Inf, NNN, [this](double x) {return Sub_Int_Func(x); });
  86. }
  87.  
  88. double Concurrent_Quad()
  89. {
  90. return MethodCall::Concurrent_Simpson(0.0, _Inf, NNN, [this](double x) {return Sub_Int_Func(x); });
  91. }
  92. };
  93.  
  94. int main(void)
  95. {
  96. double y;
  97. cout << "y = "; cin >> y;
  98. My_Class TObj(y);
  99. double Tms = clock();
  100. double F = TObj.Quad();
  101. Tms = (clock() - Tms) / CLOCKS_PER_SEC;
  102. cout.precision(8);
  103. cout << endl << "F = " << F << endl << "Time = " << Tms << "sec" << endl << endl;
  104. Tms = clock();
  105. F = TObj.Concurrent_Quad();
  106. Tms = (clock() - Tms) / CLOCKS_PER_SEC;
  107. cout << "F = " << F << endl << "Time = " << Tms << "sec" << endl;
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement