Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
named_expression.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/function_tree/roots/named_expression.hpp is free software: you can redistribute it
4//and/or modify it under the terms of the GNU General Public License as published by the Free
5//Software Foundation, either version 3 of the License, or (at your option) any later version.
6//
7//bertini2/function_tree/roots/named_expression.hpp is distributed in the hope that it will be
8//useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9//FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10//
11//You should have received a copy of the GNU General Public License along with
12//bertini2/function_tree/roots/named_expression.hpp. If not, see <http://www.gnu.org/licenses/>.
13//
14// Copyright(C) Bertini2 Development Team
15//
16// See <http://www.gnu.org/licenses/> for a copy of the license,
17// as well as COPYING. Bertini2 is provided with permitted
18// additional terms in the b2/licenses/ directory.
19
25
26#ifndef BERTINI_NAMED_EXPRESSION_HPP
27#define BERTINI_NAMED_EXPRESSION_HPP
28
30
31namespace bertini {
32namespace node{
33
50 {
51 public:
53
54
55 template<typename... Ts>
56 static
57 std::shared_ptr<NamedExpression> Make(Ts&& ...ts){
58 return std::static_pointer_cast<NamedExpression>(Intern(std::shared_ptr<Node>( new NamedExpression(ts...) )));
59 }
60
62 void print(std::ostream & target) const override
63 {
64 target << name();
65 }
66
67 // Hash-consed by (expression, name), so distinct names / expressions are distinct nodes.
68 std::size_t HashImpl() const override
69 {
70 std::size_t h = typeid(NamedExpression).hash_code();
71 HashCombine(h, EntryNode()->Hash());
72 HashCombine(h, std::hash<std::string>{}(name()));
73 return h;
74 }
75 bool IsSame(Node const& other) const override
76 {
77 auto o = dynamic_cast<NamedExpression const*>(&other);
78 // entries are interned, so pointer identity is structural equality
79 return o && name() == o->name() && EntryNode().get() == o->EntryNode().get();
80 }
81
83 void EnsureNotEmpty() const;
84
86
88 const std::shared_ptr<Node>& EntryNode() const;
89
91 std::shared_ptr<Node> Differentiate(std::shared_ptr<Variable> const& v = nullptr) const override;
92
94 int Degree(std::shared_ptr<Variable> const& v = nullptr) const override;
95 int Degree(VariableGroup const& vars) const override;
96
97 std::vector<int> MultiDegreeImpl(VariableGroup const& vars) const override;
98
99 std::shared_ptr<Node> Homogenized(VariableGroup const& vars, std::shared_ptr<Variable> const& homvar) const override;
100
101 std::shared_ptr<Node> Subs(SubstitutionMap const& substitutions) const override;
102
103 bool IsHomogeneous(std::shared_ptr<Variable> const& v = nullptr) const override;
104 bool IsHomogeneous(VariableGroup const& vars) const override;
105
107
108 virtual ~NamedExpression() = default;
109
110 protected:
111 NamedExpression() = default;
112
113
114 std::shared_ptr<Node> entry_node_ = nullptr;
115
116 private:
117 NamedExpression(std::shared_ptr<Node> const& entry, std::string const& name)
118 : NamedSymbol(name), entry_node_(entry)
119 {}
120
122
123 template <typename Archive>
124 void serialize(Archive& ar, const unsigned /*version*/) {
125 ar & boost::serialization::base_object<NamedSymbol>(*this);
126 ar & entry_node_;
127 }
128 };
129
132 inline std::shared_ptr<NamedExpression> Named(std::shared_ptr<Node> const& expr, std::string const& name)
133 {
134 return NamedExpression::Make(expr, name);
135 }
136
137} // re: namespace node
138} // re: namespace bertini
139
140#endif
bool IsSame(Node const &other) const override
Order-sensitive structural equality, shallow given interned children.
Definition named_expression.hpp:75
void print(std::ostream &target) const override
Prints as just the name (the expansion is shown by the System's Describe).
Definition named_expression.hpp:62
int Degree(std::shared_ptr< Variable > const &v=nullptr) const override
the degree is the degree of the wrapped expression
std::shared_ptr< Node > Homogenized(VariableGroup const &vars, std::shared_ptr< Variable > const &homvar) const override
Homogenize a tree, returning a NEW homogenized tree (functional / non-mutating).
const std::shared_ptr< Node > & EntryNode() const
flips the fresh-eval bit back to fresh (downward through the wrapped expression)
bool IsHomogeneous(std::shared_ptr< Variable > const &v=nullptr) const override
Check for homogeneity, absolutely with respect to all variables, including path variables and all oth...
void EnsureNotEmpty() const
throws a runtime error if the entry node is nullptr
std::shared_ptr< Node > Subs(SubstitutionMap const &substitutions) const override
Symbolically substitute variables, returning a NEW tree.
virtual ~NamedExpression()=default
change the precision of this variable-precision tree node
static std::shared_ptr< NamedExpression > Make(Ts &&...ts)
Construct (and intern) a NamedExpression node.
Definition named_expression.hpp:57
std::vector< int > MultiDegreeImpl(VariableGroup const &vars) const override
Per-node-type multidegree.
friend class boost::serialization::access
Definition named_expression.hpp:121
bool IsHomogeneous(VariableGroup const &vars) const override
Check for homogeneity, with respect to a variable group.
std::size_t HashImpl() const override
Compute this node's structural hash.
Definition named_expression.hpp:68
std::shared_ptr< Node > Differentiate(std::shared_ptr< Variable > const &v=nullptr) const override
differentiate the wrapped expression
int Degree(VariableGroup const &vars) const override
Compute the overall degree with respect to a variable group.
std::shared_ptr< Node > entry_node_
The root node of the named expression.
Definition named_expression.hpp:114
NamedSymbol(const std::string &new_name)
Parameterized constructor, sets the name of the symbol.
const std::string & name() const
Get the name of the named symbol.
An interface for all nodes in a function tree, and for a function object as well.
Definition node.hpp:114
static void HashCombine(std::size_t &seed, std::size_t value)
Combine an extra value into a running hash (boost::hash_combine recipe).
Definition node.hpp:384
std::size_t Hash() const
Memoized structural hash of this node.
Definition canonical.hpp:56
std::shared_ptr< Node > Intern(std::shared_ptr< Node > const &candidate)
Hash-cons a freshly-built node: return an existing structurally-equal node if one is live,...
std::shared_ptr< NamedExpression > Named(std::shared_ptr< Node > const &expr, std::string const &name)
Give an expression a name: Named(x*x + y*y, "a").
Definition named_expression.hpp:132
Definition algorithm_builder.hpp:36
std::map< std::string, std::shared_ptr< node::Node > > SubstitutionMap
A substitution request for Node::Subs: variable name -> the node to put in its place.
Definition node.hpp:72
std::vector< std::shared_ptr< node::Variable > > VariableGroup
An ordered group of variables (the unit of homogenization / variable grouping).
Definition node.hpp:69
Defines the abstract Symbol and NamedSymbol classes,.
#define BERTINI_DEFAULT_VISITABLE()
macro for classes which want default Accept implementation, having nothing fancy to do when accepting...
Definition visitable.hpp:121