85inline std::vector<double>
real_roots(
const std::vector<double>& cin) {
86 double c[4] = {0.0, 0.0, 0.0, 0.0};
87 for (std::size_t i = 0; i < cin.size() && i < 4; ++i) c[i] = cin[i];
89 for (
int i = 0; i < 4; ++i) scale = std::max(scale, std::fabs(c[i]));
90 std::vector<double> roots;
91 if (scale == 0.0)
return roots;
93 while (d > 0 && std::fabs(c[d]) <= 1e-14 * scale) --d;
94 if (d == 0)
return roots;
96 roots.push_back(-c[0] / c[1]);
98 const double a = c[2], b = c[1], k = c[0];
99 double disc = b * b - 4.0 * a * k;
101 if (disc < -1e-12 * b * b)
return roots;
104 const double sq = std::sqrt(disc);
105 const double qq = -0.5 * (b + (b >= 0.0 ? sq : -sq));
106 roots.push_back(qq / a);
107 if (qq != 0.0) roots.push_back(k / qq);
109 const double a = c[2] / c[3], b = c[1] / c[3], k = c[0] / c[3];
111 const double P = b - a * a / 3.0;
112 const double Q = 2.0 * a * a * a / 27.0 - a * b / 3.0 + k;
113 const double disc = (Q / 2.0) * (Q / 2.0) + (P / 3.0) * (P / 3.0) * (P / 3.0);
115 const double sq = std::sqrt(disc);
116 roots.push_back(std::cbrt(-Q / 2.0 + sq) + std::cbrt(-Q / 2.0 - sq) - a / 3.0);
117 }
else if (P == 0.0) {
118 roots.push_back(-a / 3.0);
120 const double pi = 3.14159265358979323846;
121 const double m = 2.0 * std::sqrt(-P / 3.0);
122 const double arg = 3.0 * Q / (P * m);
123 const double th = std::acos(std::min(1.0, std::max(-1.0, arg))) / 3.0;
124 for (
int j = 0; j < 3; ++j) roots.push_back(m * std::cos(th - 2.0 * pi * j / 3.0) - a / 3.0);
127 for (std::size_t r = 0; r < roots.size(); ++r) {
129 for (
int it = 0; it < 2; ++it) {
130 const double f = c[0] + x * (c[1] + x * (c[2] + x * c[3]));
131 const double df = c[1] + x * (2.0 * c[2] + x * 3.0 * c[3]);
132 if (df == 0.0)
break;
141inline std::vector<double>
polymul(
const std::vector<double>& a,
const std::vector<double>& b) {
142 std::vector<double> out(a.size() + b.size() - 1, 0.0);
143 for (std::size_t i = 0; i < a.size(); ++i)
144 for (std::size_t j = 0; j < b.size(); ++j) out[i + j] += a[i] * b[j];
149inline std::vector<double>
polysub(
const std::vector<double>& a,
const std::vector<double>& b) {
150 const std::size_t n = std::max(a.size(), b.size());
151 std::vector<double> out(n, 0.0);
152 for (std::size_t i = 0; i < n; ++i)
153 out[i] = (i < a.size() ? a[i] : 0.0) - (i < b.size() ? b[i] : 0.0);
159 double xlo,
double xhi,
double xin,
double* obj,
double* xout,
161 const double tiny = 1e-14, feastol_inner = 1e-10;
162 if (!(xin >= xlo - feastol_inner * std::max(1.0, std::fabs(xlo)) &&
163 xin <= xhi + feastol_inner * std::max(1.0, std::fabs(xhi))))
165 const double x = std::min(std::max(xin, xlo), xhi);
166 const double Dx = k.
d0 + k.
d1 * x;
167 const double Nx = k.
n0 + x * (k.
n1 + x * k.
n2);
168 double lo = 0.0, hi = 1.0;
169 for (
int i = 0; i < 2; ++i) {
170 const double a = k.
alpha[i] + k.
beta[i] * x;
171 if (std::fabs(k.
gamma[i]) <= tiny) {
172 if (a < -feastol_inner || a > 1.0 + feastol_inner)
return false;
175 const double u = -a / k.
gamma[i], v = (1.0 - a) / k.
gamma[i];
176 lo = std::max(lo, std::min(u, v));
177 hi = std::min(hi, std::max(u, v));
179 double q = 0.0, s = 0.0;
180 if (std::fabs(k.
c) <= tiny) {
182 if (std::fabs(Dx) <= tiny)
return false;
184 if (q < lo - feastol_inner || q > hi + feastol_inner)
return false;
185 s = std::min(std::max(St, 0.0), 1.0);
186 }
else if (std::fabs(Dx) <= tiny) {
189 if (lo > hi + feastol_inner || s < -feastol_inner || s > 1.0 + feastol_inner)
return false;
190 s = std::min(std::max(s, 0.0), 1.0);
191 q = std::min(std::max(0.5 * (lo + hi), lo), hi);
193 const double u = Nx / Dx, v = (Nx + k.
c) / Dx;
194 lo = std::max(lo, std::min(u, v));
195 hi = std::min(hi, std::max(u, v));
196 if (lo > hi + feastol_inner)
return false;
197 q = std::min(std::max((Nx + k.
c * St) / Dx, lo), hi);
198 s = (q * Dx - Nx) / k.
c;
200 *obj = wx * (x / X - 1.0) * (x / X - 1.0) +
ws * (s / St - 1.0) * (s / St - 1.0);
212 double wx,
double ws,
double xlo,
double xhi) {
213 const double tiny = 1e-14;
214 const std::vector<double> D = {k.
d0, k.
d1};
215 const std::vector<double> N = {k.
n0, k.
n1, k.
n2};
218 std::vector<std::vector<double>> fP, fQ;
219 std::vector<double> affA, affB;
220 fP.push_back({0.0}); fQ.push_back({1.0});
221 fP.push_back({1.0}); fQ.push_back({1.0});
222 affA.push_back(0.0); affB.push_back(0.0);
223 affA.push_back(1.0); affB.push_back(0.0);
224 for (
int i = 0; i < 2; ++i) {
225 if (std::fabs(k.
gamma[i]) > tiny) {
226 for (
int kk = 0; kk < 2; ++kk) {
227 const double bnd = kk;
229 fP.push_back({a, b}); fQ.push_back({1.0});
230 affA.push_back(a); affB.push_back(b);
234 const double svals[3] = {0.0, 1.0, St};
235 for (
int j = 0; j < 3; ++j) {
236 fP.push_back({k.
n0 + k.
c * svals[j], k.
n1, k.
n2});
240 std::vector<double> cand = {X, xlo};
241 auto append = [&](
const std::vector<double>& r) { cand.insert(cand.end(), r.begin(), r.end()); };
242 if (std::isfinite(xhi)) cand.push_back(xhi);
243 if (std::fabs(k.
d1) > tiny) cand.push_back(-k.
d0 / k.
d1);
244 for (
int i = 0; i < 2; ++i) {
245 if (std::fabs(k.
beta[i]) > tiny) {
247 cand.push_back((1.0 - k.
alpha[i]) / k.
beta[i]);
250 for (
int kk = 0; kk < 2; ++kk) append(
real_roots({k.
n0 + k.
c * kk, k.
n1, k.
n2}));
251 for (std::size_t i = 0; i < fP.size(); ++i)
252 for (std::size_t j = i + 1; j < fP.size(); ++j)
254 if (std::fabs(k.
c) <= tiny) {
255 for (
int i = 0; i < 2; ++i) {
256 for (
int kk = 0; kk < 2; ++kk) {
258 std::vector<double> gN(3);
259 for (
int t = 0; t < 3; ++t) gN[t] = -k.
gamma[i] * N[t];
264 const double A =
ws / (St * St);
265 const double Bw = wx / (X * X);
266 for (std::size_t t = 0; t < affA.size(); ++t) {
267 const double a = affA[t], b = affB[t];
268 const double s2 = (b * k.
d1 - k.
n2) / k.
c;
269 const double s1 = (a * k.
d1 + b * k.
d0 - k.
n1) / k.
c;
270 const double s0 = (a * k.
d0 - k.
n0) / k.
c - St;
271 append(
real_roots({A * s1 * s0 - Bw * X, A * (s1 * s1 + 2.0 * s2 * s0) + Bw,
272 A * 3.0 * s1 * s2, A * 2.0 * s2 * s2}));
277 for (std::size_t i = 0; i < cand.size(); ++i) {
278 if (!std::isfinite(cand[i]))
continue;
280 if (
repair_evaluate(k, X, St, wx,
ws, xlo, xhi, cand[i], &obj, &x, &s) &&