Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 2401 alg solver adjoint #2421

Merged
Merged
Show file tree
Hide file tree
Changes from 96 commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
fc65777
New implementation of algebraic_solver_vari compiles.
jgaeb Mar 10, 2021
06045c6
Compiling and most tests passing.
jgaeb Mar 10, 2021
1cb5249
algebra_solver_vari chain method calls itself recursively.
jgaeb Mar 10, 2021
0955051
Fix improper initialization of nested reverse mode stack.
jgaeb Mar 12, 2021
406f197
Merge commit '7e088168b76722426c88a75e7c9d91b719137164' into HEAD
yashikno Mar 12, 2021
fe45cbb
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 12, 2021
53e637c
Minor changes.
jgaeb Mar 23, 2021
f33250c
Fix gradient being uniformly zero.
jgaeb Mar 23, 2021
7ea35df
All tests (except for degenerate_eq_test) passing.
jgaeb Mar 23, 2021
64e3e2e
Merge linting.
jgaeb Mar 23, 2021
0ae8bb4
Merge commit '51669a650fbd2a57f1163342d30c597351080cf5' into HEAD
yashikno Mar 23, 2021
a4f1605
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 23, 2021
21ec967
Fix one additional linting issue.
jgaeb Mar 23, 2021
5b4151a
Reformat algebra_solver_vari constructor.
jgaeb Mar 23, 2021
88fda19
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 23, 2021
4b20978
Revert to passing y_eval instead of y_val to algebra_solver_var const…
jgaeb Mar 23, 2021
03d39d1
Lots of updates to the algebra solver!
bbbales2 Mar 23, 2021
f474abe
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
bbbales2 Mar 23, 2021
4848150
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 23, 2021
b8804d3
Variadic stuff
bbbales2 Mar 24, 2021
7ef6851
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
bbbales2 Mar 24, 2021
abc63db
Merge commit '21ed6f2c82e97091e1d0039790e7e24127943f2e' into HEAD
yashikno Mar 24, 2021
a68bd4e
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 24, 2021
19bea28
Add adapter.
jgaeb Mar 31, 2021
0f26004
Fix templating issue and remove print statements.
jgaeb Mar 31, 2021
7b85902
Add back specialization for when y is a vector of double instead of var.
jgaeb Mar 31, 2021
15f2fa2
Merge commit '0956b106f01187a59d32dee8a7822a6a720777e1' into HEAD
yashikno Mar 31, 2021
8421de0
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 31, 2021
d2c7a96
Add back additional tests. (Message tests are broken now?)
jgaeb Mar 31, 2021
19f9a89
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
jgaeb Mar 31, 2021
dd5e119
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Mar 31, 2021
473b659
Merge remote-tracking branch 'origin/develop' into feature/issue-2401…
bbbales2 Apr 2, 2021
fe41913
Make algebra_solver_newton double variadic
bbbales2 Apr 2, 2021
28fd6e4
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Apr 2, 2021
e93899b
Finish Newton solver.
jgaeb Apr 14, 2021
31e4a1b
Merge commit 'c0de977b5a3c701b1e71c4f7a15d806b0159d4c6' into HEAD
yashikno Apr 14, 2021
5694e57
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Apr 14, 2021
a4ed898
Added more input checks (Issue #2401)
bbbales2 Apr 17, 2021
360e9e0
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Apr 17, 2021
0a40184
Clean up how x is used (Issue #2401)
bbbales2 Apr 17, 2021
bf4f660
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
bbbales2 Apr 17, 2021
f49c28f
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Apr 17, 2021
f92741f
Removed system functor test
bbbales2 Apr 17, 2021
e7d9b1d
Switch to using Cholesky factors to do matrix solve.
jgaeb Apr 27, 2021
0532d67
Switch to LU decomp for matrix solve in general case.
jgaeb Apr 27, 2021
29185c6
Fix variable names.
jgaeb May 17, 2021
81cfdb1
Fix CppLint errors.
jgaeb May 17, 2021
21bde1b
Merge commit '5534e2238a4240a496e888ccb0ee2be09d2ef7b5' into HEAD
yashikno May 18, 2021
665c9d8
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot May 18, 2021
8f1873b
Clean up includes.
jgaeb Jun 20, 2021
4da599e
Make naming more consistent.
jgaeb Jun 20, 2021
1388c05
Make fixed point algebra solver variadic.
jgaeb Jun 20, 2021
2db3f10
Make calculation consistent with explanation and fix test typo.
jgaeb Jun 20, 2021
706eb07
Better templating.
jgaeb Jun 20, 2021
9753b97
Get all tests compiling.
jgaeb Jun 20, 2021
a73a4d3
All tests passing except one.
jgaeb Jun 21, 2021
6664afe
Fix issue in last test.
jgaeb Jun 21, 2021
3c1f8e0
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
jgaeb Jun 21, 2021
e72e481
Merge commit 'da36ba4ca4b2fcf0fea9a9214b785895f07a1c95' into HEAD
yashikno Jun 21, 2021
a1a9a70
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 21, 2021
12f2949
Remove unnecessary NOLINTs
jgaeb Jun 21, 2021
177f695
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 21, 2021
4f7b442
Properly template expression function signature.
jgaeb Jun 22, 2021
8afb387
Fully template expression so it can be applied with arena types.
jgaeb Jun 22, 2021
fbe6a8d
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 22, 2021
901b603
Revert "Fully template expression so it can be applied with arena typ…
jgaeb Jun 22, 2021
bdb74b3
Fully template the simple equation functor signature.
jgaeb Jun 22, 2021
4d472f8
(Actually) revert the mistaken change introduced before.
jgaeb Jun 22, 2021
70f02f8
Try to prevent double eval.
jgaeb Jun 22, 2021
6a58089
Switched to make_chainable_ptr in powell solver (Issue #2401)
bbbales2 Jun 25, 2021
dec5dc4
Don't double evaluate expressions (Issue #2401)
bbbales2 Jun 26, 2021
425d30b
Merge commit '00405fb90982bdd3f18b8edf746e32207e7de02b' into HEAD
yashikno Jun 26, 2021
e7317f9
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 26, 2021
d3d5fbb
Fix small signature issues.
jgaeb Jun 29, 2021
b7ac36a
Fix formatting issue.
jgaeb Jun 29, 2021
82df062
Make first set of requested changes.
jgaeb Jun 29, 2021
c8291af
Merge commit 'c2edd605b30d68da5d22665de26bb3e1203c8403' into HEAD
yashikno Jun 29, 2021
d178dfa
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 29, 2021
86ce197
Respond to all comments except LU factorization.
jgaeb Jun 30, 2021
f05ab11
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 30, 2021
ff3a35d
Fix use of long int type.
jgaeb Jun 30, 2021
f02003a
Merge commit '64778f054da0145d7aec17dea4ef38bf79448f69' into HEAD
yashikno Jun 30, 2021
4926856
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jun 30, 2021
d7d9356
Cleanup algebra solvers
SteveBronder Jun 30, 2021
b0ab910
Merge commit '6b7515c0611006b12416b214acc85adffe4d1b4c' into HEAD
yashikno Jul 11, 2021
df179ad
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 11, 2021
8d864cb
Updated newton test
bbbales2 Jul 11, 2021
a93e0a7
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
bbbales2 Jul 11, 2021
dbf5515
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 11, 2021
2414959
move newton back to rev and fix check test for algebra solver newton
SteveBronder Jul 15, 2021
a569b1f
Merge commit 'c87156b59b772f8988dbf29f242eb73169398786' into HEAD
yashikno Jul 15, 2021
48e44ed
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 15, 2021
759161a
add back tests for newton solver
SteveBronder Jul 15, 2021
5be45ce
update to remote
SteveBronder Jul 15, 2021
bd72a6e
Respond to comments and temporarily patch failing test.
jgaeb Aug 3, 2021
ecefac5
Add unsafe chainable object unit tests.
jgaeb Aug 3, 2021
6a2b5b9
Add variadic tests for newton and powell solvers.
jgaeb Aug 9, 2021
24e943a
Undo changes to fixed point solver.
jgaeb Aug 10, 2021
54dbf64
Remove fixed point solve from prim functor header.
jgaeb Aug 10, 2021
a9e511f
Fix FP solver test.
jgaeb Aug 10, 2021
a0dcf9c
Fix minor formatting issues.
jgaeb Aug 10, 2021
8dd7fdd
update to develop
SteveBronder Aug 10, 2021
e19c26e
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
SteveBronder Aug 10, 2021
f6c5db6
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 10, 2021
5ce9d32
Fix AVEC / VEC in newton solver tests.
jgaeb Aug 11, 2021
70d5817
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 11, 2021
5f7709a
Restore memory leak fix for fp test.
jgaeb Aug 11, 2021
3ad4384
Merge branch 'feature/issue-2401-alg-solver-adjoint' of github.com:jg…
jgaeb Aug 11, 2021
044fa01
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 11, 2021
fec4d1f
Fix pesky multiple evaluation bug.
jgaeb Aug 11, 2021
b852e9a
Fully document algebra_solver_powell_call_solver.
jgaeb Aug 11, 2021
8d4e2db
Merge commit '02dc560b022f328f917af80a1b9b7f1feb249ee4' into HEAD
yashikno Aug 11, 2021
09e1544
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Aug 11, 2021
0b93db8
Attempt to resolve non-const lvalue reference issue.
jgaeb Aug 12, 2021
09ca12d
hard copy x before passing it to the solver impl for powell. Use a ch…
SteveBronder Aug 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stan/math/prim/functor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stan/math/prim/functor/apply_scalar_unary.hpp>
#include <stan/math/prim/functor/apply_scalar_binary.hpp>
#include <stan/math/prim/functor/apply_vector_unary.hpp>
#include <stan/math/prim/functor/algebra_solver_fp.hpp>
#include <stan/math/prim/functor/coupled_ode_system.hpp>
#include <stan/math/prim/functor/finite_diff_gradient.hpp>
#include <stan/math/prim/functor/finite_diff_gradient_auto.hpp>
Expand Down
27 changes: 27 additions & 0 deletions stan/math/prim/functor/algebra_solver_adapter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef STAN_MATH_PRIM_FUNCTOR_ALGEBRA_SOLVER_ADAPTER_HPP
#define STAN_MATH_PRIM_FUNCTOR_ALGEBRA_SOLVER_ADAPTER_HPP

#include <ostream>
#include <vector>

/**
* Adapt the non-variadic algebra_solver_newton and algebra_solver_powell
* arguemts to the variadic algebra_solver_newton_impl and
* algebra_solver_powell_impl interfaces.
*
* @tparam F type of function to adapt.
*/
template <typename F>
struct algebra_solver_adapter {
const F& f_;

explicit algebra_solver_adapter(const F& f) : f_(f) {}

template <typename T1, typename T2, typename T3, typename T4>
auto operator()(const T1& x, std::ostream* msgs, const T2& y, const T3& dat,
const T4& dat_int) const {
return f_(x, y, dat, dat_int, msgs);
}
};

#endif
302 changes: 302 additions & 0 deletions stan/math/prim/functor/algebra_solver_fp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
#ifndef STAN_MATH_PRIM_FUNCTOR_FP_SOLVER_HPP
#define STAN_MATH_PRIM_FUNCTOR_FP_SOLVER_HPP
jgaeb marked this conversation as resolved.
Show resolved Hide resolved

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/value_of.hpp>
#include <stan/math/prim/fun/eval.hpp>
#include <stan/math/prim/functor/apply.hpp>
#include <stan/math/prim/functor/algebra_solver_adapter.hpp>
#include <kinsol/kinsol.h>
#include <sunmatrix/sunmatrix_dense.h>
#include <sunlinsol/sunlinsol_dense.h>
#include <nvector/nvector_serial.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

namespace stan {
namespace math {

/**
* KINSOL algebraic system data holder that handles
* construction & destruction of SUNDIALS data, as well as
* auxiliary data that will be used for functor evaluation.
*
* @tparam F functor type for system function.
* @tparam T_u type of scaling vector for unknowns. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
* @tparam T_f type of scaling vector for residual. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
* @tparam Args types of additional parameters to the equation system functor.
*/
template <typename F, typename T_u, typename T_f, typename... Args>
struct KinsolFixedPointEnv {
/** RHS functor. */
const F& f_;
/** system size */
const size_t N_;
/** message stream */
std::ostream* msgs_;
/** arguments and parameters */
std::tuple<const Args&...> args_tuple_;
/** KINSOL memory block */
void* mem_;
/** NVECTOR for unknowns */
N_Vector nv_x_;
/** NVECTOR for scaling u */
N_Vector nv_u_scal_;
/** NVECTOR for scaling f */
N_Vector nv_f_scal_;

KinsolFixedPointEnv(const F& f, const Eigen::MatrixXd x,
const std::vector<T_u>& u_scale,
const std::vector<T_f>& f_scale, std::ostream* const msgs,
const Args&... args)
: f_(f),
N_(x.size()),
msgs_(msgs),
args_tuple_(args...),
mem_(KINCreate()),
nv_x_(N_VNew_Serial(N_)),
nv_u_scal_(N_VNew_Serial(N_)),
nv_f_scal_(N_VNew_Serial(N_)) {
for (int i = 0; i < N_; ++i) {
NV_Ith_S(nv_x_, i) = stan::math::value_of(x(i));
NV_Ith_S(nv_u_scal_, i) = stan::math::value_of(u_scale[i]);
NV_Ith_S(nv_f_scal_, i) = stan::math::value_of(f_scale[i]);
}
}

~KinsolFixedPointEnv() {
N_VDestroy_Serial(nv_x_);
N_VDestroy_Serial(nv_u_scal_);
N_VDestroy_Serial(nv_f_scal_);
KINFree(&mem_);
}

/** Implements the user-defined function passed to KINSOL. */
static int kinsol_f_system(const N_Vector x, const N_Vector f,
void* const user_data) {
auto g = static_cast<const KinsolFixedPointEnv<F, T_u, T_f, Args...>*>(
user_data);
Eigen::Map<Eigen::VectorXd>(N_VGetArrayPointer(f), g->N_) = apply(
[&x, &g](const auto&... args) {
return g->f_(Eigen::Map<Eigen::VectorXd>(NV_DATA_S(x), g->N_),
g->msgs_, args...);
},
g->args_tuple_);
return 0;
}
};

/**
* Solve algebraic equations using KINSOL fixed point solver
*
* @tparam F type of the equation system functor f
* @tparam T_u type of scaling vector for unknowns. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
* @tparam T_f type of scaling vector for residual. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
* @param x initial point and final solution.
* @param env KINSOL solution environment
* @param f_tol Function tolerance
* @param max_num_steps max nb. of iterations.
*/
template <typename F, typename T, typename T_u, typename T_f, typename... Args,
require_eigen_vector_t<T>* = nullptr>
Eigen::VectorXd kinsol_solve_fp(const F& f, const T& x,
const double function_tolerance,
const double max_num_steps,
const std::vector<T_u>& u_scale,
const std::vector<T_f>& f_scale,
std::ostream* const msgs, const Args&... args) {
KinsolFixedPointEnv<F, T_u, T_f, Args...> env(f, x, u_scale, f_scale, msgs,
args...);
const int N = x.size();
constexpr int default_anderson_depth = 4;
const int anderson_depth = std::min(N, default_anderson_depth);

check_flag_sundials(KINSetNumMaxIters(env.mem_, max_num_steps),
"KINSetNumMaxIters");
check_flag_sundials(KINSetMAA(env.mem_, anderson_depth), "KINSetMAA");
check_flag_sundials(KINInit(env.mem_, &env.kinsol_f_system, env.nv_x_),
"KINInit");
check_flag_sundials(KINSetFuncNormTol(env.mem_, function_tolerance),
"KINSetFuncNormTol");
check_flag_sundials(KINSetUserData(env.mem_, static_cast<void*>(&env)),
"KINSetUserData");

check_flag_kinsol(
KINSol(env.mem_, env.nv_x_, KIN_FP, env.nv_u_scal_, env.nv_f_scal_),
max_num_steps);

return Eigen::Map<Eigen::VectorXd>(N_VGetArrayPointer(env.nv_x_), N);
}

/**
* Return a fixed pointer to the specified system of algebraic
* equations of form
* \[
* x = F(x; theta)
* \]
* given an initial guess \(x\), and parameters \(theta\) and data. Use the
* KINSOL solver from the SUNDIALS suite.
*
* The user can also specify the scaling controls, the function
* tolerance, and the maximum number of steps.
*
* This function is overloaded to handle both constant and var-type parameters.
* This overload handles var parameters, and checks the input, calls the
* algebraic solver, and appropriately handles derivative propagation through
* the `reverse_pass_callback`.
*
* @tparam F type of equation system function.
* @tparam T type of initial guess vector.
* @tparam T_u type of scaling vector for unknowns. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
* @tparam T_f type of scaling vector for residual. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
*
* @param[in] f functor that evaluated the system of equations.
* @param[in] x vector of starting values.
* @param[in, out] msgs the print stream for warning messages.
* @param[in] u_scale diagonal scaling matrix elements \(Du\)
* such that \(Du x\) has all components roughly the same
* magnitude when \(x\) is close to a solution.
* (ref. KINSOL user guide chap.2 sec. "Scaling")
* @param[in] f_scale diagonal scaling matrix elements such
* that \(Df (x - f(x))\) has all components roughly the same
* magnitude when \(x\) is not too close to a solution.
* (ref. KINSOL user guide chap.2 sec. "Scaling")
* @param[in] function_tolerance Function-norm stopping tolerance.
* @param[in] max_num_steps maximum number of function evaluations.
* @param[in] args additional parameters to the equation system functor.
* @pre f returns finite values when passed any value of x and the given args.
* @throw <code>std::invalid_argument</code> if x has size zero.
* @throw <code>std::invalid_argument</code> if x has non-finite elements.
* @throw <code>std::invalid_argument</code> if scaled_step_size is strictly
* negative.
* @throw <code>std::invalid_argument</code> if function_tolerance is strictly
* negative.
* @throw <code>std::invalid_argument</code> if max_num_steps is not positive.
* @throw <code>boost::math::evaluation_error</code> (which is a subclass of
* <code>std::runtime_error</code>) if solver exceeds max_num_steps.
*/
template <typename F, typename T, typename T_u, typename T_f, typename... Args,
require_eigen_vector_t<T>* = nullptr,
require_all_st_arithmetic<Args...>* = nullptr>
Eigen::VectorXd algebra_solver_fp_impl(const F& f, const T& x,
std::ostream* const msgs,
const std::vector<T_u>& u_scale,
const std::vector<T_f>& f_scale,
const double function_tolerance,
const int max_num_steps,
const Args&... args) {
const auto& x_ref = to_ref(value_of(x));

check_nonzero_size("algebra_solver_fp", "initial guess", x_ref);
check_finite("algebra_solver_fp", "initial guess", x_ref);
check_nonnegative("algebra_solver_fp", "u_scale", u_scale);
check_nonnegative("algebra_solver_fp", "f_scale", f_scale);
check_nonnegative("algebra_solver_fp", "function_tolerance",
function_tolerance);
check_positive("algebra_solver_fp", "max_num_steps", max_num_steps);
check_matching_sizes("algebra_solver_fp", "the algebraic system's output",
value_of(f(x_ref, msgs, args...)),
"the vector of unknowns, x,", x_ref);

return kinsol_solve_fp(f, x_ref, function_tolerance, max_num_steps, u_scale,
f_scale, msgs, args...);
}

/**
* Return a fixed pointer to the specified system of algebraic
* equations of form
*
* x = F(x; theta)
*
* given an initial guess x, and parameters theta and data. Use the
* KINSOL solver from the SUNDIALS suite.
*
* The user can also specify the scaling controls, the function
* tolerance, and the maximum number of steps.
*
* Signature to maintain backward compatibility, will be removed
* in the future.
*
* @tparam F type of equation system function.
* @tparam T type of initial guess vector. The final solution
* type doesn't depend on initial guess type,
* but we allow initial guess to be either data or param.
* @tparam T_u type of scaling vector for unknowns. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
* @tparam T_f type of scaling vector for residual. We allow
* it to be @c var because scaling could be parameter
* dependent. Internally these params are converted to data
* because scaling is applied.
*
* @param[in] f Functor that evaluated the system of equations.
* @param[in] x Vector of starting values.
* @param[in] y Parameter vector for the equation system. The function
* is overloaded to treat y as a vector of doubles or of a
* a template type T.
* @param[in] dat Continuous data vector for the equation system.
* @param[in] dat_int Integer data vector for the equation system.
* @param[in, out] msgs The print stream for warning messages.
* @param[in] u_scale diagonal scaling matrix elements Du
* such that Du*x has all components roughly the same
* magnitude when x is close to a solution.
* (ref. KINSOL user guide chap.2 sec. "Scaling")
* @param[in] f_scale diagonal scaling matrix elements such
* that Df*(x-f(x)) has all components roughly the same
* magnitude when x is not too close to a solution.
* (ref. KINSOL user guide chap.2 sec. "Scaling")
* @param[in] f_tol Function-norm stopping tolerance.
* @param[in] max_num_steps maximum number of function evaluations.
* @pre f returns finite values when passed any value of x and the given y, dat,
* and dat_int.
* @throw <code>std::invalid_argument</code> if x has size zero.
* @throw <code>std::invalid_argument</code> if x has non-finite elements.
* @throw <code>std::invalid_argument</code> if dat has non-finite elements.
* @throw <code>std::invalid_argument</code> if dat_int has non-finite elements.
* @throw <code>std::invalid_argument</code> if scaled_step_size is strictly
* negative.
* @throw <code>std::invalid_argument</code> if function_tolerance is strictly
* negative.
* @throw <code>std::invalid_argument</code> if max_num_steps is not positive.
* @throw <code>boost::math::evaluation_error</code> (which is a subclass of
* <code>std::runtime_error</code>) if solver exceeds max_num_steps.
*/
template <typename F, typename T1, typename T2, typename T_u, typename T_f,
require_eigen_vector_t<T1>* = nullptr,
require_eigen_vector_t<T2>* = nullptr>
Eigen::Matrix<scalar_type_t<T2>, Eigen::Dynamic, 1> algebra_solver_fp(
const F& f, const T1& x, const T2& y, const std::vector<double>& dat,
const std::vector<int>& dat_int, const std::vector<T_u>& u_scale,
const std::vector<T_f>& f_scale, std::ostream* const msgs = nullptr,
double function_tolerance = 1e-8, int max_num_steps = 200) {
return algebra_solver_fp_impl(algebra_solver_adapter<F>(f), to_ref(x), msgs,
u_scale, f_scale, function_tolerance,
max_num_steps, to_ref(y), dat, dat_int);
}

} // namespace math
} // namespace stan

#endif
Loading