15 std::size_t N = x.size();
16 std::vector<double> r, xp;
17 std::vector<std::vector<double>> J(N, std::vector<double>(N, 0));
18 std::vector<double> r0 =
call(x);
20 for (std::size_t i = 0; i < N; ++i) {
22 epsilon = 0.001 * x[i];
26 for (std::size_t j = 0; j < N; ++j) {
27 J[j][i] = (r[j] - r0[j]) / epsilon;
53 std::vector<double> f0, v;
54 std::vector<std::vector<double>> JJ;
55 std::vector<double> x0 = x;
56 Eigen::VectorXd r(x0.size());
57 Eigen::MatrixXd J(x0.size(), x0.size());
59 while (iter == 0 || std::abs(error) > tol) {
63 for (std::size_t i = 0; i < x0.size(); ++i) {
65 for (std::size_t j = 0; j < x0.size(); ++j) {
70 Eigen::VectorXd v = J.colPivHouseholderQr().solve(-r);
73 double max_relchange = -1;
74 for (std::size_t i = 0; i < x0.size(); i++) {
76 double relchange = std::abs(v(i) / x0[i]);
77 if (std::abs(x0[i]) > 1e-16 && relchange > max_relchange) {
78 max_relchange = relchange;
83 double max_abschange = v.cwiseAbs().maxCoeff();
87 if (max_relchange < 1e-12) {
92 f->
errstring =
"reached maximum number of iterations";
110 double x, dx, fval = 999;
114 while (iter < 2 || std::abs(fval) > ftol) {
116 dx = -fval / f->
deriv(x);
119 throw ValueError(
"Residual function in newton returned invalid number");
124 if (std::abs(dx / x) < 1e-11) {
128 if (iter > maxiter) {
129 f->
errstring =
"reached maximum number of iterations";
153 double x, dx, fval = 999, dfdx, d2fdx2;
163 while (f->
iter < 2 || std::abs(fval) > ftol) {
173 throw ValueError(
"Residual function in Halley returned invalid number");
176 throw ValueError(
"Derivative function in Halley returned invalid number");
179 dx = -omega * (2 * fval * dfdx) / (2 *
POW2(dfdx) - fval * d2fdx2);
183 if (std::abs(dx / x) < xtol_rel) {
187 if (f->
iter > maxiter) {
188 f->
errstring =
"reached maximum number of iterations";
213 double x, dx, fval = 999, dfdx, d2fdx2, d3fdx3;
223 while (f->
iter < 2 || std::abs(fval) > ftol) {
234 throw ValueError(
"Residual function in Householder4 returned invalid number");
237 throw ValueError(
"Derivative function in Householder4 returned invalid number");
240 throw ValueError(
"Second derivative function in Householder4 returned invalid number");
243 throw ValueError(
"Third derivative function in Householder4 returned invalid number");
246 dx = -omega * fval * (
POW2(dfdx) - fval * d2fdx2 / 2.0) / (
POW3(dfdx) - fval * dfdx * d2fdx2 + d3fdx3 *
POW2(fval) / 6.0);
250 if (std::abs(dx / x) < xtol_rel) {
254 if (f->
iter > maxiter) {
255 f->
errstring =
"reached maximum number of iterations";
274 #if defined(COOLPROP_DEEP_DEBUG)
275 static std::vector<double> xlog, flog;
281 double x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, x = x0, fval = 999;
288 if (std::abs(dx) == 0) {
292 while (f->
iter <= 2 || std::abs(fval) > tol) {
311 #if defined(COOLPROP_DEEP_DEBUG)
313 flog.push_back(fval);
317 throw ValueError(
"Residual function in secant returned invalid number");
323 double deltax = x2 - x1;
324 if (std::abs(deltax) < 1e-14) {
328 double deltay = y2 - y1;
329 if (f->
iter > 2 && std::abs(deltay) < 1e-14) {
332 x3 = x2 - omega * y2 / (y2 - y1) * (x2 - x1);
337 if (f->
iter > maxiter) {
338 f->
errstring = std::string(
"reached maximum number of iterations");
359 double x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, x, fval = 999;
362 if (std::abs(dx) == 0) {
366 while (iter <= 3 || std::abs(fval) > tol) {
370 }
else if (iter == 2) {
381 x3 = x2 - y2 / (y2 - y1) * (x2 - x1);
384 x3 = (xmin + x2) / 2;
387 x3 = (xmax + x2) / 2;
393 if (iter > maxiter) {
394 f->
errstring =
"reached maximum number of iterations";
416 #if defined(COOLPROP_DEEP_DEBUG)
417 static std::vector<double> xlog, flog;
418 xlog.clear(); flog.clear();
422 double x1=0,x2=0,x3=0,y0=0,y1=0,y2=0,x=x0,fval=999;
429 if (std::abs(dx)==0){ f->
errstring=
"dx cannot be zero";
return _HUGE;}
430 while (f->
iter<=2 || std::abs(fval)>tol)
432 if (f->
iter==1){x1=x0; x=x1;}
433 if (f->
iter==2){x2=x0+dx; x=x2;}
434 if (f->
iter>2) {x=x2;}
442 #if defined(COOLPROP_DEEP_DEBUG)
444 flog.push_back(fval);
448 if (f->
iter==1){
return x;}
449 else {
return x2-omega*y1/(y1-y0)*(x2-x1);}
451 if (f->
iter==1){y1=fval;}
454 double deltax = x2-x1;
455 if (std::abs(deltax)<1e-14){
459 double deltay = y2-y1;
460 if (f->
iter > 2 && std::abs(deltay)<1e-14){
463 x3=x2-omega*y2/(y2-y1)*(x2-x1);
464 y0=y1;y1=y2; x1=x2; x2=x3;
469 f->
errstring=std::string(
"reached maximum number of iterations");
495 double fa, fb, c, fc, m, tol, d, e, p, q, s, r;
500 if (std::abs(fb) < t) {
504 throw ValueError(
format(
"Brent's method f(b) is NAN for b = %g, other input was a = %g", b, a).c_str());
506 if (std::abs(fa) < t) {
510 throw ValueError(
format(
"Brent's method f(a) is NAN for a = %g, other input was b = %g", a, b).c_str());
513 throw ValueError(
format(
"Inputs in Brent [%f,%f] do not bracket the root. Function values are [%f,%f]", a, b, fa, fb));
519 if (std::abs(fc) < std::abs(fb)) {
531 tol = 2 * macheps * std::abs(b) + t;
532 while (std::abs(m) > tol && fb != 0) {
534 if (std::abs(e) < tol || std::abs(fa) <= std::abs(fb)) {
548 p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
549 q = (q - 1) * (r - 1) * (s - 1);
559 if (2 * p < 3 * m * q - std::abs(tol * q) || p < std::abs(0.5 * s * q)) {
568 if (std::abs(d) > tol) {
577 throw ValueError(
format(
"Brent's method f(t) is NAN for t = %g", b).c_str());
579 if (std::abs(fb) < macheps) {
588 if (std::abs(fc) < std::abs(fb)) {
598 tol = 2 * macheps * std::abs(b) + t;
609 if (iter > maxiter) {
610 throw SolutionError(
format(
"Brent's method reached maximum number of steps of %d ", maxiter));
612 if (std::abs(fb) < 2 * macheps * std::abs(b)) {