49class Ridders :
public internal::ridders_solver_traits<Functor_> {
51 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
53 using Functor = Functor_;
54 using Scalar =
typename Functor::Scalar;
55 using InputType =
typename Functor::InputType;
56 using ValueType =
typename Functor::ValueType;
57 using JacobianType =
typename Functor::JacobianType;
59 static constexpr int InputsAtCompileTime = Functor::InputsAtCompileTime;
60 static constexpr int ValuesAtCompileTime = Functor::ValuesAtCompileTime;
90 eigen_assert(
parameters.stepReduction > Scalar(1) &&
102 const Functor&
functor()
const {
return m_functor; }
110 int inputs()
const {
return m_functor.inputs(); }
112 int values()
const {
return m_functor.values(); }
129 int df(
const InputType& x, JacobianType& jac)
const {
return dfImpl(x, jac,
nullptr); }
143 int df(
const InputType& x, JacobianType& jac, InputType& error)
const {
144 error.resize(x.size());
145 return dfImpl(x, jac, &error);
150 Parameters m_parameters;
152 int dfImpl(
const InputType& input, JacobianType& jac, InputType* error)
const {
154 NUMERICAL_DIFF_REQUIRES_A_REAL_FLOATING_POINT_SCALAR);
158 const Index
values = m_functor.values();
159 eigen_assert(jac.rows() ==
values && jac.cols() == input.size());
160 if (
values == 0 || input.size() == 0) {
161 if (error) error->setZero();
165 Tableau workspace(
values, 2 * Index(m_parameters.maxSteps));
166 auto first = workspace.leftCols(m_parameters.maxSteps);
167 auto second = workspace.rightCols(m_parameters.maxSteps);
168 auto current = &first;
169 auto previous = &second;
174 const Scalar reductionSquared = m_parameters.stepReduction * m_parameters.stepReduction;
177 for (Index j = 0; j < input.size(); ++j) {
178 Scalar h = m_parameters.initialStep * numext::maxi(numext::abs(input[j]), Scalar(1));
179 Scalar previousPlus = input[j], previousMinus = input[j];
180 Scalar bestError = NumTraits<Scalar>::infinity();
181 jac.col(j).setConstant(NumTraits<Scalar>::quiet_NaN());
182 for (
int step = 0; step < m_parameters.maxSteps; ++step) {
183 Scalar xPlus = input[j] + h;
184 Scalar xMinus = input[j] - h;
185 internal::pbarrier(xPlus);
186 internal::pbarrier(xMinus);
187 if (!(numext::isfinite)(xPlus) || !(numext::isfinite)(xMinus))
break;
188 if (step > 0 && xPlus == previousPlus && xMinus == previousMinus)
break;
190 const Scalar separation = xPlus - xMinus;
191 if (!(separation > Scalar(0)) || !(numext::isfinite)(separation))
break;
192 previousPlus = xPlus;
193 previousMinus = xMinus;
202 current->col(0) = (plus - minus) / separation;
203 if (!current->col(0).allFinite()) {
204 if (separation <= Scalar(1))
break;
206 auto quotient = current->col(0).array();
207 quotient = quotient.isFiniteTyped().select(quotient, plus.array() / separation - minus.array() / separation);
208 if (!current->col(0).allFinite())
break;
210 Index bestOrder = step == 0 ? 0 : -1;
212 Scalar factor = reductionSquared;
214 for (
int order = 1; order <= step; ++order) {
217 current->col(order) =
218 current->col(order - 1) + (current->col(order - 1) - previous->col(order - 1)) / (factor - Scalar(1));
219 if (!current->col(order).allFinite()) {
224 const Scalar estimate =
225 numext::maxi((current->col(order) - current->col(order - 1)).template lpNorm<Infinity>(),
226 (current->col(order) - previous->col(order - 1)).template lpNorm<Infinity>());
227 if ((numext::isfinite)(estimate) && estimate <= bestError) {
228 bestError = estimate;
232 factor *= reductionSquared;
234 if (bestOrder >= 0) jac.col(j) = current->col(bestOrder);
237 (current->col(step) - previous->col(step - 1)).template lpNorm<Infinity>() >= Scalar(2) * bestError)
240 std::swap(current, previous);
241 h /= m_parameters.stepReduction;
243 if (error) (*error)[j] = bestError;