euler314 commited on
Commit
bb706ee
Β·
verified Β·
1 Parent(s): 06d8fdb

Update app.cpp

Browse files
Files changed (1) hide show
  1. app.cpp +119 -96
app.cpp CHANGED
@@ -1,4 +1,4 @@
1
- // app.cpp - Modified version with cubic equation solver
2
  #include <opencv2/opencv.hpp>
3
  #include <algorithm>
4
  #include <cmath>
@@ -111,7 +111,7 @@ std::vector<std::vector<double>> computeImSVsZ(double a, double y, double beta,
111
  z_values[i] = z;
112
 
113
  // Coefficients for the cubic equation:
114
- // zas^3 + [z(a+1)+a(1-y)]s^2 + [z+(a+1)-y-yΞ²(a-1)]s + 1 = 0
115
  double coef_a = z * a;
116
  double coef_b = z * (a + 1) + a * (1 - y);
117
  double coef_c = z + (a + 1) - y - y * beta * (a - 1);
@@ -346,10 +346,116 @@ void save_as_json(const std::string& filename,
346
  outfile.close();
347
  }
348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
  int main(int argc, char* argv[]) {
350
- // Check command mode
 
 
 
 
 
 
351
  if (argc < 2) {
352
- std::cerr << "Usage: " << argv[0] << " [eigenvalues|cubic] [parameters...]" << std::endl;
 
 
353
  return 1;
354
  }
355
 
@@ -357,8 +463,10 @@ int main(int argc, char* argv[]) {
357
 
358
  if (mode == "eigenvalues") {
359
  // ─── Eigenvalue analysis mode ───────────────────────────────────────────
360
- if (argc != 9) {
 
361
  std::cerr << "Usage: " << argv[0] << " eigenvalues <n> <p> <a> <y> <fineness> <theory_grid_points> <theory_tolerance> <output_file>" << std::endl;
 
362
  return 1;
363
  }
364
 
@@ -370,90 +478,15 @@ int main(int argc, char* argv[]) {
370
  int theory_grid_points = std::stoi(argv[7]);
371
  double theory_tolerance = std::stod(argv[8]);
372
  std::string output_file = argv[9];
373
- const double b = 1.0;
374
-
375
- std::cout << "Running eigenvalue analysis with parameters: n = " << n << ", p = " << p
376
- << ", a = " << a << ", y = " << y << ", fineness = " << fineness
377
- << ", theory_grid_points = " << theory_grid_points
378
- << ", theory_tolerance = " << theory_tolerance << std::endl;
379
- std::cout << "Output will be saved to: " << output_file << std::endl;
380
-
381
- // ─── Beta range parameters ────────────────────────────────────────
382
- const int num_beta_points = fineness; // Controlled by fineness parameter
383
- std::vector<double> beta_values(num_beta_points);
384
- for (int i = 0; i < num_beta_points; ++i) {
385
- beta_values[i] = static_cast<double>(i) / (num_beta_points - 1);
386
- }
387
 
388
- // ─── Storage for results ────────────────────────────────────────
389
- std::vector<double> max_eigenvalues(num_beta_points);
390
- std::vector<double> min_eigenvalues(num_beta_points);
391
- std::vector<double> theoretical_max_values(num_beta_points);
392
- std::vector<double> theoretical_min_values(num_beta_points);
393
-
394
- // ─── Random‐Gaussian X and S_n ────────────────────────────────
395
- std::mt19937_64 rng{std::random_device{}()};
396
- std::normal_distribution<double> norm(0.0, 1.0);
397
-
398
- cv::Mat X(p, n, CV_64F);
399
- for(int i = 0; i < p; ++i)
400
- for(int j = 0; j < n; ++j)
401
- X.at<double>(i,j) = norm(rng);
402
-
403
- // ─── Process each beta value ─────────────────────────────────
404
- for (int beta_idx = 0; beta_idx < num_beta_points; ++beta_idx) {
405
- double beta = beta_values[beta_idx];
406
-
407
- // Compute theoretical values with customizable precision
408
- theoretical_max_values[beta_idx] = compute_theoretical_max(a, y, beta, theory_grid_points, theory_tolerance);
409
- theoretical_min_values[beta_idx] = compute_theoretical_min(a, y, beta, theory_grid_points, theory_tolerance);
410
-
411
- // ─── Build T_n matrix ──────────────────────────────────
412
- int k = static_cast<int>(std::floor(beta * p));
413
- std::vector<double> diags(p);
414
- std::fill_n(diags.begin(), k, a);
415
- std::fill_n(diags.begin()+k, p-k, b);
416
- std::shuffle(diags.begin(), diags.end(), rng);
417
-
418
- cv::Mat T_n = cv::Mat::zeros(p, p, CV_64F);
419
- for(int i = 0; i < p; ++i){
420
- T_n.at<double>(i,i) = diags[i];
421
- }
422
-
423
- // ─── Form B_n = (1/n) * X * T_n * X^T ────────────
424
- cv::Mat B = (X.t() * T_n * X) / static_cast<double>(n);
425
-
426
- // ─── Compute eigenvalues of B ────────────────────────────
427
- cv::Mat eigVals;
428
- cv::eigen(B, eigVals);
429
- std::vector<double> eigs(n);
430
- for(int i = 0; i < n; ++i)
431
- eigs[i] = eigVals.at<double>(i, 0);
432
-
433
- max_eigenvalues[beta_idx] = *std::max_element(eigs.begin(), eigs.end());
434
- min_eigenvalues[beta_idx] = *std::min_element(eigs.begin(), eigs.end());
435
-
436
- // Progress indicator for Streamlit
437
- double progress = static_cast<double>(beta_idx + 1) / num_beta_points;
438
- std::cout << "PROGRESS:" << progress << std::endl;
439
-
440
- // Less verbose output for Streamlit
441
- if (beta_idx % 20 == 0 || beta_idx == num_beta_points - 1) {
442
- std::cout << "Processing beta = " << beta
443
- << " (" << beta_idx+1 << "/" << num_beta_points << ")" << std::endl;
444
- }
445
- }
446
-
447
- // Save data as JSON for Python to read
448
- save_as_json(output_file, beta_values, max_eigenvalues, min_eigenvalues,
449
- theoretical_max_values, theoretical_min_values);
450
-
451
- std::cout << "Data saved to " << output_file << std::endl;
452
 
453
  } else if (mode == "cubic") {
454
  // ─── Cubic equation analysis mode ───────────────────────────────────────────
455
- if (argc != 6) {
 
456
  std::cerr << "Usage: " << argv[0] << " cubic <a> <y> <beta> <num_points> <output_file>" << std::endl;
 
457
  return 1;
458
  }
459
 
@@ -463,20 +496,10 @@ int main(int argc, char* argv[]) {
463
  int num_points = std::stoi(argv[5]);
464
  std::string output_file = argv[6];
465
 
466
- std::cout << "Running cubic equation analysis with parameters: a = " << a
467
- << ", y = " << y << ", beta = " << beta << ", num_points = " << num_points << std::endl;
468
- std::cout << "Output will be saved to: " << output_file << std::endl;
469
-
470
- // Compute Im(s) vs z data
471
- std::vector<std::vector<double>> ims_data = computeImSVsZ(a, y, beta, num_points);
472
-
473
- // Save to JSON
474
- saveImSDataAsJSON(output_file, ims_data);
475
-
476
- std::cout << "Cubic equation data saved to " << output_file << std::endl;
477
 
478
  } else {
479
- std::cerr << "Unknown mode: " << mode << std::endl;
480
  std::cerr << "Use 'eigenvalues' or 'cubic'" << std::endl;
481
  return 1;
482
  }
 
1
+ // app.cpp - Modified version for command line arguments
2
  #include <opencv2/opencv.hpp>
3
  #include <algorithm>
4
  #include <cmath>
 
111
  z_values[i] = z;
112
 
113
  // Coefficients for the cubic equation:
114
+ // zasΒ³ + [z(a+1)+a(1-y)]sΒ² + [z+(a+1)-y-yΞ²(a-1)]s + 1 = 0
115
  double coef_a = z * a;
116
  double coef_b = z * (a + 1) + a * (1 - y);
117
  double coef_c = z + (a + 1) - y - y * beta * (a - 1);
 
346
  outfile.close();
347
  }
348
 
349
+ // Eigenvalue analysis function
350
+ void eigenvalueAnalysis(int n, int p, double a, double y, int fineness,
351
+ int theory_grid_points, double theory_tolerance,
352
+ const std::string& output_file) {
353
+
354
+ std::cout << "Running eigenvalue analysis with parameters: n = " << n << ", p = " << p
355
+ << ", a = " << a << ", y = " << y << ", fineness = " << fineness
356
+ << ", theory_grid_points = " << theory_grid_points
357
+ << ", theory_tolerance = " << theory_tolerance << std::endl;
358
+ std::cout << "Output will be saved to: " << output_file << std::endl;
359
+
360
+ // ─── Beta range parameters ────────────────────────────────────────
361
+ const int num_beta_points = fineness; // Controlled by fineness parameter
362
+ std::vector<double> beta_values(num_beta_points);
363
+ for (int i = 0; i < num_beta_points; ++i) {
364
+ beta_values[i] = static_cast<double>(i) / (num_beta_points - 1);
365
+ }
366
+
367
+ // ─── Storage for results ────────────────────────────────────────
368
+ std::vector<double> max_eigenvalues(num_beta_points);
369
+ std::vector<double> min_eigenvalues(num_beta_points);
370
+ std::vector<double> theoretical_max_values(num_beta_points);
371
+ std::vector<double> theoretical_min_values(num_beta_points);
372
+
373
+ // ─── Random‐Gaussian X and S_n ────────────────────────────────
374
+ std::mt19937_64 rng{std::random_device{}()};
375
+ std::normal_distribution<double> norm(0.0, 1.0);
376
+
377
+ cv::Mat X(p, n, CV_64F);
378
+ for(int i = 0; i < p; ++i)
379
+ for(int j = 0; j < n; ++j)
380
+ X.at<double>(i,j) = norm(rng);
381
+
382
+ // ─── Process each beta value ─────────────────────────────────
383
+ for (int beta_idx = 0; beta_idx < num_beta_points; ++beta_idx) {
384
+ double beta = beta_values[beta_idx];
385
+
386
+ // Compute theoretical values with customizable precision
387
+ theoretical_max_values[beta_idx] = compute_theoretical_max(a, y, beta, theory_grid_points, theory_tolerance);
388
+ theoretical_min_values[beta_idx] = compute_theoretical_min(a, y, beta, theory_grid_points, theory_tolerance);
389
+
390
+ // ─── Build T_n matrix ──────────────────────────────────
391
+ int k = static_cast<int>(std::floor(beta * p));
392
+ std::vector<double> diags(p, 1.0);
393
+ std::fill_n(diags.begin(), k, a);
394
+ std::shuffle(diags.begin(), diags.end(), rng);
395
+
396
+ cv::Mat T_n = cv::Mat::zeros(p, p, CV_64F);
397
+ for(int i = 0; i < p; ++i){
398
+ T_n.at<double>(i,i) = diags[i];
399
+ }
400
+
401
+ // ─── Form B_n = (1/n) * X * T_n * X^T ────────────
402
+ cv::Mat B = (X.t() * T_n * X) / static_cast<double>(n);
403
+
404
+ // ─── Compute eigenvalues of B ────────────────────────────
405
+ cv::Mat eigVals;
406
+ cv::eigen(B, eigVals);
407
+ std::vector<double> eigs(n);
408
+ for(int i = 0; i < n; ++i)
409
+ eigs[i] = eigVals.at<double>(i, 0);
410
+
411
+ max_eigenvalues[beta_idx] = *std::max_element(eigs.begin(), eigs.end());
412
+ min_eigenvalues[beta_idx] = *std::min_element(eigs.begin(), eigs.end());
413
+
414
+ // Progress indicator for Streamlit
415
+ double progress = static_cast<double>(beta_idx + 1) / num_beta_points;
416
+ std::cout << "PROGRESS:" << progress << std::endl;
417
+
418
+ // Less verbose output for Streamlit
419
+ if (beta_idx % 20 == 0 || beta_idx == num_beta_points - 1) {
420
+ std::cout << "Processing beta = " << beta
421
+ << " (" << beta_idx+1 << "/" << num_beta_points << ")" << std::endl;
422
+ }
423
+ }
424
+
425
+ // Save data as JSON for Python to read
426
+ save_as_json(output_file, beta_values, max_eigenvalues, min_eigenvalues,
427
+ theoretical_max_values, theoretical_min_values);
428
+
429
+ std::cout << "Data saved to " << output_file << std::endl;
430
+ }
431
+
432
+ // Cubic equation analysis function
433
+ void cubicAnalysis(double a, double y, double beta, int num_points, const std::string& output_file) {
434
+ std::cout << "Running cubic equation analysis with parameters: a = " << a
435
+ << ", y = " << y << ", beta = " << beta << ", num_points = " << num_points << std::endl;
436
+ std::cout << "Output will be saved to: " << output_file << std::endl;
437
+
438
+ // Compute Im(s) vs z data
439
+ std::vector<std::vector<double>> ims_data = computeImSVsZ(a, y, beta, num_points);
440
+
441
+ // Save to JSON
442
+ saveImSDataAsJSON(output_file, ims_data);
443
+
444
+ std::cout << "Cubic equation data saved to " << output_file << std::endl;
445
+ }
446
+
447
  int main(int argc, char* argv[]) {
448
+ // Print received arguments for debugging
449
+ std::cout << "Received " << argc << " arguments:" << std::endl;
450
+ for (int i = 0; i < argc; ++i) {
451
+ std::cout << " argv[" << i << "]: " << argv[i] << std::endl;
452
+ }
453
+
454
+ // Check for mode argument
455
  if (argc < 2) {
456
+ std::cerr << "Error: Missing mode argument." << std::endl;
457
+ std::cerr << "Usage: " << argv[0] << " eigenvalues <n> <p> <a> <y> <fineness> <theory_grid_points> <theory_tolerance> <output_file>" << std::endl;
458
+ std::cerr << " or: " << argv[0] << " cubic <a> <y> <beta> <num_points> <output_file>" << std::endl;
459
  return 1;
460
  }
461
 
 
463
 
464
  if (mode == "eigenvalues") {
465
  // ─── Eigenvalue analysis mode ───────────────────────────────────────────
466
+ if (argc != 10) {
467
+ std::cerr << "Error: Incorrect number of arguments for eigenvalues mode." << std::endl;
468
  std::cerr << "Usage: " << argv[0] << " eigenvalues <n> <p> <a> <y> <fineness> <theory_grid_points> <theory_tolerance> <output_file>" << std::endl;
469
+ std::cerr << "Received " << argc << " arguments, expected 10." << std::endl;
470
  return 1;
471
  }
472
 
 
478
  int theory_grid_points = std::stoi(argv[7]);
479
  double theory_tolerance = std::stod(argv[8]);
480
  std::string output_file = argv[9];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481
 
482
+ eigenvalueAnalysis(n, p, a, y, fineness, theory_grid_points, theory_tolerance, output_file);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
 
484
  } else if (mode == "cubic") {
485
  // ─── Cubic equation analysis mode ───────────────────────────────────────────
486
+ if (argc != 7) {
487
+ std::cerr << "Error: Incorrect number of arguments for cubic mode." << std::endl;
488
  std::cerr << "Usage: " << argv[0] << " cubic <a> <y> <beta> <num_points> <output_file>" << std::endl;
489
+ std::cerr << "Received " << argc << " arguments, expected 7." << std::endl;
490
  return 1;
491
  }
492
 
 
496
  int num_points = std::stoi(argv[5]);
497
  std::string output_file = argv[6];
498
 
499
+ cubicAnalysis(a, y, beta, num_points, output_file);
 
 
 
 
 
 
 
 
 
 
500
 
501
  } else {
502
+ std::cerr << "Error: Unknown mode: " << mode << std::endl;
503
  std::cerr << "Use 'eigenvalues' or 'cubic'" << std::endl;
504
  return 1;
505
  }