-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fct.inl
48 lines (38 loc) · 1.21 KB
/
Fct.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "armadillo/armadillo"
#include "Model.h"
#include "Var.h"
namespace netn {
template<typename T>
inline Fct<T>::Fct(const Model<T> & var, const func_t & func, const func_t & deriv)
: _var(var.toModel()), _func(func), _deriv(deriv) {}
template<typename T>
inline Fct<T>::Fct(const Fct & other)
: _var(other._var), _func(other._func), _deriv(other._deriv) {}
template<typename T>
inline T Fct<T>::eval() const {
return _func(_var->eval());
}
template<>
inline arma::mat Fct<arma::mat>::eval() const {
arma::mat result(_var->eval());
result.for_each([&](arma::mat::elem_type & elem) {elem = _func(elem); });
return result;
}
template<typename T>
inline T Fct<T>::derivPart(const Component & component) const {
return _var->derivPart(component) * _deriv(_var->eval());
}
template<>
inline arma::mat Fct<arma::mat>::derivPart(const Component & component) const {
arma::mat result(_var->eval());
arma::mat deriv(_var->derivPart(component));
for (int i = 0; i < result.size(); i++) {
result.at(i) = deriv.at(i) * _deriv(result.at(i));
}
return result;
}
template<typename T>
inline std::shared_ptr<Model<T>> Fct<T>::toModel() const {
return std::make_shared<Fct<T>>(*this);
}
}