bpp-phyl3  3.0.0
FrequencySet.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Exceptions.h>
8 
9 
10 using namespace std;
11 
12 namespace bpp
13 {
14 // FrequencySet node
15 
16 ConfiguredFrequencySet::ConfiguredFrequencySet (const Context& context, NodeRefVec&& deps, std::unique_ptr<FrequencySetInterface>&& freqset)
17  : Value<const FrequencySetInterface*>(std::move (deps), freqset.get ()), AbstractParametrizable(freqset->getNamespace()) // , context_(context)
18  , freqset_(std::move(freqset))
19 {
20  for (const auto& dep:dependencies())
21  {
22  const auto& param = std::dynamic_pointer_cast<ConfiguredParameter>(dep);
23  shareParameter_(param);
24  }
25 }
26 
28 
29 std::string ConfiguredFrequencySet::description () const { return "FreqSet(" + freqset_->getName () + ")"; }
30 
32 {
33  return "nbState=" + std::to_string (freqset_->getAlphabet ()->getSize ());
34 }
35 
36 // FrequencySet node additional arguments = (type of bpp::FrequencySet).
37 // Everything else is determined by the node dependencies.
38 
40 {
41  const auto* derived = dynamic_cast<const Self*>(&other);
42  if (derived == nullptr)
43  {
44  return false;
45  }
46  else
47  {
48  const auto& thisFS = *freqset_;
49  const auto& otherFS = *derived->freqset_;
50  return typeid (thisFS) == typeid (otherFS);
51  }
52 }
53 
55 {
56  const auto& bppFS = *freqset_;
57  return typeid (bppFS).hash_code ();
58 }
59 
60 
62 {
63  auto m = ConfiguredParametrizable::createConfigured<Target, Self>(c, std::move (deps), std::unique_ptr<Target>{dynamic_cast<Target*>(freqset_->clone ())});
64  m->config = this->config; // Duplicate derivation config
65  return m;
66 }
67 
68 // FrequenciesFromFrequencySet
69 
71  NodeRefVec&& deps, const Dimension<Eigen::RowVectorXd>& dim)
72  : Value<Eigen::RowVectorXd>(std::move (deps)), targetDimension_ (dim) {}
73 
75 {
76  using namespace numeric;
77  return debug (this->accessValueConst ()) + " targetDim=" + to_string (targetDimension_);
78 }
79 
80 // FrequenciesFromFrequencySet additional arguments = ().
82 {
83  return dynamic_cast<const Self*>(&other) != nullptr;
84 }
85 
87 {
88  // d(equFreqs)/dn = sum_i d(equFreqs)/dx_i * dx_i/dn (x_i = freqset parameters)
89  auto freqSetDep = this->dependency (0);
90  auto& freqset = static_cast<ConfiguredFrequencySet&>(*freqSetDep);
91  auto buildFWithNewFreqSet = [this, &c](NodeRef&& newFreqSet) {
92  return ConfiguredParametrizable::createRowVector<ConfiguredFrequencySet, Self>(c, {std::move (newFreqSet)}, targetDimension_);
93  };
94 
95  NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<ConfiguredFrequencySet, T >(
96  c, freqset, node, targetDimension_, buildFWithNewFreqSet);
97  return CWiseAdd<T, ReductionOf<T>>::create (c, std::move (derivativeSumDeps), targetDimension_);
98 }
99 
101 {
102  return ConfiguredParametrizable::createRowVector<ConfiguredFrequencySet, Self>(c, std::move (deps), targetDimension_);
103 }
104 
106 {
107  const auto* freqset = accessValueConstCast<const FrequencySetInterface*>(*this->dependency (0));
108  const auto& freqsFromFS = freqset->getFrequencies ();
109  auto& r = this->accessValueMutable ();
110  r = Eigen::Map<const T>(freqsFromFS.data(), static_cast<Eigen::Index>(freqsFromFS.size ()));
111 }
112 } // namespace bpp
virtual void shareParameter_(const std::shared_ptr< Parameter > &parameter)
Data flow node representing a Frequencies Set configured with parameter values.
Definition: FrequencySet.h:39
bool compareAdditionalArguments(const Node_DF &other) const
Compare node-specific configuration to another.
NumericalDerivativeConfiguration config
Configuration for numerical derivation of computation nodes using this FrequencySet.
Definition: FrequencySet.h:69
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
std::size_t hashAdditionalArguments() const
Return the hash of node-specific configuration.
std::string description() const final
Node pretty name (default = type name).
std::unique_ptr< FrequencySetInterface > freqset_
Definition: FrequencySet.h:85
Context for dataflow node construction.
Definition: DataFlow.h:527
void compute() final
Computation implementation.
FrequenciesFromFrequencySet(NodeRefVec &&deps, const Dimension< T > &dim)
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
bool compareAdditionalArguments(const Node_DF &other) const final
Compare node-specific configuration to another.
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Parametrize a set of state frequencies.
Definition: FrequencySet.h:29
Base dataflow Node class.
Definition: DataFlow.h:152
const NodeRef & dependency(std::size_t i) const noexcept
Definition: DataFlow.h:185
const NodeRefVec & dependencies() const noexcept
Definition: DataFlow.h:183
Abstract Node storing a value of type T.
Definition: DataFlow.h:352
const Eigen::RowVectorXd & accessValueConst() const noexcept
Raw value access (const).
Definition: DataFlow.h:385
Eigen::RowVectorXd & accessValueMutable() noexcept
Definition: DataFlow.h:416
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::string to_string(const NoDimension &)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
std::shared_ptr< Node_DF > NodeRef
Definition: DataFlow.h:78
Store a dimension for type T.