Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
json_writer.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//json_writer.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//json_writer.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 json_writer.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
34
35#pragma once
36
37#include <boost/json.hpp>
38
41
42#include <sstream>
43#include <string>
44#include <variant>
45
46namespace bertini{
47 namespace io{
48
65 inline boost::json::object SystemPartsJson(System const& sys)
66 {
67 namespace json = boost::json;
68 json::object parts;
69
70 auto groups_as_json = [](auto const& groups) {
71 json::array out;
72 for (auto const& grp : groups)
73 {
74 json::array names;
75 for (auto const& v : grp)
76 names.push_back(json::value(v->name()));
77 out.push_back(names);
78 }
79 return out;
80 };
81 parts["variable_groups"] = groups_as_json(sys.VariableGroups());
82 parts["hom_variable_groups"] = groups_as_json(sys.HomVariableGroups());
83
84 {
85 json::array homogenizers;
86 for (auto const& v : sys.HomogenizingVariables())
87 homogenizers.push_back(json::value(v->name()));
88 parts["homogenizing_variables"] = homogenizers;
89 }
90
91 if (sys.HavePathVariable())
92 parts["path_variable"] = sys.GetPathVariable()->name();
93 else
94 parts["path_variable"] = nullptr;
95
96 auto const functions = sys.NaturalFunctionsAsNodes();
97 {
98 // named subexpressions are discovered inside the trees (nested included)
99 json::object named;
100 std::vector<std::shared_ptr<const node::Node>> roots(functions.begin(),
101 functions.end());
102 for (auto const& ne : node::Find<node::NamedExpression>(roots))
103 {
104 std::ostringstream expr;
105 expr << *(ne->EntryNode());
106 named[ne->name()] = expr.str();
107 }
108 if (!named.empty())
109 parts["named_subexpressions"] = named;
110 }
111 {
112 json::array function_texts;
113 for (auto const& f : functions)
114 {
115 std::ostringstream expr;
116 expr << *f;
117 function_texts.push_back(json::value(expr.str()));
118 }
119 parts["functions"] = function_texts;
120 }
121
122 {
123 // one summary object per evaluation block, in evaluation order --
124 // a randomized, sliced, or blended system says so here (the exact
125 // coefficients live in the canonical encoding)
126 struct BlockKindName
127 {
128 std::string operator()(blocks::PolynomialBlock const&) const { return "polynomial"; }
129 std::string operator()(blocks::LinearFormsBlock const&) const { return "linear_forms"; }
130 std::string operator()(blocks::ProductsOfLinearsBlock const&) const { return "products_of_linears"; }
131 std::string operator()(blocks::BlendBlock<System> const&) const { return "blend"; }
132 std::string operator()(blocks::RandomizationBlock<System> const&) const { return "randomization"; }
133 };
134 json::array block_summaries;
135 for (auto const& b : sys.Blocks())
136 {
137 json::object summary;
138 summary["kind"] = std::visit(BlockKindName{}, b);
139 summary["num_functions"] = static_cast<std::int64_t>(
140 std::visit([](auto const& blk) { return blk.NumFunctions(); }, b));
141 block_summaries.push_back(summary);
142 }
143 parts["blocks"] = block_summaries;
144 }
145
146 parts["is_patched"] = sys.IsPatched();
147 if (sys.IsPatched())
148 parts["num_patches"] = static_cast<std::int64_t>(sys.NumPatches());
149
150 return parts;
151 }
152
153 } // namespace io
154} // namespace bertini
The fundamental polynomial system class for Bertini2.
Definition system.hpp:106
const Var & GetPathVariable() const
Get the system's path variable (throws if none is defined).
auto HomVariableGroups() const
Get the homogeneous (projective) variable groups in the problem.
Definition system.hpp:1807
bool IsPatched() const
Query whether a system is patched.
Definition system.hpp:1933
std::vector< Block > const & Blocks() const
Read-only access to the system's evaluation blocks (in evaluation order).
Definition system.hpp:1097
bool HavePathVariable() const
Query whether a path variable is set for this system.
size_t NumPatches() const
Get the number of patches in this system.
Definition system.hpp:790
auto VariableGroups() const
Get the affine variable groups in the problem.
Definition system.hpp:1799
std::vector< Nd > NaturalFunctionsAsNodes() const
The system's natural (pre-patch) functions as function-tree expression nodes, expanding any structure...
VariableGroup const & HomogenizingVariables() const
Get the homogenizing variables, one per affine variable group (in group order), or an empty container...
Definition system.hpp:819
A block forming the blend (coupling homotopy) H = sum_i c_i(t) * operand_i of several systems.
Definition blend_block.hpp:80
A block of system functions, each an affine linear form f_i(x) = c_i .
Definition linear_forms_block.hpp:68
The polynomial evaluation block: function trees + derivatives + SLP.
Definition polynomial_block.hpp:61
A block of system functions, each a product of linear forms.
Definition products_of_linears_block.hpp:64
A block that randomizes an overdetermined operand system down to a square one (with a constant coeffi...
Definition randomization_block.hpp:80
Discover named nodes of a given kind in a function tree (sympy's find).
Definition json_writer.hpp:47
boost::json::object SystemPartsJson(System const &sys)
A System's structure as a JSON object with fields for its parts.
Definition json_writer.hpp:65
std::vector< std::shared_ptr< T > > Find(std::shared_ptr< const Node > const &n)
All distinct nodes of kind T in the subtree rooted at n, sorted by name.
Definition algorithm_builder.hpp:36
Provides the bertini::System class.