5 #ifndef BPP_PHYL_LIKELIHOOD_DATAFLOW_PARAMETRIZABLE_H
6 #define BPP_PHYL_LIKELIHOOD_DATAFLOW_PARAMETRIZABLE_H
15 #include <unordered_map>
26 std::unordered_map<std::string, std::shared_ptr<ConfiguredParameter>>
29 std::unordered_map<std::string, std::shared_ptr<ConfiguredParameter>>
41 const std::function<
NodeRef(
const std::string&)>& getParameter);
44 const std::function<
NodeRef(
const std::string&)>& getParameter);
59 template<
typename Object,
typename Self>
61 std::unique_ptr<Object>&&
object,
62 typename std::enable_if<std::is_base_of<ParameterAliasable, Object>::value>::type* = 0)
66 throw Exception (
"createConfigured(): nullptr object");
69 const auto nbParameters =
object->getIndependentParameters ().size ();
72 checkDependencyRangeIsValue<Parameter*>(
typeid (Self), deps, 0, nbParameters);
73 return cachedAs<Self>(c, std::make_shared<Self>(c, std::move (deps), std::move (
object)));
85 template<
typename Object,
typename Self>
87 std::unique_ptr<Object>&&
object,
88 typename std::enable_if<!std::is_base_of<ParameterAliasable, Object>::value>::type* = 0)
92 throw Exception (
"createConfigured(): nullptr object");
95 const auto nbParameters =
object->getParameters ().size ();
98 checkDependencyRangeIsValue<Parameter*>(
typeid (Self), deps, 0, nbParameters);
99 return cachedAs<Self>(c, std::make_shared<Self>(c, std::move (deps), std::move (
object)));
108 template<
typename Object,
typename Self>
114 *
object, [&objectParameters](
const std::string& paramName) {
return objectParameters[paramName]; });
116 auto objectNode = ConfiguredParametrizable::createConfigured<Object, Self>(
118 std::move(depvecObject),
135 template<
typename Object,
typename Self>
138 auto nObject = std::unique_ptr<Object>(
dynamic_cast<Object*
>(
object.clone()));
142 const ParameterList& lParams = pa ? pa->getIndependentParameters() :
object.getParameters();
144 std::vector<NodeRef> dep;
145 for (
size_t i = 0; i < lParams.
size(); i++)
147 std::string name = lParams[i].getName() + suff;
151 throw Exception(
"createConfigured: unknown ConfiguredParameter " + name);
152 else name = lParams[i].getName() +
"_1";
156 throw Exception(
"createConfigured: unknown ConfiguredParameter " + name);
160 return ConfiguredParametrizable::createConfigured<Object, Self>(context, std::move(dep), std::move(nObject));
169 template<
typename ConfiguredObject,
typename Self>
175 checkNthDependencyIs<ConfiguredObject>(
typeid (Self), deps, 0);
176 return cachedAs<Value<double>>(c, std::make_shared<Self>(std::move (deps)));
186 template<
typename ConfiguredObject,
typename Self,
typename Row>
193 checkNthDependencyIs<ConfiguredObject>(
typeid (Self), deps, 0);
194 return cachedAs<Value<Row>>(c, std::make_shared<Self>(std::move (deps), dim));
197 template<
typename ConfiguredObject,
typename Self,
typename Col>
204 checkNthDependencyIs<ConfiguredObject>(
typeid (Self), deps, 0);
205 return cachedAs<Value<Col>>(c, std::make_shared<Self>(std::move (deps), dim));
213 template<
typename ConfiguredObject,
typename Self,
typename Matrix>
222 checkNthDependencyIs<ConfiguredObject>(
typeid (Self), deps, 0);
224 checkNthDependencyIs<ConfiguredParameter>(
typeid (Self), deps, 1);
226 return cachedAs<Value<Matrix>>(c, std::make_shared<Self>(std::move (deps), dim));
240 template<
typename ConfiguredObject,
typename T,
typename B>
242 Context& c, ConfiguredObject&
object,
const Node_DF& derivationNode,
const Dimension<T>& targetDimension, B buildFWithNewObject)
246 for (std::size_t i = 0; i <
object.nbDependencies (); ++i)
249 if (!
object.dependency(i))
252 auto dxi_dn =
object.dependency (i)->derive (c, derivationNode);
255 auto buildFWithNewXi = [&c, i, &object, &buildFWithNewObject](std::shared_ptr<ConfiguredParameter> newDep) {
257 NodeRefVec newObjectDeps =
object.dependencies ();
258 newObjectDeps[i] = std::move (newDep);
259 auto newObject =
object.recreate (c, std::move (newObjectDeps));
260 return buildFWithNewObject (std::move (newObject));
263 auto df_dxi = generateNumericalDerivative<T>(
264 c,
object.config,
object.dependency (i), targetDimension, buildFWithNewXi);
267 derivativeSumDeps.emplace_back (std::move (df_dxi));
269 derivativeSumDeps.emplace_back (
CWiseMul<T, std::tuple<double, T>>::create (
270 c, {std::move (dxi_dn), std::move (df_dxi)}, targetDimension));
273 return derivativeSumDeps;
Context for dataflow node construction.
Base dataflow Node class.
virtual bool hasParameter(const std::string &name) const
virtual const std::shared_ptr< Parameter > & getParameter(size_t i) const
Defines the basic types of data flow nodes.
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
void checkDependenciesNotNull(const std::type_info &contextNodeType, const NodeRefVec &deps)
Checks that all dependencies are not null, throws if not.
NodeRefVec createDependencyVector(const Parametrizable ¶metrizable, const std::function< NodeRef(const std::string &)> ¶meter)
void checkNthDependencyNotNull(const std::type_info &contextNodeType, const NodeRefVec &deps, std::size_t index)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
void checkDependencyVectorSize(const std::type_info &contextNodeType, const NodeRefVec &deps, std::size_t expectedSize)
void checkDependencyVectorMinSize(const std::type_info &contextNodeType, const NodeRefVec &deps, std::size_t expectedMinSize)
Checks the minimum size of a dependency vector, throws if mismatch.
std::unordered_map< std::string, std::shared_ptr< ConfiguredParameter > > createParameterMap(Context &c, const ParameterAliasable ¶metrizable)
std::shared_ptr< Node_DF > NodeRef
Store a dimension for type T.