44#include <boost/serialization/access.hpp>
45#include <boost/serialization/vector.hpp>
46#include <boost/serialization/shared_ptr.hpp>
63 using NE = std::shared_ptr<node::NamedExpression>;
64 using Nd = std::shared_ptr<node::Node>;
65 using Var = std::shared_ptr<node::Variable>;
91 std::vector<Nd>
const&
Functions()
const {
return functions_; }
95 size_t NumConstants()
const {
return constant_subfunctions_.size(); }
105 std::vector<int> d; d.reserve(functions_.size());
106 for (
auto const& f : functions_) d.push_back(f->Degree());
112 std::vector<int> d; d.reserve(functions_.size());
113 for (
auto const& f : functions_) d.push_back(f->Degree(vars));
122 for (
auto& f : functions_)
123 f = f->Homogenized(group, hom_var);
129 for (
auto const& f : functions_)
if (!f->IsHomogeneous(vars))
return false;
135 for (
auto const& f : functions_)
if (!f->IsPolynomial(vars))
return false;
149 for (
auto const& f : functions_)
150 out <<
" f_" << row++ <<
" = " << f <<
"\n";
161 if (precision_==new_precision)
166 slp_.precision(new_precision);
167 precision_ = new_precision;
174 template <
typename T>
177 EnsureDifferentiated();
178 SetValues<T>(vars, path_value);
179 slp_.template GetFuncValsInPlace<T>(result);
183 template <
typename T>
186 EnsureDifferentiated();
187 SetValues<T>(vars, path_value);
188 slp_.template GetJacobianInPlace<T>(J);
192 template <
typename T>
195 if (!path_variable_) { result.setZero();
return; }
196 EnsureDifferentiated();
197 SetValues<T>(vars, path_value);
198 slp_.template GetTimeDerivInPlace<T>(result);
216 if (space_derivatives_.empty())
217 DifferentiateUsingDerivatives();
218 return space_derivatives_;
223 if (path_variable_ && time_derivatives_.empty())
224 DifferentiateUsingDerivatives();
225 return time_derivatives_;
233 if (is_differentiated_)
return;
234 DifferentiateUsingDerivatives();
235 is_differentiated_ =
true;
255 for (
auto& n : space_derivatives_) n =
Simplify(n);
256 for (
auto& n : time_derivatives_) n =
Simplify(n);
265 for (
auto& f : functions_)
267 for (
auto& c : constant_subfunctions_)
268 c = std::static_pointer_cast<node::NamedExpression>(
node::Reintern(c, memo));
269 for (
auto& v : variables_)
270 v = std::static_pointer_cast<node::Variable>(
node::Reintern(v, memo));
272 path_variable_ = std::static_pointer_cast<node::Variable>(
node::Reintern(path_variable_, memo));
273 space_derivatives_.clear();
274 time_derivatives_.clear();
279 void EnsureDifferentiated()
const {
if (!is_differentiated_)
Differentiate(); }
281 template <
typename T>
282 void SetValues(
Vec<T> const& vars, T
const& path_value)
const
288 void DifferentiateUsingDerivatives()
const
290 const size_t n = functions_.size();
291 space_derivatives_.resize(n * variables_.size());
292 for (
size_t jj = 0; jj < variables_.size(); ++jj)
293 for (
size_t ii = 0; ii < n; ++ii)
294 space_derivatives_[ii + jj * n] = functions_[ii]->
Differentiate(variables_[jj]);
297 time_derivatives_.resize(n);
298 for (
size_t ii = 0; ii < n; ++ii)
299 time_derivatives_[ii] = functions_[ii]->
Differentiate(path_variable_);
303 mutable std::vector<Nd> functions_;
304 mutable std::vector<NE> constant_subfunctions_;
306 mutable std::vector<Nd> space_derivatives_;
307 mutable std::vector<Nd> time_derivatives_;
308 mutable StraightLineProgram slp_;
311 mutable Var path_variable_;
313 mutable bool is_differentiated_ =
false;
314 mutable unsigned precision_;
318 template <
typename Archive>
319 void serialize(Archive& ar,
const unsigned )
321 ar & constant_subfunctions_;
323 ar & is_differentiated_;
324 ar & space_derivatives_;
325 ar & time_derivatives_;
Compiles a System (or polynomial block) into a StraightLineProgram by visiting its function-tree node...
Definition straight_line_program.hpp:1032
SLP Compile(SourceT const &source)
Compile a source (a System or polynomial block) into a StraightLineProgram.
void SetVariableValues(Eigen::MatrixBase< Derived > const &variable_values) const
Copy the values of the variables from the passed in vector to memory.
Definition straight_line_program.hpp:867
bool IsDifferentiated() const
Query whether the block's derivatives have been computed.
Definition polynomial_block.hpp:98
void Describe(std::ostream &out, size_t &row, VariableGroup const &, bool) const
Human-facing description: one line per function, f_k = <expression>.
Definition polynomial_block.hpp:147
size_t NumFunctions() const
Get the number of functions in the block.
Definition polynomial_block.hpp:141
void Homogenize(VariableGroup const &group, Var const &hom_var)
Homogenize each function w.r.t.
Definition polynomial_block.hpp:120
size_t NumConstants() const
Get the number of constant subfunctions.
Definition polynomial_block.hpp:95
bool IsPolynomial(VariableGroup const &vars) const
Query whether every function is polynomial in a variable group.
Definition polynomial_block.hpp:133
size_t NumNaturalFunctions() const
Get the number of natural (pre-randomization) functions.
Definition polynomial_block.hpp:209
void ClearPathVariable() const
Clear the path variable.
Definition polynomial_block.hpp:83
bool DependsOnPathVariable() const
Query whether the block depends on the path variable.
Definition polynomial_block.hpp:143
void SetPathVariable(Var const &t) const
Set the path variable the function trees are evaluated against.
Definition polynomial_block.hpp:81
void Invalidate() const
Mark the block's derivatives as stale (to be recomputed on next use).
Definition polynomial_block.hpp:100
Var GetPathVariable() const
Get the block's path variable (may be null).
Definition polynomial_block.hpp:207
std::vector< int > Degrees(VariableGroup const &vars) const
Per-function degrees with respect to a given variable group.
Definition polynomial_block.hpp:110
void AddConstant(NE const &f)
Add a named constant subfunction.
Definition polynomial_block.hpp:74
std::shared_ptr< node::NamedExpression > NE
Shorthand for a shared pointer to a named expression.
Definition polynomial_block.hpp:63
void JacobianInPlace(Eigen::Ref< Mat< T > > J, Vec< T > const &vars, T const &path_value) const
Evaluate the block's Jacobian in place at the given variable and path-variable values.
Definition polynomial_block.hpp:184
void EvalInPlace(Eigen::Ref< Vec< T > > result, Vec< T > const &vars, T const &path_value) const
Evaluate the block's functions in place at the given variable and path-variable values.
Definition polynomial_block.hpp:175
void TimeDerivInPlace(Eigen::Ref< Vec< T > > result, Vec< T > const &vars, T const &path_value) const
Evaluate the block's time derivative in place (zero if there is no path variable).
Definition polynomial_block.hpp:193
std::vector< Nd > const & GetNaturalFunctions() const
Get the block's natural function trees.
Definition polynomial_block.hpp:211
std::shared_ptr< node::Node > Nd
Shorthand for a shared pointer to a generic node.
Definition polynomial_block.hpp:64
void AddFunction(Nd const &f)
Add a function (one row of the block).
Definition polynomial_block.hpp:72
unsigned Precision() const
Get the block's current working precision.
Definition polynomial_block.hpp:154
std::vector< Nd > const & Functions() const
Get const access to the block's functions.
Definition polynomial_block.hpp:91
void SetVariableOrdering(VariableGroup const &vars) const
The variable ordering + path variable the function trees are evaluated against; the owning System kee...
Definition polynomial_block.hpp:79
std::shared_ptr< node::Variable > Var
Shorthand for a shared pointer to a variable node.
Definition polynomial_block.hpp:65
void Differentiate() const
Build the symbolic derivatives (and, for SLP eval, compile the SLP from this block).
Definition polynomial_block.hpp:231
VariableGroup const & VariableOrdering() const
Get the variable ordering the functions are evaluated against.
Definition polynomial_block.hpp:203
bool HavePathVariable() const
Query whether the block has a path variable.
Definition polynomial_block.hpp:205
void Reintern(node::ReinternMemo &memo)
Re-intern this block's nodes after deserialization (ADR-0042): functions, constant subfunctions,...
Definition polynomial_block.hpp:263
std::vector< int > Degrees() const
Per-function degrees (total, and with respect to a variable group).
Definition polynomial_block.hpp:103
friend class boost::serialization::access
Definition polynomial_block.hpp:316
void SimplifyDerivatives() const
Simplify the derivative trees.
Definition polynomial_block.hpp:252
std::vector< Nd > & Functions()
Get mutable access to the block's functions.
Definition polynomial_block.hpp:89
std::vector< Nd > const & GetSpaceDerivatives() const
Get the per-variable space-derivative trees (computing them if needed).
Definition polynomial_block.hpp:214
void SimplifyFunctions() const
Simplify the function trees (and invalidate the derivatives, which must be rebuilt).
Definition polynomial_block.hpp:240
std::vector< NE > const & ConstantSubfunctions() const
Get the block's constant subfunctions.
Definition polynomial_block.hpp:93
void Precision(unsigned new_precision) const
Set the block's working precision (delegated to the compiled SLP).
Definition polynomial_block.hpp:156
PolynomialBlock()
Construct an empty polynomial block at the current default precision.
Definition polynomial_block.hpp:68
bool IsHomogeneous(VariableGroup const &vars) const
Query whether every function is homogeneous with respect to a variable group.
Definition polynomial_block.hpp:127
std::vector< Nd > const & GetTimeDerivatives() const
Get the time-derivative trees (computing them if needed).
Definition polynomial_block.hpp:221
Bertini extensions of the Eigen linear algebra library.
Collects the various header files which define the Bertini2 function tree node types.
Definition blend_block.hpp:75
std::map< Node const *, std::shared_ptr< Node > > ReinternMemo
Memo for one re-interning pass: loaded node -> its interned replacement.
Definition reintern.hpp:57
std::shared_ptr< Node > Reintern(std::shared_ptr< Node > const &root, ReinternMemo &memo)
Rebuild the DAG rooted at root through the interning factories (memoized, post-order),...
Definition algorithm_builder.hpp:36
Eigen::Matrix< NumT, Eigen::Dynamic, 1 > Vec
A dynamically-sized column vector of NumT.
Definition eigen_extensions.hpp:296
std::vector< std::shared_ptr< node::Variable > > VariableGroup
An ordered group of variables (the unit of homogenization / variable grouping).
Definition node.hpp:69
Eigen::Matrix< NumT, Eigen::Dynamic, Eigen::Dynamic > Mat
A dynamically-sized matrix of NumT.
Definition eigen_extensions.hpp:298
auto DefaultPrecision()
Get the global default precision (digits) for multiprecision numbers.
Definition mpfr_complex.hpp:135
std::shared_ptr< bertini::node::Node > Simplify(std::shared_ptr< bertini::node::Node > const &n)
Functionally simplify a tree: returns a NEW simplified tree, leaving the input untouched (non-mutatin...
Provides an Eigen-like NumTraits struct for querying traits of a number type.
Re-intern deserialized node DAGs into the live hash-consing tables (ADR-0042).
Provides the bertini::StraightLineProgram class.