13#ifndef OPTIMIST_SCALAR_ROOT_FINDER_VARONA4_HH
14#define OPTIMIST_SCALAR_ROOT_FINDER_VARONA4_HH
20 namespace ScalarRootFinder
39 template <
typename Real>
71 std::ostringstream os;
73 if (this->
m_method == Method::ORDER_4) {os <<
"4";}
74 else if (this->
m_method == Method::ORDER_8) {os <<
"8";}
75 else if (this->
m_method == Method::ORDER_16) {os <<
"16";}
76 else if (this->
m_method == Method::ORDER_32) {os <<
"32";}
77 else {os <<
"UNKNOWN";}
130 #define CMD "Optimist::ScalarRootFinder::Varona::solve(...): "
140 Real residuals, step_norm;
141 Real x_old, x_new, function_old, function_new, step_old, step_new;
142 Real first_derivative_old;
160 if (std::abs(first_derivative_old) < EPSILON_LOW) {
162 first_derivative_old = (first_derivative_old > 0.0) ? EPSILON_LOW : -EPSILON_LOW;
165 this->
compute_step(function, x_old, function_old, first_derivative_old, step_old);
168 residuals = std::abs(function_old);
169 step_norm = std::abs(step_old);
171 if (residuals < tolerance_residuals || step_norm < tolerance_step_norm) {
178 damped = this->
damp(function, x_old, function_old, step_old, x_new, function_new, step_new);
182 x_new = x_old + step_old;
188 function_old = function_new;
215 Real function_y, function_z, function_w, function_h, step_tmp, t, s, u, v;
220 step_old = -function_old/first_derivative_old;
223 if (this->
m_method == Method::ORDER_4 || this->
m_method == Method::ORDER_8 ||
226 if (std::abs(function_y) < tolerance_residuals) {
return;}
227 t = function_y/function_old;
228 step_tmp = this->
Q(t) * (function_y/first_derivative_old);
229 if (std::isfinite(step_tmp)) {step_old -= step_tmp;}
234 if (this->
m_method == Method::ORDER_8 || this->
m_method == Method::ORDER_16 ||
235 this->
m_method == Method::ORDER_32) {
237 if (std::abs(function_z) < tolerance_residuals) {
return;}
238 s = function_z/function_y;
239 step_tmp = this->
W(t, s) * (function_z/first_derivative_old);
240 if (std::isfinite(step_tmp)) {step_old -= step_tmp;}
245 if (this->
m_method == Method::ORDER_16 || this->
m_method == Method::ORDER_32) {
247 if (std::abs(function_w) < tolerance_residuals) {
return;}
248 u = function_w/function_z;
249 step_tmp = this->
H(t, s, u) * (function_w/first_derivative_old);
250 if (std::isfinite(step_tmp)) {step_old -= step_tmp;}
255 if (this->
m_method == Method::ORDER_32) {
257 if (std::abs(function_h) < tolerance_residuals) {
return;}
258 v = (function_h/function_w);
259 step_tmp = this->
J(t, s, u, v) * (function_h/first_derivative_old);
260 if (std::isfinite(step_tmp)) {step_old -= step_tmp;}
265 "Optimist::ScalarRootFinder::Varona::compute_step(...): step is not finite.");
273 static Real
Q(Real t) {
return 1.0 + 2.0*t;}
281 static Real
W(Real t, Real s) {
return t*t*(1.0 - 4.0*t) + (4.0*s + 2.0)*t + s + 1.0;}
290 static Real
H(Real t, Real s, Real u) {
297 ((8.0*u + 6.0*t2 + 4.0)*s -
298 (6.0*t8 + 4.0*(s + u + 1.0))*t1 +
299 2.0*t8 - 4.0*t17 + t23 + 2.0)*t +
300 t1*(t8 + s + u + 1.0) +
301 (1.0 - 3.0*t2 + t23)*s +
313 static Real
J(Real t, Real s, Real u, Real v) {
322 Real t65{u + 1.0 + v};
323 Real t76{(-2.0*t22 + u + 4.0*v + 2.0)*u};
325 (-1.0 + 2.0*t)*(2.0 + 5.0*t)*u*t*t2 +
326 (4.0*t + 1.0)*u*s*t2 +
327 (u*t22 - 2.0*u*v - u - v - 1.0)*(4.0*t17 + 3.0*t + 1.0)*( - 1.0 + t) -
328 8.0*(t22*(t17/2.0 - 1.0/4.0) + u*(t17*t32 - 5.0/8.0*t34 - 3.0/4.0*t32 +
329 3.0/8.0*t37 + 3.0/4.0*t17 - t/8.0 - 1.0/4.0) + 3.0/4.0*t46*(t + 1.0/2.0)*(t - 2.0/3.0))*t*t1 +
330 4.0*(t22*( - 3.0/2.0*t - 1.0/4.0) + u*(t34 - t32 - 3.0/2.0*t37 + t17/4.0 - t - 1.0/4.0) -
331 t46*(t + 1.0/4.0))*s*t1 + (1.0 + v + t65*t17 - 4.0*t65*t37 - 3.0*t65*t32 + 6.0*t65*t34 +
332 t76 + 4.0*(1.0 + v + t76)*t)*s;
#define OPTIMIST_WARNING(MSG)
Definition Optimist.hh:52
#define OPTIMIST_ASSERT_WARNING(COND, MSG)
Definition Optimist.hh:60
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:43
ScalarRootFinder()
Definition ScalarRootFinder.hh:66
typename ScalarRootFinder< Real, Varona< Real > >::SecondDerivativeWrapper SecondDerivativeWrapper
Definition Varona.hh:53
void enable_8th_order_method()
Definition Varona.hh:101
void set_method(Method t_method)
Definition Varona.hh:117
Varona()
Definition Varona.hh:63
static constexpr bool requires_second_derivative
Definition Varona.hh:45
typename ScalarRootFinder< Real, Varona< Real > >::FunctionWrapper FunctionWrapper
Definition Varona.hh:51
Method m_method
Definition Varona.hh:56
void enable_32th_order_method()
Definition Varona.hh:111
static constexpr bool requires_function
Definition Varona.hh:43
void enable_16th_order_method()
Definition Varona.hh:106
enum class Method :Integer {ORDER_4=41, ORDER_8=8, ORDER_16=16, ORDER_32=32} Method
Definition Varona.hh:50
bool solve_impl(FunctionWrapper function, FirstDerivativeWrapper first_derivative, Real x_ini, Real &x_sol)
Definition Varona.hh:127
Method method() const
Definition Varona.hh:85
static Real J(Real t, Real s, Real u, Real v)
Definition Varona.hh:313
void method(Method t_method)
Definition Varona.hh:91
static Real H(Real t, Real s, Real u)
Definition Varona.hh:290
typename ScalarRootFinder< Real, Varona< Real > >::FirstDerivativeWrapper FirstDerivativeWrapper
Definition Varona.hh:52
static Real Q(Real t)
Definition Varona.hh:273
void compute_step(FunctionWrapper function, Real x_old, Real function_old, Real first_derivative_old, Real &step_old)
Definition Varona.hh:212
std::string name_impl() const
Definition Varona.hh:69
static constexpr bool requires_first_derivative
Definition Varona.hh:44
void enable_4th_order_method()
Definition Varona.hh:96
static Real W(Real t, Real s)
Definition Varona.hh:281
Real m_tolerance
Definition Solver.hh:92
bool m_verbose
Definition Solver.hh:93
bool m_converged
Definition Solver.hh:99
bool m_damped
Definition Solver.hh:94
void store_trace(const InputType &x)
Definition Solver.hh:750
void header()
Definition Solver.hh:799
void reset()
Definition Solver.hh:693
bool damp(FunctionWrapper function, InputType const &x_old, InputType const &function_old, InputType const &step_old, InputType &x_new, InputType &function_new, InputType &step_new)
Definition Solver.hh:763
void evaluate_first_derivative(FirstDerivativeWrapper function, const InputType &x, FirstDerivativeType &out)
Definition Solver.hh:724
void bottom()
Definition Solver.hh:829
void evaluate_function(FunctionWrapper function, const InputType &x, OutputType &out)
Definition Solver.hh:710
void info(Real residuals, std::string const ¬es="-")
Definition Solver.hh:853
Integer m_iterations
Definition Solver.hh:85
Integer m_max_iterations
Definition Solver.hh:86
Namespace for the Optimist library.
Definition Optimist.hh:87
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:95