bpp-phyl3  3.0.0
Sequence_DF.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LIKELIHOOD_DATAFLOW_SEQUENCE_DF_H
6 #define BPP_PHYL_LIKELIHOOD_DATAFLOW_SEQUENCE_DF_H
7 
8 #include <Bpp/Exceptions.h>
10 #include <functional>
11 #include <unordered_map>
12 
13 #include "Definitions.h"
14 
15 namespace bpp
16 {
22 class Sequence_DF : public Value<MatrixLik>
23 {
24 private:
25  std::string name_;
26 
27 public:
28  using Self = Sequence_DF;
29  using T = MatrixLik;
30 
31  static ValueRef<T> create (Context& c, T&& value, const std::string& name)
32  {
33  return cachedAs<Self>(c, std::make_shared<Self>(std::move(value), name));
34  }
35 
36  Sequence_DF (T&& value, const std::string& name) :
37  Value<T>(NodeRefVec{}, std::move(value)),
38  name_(name)
39  {
40  this->makeValid (); // Always valid
41  }
42 
43  const std::string& getName() const
44  {
45  return name_;
46  }
47 
48  std::string debugInfo () const final
49  {
50  using namespace numeric;
51  return debug(this->accessValueConst ());
52  }
53 
54  std::string description () const final
55  {
56  return Node_DF::description() + " " + name_;
57  }
58 
59  bool compareAdditionalArguments (const Node_DF& other) const override
60  {
61  const auto* derived = dynamic_cast<const Self*>(&other);
62  return derived != nullptr && name_ == derived->name_ && this->accessValueConst () == derived->accessValueConst ();
63  }
64 
65  std::string color () const override
66  {
67  return "grey";
68  }
69 
70  NodeRef recreate (Context& c, NodeRefVec&& deps) final
71  {
72  checkRecreateWithoutDependencies (typeid (Self), deps);
73  return this->shared_from_this ();
74  }
75 
76  std::size_t hashAdditionalArguments () const override
77  {
78  using namespace numeric;
79  size_t seed = hash (this->accessValueConst ());
80  combineHash<std::string>(seed, name_);
81  return seed;
82  }
83 
84  NodeRef derive (Context& c, const Node_DF& node) final
85  {
86  const auto dim = Dimension<T>(this->accessValueConst ());
87  if (&node == this)
88  {
89  return ConstantOne<T>::create (c, dim);
90  }
91  return ConstantZero<T>::create (c, dim);
92  }
93 
94 private:
95  void compute () final
96  {
97  // Constant is valid from construction
98  failureComputeWasCalled (typeid (*this));
99  }
100 };
101 } // namespace bpp
102 #endif // BPP_PHYL_LIKELIHOOD_DATAFLOW_SEQUENCE_DF_H
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantOne node of the given dimension.
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantZero node of the given dimension.
Context for dataflow node construction.
Definition: DataFlow.h:527
Base dataflow Node class.
Definition: DataFlow.h:152
virtual std::string description() const
Node pretty name (default = type name).
Definition: DataFlow.cpp:151
void makeValid() noexcept
Definition: DataFlow.h:271
Data flow node representing a Sequence as a Value<Eigen::MatrixXd> with a name.
Definition: Sequence_DF.h:23
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Definition: Sequence_DF.h:70
bool compareAdditionalArguments(const Node_DF &other) const override
Compare node-specific configuration to another.
Definition: Sequence_DF.h:59
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Sequence_DF.h:84
const std::string & getName() const
Definition: Sequence_DF.h:43
std::string description() const final
Node pretty name (default = type name).
Definition: Sequence_DF.h:54
std::string name_
Definition: Sequence_DF.h:25
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Sequence_DF.h:48
static ValueRef< T > create(Context &c, T &&value, const std::string &name)
Definition: Sequence_DF.h:31
std::size_t hashAdditionalArguments() const override
Return the hash of node-specific configuration.
Definition: Sequence_DF.h:76
std::string color() const override
Definition: Sequence_DF.h:65
void compute() final
Computation implementation.
Definition: Sequence_DF.h:95
Sequence_DF(T &&value, const std::string &name)
Definition: Sequence_DF.h:36
Abstract Node storing a value of type T.
Definition: DataFlow.h:352
const MatrixLik & accessValueConst() const noexcept
Raw value access (const).
Definition: DataFlow.h:385
std::string debug(const T &t, typename std::enable_if< std::is_arithmetic< T >::value >::type *=0)
Defines the basic types of data flow nodes.
std::size_t hash(const MatrixDimension &dim)
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
ExtendedFloatMatrixXd MatrixLik
Definition: Definitions.h:13
void checkRecreateWithoutDependencies(const std::type_info &contextNodeType, const NodeRefVec &deps)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
void failureComputeWasCalled(const std::type_info &nodeType)
Definition: DataFlow.cpp:51
std::shared_ptr< Node_DF > NodeRef
Definition: DataFlow.h:78
Store a dimension for type T.