13#include <pybind11/detail/typeid.h>
14#include <pybind11/pybind11.h>
15namespace py = pybind11;
19 : py::cast_error(e),
from(std::move(
from)),
to(std::move(
to)) {}
24template <
class T,
class A>
26 return [attr](T &t,
const py::handle &h) {
28 t.*attr = h.cast<
A>();
29 }
catch (
const py::cast_error &e) {
34template <
class T,
class A>
36 return [attr](
const T &t) {
return py::cast(t.*attr); };
45 std::function<void(T &,
const py::handle &)>
set;
46 std::function<py::object(
const T &)>
get;
57 const auto &m = kwargs_to_struct_table<T>;
58 for (
auto &&[key, val] : kwargs) {
59 auto skey = key.template cast<std::string>();
60 auto it = m.find(skey);
62 throw py::key_error(
"Unknown parameter " + skey);
64 it->second.set(t, val);
66 throw std::runtime_error(
"Error converting parameter '" + skey +
"' from " + e.
from +
67 " to '" + e.
to +
"': " + e.what());
68 }
catch (
const std::runtime_error &e) {
69 throw std::runtime_error(
"Error setting parameter '" + skey +
"': " + e.what());
76 const auto &m = kwargs_to_struct_table<T>;
78 for (
auto &&[key, val] : m) {
79 py::object o = val.get(t);
80 if (py::hasattr(o,
"to_dict"))
81 o = o.attr(
"to_dict")();
82 d[key.c_str()] = std::move(o);
96 return struct_to_dict_helper<T>(t);
101 return std::holds_alternative<T>(p) ? std::get<T>(p)
102 : kwargs_to_struct<T>(std::get<py::dict>(p));
109 kwargs_to_struct_table<quala::LBFGSParams>{
128 kwargs_to_struct_table<quala::AndersonAccelParams>{
136 kwargs_to_struct_table<quala::BroydenGoodParams>{
std::function< py::object(const T &)> get
attr_setter_fun_t(A T::*attr)
std::function< void(T &, const py::handle &)> set
py::dict struct_to_dict(const T &t)
void kwargs_to_struct_helper(T &t, const py::kwargs &kwargs)
std::map< std::string, attr_setter_fun_t< T > > kwargs_to_struct_table_t
kwargs_to_struct_table_t< T > kwargs_to_struct_table
auto attr_setter(A T::*attr)
T kwargs_to_struct(const py::kwargs &kwargs)
T var_kwargs_to_struct(const std::variant< T, py::dict > &p)
auto attr_getter(A T::*attr)
py::dict struct_to_dict_helper(const T &t)
real_t powell_damping_factor
Powell's trick, damping, prevents nonsingularity.
real_t min_abs_s
Reject update if .
real_t min_div_abs
Reject update if .
CBFGSParams cbfgs
Parameters in the cautious BFGS update condition.
bool restarted
If set to true, the buffer is cleared after memory iterations.
length_t memory
Length of the history to keep.
bool force_pos_def
If set to true, the inverse Hessian estimate should remain definite, i.e.
real_t min_div_fac
Reject update if .
cast_error_with_types(const py::cast_error &e, std::string from, std::string to)