fork download
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <vector>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int seqToIndex(vector<double>::iterator it, int r, int d)
  8. {
  9. int index = 0;
  10. for(int i = 0; i < d; ++i)
  11. {
  12. int index_i = int(*it * r);
  13. index += index_i * pow(r, i);
  14. ++it;
  15. }
  16. return index;
  17. }
  18.  
  19. double chi2(const vector<int>& freqTable, int N, int s, int d)
  20. {
  21. double ans = 0;
  22. double temp = double(N) / double(s * d);
  23. for(int i = 0; i < s; ++i)
  24. ans += pow(freqTable [i] - temp, 2);
  25. ans /= temp;
  26. return ans;
  27. }
  28.  
  29. double P(double calc_chi2, double s)
  30. {
  31. double l = s - 1.0;
  32. int N = 1000;
  33. double h = calc_chi2 / N;
  34. double intSum = 0.0;
  35. auto f = [] (double x) { return pow(x, l / 2.0 - 1.0) * exp(-x / 2.0); };
  36. for(int i = 1; i < N - 1; i += 2)
  37. intSum += f((i - 1) * h) + 4 * f(i * h) + f((i + 1) * h);
  38. intSum *= h / 3.0;
  39. double ans = (1.0 - intSum) / (pow(2.0, l / 2.0) * tgamma(l / 2.0));
  40. return ans;
  41. }
  42.  
  43. int main()
  44. {
  45. int r = 4;
  46. int d = 6;
  47. int s = pow(r, d);
  48. int N = d * 1000000;
  49. vector<double> alpha;
  50. vector<int> freqTable(s, 0);
  51. srand(42);
  52. for(int i = 0; i < N; ++i)
  53. {
  54. double a = double(rand()) / double(RAND_MAX);
  55. alpha.push_back(a);
  56. }
  57. int i = 0;
  58. while(i < N)
  59. {
  60. int index = seqToIndex(alpha.begin() + i, r, d);
  61. ++freqTable [index];
  62. i += d;
  63. }
  64. double calc_chi2 = chi2(freqTable, N, s, d);
  65. cout << calc_chi2 << endl;
  66. cout << P(calc_chi2, s) << endl;
  67. return 0;
  68. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In lambda function:
prog.cpp:35:44: error: 'l' is not captured
     auto f = [] (double x) { return pow(x, l / 2.0 - 1.0) * exp(-x / 2.0); };
                                            ^
prog.cpp: In function 'double P(double, double)':
prog.cpp:37:47: error: invalid operands of types 'int' and 'void' to binary 'operator*'
         intSum += f((i - 1) * h) + 4 * f(i * h) + f((i + 1) * h);
                                               ^
stdout
Standard output is empty