-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fct.h
72 lines (60 loc) · 1.69 KB
/
Fct.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <memory>
#include <functional>
#include <math.h>
namespace netn {
template <typename T> class Model;
struct Component;
typedef std::function<double(double)> func_t;
template <typename T>
class Fct : public Model<T> {
public:
Fct(const Model<T> & var, const func_t & func, const func_t & deriv);
Fct(const Fct & other);
virtual ~Fct() = default;
T eval() const override;
T derivPart(const Component & component) const override;
std::shared_ptr<Model<T>> toModel() const override;
private:
func_t _func;
func_t _deriv;
std::shared_ptr<Model<T>> _var;
};
template <typename T>
Fct<T> sin(const Model<T> & model) {
return Fct<T>(model,
[](double x) {return std::sin(x); },
[](double x) {return std::cos(x); });
}
template <typename T>
Fct<T> cos(const Model<T> & model) {
return Fct<T>(model,
[](double x) {return std::cos(x); },
[](double x) {return - std::sin(x); });
}
template <typename T>
Fct<T> exp(const Model<T> & model) {
return Fct<T>(model,
[](double x) {return std::exp(x); },
[](double x) {return std::exp(x); });
}
template <typename T>
Fct<T> log(const Model<T> & model) {
return Fct<T>(model,
[](double x) {return std::log(x); },
[](double x) {return 1 / x; });
}
template <typename T>
Fct<T> pow(const Model<T> & model, double exp) {
return Fct<T>(model,
[exp](double x) {return std::pow(x, exp); },
[exp](double x) {return exp * std::pow(x, exp - 1); });
}
template <typename T>
Fct<T> sigmoid(const Model<T> & model) {
return Fct<T>(model,
[](double x) {return 1 / (1 + std::exp(-x)); },
[](double x) {double ex = std::exp(x); return ex / ((1 + ex) * (1 + ex)); });
}
}
#include "Fct.inl"