Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
midpath_check.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//include/bertini2/nag_algorithms/midpath_check.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//include/bertini2/nag_algorithms/midpath_check.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 include/bertini2/nag_algorithms/midpath_check.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
29
30
31#pragma once
32
35
36namespace bertini{
37 namespace algorithm{
38
39
50 template<typename RealT, typename ComplexT, typename MetaDataType>
51 struct MidpathChecker : public detail::Configured<MidPathConfig>
52 {
56 using PathIndT = unsigned long long;
57
58
59 MidpathChecker() = default;
60
66 template<typename ...T>
67 MidpathChecker( T const& ...config) : AlgConf(config...)
68 {
69 passed_ = true;
70 }
71
72
80
88 {
89 index_ = index;
90 crossed_with_.push_back( std::make_pair(crossed_with, same_start) );
91 rerun_ = !same_start;
92 }
93
94
97 {
98 return index_;
99 }
100
105 void crossed_with(std::pair<PathIndT, bool> crossed)
106 {
107 crossed_with_.push_back(crossed);
108 }
109
114 void rerun(bool same_start)
115 {
116 rerun_ = rerun_ || !same_start;
117 }
118
120 bool rerun() const
121 {
122 return rerun_;
123 }
124
125
126 private:
127 PathIndT index_;
128 std::vector< std::pair<PathIndT, bool> > crossed_with_; // first is index of path crossed with, second is bool = true if had same starting point as crossed path
129 bool rerun_;
130
131
132 }; //re: struct CrossedPath
133
134
135
137 bool Passed() const
138 {
139 return passed_;
140 }
141
142
144 const auto& SamePointTol() const
145 {
146 return this->template Get<MidPathConfT>().same_point_tolerance;
147 }
148
159 template <typename StartSystemT>
160 bool Check(BoundaryData const& boundary_data, StartSystemT const& start_system)
161 {
162 // Each Check is independent: the algorithm calls this repeatedly (once before any
163 // resolve, then again after each MidpathResolve). Reset the accumulated state so a
164 // later call reports *this* call's crossings, not the union with prior calls.
165 // Without this, passed_ (only ever set false below) would stay false forever once
166 // any crossing was seen, and crossed_paths_ would grow without bound -- the resolve
167 // loop in EGBoundaryAction would then never see a clean pass and would re-track
168 // stale indices.
169 passed_ = true;
170 crossed_paths_.clear();
171
172 for (PathIndT ii = 0; ii < boundary_data.size(); ++ii)
173 {
174 if ( boundary_data[ii].success_code != SuccessCode::Success)
175 continue;
176
177 const Vec<ComplexT>& solution_ii = boundary_data[ii].path_point;
178 const auto start_ii = start_system.template StartPoint<ComplexT>(ii);
179
180 for (PathIndT jj = ii+1; jj < boundary_data.size(); ++jj)
181 {
182 if ( boundary_data[jj].success_code != SuccessCode::Success)
183 continue;
184
185 const Vec<ComplexT>& solution_jj = boundary_data[jj].path_point;
186 const Vec<ComplexT> diff_sol = solution_ii - solution_jj;
187
188 if ((diff_sol.template lpNorm<Eigen::Infinity>()/solution_ii.template lpNorm<Eigen::Infinity>()) < SamePointTol())
189 {
190 bool i_already_stored = false;
191 bool j_already_stored = false;
192 // Check if start points are the same
193
194 const auto start_jj = start_system.template StartPoint<ComplexT>(jj);
195 auto diff_start = start_ii - start_jj;
196 // same_start is true when the two paths *began at the same start point*
197 // (their start points differ by less than the tolerance). That governs
198 // whether this coincidence is a genuine path crossing worth re-tracking:
199 // CrossedPath sets rerun_ = !same_start, so two *distinct* starts that
200 // have collided (same_start == false) get re-tracked, while two paths
201 // that legitimately share a start point do not. NOTE: this comparison
202 // was historically inverted (>), which flipped rerun and meant genuine
203 // crossings were never re-tracked.
204 bool same_start = (diff_start.template lpNorm<Eigen::Infinity>() < SamePointTol());
205
206
207 // Check if path has already been stored in crossed_paths_
208 for(auto& v : crossed_paths_)
209 {
210 if(v.index() == ii)
211 {
212 v.crossed_with(std::make_pair(jj,same_start));
213 i_already_stored = true;
214 v.rerun(same_start);
215 }
216 if(v.index() == jj)
217 {
218 v.crossed_with(std::make_pair(ii,same_start));
219 j_already_stored = true;
220 v.rerun(same_start);
221 }
222 }
223
224 // If not already stored, create CrossedPath object and add to crossed_paths_
225 if(!i_already_stored)
226 {
227 CrossedPath tempPath(ii, jj, same_start);
228 crossed_paths_.push_back(tempPath);
229 }
230 if(!j_already_stored)
231 {
232 CrossedPath tempPath(jj, ii, same_start);
233 crossed_paths_.push_back(tempPath);
234 }
235
236 passed_ = false;
237
238 }
239 }
240 }
241 return passed_;
242 };
243
244
246 const std::vector<CrossedPath> GetCrossedPaths() const
247 {
248 return crossed_paths_;
249 }
250
251
252
253 private:
254
255 std::vector<CrossedPath> crossed_paths_; // Data for all paths that crossed on last check
256 bool passed_; // Did the check pass?
257
258
259 }; //re: struct MidpathChecker
260
261 } // re: namespace algorithm
262}// re: namespace bertini
Contains the detail/configured helper type, for templated getting/setting/storage of config structs.
Definition algorithm_base.hpp:34
std::vector< T > SolnCont
Definition config.hpp:32
Definition zero_dim_solve.hpp:68
Definition algorithm_builder.hpp:36
Eigen::Matrix< NumT, Eigen::Dynamic, 1 > Vec
A dynamically-sized column vector of NumT.
Definition eigen_extensions.hpp:296
@ Success
Definition config.hpp:51
Configuration for the midpath (path-crossing) check.
Definition config.hpp:80
PathIndT index() const
Definition midpath_check.hpp:96
bool rerun() const
Definition midpath_check.hpp:120
void crossed_with(std::pair< PathIndT, bool > crossed)
Record an additional path that this one crosses with.
Definition midpath_check.hpp:105
CrossedPath(PathIndT index, PathIndT crossed_with, bool same_start)
Record a path crossing.
Definition midpath_check.hpp:87
void rerun(bool same_start)
Update whether this path needs rerunning, given another crossing.
Definition midpath_check.hpp:114
const std::vector< CrossedPath > GetCrossedPaths() const
Definition midpath_check.hpp:246
const auto & SamePointTol() const
Definition midpath_check.hpp:144
MidPathConfig MidPathConfT
The configuration type this checker reads.
Definition midpath_check.hpp:53
SolnCont< MetaDataType > BoundaryData
Container of solution data at the endgame boundary.
Definition midpath_check.hpp:55
MidpathChecker(T const &...config)
Construct a MidpathChecker, given the system it is checking, and the tolerance which should be used t...
Definition midpath_check.hpp:67
bool Check(BoundaryData const &boundary_data, StartSystemT const &start_system)
Checks the solution data at the endgame boundary to see if any paths have crossed during tracking bef...
Definition midpath_check.hpp:160
unsigned long long PathIndT
Integer type indexing solution paths.
Definition midpath_check.hpp:56
detail::Configured< MidPathConfig > AlgConf
The Configured base alias.
Definition midpath_check.hpp:54
bool Passed() const
Definition midpath_check.hpp:137
This templated struct allows one to provide a variable typelist of config structs (or anything else f...
Definition configured.hpp:83
const T & Get() const
Definition configured.hpp:94