1- #ifndef STAN_MATH_REV_FUNCTOR_ALGEBRA_SOLVER_POWELL_HPP
2- #define STAN_MATH_REV_FUNCTOR_ALGEBRA_SOLVER_POWELL_HPP
1+ #ifndef STAN_MATH_REV_FUNCTOR_SOLVE_POWELL_HPP
2+ #define STAN_MATH_REV_FUNCTOR_SOLVE_POWELL_HPP
33
44#include < stan/math/rev/meta.hpp>
55#include < stan/math/rev/core.hpp>
@@ -45,11 +45,10 @@ namespace math {
4545 */
4646template <typename F, typename T, typename ... Args,
4747 require_eigen_vector_t <T>* = nullptr >
48- T& algebra_solver_powell_call_solver (const F& f, T& x, std::ostream* const msgs,
49- const double relative_tolerance,
50- const double function_tolerance,
51- const int64_t max_num_steps,
52- const Args&... args) {
48+ T& solve_powell_call_solver (const F& f, T& x, std::ostream* const msgs,
49+ const double relative_tolerance,
50+ const double function_tolerance,
51+ const int64_t max_num_steps, const Args&... args) {
5352 // Construct the solver
5453 hybrj_functor_solver<F> hfs (f);
5554 Eigen::HybridNonLinearSolver<hybrj_functor_solver<F>> solver (hfs);
@@ -103,11 +102,11 @@ T& algebra_solver_powell_call_solver(const F& f, T& x, std::ostream* const msgs,
103102 *
104103 * @param[in] f Functor that evaluates the system of equations.
105104 * @param[in] x Vector of starting values (initial guess).
106- * @param[in, out] msgs the print stream for warning messages.
107105 * @param[in] relative_tolerance determines the convergence criteria
108106 * for the solution.
109107 * @param[in] function_tolerance determines whether roots are acceptable.
110108 * @param[in] max_num_steps maximum number of function evaluations.
109+ * @param[in, out] msgs the print stream for warning messages.
111110 * @param[in] args additional parameters to the equation system functor.
112111 * @return theta Vector of solutions to the system of equations.
113112 * @pre f returns finite values when passed any value of x and the given args.
@@ -126,12 +125,12 @@ T& algebra_solver_powell_call_solver(const F& f, T& x, std::ostream* const msgs,
126125template <typename F, typename T, typename ... Args,
127126 require_eigen_vector_t <T>* = nullptr ,
128127 require_all_st_arithmetic<Args...>* = nullptr >
129- Eigen::VectorXd algebra_solver_powell_impl (const F& f, const T& x,
130- std::ostream* const msgs ,
131- const double relative_tolerance ,
132- const double function_tolerance ,
133- const int64_t max_num_steps ,
134- const Args&... args) {
128+ Eigen::VectorXd solve_powell_tol (const F& f, const T& x,
129+ const double relative_tolerance ,
130+ const double function_tolerance ,
131+ const int64_t max_num_steps ,
132+ std::ostream* const msgs ,
133+ const Args&... args) {
135134 auto x_ref = eval (value_of (x));
136135 auto args_vals_tuple = std::make_tuple (to_ref (args)...);
137136
@@ -141,20 +140,18 @@ Eigen::VectorXd algebra_solver_powell_impl(const F& f, const T& x,
141140 args_vals_tuple);
142141 };
143142
144- check_nonzero_size (" algebra_solver_powell " , " initial guess" , x_ref);
145- check_finite (" algebra_solver_powell " , " initial guess" , x_ref);
143+ check_nonzero_size (" solve_powell " , " initial guess" , x_ref);
144+ check_finite (" solve_powell " , " initial guess" , x_ref);
146145 check_nonnegative (" alegbra_solver_powell" , " relative_tolerance" ,
147146 relative_tolerance);
148- check_nonnegative (" algebra_solver_powell" , " function_tolerance" ,
149- function_tolerance);
150- check_positive (" algebra_solver_powell" , " max_num_steps" , max_num_steps);
151- check_matching_sizes (" algebra_solver" , " the algebraic system's output" ,
147+ check_nonnegative (" solve_powell" , " function_tolerance" , function_tolerance);
148+ check_positive (" solve_powell" , " max_num_steps" , max_num_steps);
149+ check_matching_sizes (" solve_powell" , " the algebraic system's output" ,
152150 f_wrt_x (x_ref), " the vector of unknowns, x," , x_ref);
153151
154152 // Solve the system
155- return algebra_solver_powell_call_solver (f_wrt_x, x_ref, msgs,
156- relative_tolerance,
157- function_tolerance, max_num_steps);
153+ return solve_powell_call_solver (f_wrt_x, x_ref, msgs, relative_tolerance,
154+ function_tolerance, max_num_steps);
158155}
159156
160157/* *
@@ -163,31 +160,20 @@ Eigen::VectorXd algebra_solver_powell_impl(const F& f, const T& x,
163160 * which get passed into the algebraic system.
164161 * Use Powell's dogleg solver.
165162 *
166- * The user can also specify the relative tolerance
167- * (xtol in Eigen's code), the function tolerance,
168- * and the maximum number of steps (maxfev in Eigen's code).
163+ * This signature does not let the user specify the tuning parameters of the
164+ * solver (instead default values are used).
169165 *
170166 * @tparam F type of equation system function
171- * @tparam T1 type of elements in the x vector
172- * @tparam T2 type of elements in the y vector
167+ * @tparam T type of elements in the x vector
168+ * @tparam Args types of additional input to the equation system functor
173169 *
174170 * @param[in] f Functor that evaluates the system of equations.
175171 * @param[in] x Vector of starting values (initial guess).
176- * @param[in] y parameter vector for the equation system.
177- * @param[in] dat continuous data vector for the equation system.
178- * @param[in] dat_int integer data vector for the equation system.
179172 * @param[in, out] msgs the print stream for warning messages.
180- * @param[in] relative_tolerance determines the convergence criteria
181- * for the solution.
182- * @param[in] function_tolerance determines whether roots are acceptable.
183- * @param[in] max_num_steps maximum number of function evaluations.
173+ * @param[in] args Additional parameters to the equation system functor.
184174 * @return theta Vector of solutions to the system of equations.
185175 * @throw <code>std::invalid_argument</code> if x has size zero.
186176 * @throw <code>std::invalid_argument</code> if x has non-finite elements.
187- * @throw <code>std::invalid_argument</code> if y has non-finite elements.
188- * @throw <code>std::invalid_argument</code> if dat has non-finite elements.
189- * @throw <code>std::invalid_argument</code> if dat_int has non-finite
190- * elements.
191177 * @throw <code>std::invalid_argument</code> if relative_tolerance is strictly
192178 * negative.
193179 * @throw <code>std::invalid_argument</code> if function_tolerance is strictly
@@ -197,17 +183,20 @@ Eigen::VectorXd algebra_solver_powell_impl(const F& f, const T& x,
197183 * @throw <code>std::domain_error</code> if the norm of the solution exceeds
198184 * the function tolerance.
199185 */
200- template <typename F, typename T1, typename T2,
201- require_all_eigen_vector_t <T1, T2>* = nullptr >
202- Eigen::Matrix<value_type_t <T2>, Eigen::Dynamic, 1 > algebra_solver_powell (
203- const F& f, const T1& x, const T2& y, const std::vector<double >& dat,
204- const std::vector<int >& dat_int, std::ostream* const msgs = nullptr ,
205- const double relative_tolerance = 1e-10 ,
206- const double function_tolerance = 1e-6 ,
207- const int64_t max_num_steps = 1e+3 ) {
208- return algebra_solver_powell_impl (algebra_solver_adapter<F>(f), x, msgs,
209- relative_tolerance, function_tolerance,
210- max_num_steps, y, dat, dat_int);
186+ template <typename F, typename T, typename ... T_Args,
187+ require_eigen_vector_t <T>* = nullptr >
188+ Eigen::Matrix<stan::return_type_t <T_Args...>, Eigen::Dynamic, 1 > solve_powell (
189+ const F& f, const T& x, std::ostream* const msgs, const T_Args&... args) {
190+ double relative_tolerance = 1e-10 ;
191+ double function_tolerance = 1e-6 ;
192+ int64_t max_num_steps = 200 ;
193+ const auto & args_ref_tuple = std::make_tuple (to_ref (args)...);
194+ return math::apply (
195+ [&](const auto &... args_refs) {
196+ return solve_powell_tol (f, x, relative_tolerance, function_tolerance,
197+ max_num_steps, msgs, args_refs...);
198+ },
199+ args_ref_tuple);
211200}
212201
213202/* *
@@ -265,8 +254,9 @@ Eigen::Matrix<value_type_t<T2>, Eigen::Dynamic, 1> algebra_solver(
265254 const double relative_tolerance = 1e-10 ,
266255 const double function_tolerance = 1e-6 ,
267256 const int64_t max_num_steps = 1e+3 ) {
268- return algebra_solver_powell (f, x, y, dat, dat_int, msgs, relative_tolerance,
269- function_tolerance, max_num_steps);
257+ return solve_powell_tol (algebra_solver_adapter<F>(f), x, relative_tolerance,
258+ function_tolerance, max_num_steps, msgs, y, dat,
259+ dat_int);
270260}
271261
272262/* *
@@ -309,11 +299,11 @@ Eigen::Matrix<value_type_t<T2>, Eigen::Dynamic, 1> algebra_solver(
309299 *
310300 * @param[in] f Functor that evaluates the system of equations.
311301 * @param[in] x Vector of starting values (initial guess).
312- * @param[in, out] msgs the print stream for warning messages.
313302 * @param[in] relative_tolerance determines the convergence criteria
314303 * for the solution.
315304 * @param[in] function_tolerance determines whether roots are acceptable.
316305 * @param[in] max_num_steps maximum number of function evaluations.
306+ * @param[in, out] msgs the print stream for warning messages.
317307 * @param[in] args Additional parameters to the equation system functor.
318308 * @return theta Vector of solutions to the system of equations.
319309 * @pre f returns finite values when passed any value of x and the given args.
@@ -332,10 +322,10 @@ Eigen::Matrix<value_type_t<T2>, Eigen::Dynamic, 1> algebra_solver(
332322template <typename F, typename T, typename ... T_Args,
333323 require_eigen_vector_t <T>* = nullptr ,
334324 require_any_st_var<T_Args...>* = nullptr >
335- Eigen::Matrix<var, Eigen::Dynamic, 1 > algebra_solver_powell_impl (
336- const F& f, const T& x, std::ostream* const msgs ,
337- const double relative_tolerance , const double function_tolerance ,
338- const int64_t max_num_steps , const T_Args&... args) {
325+ Eigen::Matrix<var, Eigen::Dynamic, 1 > solve_powell_tol (
326+ const F& f, const T& x, const double relative_tolerance ,
327+ const double function_tolerance , const int64_t max_num_steps ,
328+ std::ostream* const msgs , const T_Args&... args) {
339329 auto x_ref = eval (value_of (x));
340330 auto arena_args_tuple = make_chainable_ptr (std::make_tuple (eval (args)...));
341331 auto args_vals_tuple = math::apply (
@@ -350,19 +340,18 @@ Eigen::Matrix<var, Eigen::Dynamic, 1> algebra_solver_powell_impl(
350340 args_vals_tuple);
351341 };
352342
353- check_nonzero_size (" algebra_solver_powell " , " initial guess" , x_ref);
354- check_finite (" algebra_solver_powell " , " initial guess" , x_ref);
343+ check_nonzero_size (" solve_powell " , " initial guess" , x_ref);
344+ check_finite (" solve_powell " , " initial guess" , x_ref);
355345 check_nonnegative (" alegbra_solver_powell" , " relative_tolerance" ,
356346 relative_tolerance);
357- check_nonnegative (" algebra_solver_powell" , " function_tolerance" ,
358- function_tolerance);
359- check_positive (" algebra_solver_powell" , " max_num_steps" , max_num_steps);
360- check_matching_sizes (" algebra_solver" , " the algebraic system's output" ,
347+ check_nonnegative (" solve_powell" , " function_tolerance" , function_tolerance);
348+ check_positive (" solve_powell" , " max_num_steps" , max_num_steps);
349+ check_matching_sizes (" solve_powell" , " the algebraic system's output" ,
361350 f_wrt_x (x_ref), " the vector of unknowns, x," , x_ref);
362351
363352 // Solve the system
364- algebra_solver_powell_call_solver (f_wrt_x, x_ref, msgs, relative_tolerance,
365- function_tolerance, max_num_steps);
353+ solve_powell_call_solver (f_wrt_x, x_ref, msgs, relative_tolerance,
354+ function_tolerance, max_num_steps);
366355
367356 Eigen::MatrixXd Jf_x;
368357 Eigen::VectorXd f_x;
0 commit comments