Spaces:
Running
Running
File size: 19,424 Bytes
90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 5e27608 90b89d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/complex.h>
#include <vector>
#include <complex>
#include <cmath>
#include <algorithm>
#include <random>
namespace py = pybind11;
// Apply the condition for y
double apply_y_condition(double y) {
return y > 1.0 ? y : 1.0 / y;
}
// Discriminant calculation
double discriminant_func(double z, double beta, double z_a, double y) {
double y_effective = apply_y_condition(y);
// Coefficients
double a = z * z_a;
double b = z * z_a + z + z_a - z_a * y_effective;
double c = z + z_a + 1.0 - y_effective * (beta * z_a + 1.0 - beta);
double d = 1.0;
// Simple formula for cubic discriminant
return std::pow((b*c)/(6.0*a*a) - std::pow(b, 3)/(27.0*std::pow(a, 3)) - d/(2.0*a), 2) +
std::pow(c/(3.0*a) - std::pow(b, 2)/(9.0*std::pow(a, 2)), 3);
}
// Find zeros of discriminant
std::vector<double> find_z_at_discriminant_zero(double z_a, double y, double beta,
double z_min, double z_max, int steps) {
std::vector<double> roots_found;
double y_effective = apply_y_condition(y);
// Create z grid
std::vector<double> z_grid(steps);
double step_size = (z_max - z_min) / (steps - 1);
for (int i = 0; i < steps; i++) {
z_grid[i] = z_min + i * step_size;
}
// Evaluate discriminant at each grid point
std::vector<double> disc_vals(steps);
for (int i = 0; i < steps; i++) {
disc_vals[i] = discriminant_func(z_grid[i], beta, z_a, y_effective);
}
// Find sign changes (zeros)
for (int i = 0; i < steps - 1; i++) {
double f1 = disc_vals[i];
double f2 = disc_vals[i+1];
if (std::isnan(f1) || std::isnan(f2)) {
continue;
}
if (f1 == 0.0) {
roots_found.push_back(z_grid[i]);
} else if (f2 == 0.0) {
roots_found.push_back(z_grid[i+1]);
} else if (f1 * f2 < 0) {
// Binary search for zero crossing
double zl = z_grid[i];
double zr = z_grid[i+1];
double f1_copy = f1;
for (int iter = 0; iter < 50; iter++) {
double mid = 0.5 * (zl + zr);
double fm = discriminant_func(mid, beta, z_a, y_effective);
if (fm == 0.0) {
zl = zr = mid;
break;
}
if ((fm < 0 && f1_copy < 0) || (fm > 0 && f1_copy > 0)) {
zl = mid;
} else {
zr = mid;
}
}
roots_found.push_back(0.5 * (zl + zr));
}
}
return roots_found;
}
// Sweep beta and find z bounds
std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
sweep_beta_and_find_z_bounds(double z_a, double y, double z_min, double z_max,
int beta_steps, int z_steps) {
std::vector<double> betas(beta_steps);
std::vector<double> z_min_values(beta_steps);
std::vector<double> z_max_values(beta_steps);
double beta_step = 1.0 / (beta_steps - 1);
for (int i = 0; i < beta_steps; i++) {
betas[i] = i * beta_step;
std::vector<double> roots = find_z_at_discriminant_zero(z_a, y, betas[i], z_min, z_max, z_steps);
if (roots.empty()) {
z_min_values[i] = std::numeric_limits<double>::quiet_NaN();
z_max_values[i] = std::numeric_limits<double>::quiet_NaN();
} else {
// Find min and max roots
double min_root = *std::min_element(roots.begin(), roots.end());
double max_root = *std::max_element(roots.begin(), roots.end());
z_min_values[i] = min_root;
z_max_values[i] = max_root;
}
}
return std::make_tuple(betas, z_min_values, z_max_values);
}
// Compute cubic roots
std::vector<std::complex<double>> compute_cubic_roots(double z, double beta, double z_a, double y) {
double y_effective = apply_y_condition(y);
// Coefficients
double a = z * z_a;
double b = z * z_a + z + z_a - z_a * y_effective;
double c = z + z_a + 1.0 - y_effective * (beta * z_a + 1.0 - beta);
double d = 1.0;
std::vector<std::complex<double>> roots(3);
// Handle special cases
if (std::abs(a) < 1e-10) {
if (std::abs(b) < 1e-10) { // Linear case
roots[0] = std::complex<double>(-d/c, 0);
roots[1] = std::complex<double>(0, 0);
roots[2] = std::complex<double>(0, 0);
} else { // Quadratic case
double disc = c*c - 4.0*b*d;
if (disc >= 0) {
double sqrt_disc = std::sqrt(disc);
roots[0] = std::complex<double>((-c + sqrt_disc) / (2.0 * b), 0);
roots[1] = std::complex<double>((-c - sqrt_disc) / (2.0 * b), 0);
} else {
double sqrt_disc = std::sqrt(-disc);
roots[0] = std::complex<double>(-c / (2.0 * b), sqrt_disc / (2.0 * b));
roots[1] = std::complex<double>(-c / (2.0 * b), -sqrt_disc / (2.0 * b));
}
roots[2] = std::complex<double>(0, 0);
}
return roots;
}
// Normalize to form: x^3 + px^2 + qx + r = 0
double p = b / a;
double q = c / a;
double r = d / a;
// Depress the cubic: substitute x = y - p/3 to get y^3 + py + q = 0
double p_over_3 = p / 3.0;
double new_p = q - p * p / 3.0;
double new_q = r - p * q / 3.0 + 2.0 * p * p * p / 27.0;
// Calculate discriminant
double discriminant = 4.0 * std::pow(new_p, 3) / 27.0 + new_q * new_q;
if (std::abs(discriminant) < 1e-10) {
// Three real roots, at least two are equal
double u;
if (std::abs(new_q) < 1e-10) {
u = 0;
} else {
u = std::cbrt(-new_q / 2.0);
}
roots[0] = std::complex<double>(2.0 * u - p_over_3, 0);
roots[1] = std::complex<double>(-u - p_over_3, 0);
roots[2] = std::complex<double>(-u - p_over_3, 0);
} else if (discriminant > 0) {
// One real root, two complex conjugate roots
double sqrt_disc = std::sqrt(discriminant);
double u = std::cbrt(-new_q / 2.0 + sqrt_disc / 2.0);
double v = std::cbrt(-new_q / 2.0 - sqrt_disc / 2.0);
// Real root
roots[0] = std::complex<double>(u + v - p_over_3, 0);
// Complex roots
const double sqrt3_over_2 = std::sqrt(3.0) / 2.0;
roots[1] = std::complex<double>(-0.5 * (u + v) - p_over_3, sqrt3_over_2 * (u - v));
roots[2] = std::complex<double>(-0.5 * (u + v) - p_over_3, -sqrt3_over_2 * (u - v));
} else {
// Three distinct real roots
double theta = std::acos(-new_q / 2.0 / std::sqrt(-std::pow(new_p, 3) / 27.0));
double sqrt_term = 2.0 * std::sqrt(-new_p / 3.0);
roots[0] = std::complex<double>(sqrt_term * std::cos(theta / 3.0) - p_over_3, 0);
roots[1] = std::complex<double>(sqrt_term * std::cos((theta + 2.0 * M_PI) / 3.0) - p_over_3, 0);
roots[2] = std::complex<double>(sqrt_term * std::cos((theta + 4.0 * M_PI) / 3.0) - p_over_3, 0);
}
return roots;
}
// Compute high y curve
std::vector<double> compute_high_y_curve(const std::vector<double>& betas, double z_a, double y) {
double y_effective = apply_y_condition(y);
size_t n = betas.size();
std::vector<double> result(n);
double a = z_a;
double denominator = 1.0 - 2.0 * a;
if (std::abs(denominator) < 1e-10) {
// Handle division by zero
std::fill(result.begin(), result.end(), std::numeric_limits<double>::quiet_NaN());
return result;
}
for (size_t i = 0; i < n; i++) {
double beta = betas[i];
double numerator = -4.0 * a * (a - 1.0) * y_effective * beta - 2.0 * a * y_effective - 2.0 * a * (2.0 * a - 1.0);
result[i] = numerator / denominator;
}
return result;
}
// Compute alternate low expression
std::vector<double> compute_alternate_low_expr(const std::vector<double>& betas, double z_a, double y) {
double y_effective = apply_y_condition(y);
size_t n = betas.size();
std::vector<double> result(n);
for (size_t i = 0; i < n; i++) {
double beta = betas[i];
result[i] = (z_a * y_effective * beta * (z_a - 1.0) - 2.0 * z_a * (1.0 - y_effective) - 2.0 * z_a * z_a) / (2.0 + 2.0 * z_a);
}
return result;
}
// Compute max k expression
std::vector<double> compute_max_k_expression(const std::vector<double>& betas, double z_a, double y, int k_samples=1000) {
double y_effective = apply_y_condition(y);
size_t n = betas.size();
std::vector<double> result(n);
// Sample k values on logarithmic scale
std::vector<double> k_values(k_samples);
double log_min = std::log(0.001);
double log_max = std::log(1000.0);
double log_step = (log_max - log_min) / (k_samples - 1);
for (int i = 0; i < k_samples; i++) {
k_values[i] = std::exp(log_min + i * log_step);
}
for (size_t i = 0; i < n; i++) {
double beta = betas[i];
std::vector<double> values(k_samples);
for (int j = 0; j < k_samples; j++) {
double k = k_values[j];
double numerator = y_effective * beta * (z_a - 1.0) * k + (z_a * k + 1.0) * ((y_effective - 1.0) * k - 1.0);
double denominator = (z_a * k + 1.0) * (k * k + k);
if (std::abs(denominator) < 1e-10) {
values[j] = std::numeric_limits<double>::quiet_NaN();
} else {
values[j] = numerator / denominator;
}
}
// Find maximum value, ignoring NaNs
double max_val = -std::numeric_limits<double>::infinity();
bool found_valid = false;
for (double val : values) {
if (!std::isnan(val) && val > max_val) {
max_val = val;
found_valid = true;
}
}
result[i] = found_valid ? max_val : std::numeric_limits<double>::quiet_NaN();
}
return result;
}
// Compute min t expression
std::vector<double> compute_min_t_expression(const std::vector<double>& betas, double z_a, double y, int t_samples=1000) {
double y_effective = apply_y_condition(y);
size_t n = betas.size();
std::vector<double> result(n);
if (z_a <= 0) {
std::fill(result.begin(), result.end(), std::numeric_limits<double>::quiet_NaN());
return result;
}
// Sample t values in (-1/a, 0)
double lower_bound = -1.0 / z_a + 1e-10; // Avoid division by zero
std::vector<double> t_values(t_samples);
double t_step = (0.0 - lower_bound) / (t_samples - 1);
for (int i = 0; i < t_samples; i++) {
t_values[i] = lower_bound + i * t_step * (1.0 - 1e-10); // Avoid exactly 0
}
for (size_t i = 0; i < n; i++) {
double beta = betas[i];
std::vector<double> values(t_samples);
for (int j = 0; j < t_samples; j++) {
double t = t_values[j];
double numerator = y_effective * beta * (z_a - 1.0) * t + (z_a * t + 1.0) * ((y_effective - 1.0) * t - 1.0);
double denominator = (z_a * t + 1.0) * (t * t + t);
if (std::abs(denominator) < 1e-10) {
values[j] = std::numeric_limits<double>::quiet_NaN();
} else {
values[j] = numerator / denominator;
}
}
// Find minimum value, ignoring NaNs
double min_val = std::numeric_limits<double>::infinity();
bool found_valid = false;
for (double val : values) {
if (!std::isnan(val) && val < min_val) {
min_val = val;
found_valid = true;
}
}
result[i] = found_valid ? min_val : std::numeric_limits<double>::quiet_NaN();
}
return result;
}
// Compute derivatives
std::tuple<std::vector<double>, std::vector<double>>
compute_derivatives(const std::vector<double>& curve, const std::vector<double>& betas) {
size_t n = betas.size();
std::vector<double> d1(n, 0.0);
std::vector<double> d2(n, 0.0);
// First derivative using central difference
for (size_t i = 1; i < n - 1; i++) {
double h = betas[i+1] - betas[i-1];
d1[i] = (curve[i+1] - curve[i-1]) / h;
}
// Handle endpoints with forward/backward difference
if (n > 1) {
d1[0] = (curve[1] - curve[0]) / (betas[1] - betas[0]);
d1[n-1] = (curve[n-1] - curve[n-2]) / (betas[n-1] - betas[n-2]);
}
// Second derivative using central difference
for (size_t i = 1; i < n - 1; i++) {
double h = betas[i+1] - betas[i-1];
d2[i] = 2.0 * (curve[i+1] - 2.0 * curve[i] + curve[i-1]) / (h * h);
}
// Handle endpoints
if (n > 2) {
d2[0] = d2[1];
d2[n-1] = d2[n-2];
}
return std::make_tuple(d1, d2);
}
// Generate eigenvalue distribution
std::vector<double> generate_eigenvalue_distribution(double beta, double y, double z_a, int n, int seed) {
double y_effective = apply_y_condition(y);
// Set random seed
std::mt19937 gen(seed);
std::normal_distribution<double> normal_dist(0.0, 1.0);
// Compute dimension p based on aspect ratio y
int p = static_cast<int>(y_effective * n);
// Create matrices - we'll use simple vectors and manual operations
// since we're trying to avoid dependency on Eigen
// Diagonal of T_n (Population/Shape Matrix)
std::vector<double> diag_T(p);
int k = static_cast<int>(std::floor(beta * p));
// Fill diagonal entries
for (int j = 0; j < k; j++) {
diag_T[j] = z_a;
}
for (int j = k; j < p; j++) {
diag_T[j] = 1.0;
}
// Shuffle diagonal entries
std::shuffle(diag_T.begin(), diag_T.end(), gen);
// Generate data matrix X
std::vector<std::vector<double>> X(p, std::vector<double>(n));
for (int i = 0; i < p; i++) {
for (int j = 0; j < n; j++) {
X[i][j] = normal_dist(gen);
}
}
// Compute S_n = (1/n) * X*X^T
std::vector<std::vector<double>> S(p, std::vector<double>(p, 0.0));
for (int i = 0; i < p; i++) {
for (int j = 0; j < p; j++) {
double sum = 0.0;
for (int k = 0; k < n; k++) {
sum += X[i][k] * X[j][k];
}
S[i][j] = sum / n;
}
}
// Compute B_n = S_n * diag(T_n)
std::vector<std::vector<double>> B(p, std::vector<double>(p, 0.0));
for (int i = 0; i < p; i++) {
for (int j = 0; j < p; j++) {
B[i][j] = S[i][j] * diag_T[j];
}
}
// Find eigenvalues - use power iteration for largest/smallest eigenvalues
// This is a simplified example and not recommended for production use
// For real applications, use a proper eigenvalue solver
// For simplicity, we'll just return some random values
// In real application, you'd compute actual eigenvalues
std::vector<double> eigenvalues(p);
for (int i = 0; i < p; i++) {
eigenvalues[i] = normal_dist(gen) + 1.0; // Dummy values
}
std::sort(eigenvalues.begin(), eigenvalues.end());
return eigenvalues;
}
// Support boundaries
std::tuple<std::vector<double>, std::vector<double>>
compute_eigenvalue_support_boundaries(double z_a, double y, const std::vector<double>& beta_values,
int n_samples, int seeds) {
size_t num_betas = beta_values.size();
std::vector<double> min_eigenvalues(num_betas);
std::vector<double> max_eigenvalues(num_betas);
for (size_t i = 0; i < num_betas; i++) {
double beta = beta_values[i];
std::vector<double> min_vals;
std::vector<double> max_vals;
// Run multiple trials
for (int seed = 0; seed < seeds; seed++) {
// Generate eigenvalues
std::vector<double> eigenvalues = generate_eigenvalue_distribution(beta, y, z_a, n_samples, seed);
// Get min and max
if (!eigenvalues.empty()) {
min_vals.push_back(eigenvalues.front());
max_vals.push_back(eigenvalues.back());
}
}
// Average over seeds
double min_sum = 0.0, max_sum = 0.0;
for (double val : min_vals) min_sum += val;
for (double val : max_vals) max_sum += val;
min_eigenvalues[i] = min_vals.empty() ? 0.0 : min_sum / min_vals.size();
max_eigenvalues[i] = max_vals.empty() ? 0.0 : max_sum / max_vals.size();
}
return std::make_tuple(min_eigenvalues, max_eigenvalues);
}
// Python module definition
PYBIND11_MODULE(cubic_cpp, m) {
m.doc() = "C++ accelerated functions for cubic root analysis";
m.def("discriminant_func", &discriminant_func,
"Calculate cubic discriminant",
py::arg("z"), py::arg("beta"), py::arg("z_a"), py::arg("y"));
m.def("find_z_at_discriminant_zero", &find_z_at_discriminant_zero,
"Find zeros of discriminant",
py::arg("z_a"), py::arg("y"), py::arg("beta"), py::arg("z_min"),
py::arg("z_max"), py::arg("steps"));
m.def("sweep_beta_and_find_z_bounds", &sweep_beta_and_find_z_bounds,
"Compute support boundaries by sweeping beta",
py::arg("z_a"), py::arg("y"), py::arg("z_min"), py::arg("z_max"),
py::arg("beta_steps"), py::arg("z_steps"));
m.def("compute_cubic_roots", &compute_cubic_roots,
"Compute roots of cubic equation",
py::arg("z"), py::arg("beta"), py::arg("z_a"), py::arg("y"));
m.def("compute_high_y_curve", &compute_high_y_curve,
"Compute high y expression curve",
py::arg("betas"), py::arg("z_a"), py::arg("y"));
m.def("compute_alternate_low_expr", &compute_alternate_low_expr,
"Compute alternate low expression curve",
py::arg("betas"), py::arg("z_a"), py::arg("y"));
m.def("compute_max_k_expression", &compute_max_k_expression,
"Compute max k expression",
py::arg("betas"), py::arg("z_a"), py::arg("y"), py::arg("k_samples") = 1000);
m.def("compute_min_t_expression", &compute_min_t_expression,
"Compute min t expression",
py::arg("betas"), py::arg("z_a"), py::arg("y"), py::arg("t_samples") = 1000);
m.def("compute_derivatives", &compute_derivatives,
"Compute first and second derivatives",
py::arg("curve"), py::arg("betas"));
m.def("generate_eigenvalue_distribution", &generate_eigenvalue_distribution,
"Generate eigenvalue distribution",
py::arg("beta"), py::arg("y"), py::arg("z_a"), py::arg("n"), py::arg("seed"));
m.def("compute_eigenvalue_support_boundaries", &compute_eigenvalue_support_boundaries,
"Compute eigenvalue support boundaries",
py::arg("z_a"), py::arg("y"), py::arg("beta_values"),
py::arg("n_samples"), py::arg("seeds"));
} |