Spaces:
Running
Running
Create app.cpp
Browse files
app.cpp
ADDED
@@ -0,0 +1,461 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// app.cpp - Modified version of eigen_analysis_corrected.cpp for Streamlit integration
|
2 |
+
#include <opencv2/opencv.hpp>
|
3 |
+
#include <algorithm>
|
4 |
+
#include <cmath>
|
5 |
+
#include <iostream>
|
6 |
+
#include <iomanip>
|
7 |
+
#include <numeric>
|
8 |
+
#include <random>
|
9 |
+
#include <vector>
|
10 |
+
#include <limits>
|
11 |
+
#include <sstream>
|
12 |
+
|
13 |
+
// Function to compute the theoretical max value
|
14 |
+
double compute_theoretical_max(double a, double y, double beta) {
|
15 |
+
auto f = [a, y, beta](double k) -> double {
|
16 |
+
return (y * beta * (a - 1) * k + (a * k + 1) * ((y - 1) * k - 1)) /
|
17 |
+
((a * k + 1) * (k * k + k)); // Divide by y here
|
18 |
+
};
|
19 |
+
|
20 |
+
// Use numerical optimization to find the maximum
|
21 |
+
// Grid search followed by golden section search
|
22 |
+
double best_k = 1.0;
|
23 |
+
double best_val = f(best_k);
|
24 |
+
|
25 |
+
// Initial grid search over a wide range
|
26 |
+
const int num_grid_points = 200;
|
27 |
+
for (int i = 0; i < num_grid_points; ++i) {
|
28 |
+
double k = 0.01 + 100.0 * i / (num_grid_points - 1); // From 0.01 to 100
|
29 |
+
double val = f(k);
|
30 |
+
if (val > best_val) {
|
31 |
+
best_val = val;
|
32 |
+
best_k = k;
|
33 |
+
}
|
34 |
+
}
|
35 |
+
|
36 |
+
// Refine with golden section search
|
37 |
+
double a_gs = std::max(0.01, best_k / 10.0);
|
38 |
+
double b_gs = best_k * 10.0;
|
39 |
+
const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
|
40 |
+
const double tolerance = 1e-10;
|
41 |
+
|
42 |
+
double c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
|
43 |
+
double d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
|
44 |
+
|
45 |
+
while (std::abs(b_gs - a_gs) > tolerance) {
|
46 |
+
if (f(c_gs) > f(d_gs)) {
|
47 |
+
b_gs = d_gs;
|
48 |
+
d_gs = c_gs;
|
49 |
+
c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
|
50 |
+
} else {
|
51 |
+
a_gs = c_gs;
|
52 |
+
c_gs = d_gs;
|
53 |
+
d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
|
54 |
+
}
|
55 |
+
}
|
56 |
+
|
57 |
+
// Multiply the result by y before returning
|
58 |
+
return f((a_gs + b_gs) / 2.0) *y ;
|
59 |
+
}
|
60 |
+
|
61 |
+
// Function to compute the theoretical min value
|
62 |
+
double compute_theoretical_min(double a, double y, double beta) {
|
63 |
+
auto f = [a, y, beta](double t) -> double {
|
64 |
+
return (y * beta * (a - 1) * t + (a * t + 1) * ((y - 1) * t - 1)) /
|
65 |
+
((a * t + 1) * (t * t + t) * y); // Divide by y here
|
66 |
+
};
|
67 |
+
|
68 |
+
// Use numerical optimization to find the minimum
|
69 |
+
// Grid search followed by golden section search
|
70 |
+
double best_t = -0.5 / a; // Midpoint of (-1/a, 0)
|
71 |
+
double best_val = f(best_t);
|
72 |
+
|
73 |
+
// Initial grid search over the range (-1/a, 0)
|
74 |
+
const int num_grid_points = 200;
|
75 |
+
for (int i = 1; i < num_grid_points; ++i) {
|
76 |
+
// From slightly above -1/a to slightly below 0
|
77 |
+
double t = -0.999/a + 0.998/a * i / (num_grid_points - 1);
|
78 |
+
if (t >= 0 || t <= -1.0/a) continue; // Ensure t is in range (-1/a, 0)
|
79 |
+
|
80 |
+
double val = f(t);
|
81 |
+
if (val < best_val) {
|
82 |
+
best_val = val;
|
83 |
+
best_t = t;
|
84 |
+
}
|
85 |
+
}
|
86 |
+
|
87 |
+
// Refine with golden section search
|
88 |
+
double a_gs = -0.999/a; // Slightly above -1/a
|
89 |
+
double b_gs = -0.001/a; // Slightly below 0
|
90 |
+
const double golden_ratio = (1.0 + std::sqrt(5.0)) / 2.0;
|
91 |
+
const double tolerance = 1e-10;
|
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;
|
95 |
+
|
96 |
+
while (std::abs(b_gs - a_gs) > tolerance) {
|
97 |
+
if (f(c_gs) < f(d_gs)) {
|
98 |
+
b_gs = d_gs;
|
99 |
+
d_gs = c_gs;
|
100 |
+
c_gs = b_gs - (b_gs - a_gs) / golden_ratio;
|
101 |
+
} else {
|
102 |
+
a_gs = c_gs;
|
103 |
+
c_gs = d_gs;
|
104 |
+
d_gs = a_gs + (b_gs - a_gs) / golden_ratio;
|
105 |
+
}
|
106 |
+
}
|
107 |
+
|
108 |
+
// Multiply the result by y before returning
|
109 |
+
return f((a_gs + b_gs) / 2.0) *y ;
|
110 |
+
}
|
111 |
+
|
112 |
+
int main(int argc, char* argv[]) {
|
113 |
+
// βββ Inputs from command line βββββββββββββββββββββββββββββββββββββββββββ
|
114 |
+
if (argc != 5) {
|
115 |
+
std::cerr << "Usage: " << argv[0] << " <n> <p> <a> <y>" << std::endl;
|
116 |
+
return 1;
|
117 |
+
}
|
118 |
+
|
119 |
+
int n = std::stoi(argv[1]);
|
120 |
+
int p = std::stoi(argv[2]);
|
121 |
+
double a = std::stod(argv[3]);
|
122 |
+
double y = std::stod(argv[4]);
|
123 |
+
const double b = 1.0;
|
124 |
+
|
125 |
+
std::cout << "Running with parameters: n = " << n << ", p = " << p
|
126 |
+
<< ", a = " << a << ", y = " << y << std::endl;
|
127 |
+
|
128 |
+
// βββ Beta range parameters ββββββββββββββββββββββββββββββββββββββββ
|
129 |
+
const int num_beta_points = 100; // More points for smoother curves
|
130 |
+
std::vector<double> beta_values(num_beta_points);
|
131 |
+
for (int i = 0; i < num_beta_points; ++i) {
|
132 |
+
beta_values[i] = static_cast<double>(i) / (num_beta_points - 1);
|
133 |
+
}
|
134 |
+
|
135 |
+
// βββ Storage for results ββββββββββββββββββββββββββββββββββββββββ
|
136 |
+
std::vector<double> max_eigenvalues(num_beta_points);
|
137 |
+
std::vector<double> min_eigenvalues(num_beta_points);
|
138 |
+
std::vector<double> theoretical_max_values(num_beta_points);
|
139 |
+
std::vector<double> theoretical_min_values(num_beta_points);
|
140 |
+
|
141 |
+
// βββ RandomβGaussian X and S_n ββββββββββββββββββββββββββββββββ
|
142 |
+
std::mt19937_64 rng{std::random_device{}()};
|
143 |
+
std::normal_distribution<double> norm(0.0, 1.0);
|
144 |
+
|
145 |
+
cv::Mat X(p, n, CV_64F);
|
146 |
+
for(int i = 0; i < p; ++i)
|
147 |
+
for(int j = 0; j < n; ++j)
|
148 |
+
X.at<double>(i,j) = norm(rng);
|
149 |
+
|
150 |
+
// βββ Process each beta value βββββββββββββββββββββββββββββββββ
|
151 |
+
for (int beta_idx = 0; beta_idx < num_beta_points; ++beta_idx) {
|
152 |
+
double beta = beta_values[beta_idx];
|
153 |
+
|
154 |
+
// Compute theoretical values
|
155 |
+
theoretical_max_values[beta_idx] = compute_theoretical_max(a, y, beta);
|
156 |
+
theoretical_min_values[beta_idx] = compute_theoretical_min(a, y, beta);
|
157 |
+
|
158 |
+
// βββ Build T_n matrix ββββββββββββββββββββββββββββββββββ
|
159 |
+
int k = static_cast<int>(std::floor(beta * p));
|
160 |
+
std::vector<double> diags(p);
|
161 |
+
std::fill_n(diags.begin(), k, a);
|
162 |
+
std::fill_n(diags.begin()+k, p-k, b);
|
163 |
+
std::shuffle(diags.begin(), diags.end(), rng);
|
164 |
+
|
165 |
+
cv::Mat T_n = cv::Mat::zeros(p, p, CV_64F);
|
166 |
+
for(int i = 0; i < p; ++i){
|
167 |
+
T_n.at<double>(i,i) = diags[i];
|
168 |
+
}
|
169 |
+
|
170 |
+
// βββ Form B_n = (1/n) * X * T_n * X^T ββββββββββββ
|
171 |
+
cv::Mat B = (X.t() * T_n * X) / static_cast<double>(n);
|
172 |
+
|
173 |
+
// βββ Compute eigenvalues of B ββββββββββββββββββββββββββββ
|
174 |
+
cv::Mat eigVals;
|
175 |
+
cv::eigen(B, eigVals);
|
176 |
+
std::vector<double> eigs(n);
|
177 |
+
for(int i = 0; i < n; ++i)
|
178 |
+
eigs[i] = eigVals.at<double>(i, 0);
|
179 |
+
|
180 |
+
max_eigenvalues[beta_idx] = *std::max_element(eigs.begin(), eigs.end());
|
181 |
+
min_eigenvalues[beta_idx] = *std::min_element(eigs.begin(), eigs.end());
|
182 |
+
|
183 |
+
// Progress indicator - modified to be less verbose for Streamlit
|
184 |
+
if (beta_idx % 20 == 0) {
|
185 |
+
std::cout << "Processing beta = " << beta
|
186 |
+
<< " (" << beta_idx+1 << "/" << num_beta_points << ")" << std::endl;
|
187 |
+
}
|
188 |
+
}
|
189 |
+
|
190 |
+
// βββ Prepare canvas for plotting ββββββββββββββββββββββββββββββββ
|
191 |
+
const int H = 950, W = 1200; // Taller canvas to accommodate legend below
|
192 |
+
cv::Mat canvas(H, W, CV_8UC3, cv::Scalar(250, 250, 250)); // Slightly off-white background
|
193 |
+
|
194 |
+
// βββ Find min/max for scaling βββββββββββββββββββββββββββββββββββ
|
195 |
+
double min_y = std::numeric_limits<double>::max();
|
196 |
+
double max_y = std::numeric_limits<double>::lowest();
|
197 |
+
|
198 |
+
for (double v : max_eigenvalues) max_y = std::max(max_y, v);
|
199 |
+
for (double v : min_eigenvalues) min_y = std::min(min_y, v);
|
200 |
+
for (double v : theoretical_max_values) max_y = std::max(max_y, v);
|
201 |
+
for (double v : theoretical_min_values) min_y = std::min(min_y, v);
|
202 |
+
|
203 |
+
// Add some padding
|
204 |
+
double y_padding = (max_y - min_y) * 0.15; // More padding for better spacing
|
205 |
+
min_y -= y_padding;
|
206 |
+
max_y += y_padding;
|
207 |
+
|
208 |
+
// βββ Draw coordinate axes βββββββββββββββββββββββββββββββββββββββ
|
209 |
+
const int margin = 100; // Larger margin for better spacing
|
210 |
+
const int plot_width = W - 2 * margin;
|
211 |
+
const int plot_height = H - 2 * margin - 150; // Reduced height to make room for legend below
|
212 |
+
|
213 |
+
// Plot area background (light gray)
|
214 |
+
cv::rectangle(canvas,
|
215 |
+
cv::Point(margin, margin),
|
216 |
+
cv::Point(W - margin, margin + plot_height),
|
217 |
+
cv::Scalar(245, 245, 245), cv::FILLED);
|
218 |
+
|
219 |
+
// X-axis (beta)
|
220 |
+
cv::line(canvas,
|
221 |
+
cv::Point(margin, margin + plot_height),
|
222 |
+
cv::Point(W - margin, margin + plot_height),
|
223 |
+
cv::Scalar(40, 40, 40), 2);
|
224 |
+
|
225 |
+
// Y-axis (eigenvalues)
|
226 |
+
cv::line(canvas,
|
227 |
+
cv::Point(margin, margin + plot_height),
|
228 |
+
cv::Point(margin, margin),
|
229 |
+
cv::Scalar(40, 40, 40), 2);
|
230 |
+
|
231 |
+
// βββ Draw axes labels ββββββββββββββββββββββββββββββββββββββββββββ
|
232 |
+
cv::putText(canvas, "Ξ²",
|
233 |
+
cv::Point(W - margin/2, margin + plot_height + 30),
|
234 |
+
cv::FONT_HERSHEY_COMPLEX, 1.0, cv::Scalar(0, 0, 0), 2);
|
235 |
+
|
236 |
+
// Y-axis label (fixed - no rotation)
|
237 |
+
cv::putText(canvas, "Eigenvalues",
|
238 |
+
cv::Point(margin/4, margin/2 - 10),
|
239 |
+
cv::FONT_HERSHEY_COMPLEX, 1.0, cv::Scalar(0, 0, 0), 2);
|
240 |
+
|
241 |
+
// βββ Draw title βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
242 |
+
std::stringstream title_ss;
|
243 |
+
title_ss << std::fixed << std::setprecision(2);
|
244 |
+
title_ss << "Eigenvalue Analysis: a = " << a << ", y = " << y;
|
245 |
+
cv::putText(canvas, title_ss.str(),
|
246 |
+
cv::Point(W/2 - 200, 45),
|
247 |
+
cv::FONT_HERSHEY_COMPLEX, 1.2, cv::Scalar(0, 0, 0), 2);
|
248 |
+
|
249 |
+
// βββ Draw grid lines ββββββββββββββββββββββββββββββββββββββββββββββββ
|
250 |
+
const int num_grid_lines = 11; // 0.0, 0.1, 0.2, ..., 1.0 for beta
|
251 |
+
for (int i = 0; i < num_grid_lines; ++i) {
|
252 |
+
// Horizontal grid lines
|
253 |
+
int y_pos = margin + i * (plot_height / (num_grid_lines - 1));
|
254 |
+
cv::line(canvas,
|
255 |
+
cv::Point(margin, y_pos),
|
256 |
+
cv::Point(W - margin, y_pos),
|
257 |
+
cv::Scalar(220, 220, 220), 1);
|
258 |
+
|
259 |
+
// Vertical grid lines
|
260 |
+
int x_pos = margin + i * (plot_width / (num_grid_lines - 1));
|
261 |
+
cv::line(canvas,
|
262 |
+
cv::Point(x_pos, margin),
|
263 |
+
cv::Point(x_pos, margin + plot_height),
|
264 |
+
cv::Scalar(220, 220, 220), 1);
|
265 |
+
|
266 |
+
// X-axis labels (beta values)
|
267 |
+
double beta_val = static_cast<double>(i) / (num_grid_lines - 1);
|
268 |
+
std::stringstream ss;
|
269 |
+
ss << std::fixed << std::setprecision(1) << beta_val;
|
270 |
+
cv::putText(canvas, ss.str(),
|
271 |
+
cv::Point(x_pos - 10, margin + plot_height + 30),
|
272 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 0), 1);
|
273 |
+
|
274 |
+
// Y-axis labels (eigenvalue values)
|
275 |
+
double eig_val = min_y + (max_y - min_y) * i / (num_grid_lines - 1);
|
276 |
+
std::stringstream ss2;
|
277 |
+
ss2 << std::fixed << std::setprecision(2) << eig_val;
|
278 |
+
cv::putText(canvas, ss2.str(),
|
279 |
+
cv::Point(margin/2 - 40, margin + plot_height - i * (plot_height / (num_grid_lines - 1)) + 5),
|
280 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 0), 1);
|
281 |
+
}
|
282 |
+
|
283 |
+
// βββ Draw the four curves βββββββββββββββββββββββββββββββββββββββββββ
|
284 |
+
// Convert data points to pixel coordinates
|
285 |
+
auto to_point = [&](double beta, double val) -> cv::Point {
|
286 |
+
int x = margin + static_cast<int>(beta * plot_width);
|
287 |
+
int y = margin + plot_height - static_cast<int>((val - min_y) / (max_y - min_y) * plot_height);
|
288 |
+
return cv::Point(x, y);
|
289 |
+
};
|
290 |
+
|
291 |
+
// Better colors for visibility
|
292 |
+
cv::Scalar emp_max_color(60, 60, 220); // Dark red
|
293 |
+
cv::Scalar emp_min_color(220, 60, 60); // Dark blue
|
294 |
+
cv::Scalar theo_max_color(30, 180, 30); // Dark green
|
295 |
+
cv::Scalar theo_min_color(180, 30, 180); // Dark purple
|
296 |
+
|
297 |
+
// Empirical max eigenvalues (red)
|
298 |
+
std::vector<cv::Point> max_eig_points;
|
299 |
+
for (int i = 0; i < num_beta_points; ++i) {
|
300 |
+
max_eig_points.push_back(to_point(beta_values[i], max_eigenvalues[i]));
|
301 |
+
}
|
302 |
+
cv::polylines(canvas, max_eig_points, false, emp_max_color, 3);
|
303 |
+
|
304 |
+
// Empirical min eigenvalues (blue)
|
305 |
+
std::vector<cv::Point> min_eig_points;
|
306 |
+
for (int i = 0; i < num_beta_points; ++i) {
|
307 |
+
min_eig_points.push_back(to_point(beta_values[i], min_eigenvalues[i]));
|
308 |
+
}
|
309 |
+
cv::polylines(canvas, min_eig_points, false, emp_min_color, 3);
|
310 |
+
|
311 |
+
// Theoretical max values (green)
|
312 |
+
std::vector<cv::Point> theo_max_points;
|
313 |
+
for (int i = 0; i < num_beta_points; ++i) {
|
314 |
+
theo_max_points.push_back(to_point(beta_values[i], theoretical_max_values[i]));
|
315 |
+
}
|
316 |
+
cv::polylines(canvas, theo_max_points, false, theo_max_color, 3);
|
317 |
+
|
318 |
+
// Theoretical min values (purple)
|
319 |
+
std::vector<cv::Point> theo_min_points;
|
320 |
+
for (int i = 0; i < num_beta_points; ++i) {
|
321 |
+
theo_min_points.push_back(to_point(beta_values[i], theoretical_min_values[i]));
|
322 |
+
}
|
323 |
+
cv::polylines(canvas, theo_min_points, false, theo_min_color, 3);
|
324 |
+
|
325 |
+
// βββ Draw markers on the curves for better visibility ββββββββββββββ
|
326 |
+
const int marker_interval = 10; // Show markers every 10 points
|
327 |
+
for (int i = 0; i < num_beta_points; i += marker_interval) {
|
328 |
+
// Max empirical eigenvalue markers
|
329 |
+
cv::circle(canvas, max_eig_points[i], 5, emp_max_color, cv::FILLED);
|
330 |
+
cv::circle(canvas, max_eig_points[i], 5, cv::Scalar(255, 255, 255), 1);
|
331 |
+
|
332 |
+
// Min empirical eigenvalue markers
|
333 |
+
cv::circle(canvas, min_eig_points[i], 5, emp_min_color, cv::FILLED);
|
334 |
+
cv::circle(canvas, min_eig_points[i], 5, cv::Scalar(255, 255, 255), 1);
|
335 |
+
|
336 |
+
// Theoretical max markers
|
337 |
+
cv::drawMarker(canvas, theo_max_points[i], theo_max_color, cv::MARKER_DIAMOND, 10, 2);
|
338 |
+
|
339 |
+
// Theoretical min markers
|
340 |
+
cv::drawMarker(canvas, theo_min_points[i], theo_min_color, cv::MARKER_DIAMOND, 10, 2);
|
341 |
+
}
|
342 |
+
|
343 |
+
// βββ Draw legend BELOW the graph ββββββββββββββββββββββββββββββββββββ
|
344 |
+
// Set up dimensions for the legend
|
345 |
+
const int legend_width = 600;
|
346 |
+
const int legend_height = 100;
|
347 |
+
// Center the legend horizontally
|
348 |
+
const int legend_x = W/2 - legend_width/2;
|
349 |
+
// Position legend below the graph
|
350 |
+
const int legend_y = margin + plot_height + 70;
|
351 |
+
const int line_length = 40;
|
352 |
+
const int line_spacing = 35;
|
353 |
+
|
354 |
+
// Box around legend with shadow effect
|
355 |
+
cv::rectangle(canvas,
|
356 |
+
cv::Point(legend_x + 3, legend_y + 3),
|
357 |
+
cv::Point(legend_x + legend_width + 3, legend_y + legend_height + 3),
|
358 |
+
cv::Scalar(180, 180, 180), cv::FILLED); // Shadow
|
359 |
+
cv::rectangle(canvas,
|
360 |
+
cv::Point(legend_x, legend_y),
|
361 |
+
cv::Point(legend_x + legend_width, legend_y + legend_height),
|
362 |
+
cv::Scalar(240, 240, 240), cv::FILLED); // Main box
|
363 |
+
cv::rectangle(canvas,
|
364 |
+
cv::Point(legend_x, legend_y),
|
365 |
+
cv::Point(legend_x + legend_width, legend_y + legend_height),
|
366 |
+
cv::Scalar(0, 0, 0), 1); // Border
|
367 |
+
|
368 |
+
// Legend title
|
369 |
+
cv::putText(canvas, "Legend",
|
370 |
+
cv::Point(legend_x + legend_width/2 - 30, legend_y + 20),
|
371 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 0), 1);
|
372 |
+
cv::line(canvas,
|
373 |
+
cv::Point(legend_x + 5, legend_y + 30),
|
374 |
+
cv::Point(legend_x + legend_width - 5, legend_y + 30),
|
375 |
+
cv::Scalar(150, 150, 150), 1);
|
376 |
+
|
377 |
+
// Two legend entries per row, in two columns
|
378 |
+
// First row
|
379 |
+
// Empirical max (red)
|
380 |
+
cv::line(canvas,
|
381 |
+
cv::Point(legend_x + 20, legend_y + 50),
|
382 |
+
cv::Point(legend_x + 20 + line_length, legend_y + 50),
|
383 |
+
emp_max_color, 3);
|
384 |
+
cv::circle(canvas, cv::Point(legend_x + 20 + line_length/2, legend_y + 50), 5, emp_max_color, cv::FILLED);
|
385 |
+
cv::putText(canvas, "Empirical Max Eigenvalue",
|
386 |
+
cv::Point(legend_x + 20 + line_length + 10, legend_y + 50 + 5),
|
387 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 0), 1);
|
388 |
+
|
389 |
+
// Empirical min (blue)
|
390 |
+
cv::line(canvas,
|
391 |
+
cv::Point(legend_x + 20 + legend_width/2, legend_y + 50),
|
392 |
+
cv::Point(legend_x + 20 + line_length + legend_width/2, legend_y + 50),
|
393 |
+
emp_min_color, 3);
|
394 |
+
cv::circle(canvas, cv::Point(legend_x + 20 + line_length/2 + legend_width/2, legend_y + 50), 5, emp_min_color, cv::FILLED);
|
395 |
+
cv::putText(canvas, "Empirical Min Eigenvalue",
|
396 |
+
cv::Point(legend_x + 20 + line_length + 10 + legend_width/2, legend_y + 50 + 5),
|
397 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 0), 1);
|
398 |
+
|
399 |
+
// Second row
|
400 |
+
// Theoretical max (green)
|
401 |
+
cv::line(canvas,
|
402 |
+
cv::Point(legend_x + 20, legend_y + 80),
|
403 |
+
cv::Point(legend_x + 20 + line_length, legend_y + 80),
|
404 |
+
theo_max_color, 3);
|
405 |
+
cv::drawMarker(canvas, cv::Point(legend_x + 20 + line_length/2, legend_y + 80),
|
406 |
+
theo_max_color, cv::MARKER_DIAMOND, 10, 2);
|
407 |
+
cv::putText(canvas, "Theoretical Max Function",
|
408 |
+
cv::Point(legend_x + 20 + line_length + 10, legend_y + 80 + 5),
|
409 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 0), 1);
|
410 |
+
|
411 |
+
// Theoretical min (purple)
|
412 |
+
cv::line(canvas,
|
413 |
+
cv::Point(legend_x + 20 + legend_width/2, legend_y + 80),
|
414 |
+
cv::Point(legend_x + 20 + line_length + legend_width/2, legend_y + 80),
|
415 |
+
theo_min_color, 3);
|
416 |
+
cv::drawMarker(canvas, cv::Point(legend_x + 20 + line_length/2 + legend_width/2, legend_y + 80),
|
417 |
+
theo_min_color, cv::MARKER_DIAMOND, 10, 2);
|
418 |
+
cv::putText(canvas, "Theoretical Min Function",
|
419 |
+
cv::Point(legend_x + 20 + line_length + 10 + legend_width/2, legend_y + 80 + 5),
|
420 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 0), 1);
|
421 |
+
|
422 |
+
// βββ Draw mathematical formulas in a box ββββββββββββββββββββββββββββββ
|
423 |
+
cv::rectangle(canvas,
|
424 |
+
cv::Point(margin + 3, H - 80 + 3),
|
425 |
+
cv::Point(W - margin + 3, H - 20 + 3),
|
426 |
+
cv::Scalar(180, 180, 180), cv::FILLED); // Shadow
|
427 |
+
cv::rectangle(canvas,
|
428 |
+
cv::Point(margin, H - 80),
|
429 |
+
cv::Point(W - margin, H - 20),
|
430 |
+
cv::Scalar(240, 240, 240), cv::FILLED); // Main box
|
431 |
+
cv::rectangle(canvas,
|
432 |
+
cv::Point(margin, H - 80),
|
433 |
+
cv::Point(W - margin, H - 20),
|
434 |
+
cv::Scalar(0, 0, 0), 1); // Border
|
435 |
+
|
436 |
+
std::string formula_text1 = "Max Function: max{k β (0,β)} [yΞ²(a-1)k + (ak+1)((y-1)k-1)]/[(ak+1)(kΒ²+k)y]";
|
437 |
+
std::string formula_text2 = "Min Function: min{t β (-1/a,0)} [yΞ²(a-1)t + (at+1)((y-1)t-1)]/[(at+1)(tΒ²+t)y]";
|
438 |
+
|
439 |
+
cv::putText(canvas, formula_text1,
|
440 |
+
cv::Point(margin + 20, H - 55),
|
441 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.6, theo_max_color, 2);
|
442 |
+
|
443 |
+
cv::putText(canvas, formula_text2,
|
444 |
+
cv::Point(W/2 + 20, H - 55),
|
445 |
+
cv::FONT_HERSHEY_SIMPLEX, 0.6, theo_min_color, 2);
|
446 |
+
|
447 |
+
// βββ Draw parameter info ββββββββββββββββββββββββββββββββββββββββββββ
|
448 |
+
std::stringstream params_ss;
|
449 |
+
params_ss << std::fixed << std::setprecision(2);
|
450 |
+
params_ss << "Parameters: n = " << n << ", p = " << p << ", a = " << a << ", y = " << y;
|
451 |
+
cv::putText(canvas, params_ss.str(),
|
452 |
+
cv::Point(margin, 80),
|
453 |
+
cv::FONT_HERSHEY_COMPLEX, 0.8, cv::Scalar(0, 0, 0), 1);
|
454 |
+
|
455 |
+
// βββ Save the image to the output directory βββββββββββββββββββββββββββ
|
456 |
+
std::string output_path = "/app/output/eigenvalue_analysis.png";
|
457 |
+
cv::imwrite(output_path, canvas);
|
458 |
+
std::cout << "Plot saved as " << output_path << std::endl;
|
459 |
+
|
460 |
+
return 0;
|
461 |
+
}
|