Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
newton_refine.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/nag_algorithms/newton_refine.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/nag_algorithms/newton_refine.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/nag_algorithms/newton_refine.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// silviana amethyst, university of wisconsin eau claire
17
38
39#pragma once
40
44
45namespace bertini {
46namespace algorithm {
47
53template <typename ComplexT>
61
85template <typename ComplexT>
87 Vec<ComplexT> const& start,
88 NumErrorT tolerance,
89 unsigned max_iterations,
90 ComplexT time = ComplexT(0))
91{
92 const auto n_funcs = S.NumTotalFunctions();
93 const auto n_vars = S.NumVariables();
94 if (n_funcs != n_vars)
95 {
96 std::stringstream ss;
97 ss << "NewtonRefine requires a SQUARE system, but this one has "
98 << n_funcs << " total functions (user functions plus patches) over "
99 << n_vars << " variables. Square an overdetermined system by "
100 << "randomization (multiply by a generic full-rank matrix) before refining.";
101 throw std::runtime_error(ss.str());
102 }
103 if (start.size() != static_cast<Eigen::Index>(n_vars))
104 {
105 std::stringstream ss;
106 ss << "NewtonRefine: start point has " << start.size()
107 << " coordinates but the system has " << n_vars << " variables.";
108 throw std::runtime_error(ss.str());
109 }
110
112 start, static_cast<NumErrorT>(-1), 0};
113
114 Vec<ComplexT> f(n_funcs);
115 Mat<ComplexT> J(n_funcs, n_vars);
116 Vec<ComplexT> step(n_vars);
118 lu.ChangeSize(static_cast<unsigned>(n_vars)); // the LU workspace is stateful:
119 // without this, Factor/Solve run
120 // over a 0-dimensional system
121
122 for (unsigned it = 0; it < max_iterations; ++it)
123 {
124 if (S.HavePathVariable())
125 S.SetAndReset(result.point, time);
126 else
127 S.SetAndReset(result.point);
128 S.EvalInPlace(f);
129 S.JacobianInPlace(J);
130
132 {
134 return result;
135 }
136 lu.Solve(f, step); // step = +J^{-1} f = -(Newton step)
137 result.point -= step;
138 result.iterations = it + 1;
139 result.achieved = static_cast<NumErrorT>(
140 step.template lpNorm<Eigen::Infinity>());
141
142 if (result.achieved <= tolerance)
143 {
144 result.code = SuccessCode::Success;
145 return result;
146 }
147 }
148 return result;
149}
150
151} // namespace algorithm
152} // namespace bertini
The fundamental polynomial system class for Bertini2.
Definition system.hpp:106
void SetAndReset(Vec< T > const &new_space, T const &new_time) const
Stage the system's current point (variables and path value) for a subsequent Eval/Jacobian.
Definition system.hpp:964
void EvalInPlace(Vec< T > &function_values) const
Evaluate the system using the previously set variable (and time) values, in place.
Definition system.hpp:213
void JacobianInPlace(Mat< T > &J) const
Evaluate the Jacobian matrix of the system, using the previous space and time values,...
Definition system.hpp:413
bool HavePathVariable() const
Query whether a path variable is set for this system.
size_t NumVariables() const
Get the total number of variables in this system, including homogenizing variables.
size_t NumTotalFunctions() const
Get the total number of functions, including patches.
Stateful partial-pivot LU factor/solve with preallocated, precision-adjustable banks.
Definition lu_solver.hpp:58
void ChangeSize(unsigned n)
(Re)size the workspace to hold n x n systems.
Definition lu_solver.hpp:65
MatrixSuccessCode Factor(Mat< NumT > const &A)
Factor a copy of A with partial pivoting; A is preserved.
Definition lu_solver.hpp:90
void Solve(Vec< NumT > const &b, Eigen::MatrixBase< OutDerived > const &x_out) const
Solve A x = b using the stored factors.
Definition lu_solver.hpp:111
A stateful partial-pivot LU solver that owns precision-adjustable temporary banks.
Definition algorithm_base.hpp:34
NewtonRefineResult< ComplexT > NewtonRefine(System const &S, Vec< ComplexT > const &start, NumErrorT tolerance, unsigned max_iterations, ComplexT time=ComplexT(0))
Newton-refine a point against a square System, without a tracker.
Definition newton_refine.hpp:86
Definition algorithm_builder.hpp:36
@ Success
The decomposition looks healthy.
Definition eigen_extensions.hpp:465
Eigen::Matrix< NumT, Eigen::Dynamic, 1 > Vec
A dynamically-sized column vector of NumT.
Definition eigen_extensions.hpp:296
Eigen::Matrix< NumT, Eigen::Dynamic, Eigen::Dynamic > Mat
A dynamically-sized matrix of NumT.
Definition eigen_extensions.hpp:298
double NumErrorT
The numeric type used to hold error/tolerance magnitudes throughout Bertini2 – tracking tolerances,...
Definition num_traits.hpp:48
SuccessCode
Definition config.hpp:42
@ FailedToConverge
Definition config.hpp:55
@ Success
Definition config.hpp:51
@ MatrixSolveFailure
Definition config.hpp:56
The outcome of a standalone Newton refinement.
Definition newton_refine.hpp:55
SuccessCode code
Success when the step norm reached the tolerance; FailedToConverge or MatrixSolveFailure otherwise.
Definition newton_refine.hpp:56
unsigned iterations
Number of Newton iterations taken.
Definition newton_refine.hpp:59
Vec< ComplexT > point
The refined point (the best iterate reached, even on failure).
Definition newton_refine.hpp:57
NumErrorT achieved
Infinity norm of the last Newton step – the consecutive-approximation agreement actually achieved.
Definition newton_refine.hpp:58
Provides the bertini::System class.