Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//node.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//node.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 node.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2021 by 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// individual authors of this file include:
23// James Collins
24// West Texas A&M University
25// Spring, Summer 2015
26//
27//
28// silviana amethyst
29// University of Wisconsin - Eau Claire
30//
31// Created by Collins, James B. on 4/30/15.
32
33
40
41#ifndef BERTINI_NODE_BASE_HPP
42#define BERTINI_NODE_BASE_HPP
43
44
45
46
47#include <iostream>
48#include <string>
49#include <tuple>
50
51#include <boost/type_index.hpp>
52
56
57#include <boost/archive/text_oarchive.hpp>
58#include <boost/archive/text_iarchive.hpp>
59#include <boost/serialization/export.hpp>
60#include <boost/serialization/shared_ptr.hpp>
61#include <boost/serialization/vector.hpp>
62#include <boost/serialization/tracking.hpp>
63#include <boost/serialization/base_object.hpp>
64#include <boost/serialization/nvp.hpp>
65
66
67
68
69
70namespace bertini {
71
72 namespace node{
73 class Variable;
74 }
75
76using VariableGroup = std::vector< std::shared_ptr<node::Variable> >;
77
78
79
80enum class VariableGroupType
81{
82 Homogeneous,
83 Affine,
84 Ungrouped
85};
86
87
88namespace node{
89
90namespace detail{
91 template<typename T>
92 struct FreshEvalSelector
93 {};
94
95 template<>
96 struct FreshEvalSelector<dbl>
97 {
98
99 template<typename N>
100 static dbl Run(N const& n, std::shared_ptr<Variable> const& diff_variable)
101 {
102 return n.FreshEval_d(diff_variable);
103 }
104
105
106 template<typename N>
107 static void RunInPlace(dbl& evaluation_value, N const& n, std::shared_ptr<Variable> const& diff_variable)
108 {
109 n.FreshEval_d(evaluation_value, diff_variable);
110 }
111
112 };
113
114 template<>
115 struct FreshEvalSelector<mpfr_complex>
116 {
117 template<typename N>
118 static mpfr_complex Run(N const& n, std::shared_ptr<Variable> const& diff_variable)
119 {
120 return n.FreshEval_mp(diff_variable);
121 }
122
123
124 template<typename N>
125 static void RunInPlace(mpfr_complex& evaluation_value, N const& n, std::shared_ptr<Variable> const& diff_variable)
126 {
127 n.FreshEval_mp(evaluation_value, diff_variable);
128 }
129
130 };
131}
141class Node : public virtual VisitableBase<>, public std::enable_shared_from_this<Node>
142{
143 friend detail::FreshEvalSelector<dbl>;
144 friend detail::FreshEvalSelector<mpfr_complex>;
145public:
146
147 virtual ~Node() = default;
148
150
151
152
156 virtual void Reset() const = 0;
157
159
160
161
162 public:
172 template<typename T>
173 T Eval(std::shared_ptr<Variable> const& diff_variable = nullptr) const
174 {
175 T result;
176 EvalInPlace(result, diff_variable);
177 return result;
178 }
179
180
190 template<typename T>
191 void EvalInPlace(T& eval_value, std::shared_ptr<Variable> const& diff_variable = nullptr) const;
192
193
195
196
206 virtual unsigned EliminateOnes() = 0;
207
217 virtual unsigned EliminateZeros() = 0;
218
219
225 virtual unsigned ReduceDepth();
226
230 virtual void print(std::ostream& target) const = 0;
231
232
240 virtual std::shared_ptr<Node> Differentiate(std::shared_ptr<Variable> const& v = nullptr) const = 0;
241
248 virtual int Degree(std::shared_ptr<Variable> const& v = nullptr) const = 0;
249
250
251
257 virtual std::vector<int> MultiDegree(VariableGroup const& vars) const = 0;
258
265 virtual int Degree(VariableGroup const& vars) const = 0;
266
273 virtual void Homogenize(VariableGroup const& vars, std::shared_ptr<Variable> const& homvar) = 0;
274
280 virtual bool IsHomogeneous(std::shared_ptr<Variable> const& v = nullptr) const = 0;
281
287 virtual bool IsHomogeneous(VariableGroup const& vars) const = 0;
288
289
295 virtual void precision(unsigned int prec) const = 0;
296
297 unsigned precision() const;
298
300
306 bool IsPolynomial(std::shared_ptr<Variable> const&v = nullptr) const;
307
308
314 bool IsPolynomial(VariableGroup const&v) const;
315
316
317
318
319
320
321protected:
322 //Stores the current value of the node in all required types
323 //We must hard code in all types that we want here.
324 //TODO: Initialize this to some default value, second = false
325 mutable std::tuple< std::pair<dbl,bool>, std::pair<mpfr_complex,bool> > current_value_;
326
327
328
330
336 virtual dbl FreshEval_d(std::shared_ptr<Variable> const&) const = 0;
337
343 virtual void FreshEval_d(dbl& evaluation_value, std::shared_ptr<Variable> const&) const = 0;
344
345
351 virtual mpfr_complex FreshEval_mp(std::shared_ptr<Variable> const&) const = 0;
352
358 virtual void FreshEval_mp(mpfr_complex& evaluation_value, std::shared_ptr<Variable> const&) const = 0;
359
360
361
362
364
365
369 void ResetStoredValues() const;
370
371 Node();
372
373private:
374 friend std::ostream& operator<<(std::ostream & out, const Node& N);
375
376 friend class boost::serialization::access;
377
378 template <typename Archive>
379 void serialize(Archive& ar, const unsigned version) {
380 register_derived_node_types(ar);
381 }
382
383}; // re: class node
384
388 inline std::ostream& operator<<(std::ostream & out, const Node& N)
389 {
390 N.print(out);
391 return out;
392 }
393
394 inline std::ostream& operator<<(std::ostream & out, const std::shared_ptr<Node>& N)
395 {
396 N->print(out);
397 return out;
398 }
399
400
401
402 // inherit from this to get a nice method of producing shared pointers to specific type, solving the diamond problem
403 //
404 // T is a derived type
405 //
406 // I adapted from:
407 // https://stackoverflow.com/questions/16082785/use-of-enable-shared-from-this-with-multiple-inheritance
408 //
409 template<typename T>
410 struct EnableSharedFromThisVirtual: public virtual Node
411 {
412
413 public:
414
415 std::shared_ptr<T> shared_from_this() {
416 return std::dynamic_pointer_cast<T>(Node::shared_from_this());
417 }
418
419 std::shared_ptr<const T> shared_from_this() const{
420 return std::dynamic_pointer_cast<const T>(Node::shared_from_this());
421 }
422
423
424
425 template <class ThisT>
426 std::shared_ptr<ThisT> downcast_shared_from_this(){
427 return std::dynamic_pointer_cast<ThisT>(Node::shared_from_this());
428 }
429
430 template <class ThisT>
431 std::shared_ptr<const ThisT> downcast_shared_from_this() const{
432 return std::dynamic_pointer_cast<const ThisT>(Node::shared_from_this());
433 }
434
435 };
436
437
438
439 } // re: namespace node
440} // re: namespace bertini
441
442
443
444#endif
445/* defined(BERTINI_NODE_BASE_HPP) */
446
447
448
449
450
451
The base class for visitable types.
Definition visitable.hpp:88
Abstract base class for the Bertini hybrid-precision (double-multiple) expression tree.
Definition node.hpp:142
bool IsPolynomial(VariableGroup const &v) const
virtual void FreshEval_mp(mpfr_complex &evaluation_value, std::shared_ptr< Variable > const &) const =0
virtual void Homogenize(VariableGroup const &vars, std::shared_ptr< Variable > const &homvar)=0
bool IsPolynomial(std::shared_ptr< Variable > const &v=nullptr) const
virtual mpfr_complex FreshEval_mp(std::shared_ptr< Variable > const &) const =0
virtual unsigned EliminateZeros()=0
A transform function, which eliminates nodes from the tree.
virtual int Degree(std::shared_ptr< Variable > const &v=nullptr) const =0
virtual unsigned EliminateOnes()=0
A transform function, which eliminates nodes from the tree.
virtual int Degree(VariableGroup const &vars) const =0
virtual bool IsHomogeneous(std::shared_ptr< Variable > const &v=nullptr) const =0
void ResetStoredValues() const
virtual bool IsHomogeneous(VariableGroup const &vars) const =0
T Eval(std::shared_ptr< Variable > const &diff_variable=nullptr) const
Definition node.hpp:173
virtual std::shared_ptr< Node > Differentiate(std::shared_ptr< Variable > const &v=nullptr) const =0
Compute the derivative with respect to a single variable.
friend std::ostream & operator<<(std::ostream &out, const Node &N)
Definition node.hpp:388
virtual void print(std::ostream &target) const =0
virtual unsigned ReduceDepth()
A mutating function which finds ways to reduce the depth of the tree.
void EvalInPlace(T &eval_value, std::shared_ptr< Variable > const &diff_variable=nullptr) const
virtual void FreshEval_d(dbl &evaluation_value, std::shared_ptr< Variable > const &) const =0
virtual std::vector< int > MultiDegree(VariableGroup const &vars) const =0
virtual void precision(unsigned int prec) const =0
virtual void Reset() const =0
virtual dbl FreshEval_d(std::shared_ptr< Variable > const &) const =0
Represents variable leaves in the function tree.
Definition variable.hpp:59
Forward declarations for types in the node:: namespace.
std::ostream & operator<<(std::ostream &out, const Node &N)
Definition node.hpp:388
Provides an Eigen-like NumTraits struct for querying traits of a number type.
Contains the visitable base types.