euler314 commited on
Commit
a9353c5
Β·
verified Β·
1 Parent(s): 3e88252

Update app.cpp

Browse files
Files changed (1) hide show
  1. app.cpp +20 -18
app.cpp CHANGED
@@ -13,7 +13,7 @@
13
  #include <fstream>
14
 
15
  // Function to compute the theoretical max value
16
- double compute_theoretical_max(double a, double y, double beta) {
17
  auto f = [a, y, beta](double k) -> double {
18
  return (y * beta * (a - 1) * k + (a * k + 1) * ((y - 1) * k - 1)) /
19
  ((a * k + 1) * (k * k + k));
@@ -25,7 +25,7 @@ double compute_theoretical_max(double a, double y, double beta) {
25
  double best_val = f(best_k);
26
 
27
  // Initial grid search over a wide range
28
- const int num_grid_points = 200;
29
  for (int i = 0; i < num_grid_points; ++i) {
30
  double k = 0.01 + 100.0 * i / (num_grid_points - 1); // From 0.01 to 100
31
  double val = f(k);
@@ -39,7 +39,6 @@ double compute_theoretical_max(double a, double y, double beta) {
39
  double a_gs = std::max(0.01, best_k / 10.0);
40
  double b_gs = best_k * 10.0;
41
  const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
42
- const double tolerance = 1e-10;
43
 
44
  double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
45
  double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
@@ -56,15 +55,15 @@ double compute_theoretical_max(double a, double y, double beta) {
56
  }
57
  }
58
 
59
- // Multiply the result by y before returning
60
- return f((a_gs + b_gs) / 2.0) * y;
61
  }
62
 
63
  // Function to compute the theoretical min value
64
- double compute_theoretical_min(double a, double y, double beta) {
65
  auto f = [a, y, beta](double t) -> double {
66
  return (y * beta * (a - 1) * t + (a * t + 1) * ((y - 1) * t - 1)) /
67
- ((a * t + 1) * (t * t + t) * y);
68
  };
69
 
70
  // Use numerical optimization to find the minimum
@@ -73,7 +72,7 @@ double compute_theoretical_min(double a, double y, double beta) {
73
  double best_val = f(best_t);
74
 
75
  // Initial grid search over the range (-1/a, 0)
76
- const int num_grid_points = 200;
77
  for (int i = 1; i < num_grid_points; ++i) {
78
  // From slightly above -1/a to slightly below 0
79
  double t = -0.999/a + 0.998/a * i / (num_grid_points - 1);
@@ -90,7 +89,6 @@ double compute_theoretical_min(double a, double y, double beta) {
90
  double a_gs = -0.999/a; // Slightly above -1/a
91
  double b_gs = -0.001/a; // Slightly below 0
92
  const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
93
- const double tolerance = 1e-10;
94
 
95
  double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
96
  double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
@@ -107,8 +105,8 @@ double compute_theoretical_min(double a, double y, double beta) {
107
  }
108
  }
109
 
110
- // Multiply the result by y before returning
111
- return f((a_gs + b_gs) / 2.0) * y;
112
  }
113
 
114
  // Function to save data as JSON
@@ -177,8 +175,8 @@ void save_as_json(const std::string& filename,
177
 
178
  int main(int argc, char* argv[]) {
179
  // ─── Inputs from command line ───────────────────────────────────────────
180
- if (argc != 7) {
181
- std::cerr << "Usage: " << argv[0] << " <n> <p> <a> <y> <fineness> <output_file>" << std::endl;
182
  return 1;
183
  }
184
 
@@ -187,11 +185,15 @@ int main(int argc, char* argv[]) {
187
  double a = std::stod(argv[3]);
188
  double y = std::stod(argv[4]);
189
  int fineness = std::stoi(argv[5]);
190
- std::string output_file = argv[6];
 
 
191
  const double b = 1.0;
192
 
193
  std::cout << "Running with parameters: n = " << n << ", p = " << p
194
- << ", a = " << a << ", y = " << y << ", fineness = " << fineness << std::endl;
 
 
195
  std::cout << "Output will be saved to: " << output_file << std::endl;
196
 
197
  // ─── Beta range parameters ────────────────────────────────────────
@@ -220,9 +222,9 @@ int main(int argc, char* argv[]) {
220
  for (int beta_idx = 0; beta_idx < num_beta_points; ++beta_idx) {
221
  double beta = beta_values[beta_idx];
222
 
223
- // Compute theoretical values
224
- theoretical_max_values[beta_idx] = compute_theoretical_max(a, y, beta);
225
- theoretical_min_values[beta_idx] = compute_theoretical_min(a, y, beta);
226
 
227
  // ─── Build T_n matrix ──────────────────────────────────
228
  int k = static_cast<int>(std::floor(beta * p));
 
13
  #include <fstream>
14
 
15
  // Function to compute the theoretical max value
16
+ double compute_theoretical_max(double a, double y, double beta, int grid_points, double tolerance) {
17
  auto f = [a, y, beta](double k) -> double {
18
  return (y * beta * (a - 1) * k + (a * k + 1) * ((y - 1) * k - 1)) /
19
  ((a * k + 1) * (k * k + k));
 
25
  double best_val = f(best_k);
26
 
27
  // Initial grid search over a wide range
28
+ const int num_grid_points = grid_points;
29
  for (int i = 0; i < num_grid_points; ++i) {
30
  double k = 0.01 + 100.0 * i / (num_grid_points - 1); // From 0.01 to 100
31
  double val = f(k);
 
39
  double a_gs = std::max(0.01, best_k / 10.0);
40
  double b_gs = best_k * 10.0;
41
  const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
 
42
 
43
  double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
44
  double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
 
55
  }
56
  }
57
 
58
+ // Return the value without multiplying by y (as per correction)
59
+ return f((a_gs + b_gs) / 2.0);
60
  }
61
 
62
  // Function to compute the theoretical min value
63
+ double compute_theoretical_min(double a, double y, double beta, int grid_points, double tolerance) {
64
  auto f = [a, y, beta](double t) -> double {
65
  return (y * beta * (a - 1) * t + (a * t + 1) * ((y - 1) * t - 1)) /
66
+ ((a * t + 1) * (t * t + t));
67
  };
68
 
69
  // Use numerical optimization to find the minimum
 
72
  double best_val = f(best_t);
73
 
74
  // Initial grid search over the range (-1/a, 0)
75
+ const int num_grid_points = grid_points;
76
  for (int i = 1; i < num_grid_points; ++i) {
77
  // From slightly above -1/a to slightly below 0
78
  double t = -0.999/a + 0.998/a * i / (num_grid_points - 1);
 
89
  double a_gs = -0.999/a; // Slightly above -1/a
90
  double b_gs = -0.001/a; // Slightly below 0
91
  const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
 
92
 
93
  double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
94
  double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
 
105
  }
106
  }
107
 
108
+ // Return the value without multiplying by y (as per correction)
109
+ return f((a_gs + b_gs) / 2.0);
110
  }
111
 
112
  // Function to save data as JSON
 
175
 
176
  int main(int argc, char* argv[]) {
177
  // ─── Inputs from command line ───────────────────────────────────────────
178
+ if (argc != 9) {
179
+ std::cerr << "Usage: " << argv[0] << " <n> <p> <a> <y> <fineness> <theory_grid_points> <theory_tolerance> <output_file>" << std::endl;
180
  return 1;
181
  }
182
 
 
185
  double a = std::stod(argv[3]);
186
  double y = std::stod(argv[4]);
187
  int fineness = std::stoi(argv[5]);
188
+ int theory_grid_points = std::stoi(argv[6]);
189
+ double theory_tolerance = std::stod(argv[7]);
190
+ std::string output_file = argv[8];
191
  const double b = 1.0;
192
 
193
  std::cout << "Running with parameters: n = " << n << ", p = " << p
194
+ << ", a = " << a << ", y = " << y << ", fineness = " << fineness
195
+ << ", theory_grid_points = " << theory_grid_points
196
+ << ", theory_tolerance = " << theory_tolerance << std::endl;
197
  std::cout << "Output will be saved to: " << output_file << std::endl;
198
 
199
  // ─── Beta range parameters ────────────────────────────────────────
 
222
  for (int beta_idx = 0; beta_idx < num_beta_points; ++beta_idx) {
223
  double beta = beta_values[beta_idx];
224
 
225
+ // Compute theoretical values with customizable precision
226
+ theoretical_max_values[beta_idx] = compute_theoretical_max(a, y, beta, theory_grid_points, theory_tolerance);
227
+ theoretical_min_values[beta_idx] = compute_theoretical_min(a, y, beta, theory_grid_points, theory_tolerance);
228
 
229
  // ─── Build T_n matrix ──────────────────────────────────
230
  int k = static_cast<int>(std::floor(beta * p));