Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
output.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/nag_algorithms/output.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/output.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/output.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
22
30
31#pragma once
32
35
36
37namespace bertini {
38namespace algorithm {
39namespace output {
40
44template <typename AlgoT>
46{ };
47
48
49template <typename ...T>
50struct Classic
51{};
52
53
61template <typename A, typename B, typename C>
62struct Classic <HomotopySolver<A,B,C>>
63{
65
67 template <typename OutT>
68 static
69 void All(OutT & out, ZDT const& zd)
70 {
71 out << "\n\n\n MAINDATA \n\n\n";
72 MainData(out, zd);
73
74 out << "\n\n\n RAWDATA \n\n\n";
75 RawData(out, zd);
76 }
77
78
80 template <typename OutT>
81 static
82 void MainData(OutT & out, ZDT const& zd)
83 {
84 NumVariables(out, zd);
85 Variables(out, zd,"\n\n");
86
87 const auto& s = zd.SolutionsInternalCoords();
88 const auto n = s.size();
89 for (decltype(s.size()) ii{0}; ii<n; ++ii)
90 {
91 // only successful endgames have a final approximation to report
92 if (zd.SolutionMetadata()[ii].endgame_success_code != SuccessCode::Success)
93 continue;
94
95 EndPointMDFull(ii, out, zd);
96 EndPoint(ii, out, zd,"\n\n");
97 }
98
99 out << "\n\n";
100 TargetSystem(out,zd,"\n\n");
101
102 StartSystem(out,zd,"\n\n");
103
104 Homotopy(out,zd,"\n\n");
105 }
106
108 template <typename OutT>
109 static
110 void RawData(OutT & out, ZDT const& zd)
111 {
112 const auto n = zd.SolutionsInternalCoords().size();
113 NumVariables(out, zd,"\n\n");
114 for (decltype(zd.SolutionsInternalCoords().size()) ii{0}; ii<n; ++ii)
115 {
116 // only successful endgames have a final approximation to report
117 if (zd.SolutionMetadata()[ii].endgame_success_code != SuccessCode::Success)
118 continue;
119 EndPointMDRaw(ii,out,zd,"\n\n");
120 }
121
122 out << "\n\n";
123 TargetSystem(out,zd,"\n\n");
124 }
125
126
127 // --- Bertini 1.7 machine-readable solution files -------------------------------------
128 //
129 // These mirror the classic solution-file format so that tooling written against Bertini 1.7
130 // reads Bertini 2's output unchanged: the first line is the solution count, then each
131 // solution is a block of NumVariables coordinate lines ("re im", scientific), blocks
132 // separated by a blank line. Byte-for-byte equality with Bertini 1 is neither sought nor
133 // possible (different implementations and RNG); the contract is *machine-readable* parity.
134 // Coordinates are in user (dehomogenized) coordinates, as Bertini 1 reports them.
135
136 // Write a count-led list of solution points (each a Vec): "<count>\n\n" then, per point,
137 // the coordinate lines followed by a blank-line separator.
139 template <typename OutT, typename SolListT>
140 static void SolutionList(OutT & out, SolListT const& sols)
141 {
142 out << sols.size() << "\n\n";
143 for (auto const& v : sols)
144 {
145 generators::Classic::generate(boost::spirit::ostream_iterator(out), v);
146 out << "\n";
147 }
148 }
149
151 template <typename OutT>
152 static void FiniteSolutions(OutT & out, ZDT const& zd) { SolutionList(out, zd.FiniteSolutions()); }
153
155 template <typename OutT>
156 static void RealFiniteSolutions(OutT & out, ZDT const& zd) { SolutionList(out, zd.RealSolutions()); }
157
159 template <typename OutT>
160 static void NonsingularSolutions(OutT & out, ZDT const& zd) { SolutionList(out, zd.NonsingularSolutions()); }
161
163 template <typename OutT>
164 static void SingularSolutions(OutT & out, ZDT const& zd) { SolutionList(out, zd.SingularSolutions()); }
165
166 // raw_solutions: every successful endpoint, each preceded by its path number (Bertini 1 lists
167 // the raw endpoints before finite/infinite classification, tagged by path).
169 template <typename OutT>
170 static void RawSolutions(OutT & out, ZDT const& zd)
171 {
172 auto const& md = zd.SolutionMetadata();
173 auto const& sols = zd.SolutionsUserCoords();
174 const auto m = std::min(md.size(), sols.size());
175
176 std::size_t count{0};
177 for (std::size_t ii{0}; ii<m; ++ii)
178 if (md[ii].endgame_success_code == SuccessCode::Success)
179 ++count;
180
181 out << count << "\n\n";
182 for (std::size_t ii{0}; ii<m; ++ii)
183 {
184 if (md[ii].endgame_success_code != SuccessCode::Success)
185 continue;
186 out << md[ii].path_index << "\n";
187 generators::Classic::generate(boost::spirit::ostream_iterator(out), sols[ii]);
188 out << "\n";
189 }
190 }
191
192
194 template <typename OutT>
195 static
196 void NumVariables(OutT & out, ZDT const& zd, std::string const& additional = "\n")
197 {
198 const auto& sys = zd.TargetSystem();
199 out << sys.NumVariables() << additional;
200 }
201
202
204 template <typename OutT>
205 static
206 void Variables(OutT & out, ZDT const& zd, std::string const& additional = "\n")
207 {
208 const auto& sys = zd.TargetSystem();
209 const auto& vars = sys.VariableOrdering();
210 for (const auto& x : vars)
211 out << *x << ' ';
212 out << additional;
213 }
214
215
217 template <typename OutT>
218 static
219 void TargetSystem(OutT & out, ZDT const& zd, std::string const& additional = "\n")
220 {
221 out << zd.TargetSystem() << additional;
222 }
223
225 template <typename OutT>
226 static
227 void StartSystem(OutT & out, ZDT const& zd, std::string const& additional = "\n")
228 {
229 out << zd.StartSystem() << additional;
230 }
231
233 template <typename OutT>
234 static
235 void Homotopy(OutT & out, ZDT const& zd, std::string const& additional = "\n")
236 {
237 out << zd.Homotopy() << additional;
238 }
239
241 template <typename IndexT, typename OutT>
242 static
243 void EndPoint(IndexT const& ind, OutT & out, ZDT const& zd, std::string const& additional = "")
244 {
245 generators::Classic::generate(boost::spirit::ostream_iterator(out), zd.SolutionsInternalCoords()[ind]);
246 out << additional;
247 }
248
250 template <typename IndexT, typename OutT>
251 static
252 void EndPointDehom(IndexT const& ind, OutT & out, ZDT const& zd, std::string const& additional = "")
253 {
255
256 generators::Classic::generate(boost::spirit::ostream_iterator(out), zd.SolutionsUserCoords()[ind]);
257 out << additional;
258 }
259
261 template <typename IndexT, typename OutT>
262 static
263 void EndPointMDFull(IndexT const& ind, OutT & out, ZDT const& zd, std::string const& additional = "\n")
264 {
265 const auto& data = zd.SolutionMetadata()[ind];
266 out << data.path_index << '\n'
267 << data.solution_index << '\n'
268 << data.condition_number << '\n'
269 << data.function_residual << '\n'
270 << data.newton_residual << '\n';
271 generators::Classic::generate(boost::spirit::ostream_iterator(out), data.final_time_used);
272 out << '\n' << data.max_precision_used << '\n';
273
274 generators::Classic::generate(boost::spirit::ostream_iterator(out), data.time_of_first_prec_increase);
275 out << '\n' << data.accuracy_estimate_internal_coords << '\n'
276 << data.accuracy_estimate_user_coords << '\n'
277 << data.cycle_num << '\n'
278 << data.multiplicity << '\n'
279 << data.pre_endgame_success_code << ' ' << data.endgame_success_code << '\n';
280 out << additional;
281 }
282
283
284
286 template <typename IndexT, typename OutT>
287 static
288 void EndPointMDRaw(IndexT const& ind, OutT & out, ZDT const& zd, std::string const& additional = "\n")
289 {
290 const auto& pt = zd.SolutionsInternalCoords()[ind];
291 const auto& data = zd.SolutionMetadata()[ind];
292 out << data.path_index << '\n'
293 << Precision(pt) << '\n';
294 EndPoint(ind, out, zd);
295 out << data.function_residual << '\n'
296 << data.condition_number << '\n'
297 << data.newton_residual << '\n';
298 generators::Classic::generate(boost::spirit::ostream_iterator(out), data.final_time_used);
299 out << '\n' << data.accuracy_estimate_internal_coords << '\n';
300 generators::Classic::generate(boost::spirit::ostream_iterator(out), data.time_of_first_prec_increase);
301 out << '\n' << data.cycle_num << '\n'
302 << data.endgame_success_code << '\n'
303 << additional;
304 }
305
306
307};
308
309
310// ZeroDimSolver is-a HomotopySolver, so it reuses the engine's classic output verbatim: the static
311// methods take a HomotopySolver const&, to which a ZeroDimSolver const& binds (public derivation).
312template <typename A, typename B, typename C>
313struct Classic <ZeroDimSolver<A,B,C>> : Classic <HomotopySolver<A,B,C>>
314{};
315
316
319{
320
322 template<typename AlgoT>
323 static
324 auto Extract(AlgoT const& alg)
325 {
326 using BCT = typename AlgoTraits<AlgoT>::BaseComplexT;
327
328 SampCont<BCT> solns;
329
330 const auto& s = alg.SolutionsUserCoords();
331 const auto& m = alg.SolutionMetadata();
332 const auto n = s.size();
333 for (decltype(s.size()) ii{0}; ii<n; ++ii)
334 {
335 const auto& d = m[ii];
336 if (d.endgame_success_code == SuccessCode::Success &&
337 d.multiplicity==1)
338 {
339 solns.push_back(s[ii]);
340 }
341 }
342
343 return solns;
344 }
345};
346
347
350{
351
353 template<typename AlgoT>
354 static
355 auto Extract(AlgoT const& alg)
356 {
357 using BCT = typename AlgoTraits<AlgoT>::BaseComplexT;
358
359 SampCont<BCT> solns;
360
361 const auto& s = alg.SolutionsUserCoords();
362 const auto n = s.size();
363 for (decltype(s.size()) ii{0}; ii<n; ++ii)
364 {
365 solns.push_back(s[ii]);
366 }
367
368 return solns;
369 }
370};
371
372
373} // namespace output
374} // namespace algorithm
375} // namespace bertini
Provides the generators for bertini2.
Definition output.hpp:39
Definition algorithm_base.hpp:34
Definition algorithm_builder.hpp:36
std::deque< Vec< T > > SampCont
Definition config.hpp:33
auto DefaultPrecision()
Get the global default precision (digits) for multiprecision numbers.
Definition mpfr_complex.hpp:135
@ Success
Definition config.hpp:51
unsigned Precision(Eigen::MatrixBase< Derived > const &v)
Get the precision of an Eigen object.
Definition eigen_extensions.hpp:339
Definition config.hpp:271
The continuation primitive: given a homotopy + a source of start points, track each path through the ...
Definition zero_dim_solve.hpp:546
SolnCont< Vec< BaseComplexT > > RealSolutions(bool user_coords=true, bool merge_multiplicities=false) const
The real finite solutions (is_real applies the configured tolerance).
Definition zero_dim_solve.hpp:1341
const auto & SolutionsInternalCoords() const
Get the computed solutions in the solver's internal coordinates: homogenized, and lying on the target...
Definition zero_dim_solve.hpp:1296
SolnCont< Vec< BaseComplexT > > NonsingularSolutions(bool user_coords=true, bool merge_multiplicities=false) const
The nonsingular finite solutions (simple, well-conditioned roots).
Definition zero_dim_solve.hpp:1348
const SystemType & TargetSystem() const
Definition zero_dim_solve.hpp:624
const SystemType & Homotopy() const
Definition zero_dim_solve.hpp:628
const StartSystemBaseT & StartSystem() const
Definition zero_dim_solve.hpp:626
const auto & SolutionsUserCoords() const
Get the computed solutions, in the coordinates of the user's original variables.
Definition zero_dim_solve.hpp:1263
SolnCont< Vec< BaseComplexT > > FiniteSolutions(bool user_coords=true, bool merge_multiplicities=false) const
The finite solutions: successful, finite endpoints that ARE solutions of the target (is_finite applie...
Definition zero_dim_solve.hpp:1334
const auto & SolutionMetadata() const
Get the metadat associated with the final computed solutions.
Definition zero_dim_solve.hpp:1392
SolnCont< Vec< BaseComplexT > > SingularSolutions(bool user_coords=true, bool merge_multiplicities=false) const
The singular finite solutions (multiple or ill-conditioned roots).
Definition zero_dim_solve.hpp:1355
The zero-dimensional solve algorithm: given a polynomial system, form a start system and a homotopy,...
Definition zero_dim_solve.hpp:3255
Extracts all solutions from a solver's results.
Definition output.hpp:350
static auto Extract(AlgoT const &alg)
Collect the user-coordinate points of every solution.
Definition output.hpp:355
HomotopySolver< A, B, C > ZDT
The solver type whose results are formatted.
Definition output.hpp:64
static void EndPointDehom(IndexT const &ind, OutT &out, ZDT const &zd, std::string const &additional="")
Write one endpoint in user (dehomogenized) coordinates.
Definition output.hpp:252
static void RawData(OutT &out, ZDT const &zd)
Write the "raw data" section (per-endpoint raw metadata and the target system).
Definition output.hpp:110
static void NonsingularSolutions(OutT &out, ZDT const &zd)
Write the nonsingular solutions as a classic solution list.
Definition output.hpp:160
static void RawSolutions(OutT &out, ZDT const &zd)
Write every successful raw endpoint, each tagged by its path number (Bertini 1 order).
Definition output.hpp:170
static void SingularSolutions(OutT &out, ZDT const &zd)
Write the singular solutions as a classic solution list.
Definition output.hpp:164
static void EndPointMDFull(IndexT const &ind, OutT &out, ZDT const &zd, std::string const &additional="\n")
Write one endpoint's full metadata block (main-data form).
Definition output.hpp:263
static void StartSystem(OutT &out, ZDT const &zd, std::string const &additional="\n")
Write the start system.
Definition output.hpp:227
static void MainData(OutT &out, ZDT const &zd)
Write the human-readable "main data" section (variables, endpoints, and systems).
Definition output.hpp:82
static void EndPointMDRaw(IndexT const &ind, OutT &out, ZDT const &zd, std::string const &additional="\n")
Write one endpoint's metadata block (raw-data form).
Definition output.hpp:288
static void SolutionList(OutT &out, SolListT const &sols)
Write a count-led, blank-line-separated list of solution points (Bertini 1.7 format).
Definition output.hpp:140
static void Homotopy(OutT &out, ZDT const &zd, std::string const &additional="\n")
Write the homotopy.
Definition output.hpp:235
static void EndPoint(IndexT const &ind, OutT &out, ZDT const &zd, std::string const &additional="")
Write one endpoint in internal (homogeneous) coordinates.
Definition output.hpp:243
static void FiniteSolutions(OutT &out, ZDT const &zd)
Write the finite solutions as a classic solution list.
Definition output.hpp:152
static void TargetSystem(OutT &out, ZDT const &zd, std::string const &additional="\n")
Write the target system.
Definition output.hpp:219
static void Variables(OutT &out, ZDT const &zd, std::string const &additional="\n")
Write the names of the target system's variables, in order.
Definition output.hpp:206
static void NumVariables(OutT &out, ZDT const &zd, std::string const &additional="\n")
Write the number of variables in the target system.
Definition output.hpp:196
static void All(OutT &out, ZDT const &zd)
Write both the main-data and raw-data sections.
Definition output.hpp:69
static void RealFiniteSolutions(OutT &out, ZDT const &zd)
Write the real finite solutions as a classic solution list.
Definition output.hpp:156
Definition output.hpp:51
Extracts the nonsingular (multiplicity-one) solutions from a solver's results.
Definition output.hpp:319
static auto Extract(AlgoT const &alg)
Collect the user-coordinate points of every successful, multiplicity-one solution.
Definition output.hpp:324
Prints data from algorithms.
Definition output.hpp:46
static bool generate(OutputIterator sink, double const &c)
Emit a double as a classic-format complex (imaginary part zero).
Definition generators.hpp:117
Provides the algorithm for computing all zero-dimensional solutions for an algberaic system.